xsdata.formats.dataclass.parsers module

class xsdata.formats.dataclass.parsers.JsonParser(name_generator=<function ModelInspect.<lambda>>, cache=<factory>)[source]

Bases: xsdata.formats.mixins.AbstractParser, xsdata.formats.dataclass.mixins.ModelInspect

parse(source, clazz)[source]

Parse the JSON input stream and return the resulting object tree.

Return type

~T

parse_context(data, clazz)[source]

Recursively build the given model from the input dict data.

Raises

TypeError – When parsing fails for any reason

Return type

~T

bind_value(var, value)[source]

Bind value according to the class var.

The return value can be: - a dataclass instance - a dictionary with unknown attributes - a list of unknown elements - an enumeration - a primitive value

Return type

Any

static get_value(data, field)[source]

Find the field value in the given dictionary or return the default field value.

cache = None
class xsdata.formats.dataclass.parsers.QueueItem(type, index, meta=None, position=<factory>)[source]

Bases: object

type: Type = None
index: int = None
meta: Optional[xsdata.formats.dataclass.mixins.ClassMeta] = None
position: int = None
class xsdata.formats.dataclass.parsers.XmlParser(name_generator=<function ModelInspect.<lambda>>, cache=<factory>, index=<factory>)[source]

Bases: xsdata.formats.mixins.AbstractXmlParser, xsdata.formats.dataclass.mixins.ModelInspect

index: int = None
queue: List[Optional[QueueItem]] = None
namespace: Optional[str] = None
objects: List[Tuple[QName, Any]] = None
parse_context(context, clazz)[source]

Dispatch elements to handlers as they arrive and are fully parsed.

Initialize queue with clazz metadata and reset pending objects list.

Raises

ValueError – When the requested type doesn’t match the result object

Return type

~T

start_node(element)[source]

Prepare metadata queue to bind the given element to a dataclass object.

In order: - If last item in queue is None assume we are inside mixed content - If element qname is not a var in the last item in the queue.

  • Check if the first element element is root and skip the rest.

  • Check if the last queue item supports mixed content and setup the queue to

bypass direct data binding to a new dataclass object.

  • If element qname is a var in the last item in the queue append its class

metadata to the queue for the upcoming data binding.

Raises

Value – When the parser doesn’t know how to handle the given element.

end_node(element)[source]

Build an objects tree for the given element.

Construct a dataclass instance with the attributes of the given element and if any pending objects that belong to the model. Otherwise parse as a primitive type the element’s text content.

Returns object

A dataclass object or a python primitive value.

Raises

ValueError – When parser has no data bind strategy for the given object.

Return type

Optional[~T]

emit_event(event, name, **kwargs)[source]

Call if exist the parser’s hook for the given element and event.

fetch_class_children(item)[source]

Return a dictionary of qualified object names and their values for the given queue item.

Raises

ValueError – if queue item type is primitive.

Return type

Dict[str, Any]

bind_element_attrs(metadata, element)[source]

Parse the given element’s attributes and any text content and return a dictionary of field names and values based on the given class metadata.

Return type

Dict

bind_element_text(metadata, element)[source]
bind_element_any(metadata, element)[source]
classmethod parse_any_element(element)[source]
classmethod parse_mixed_content(element)[source]

Parse element mixed content by preserving the raw string.