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.JsonEncoder(*, 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.JsonEncoder'>, indent=None)[source]

Bases: xsdata.formats.mixins.AbstractSerializer

Variables
dict_factory

alias of builtins.dict

encoder

alias of JsonEncoder

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

Convert the given object tree to json string.

Return type

str

class xsdata.formats.dataclass.serializers.XmlSerializer(name_generator=<function ModelInspect.<lambda>>, cache=<factory>, xml_declaration=True, encoding='UTF-8', pretty_print=False)[source]

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

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

Convert the given object tree to xml string.

Return type

str

render_tree(obj, namespace=None)[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, namespaces)[source]

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

Return type

Element

set_attribute(parent, value, var)[source]
set_attributes(parent, value)[source]
set_text(parent, value)[source]
set_children(parent, value, var, namespaces)[source]
classmethod set_any_children(parent, value, namespaces)[source]
static set_nil_attribute(var, element, namespaces)[source]
static render_value(value)[source]
Return type

str