Skip to content

json

xsdata.formats.dataclass.parsers.json

JsonParser dataclass

Bases: DictDecoder, AbstractParser

Json parser for data classes.

Parameters:

Name Type Description Default
config ParserConfig

Parser configuration

field(default_factory=ParserConfig)
context XmlContext

The models context instance

field(default_factory=XmlContext)
load_factory Callable

Json loader factory

field(default=load)
Source code in xsdata/formats/dataclass/parsers/json.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@dataclass
class JsonParser(DictDecoder, AbstractParser):
    """Json parser for data classes.

    Args:
        config: Parser configuration
        context: The models context instance
        load_factory: Json loader factory
    """

    load_factory: Callable = field(default=json.load)

    def parse(self, source: Any, clazz: Optional[Type[T]] = None) -> T:
        """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.

        Args:
            source: The source file name or stream to parse
            clazz: The target class type to parse the source object

        Returns:
            An instance of the specified class representing the parsed content.
        """
        data = self.load_json(source)
        return self.decode(data, clazz)

    def load_json(self, source: Any) -> Union[Dict, List]:
        """Load the given json source filename or stream.

        Args:
            source: A file name or file stream

        Returns:
            The loaded dictionary or list of dictionaries.
        """
        if not hasattr(source, "read"):
            with open(source, "rb") as fp:
                return self.load_factory(fp)

        return self.load_factory(source)

parse(source, clazz=None)

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:

Name Type Description Default
source Any

The source file name or stream to parse

required
clazz Optional[Type[T]]

The target class type to parse the source object

None

Returns:

Type Description
T

An instance of the specified class representing the parsed content.

Source code in xsdata/formats/dataclass/parsers/json.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def parse(self, source: Any, clazz: Optional[Type[T]] = None) -> T:
    """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.

    Args:
        source: The source file name or stream to parse
        clazz: The target class type to parse the source object

    Returns:
        An instance of the specified class representing the parsed content.
    """
    data = self.load_json(source)
    return self.decode(data, clazz)

load_json(source)

Load the given json source filename or stream.

Parameters:

Name Type Description Default
source Any

A file name or file stream

required

Returns:

Type Description
Union[Dict, List]

The loaded dictionary or list of dictionaries.

Source code in xsdata/formats/dataclass/parsers/json.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def load_json(self, source: Any) -> Union[Dict, List]:
    """Load the given json source filename or stream.

    Args:
        source: A file name or file stream

    Returns:
        The loaded dictionary or list of dictionaries.
    """
    if not hasattr(source, "read"):
        with open(source, "rb") as fp:
            return self.load_factory(fp)

    return self.load_factory(source)