Skip to content

# json

## `xsdata.formats.dataclass.serializers.json`

### `JsonSerializer` `dataclass`

Bases: `DictEncoder`

Json serializer for data classes.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`config` | `SerializerConfig` | The serializer config instance | `SerializerConfig()`
`context` | `XmlContext` | The models context instance | `XmlContext()`
`dict_factory` | `Callable` | Dictionary factory | `dict`
`dump_factory` | `Callable` | Json dump factory e.g. json.dump | `dump`

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

    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

    @dataclass
    class JsonSerializer(DictEncoder):
        """Json serializer for data classes.

        Args:
            config: The serializer config instance
            context: The models context instance
            dict_factory: Dictionary factory
            dump_factory: Json dump factory e.g. json.dump
        """

        dump_factory: Callable = field(default=json.dump)

        def render(self, obj: Any) -> str:
            """Serialize the input model instance to json string.

            Args:
                obj: The input model instance

            Returns:
                The serialized json string output.
            """
            output = StringIO()
            self.write(output, obj)
            return output.getvalue()

        def write(self, out: TextIO, obj: Any) -> None:
            """Serialize the given object to the output text stream.

            Args:
                out: The output text stream
                obj: The input model instance to serialize
            """
            self.dump_factory(self.encode(obj), out, indent=self.config.indent)

#### `render(obj)`

Serialize the input model instance to json string.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`obj` | `Any` | The input model instance | required

Returns:

Type | Description
--- | ---
`str` | The serialized json string output.

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

    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34

    def render(self, obj: Any) -> str:
        """Serialize the input model instance to json string.

        Args:
            obj: The input model instance

        Returns:
            The serialized json string output.
        """
        output = StringIO()
        self.write(output, obj)
        return output.getvalue()

#### `write(out, obj)`

Serialize the given object to the output text stream.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`out` | `TextIO` | The output text stream | required
`obj` | `Any` | The input model instance to serialize | required

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

    36
    37
    38
    39
    40
    41
    42
    43

    def write(self, out: TextIO, obj: Any) -> None:
        """Serialize the given object to the output text stream.

        Args:
            out: The output text stream
            obj: The input model instance to serialize
        """
        self.dump_factory(self.encode(obj), out, indent=self.config.indent)