JsonParser#

class xsdata.formats.dataclass.parsers.JsonParser(config=<factory>, context=<factory>, load_factory=<function load>)[source]#

Json parser for data classes.

Parameters:
load_factory(*, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)#

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object.

object_hook is an optional function that will be called with the result of any object literal decode (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders. If object_hook is also defined, the object_pairs_hook takes priority.

To use a custom JSONDecoder subclass, specify it with the cls kwarg; otherwise JSONDecoder is used.

parse(source, clazz=None)[source]#

Parse the input stream into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • source (Any) – The source file name or stream to parse

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the source object

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.

load_json(source)[source]#

Load the given json source filename or stream.

Parameters:

source (Any) – A file name or file stream

Return type:

Union[Dict, List]

Returns:

The loaded dictionary or list of dictionaries.

verify_type(clazz, data)[source]#

Verify the given data matches the given clazz.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
Return type:

Type[TypeVar(T)]

Returns:

The clazz type to bind the loaded data.

detect_type(data)[source]#

Locate the target clazz type from the data keys.

Parameters:

data (Union[Dict, List]) – The loaded dictionary or list of dictionaries

Return type:

Type[TypeVar(T)]

Returns:

The clazz type to bind the loaded data.

bind_dataclass(data, clazz)[source]#

Create a new instance of the given class type with the given data.

Parameters:
  • data (Dict) – The loaded data

  • clazz (Type[TypeVar(T)]) – The target class type to bind the input data

Return type:

TypeVar(T)

Returns:

An instance of the class type representing the parsed content.

bind_derived_dataclass(data, clazz)[source]#

Bind the input data to the given class type.

Examples

>>> {
    "qname": "foo",
    "type": "my:type",
    "value": {"prop": "value"}
}
Parameters:
  • data (Dict) – The derived element dictionary

  • clazz (Type[TypeVar(T)]) – The target class type to bind the input data

Return type:

Any

Returns:

An instance of the class type representing the parsed content.

bind_best_dataclass(data, classes)[source]#

Bind the input data to all the given classes and return best match.

Parameters:
  • data (Dict) – The derived element dictionary

  • classes (Iterable[Type[TypeVar(T)]]) – The target class types to try

Return type:

TypeVar(T)

Returns:

An instance of one of the class types representing the parsed content.

bind_optional_dataclass(data, clazz)[source]#

Bind the input data to the given class type.

This is a strict process, if there is any warning the process returns None. This method is used to test if te data fit into the class type.

Parameters:
  • data (Dict) – The derived element dictionary

  • clazz (Type[TypeVar(T)]) – The target class type to bind the input data

Return type:

Optional[TypeVar(T)]

Returns:

An instance of the class type representing the parsed content or None if there is any warning or error.

bind_value(meta, var, value, recursive=False)[source]#

Main entry point for binding values.

Parameters:
  • meta (XmlMeta) – The parent xml meta instance

  • var (XmlVar) – The xml var descriptor for the field

  • value (Any) – The data value

  • recursive (bool) – Whether this is a recursive call

Return type:

Any

Returns:

The parsed object

bind_text(meta, var, value)[source]#

Bind text/tokens value entrypoint.

Parameters:
  • meta (XmlMeta) – The parent xml meta instance

  • var (XmlVar) – The xml var descriptor for the field

  • value (Any) – The data value

Return type:

Any

Returns:

The parsed tokens or text value.

bind_complex_type(meta, var, data)[source]#

Bind complex values entrypoint.

Parameters:
  • meta (XmlMeta) – The parent xml meta instance

  • var (XmlVar) – The xml var descriptor for the field

  • data (Dict) – The complex data value

Return type:

Any

Returns:

The parsed dataclass instance.

bind_derived_value(meta, var, data)[source]#

Bind derived data entrypoint.

The data is representation of a derived element, e.g. {

“qname”: “foo”, “type”: “my:type” “value”: Any

}

The data value can be a primitive value or a complex value.

Parameters:
  • meta (XmlMeta) – The parent xml meta instance

  • var (XmlVar) – The xml var descriptor for the field

  • data (Dict) – The derived element data

Return type:

Any

Returns:

The parsed object.

classmethod find_var(xml_vars, local_name, is_list=False)[source]#

Match the name to a xml variable.

Parameters:
  • xml_vars (List[XmlVar]) – A list of xml vars

  • local_name (str) – A key from the loaded data

  • is_list (bool) – Whether the data value is an array

Return type:

Optional[XmlVar]

Returns:

One of the xml vars, if all search attributes match, None otherwise.

__eq__(other)#

Return self==value.

__hash__ = None#
from_bytes(source, clazz=None)#

Parse the input source bytes object into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • source (bytes) – The source bytes object to parse

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the source bytes object

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.

from_path(path, clazz=None)#

Parse the input file into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • path (Path) – The path to the input file

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the file into

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.

from_string(source, clazz=None)#

Parse the input source string into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:
  • source (str) – The source string to parse

  • clazz (Optional[Type[TypeVar(T)]]) – The target class type to parse the source string into

Return type:

TypeVar(T)

Returns:

An instance of the specified class representing the parsed content.