xsdata.formats.dataclass.serializers module

xsdata.formats.dataclass.serializers.filter_none(x)[source]
class xsdata.formats.dataclass.serializers.DictFactory[source]

Bases: object

FILTER_NONE()
class xsdata.formats.dataclass.serializers.EnumEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
class xsdata.formats.dataclass.serializers.DictSerializer(dict_factory=<class 'dict'>)[source]

Bases: xsdata.formats.mixins.AbstractSerializer

dict_factory

alias of builtins.dict

render(obj)[source]

Convert the given object tree to dictionary with primitive values.

Return type

Dict

class xsdata.formats.dataclass.serializers.JsonSerializer(dict_factory=<class 'dict'>, encoder=<class 'xsdata.formats.dataclass.serializers.EnumEncoder'>, indent=None)[source]

Bases: xsdata.formats.mixins.AbstractSerializer

Parameters
  • dict_factory (Callable) – Callable to generate dictionary

  • encoder (Type[JSONEncoder]) – Value encoder

  • indent (Optional[int]) – Pretty print indent

dict_factory

alias of builtins.dict

encoder

alias of EnumEncoder

indent: Optional[int] = None
render(obj)[source]

Convert the given object tree to json string.

Return type

str

class xsdata.formats.dataclass.serializers.XmlSerializer(xml_declaration=True, encoding='UTF-8', pretty_print=False)[source]

Bases: xsdata.formats.mixins.AbstractSerializer, xsdata.formats.dataclass.mixins.ModelInspect

Parameters
  • xml_declaration (bool) – Add xml declaration

  • encoding (str) – Result text encoding

  • pretty_print (bool) – Enable pretty output

xml_declaration: bool = True
encoding: str = 'UTF-8'
pretty_print: bool = False
ns_list: list = None
render(obj)[source]

Convert the given object tree to xml string.

Return type

str

render_tree(obj)[source]

Convert a dataclass instance to a nested Element structure.

If the instance class is generated from the xsdata cli the root element’s name will be auto assigned otherwise it will default to the class name.

Raises

TypeError – If the instance is not a dataclass

Return type

Element

render_node(obj, parent)[source]

Recursively traverse the given dataclass instance fields and build the lxml Element structure.

Return type

Element

render_tag(name, namespace=None)[source]
Return type

QName

static render_value(value)[source]
Return type

str