Skip to content

# json

## `xsdata.formats.dataclass.parsers.json`

### `JsonParser` `dataclass`

Bases: `DictDecoder`

Json parser for data classes.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`config` | `ParserConfig` | Parser configuration | `ParserConfig()`
`context` | `XmlContext` | The models context instance | `XmlContext()`
`load_factory` | `Callable` | Json loader factory | `load`

Source code in `xsdata/formats/dataclass/parsers/json.py`

    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
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98

    @dataclass
    class JsonParser(DictDecoder):
        """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 from_path(self, path: pathlib.Path, clazz: type[T] | None = None) -> T:
            """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.

            Args:
                path: The path to the input file
                clazz: The target class type to parse the file into

            Returns:
                An instance of the specified class representing the parsed content.
            """
            return self.parse(str(path.resolve()), clazz)

        def from_string(self, source: str, clazz: type[T] | None = None) -> T:
            """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.

            Args:
                source: The source string to parse
                clazz: The target class type to parse the source string into

            Returns:
                An instance of the specified class representing the parsed content.
            """
            return self.from_bytes(source.encode(), clazz)

        def from_bytes(self, source: bytes, clazz: type[T] | None = None) -> T:
            """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.

            Args:
                source: The source bytes object to parse
                clazz: The target class type to parse the source bytes object

            Returns:
                An instance of the specified class representing the parsed content.
            """
            return self.parse(io.BytesIO(source), clazz)

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

#### `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:

Name | Type | Description | Default
--- | --- | --- | ---
`path` | `Path` | The path to the input file | required
`clazz` | `type[T] | None` | The target class type to parse the file into | `None`

Returns:

Type | Description
--- | ---
`T` | An instance of the specified class representing the parsed content.

Source code in `xsdata/formats/dataclass/parsers/json.py`

    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

    def from_path(self, path: pathlib.Path, clazz: type[T] | None = None) -> T:
        """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.

        Args:
            path: The path to the input file
            clazz: The target class type to parse the file into

        Returns:
            An instance of the specified class representing the parsed content.
        """
        return self.parse(str(path.resolve()), clazz)

#### `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:

Name | Type | Description | Default
--- | --- | --- | ---
`source` | `str` | The source string to parse | required
`clazz` | `type[T] | None` | The target class type to parse the source string into | `None`

Returns:

Type | Description
--- | ---
`T` | An instance of the specified class representing the parsed content.

Source code in `xsdata/formats/dataclass/parsers/json.py`

    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52

    def from_string(self, source: str, clazz: type[T] | None = None) -> T:
        """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.

        Args:
            source: The source string to parse
            clazz: The target class type to parse the source string into

        Returns:
            An instance of the specified class representing the parsed content.
        """
        return self.from_bytes(source.encode(), clazz)

#### `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:

Name | Type | Description | Default
--- | --- | --- | ---
`source` | `bytes` | The source bytes object to parse | required
`clazz` | `type[T] | None` | The target class type to parse the source bytes 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`

    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67

    def from_bytes(self, source: bytes, clazz: type[T] | None = None) -> T:
        """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.

        Args:
            source: The source bytes object to parse
            clazz: The target class type to parse the source bytes object

        Returns:
            An instance of the specified class representing the parsed content.
        """
        return self.parse(io.BytesIO(source), clazz)

#### `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` | `type[T] | None` | 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`

    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83

    def parse(self, source: Any, clazz: type[T] | None = 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
--- | ---
`dict | list` | The loaded dictionary or list of dictionaries.

Source code in `xsdata/formats/dataclass/parsers/json.py`

    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98

    def load_json(self, source: Any) -> 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)