[docs]@dataclassclassJsonSerializer(AbstractSerializer):""" Json serializer for dataclasses. :param config: Serializer configuration :param context: Model context provider :param dict_factory: Override default dict factory to add further logic :param dump_factory: Override default json.dump call with another implementation :param indent: Output indentation level """config:SerializerConfig=field(default_factory=SerializerConfig)context:XmlContext=field(default_factory=XmlContext)dict_factory:Callable=field(default=dict)dump_factory:Callable=field(default=json.dump)indent:Optional[int]=field(default=None)
[docs]defrender(self,obj:object)->str:"""Convert the given object tree to json string."""output=StringIO()self.write(output,obj)returnoutput.getvalue()
[docs]defwrite(self,out:TextIO,obj:Any):""" Write the given object tree to the output text stream. :param out: The output stream :param obj: The input dataclass instance """indent:Optional[int]=Noneifself.indent:warnings.warn("JsonSerializer indent property is deprecated, use SerializerConfig",DeprecationWarning,)indent=self.indentelifself.config.pretty_print:indent=2self.dump_factory(self.convert(obj),out,indent=indent)