py_serializable

Submodules

Attributes

logger

The logger. The thing that captures all this package has to say.

Classes

ViewType

Base of all views.

SerializationType

Enum to define the different formats supported for serialization and deserialization.

XmlArraySerializationType

Enum to differentiate how array-type properties (think Iterables) are serialized.

XmlStringSerializationType

Enum to differentiate how string-type properties are serialized.

ObjectMetadataLibrary

namespace-like

Functions

serializable_enum(…)

Decorator

serializable_class(…)

Decorator used to tell py_serializable that a class is to be included in (de-)serialization.

type_mapping(→ Callable[[_F], _F])

Decorator

include_none(→ Callable[[_F], _F])

Decorator

json_name(→ Callable[[_F], _F])

Decorator

string_format(→ Callable[[_F], _F])

Decorator

view(→ Callable[[_F], _F])

Decorator

xml_attribute(→ Callable[[_F], _F])

Decorator

xml_array(→ Callable[[_F], _F])

Decorator

xml_string(→ Callable[[_F], _F])

Decorator

xml_name(→ Callable[[_F], _F])

Decorator

xml_sequence(→ Callable[[_F], _F])

Decorator

Package Contents

py_serializable.logger

The logger. The thing that captures all this package has to say. Feel free to modify its level and attach handlers to it.

class py_serializable.ViewType

Base of all views.

class py_serializable.SerializationType

Bases: str, enum.Enum

Enum to define the different formats supported for serialization and deserialization.

JSON = 'JSON'
XML = 'XML'
class py_serializable.XmlArraySerializationType

Bases: enum.Enum

Enum to differentiate how array-type properties (think Iterables) are serialized.

Given a Warehouse has a property boxes that returns List[Box]:

FLAT would allow for XML looking like:

`` <warehouse>

<box>..box 1..</box> <box>..box 2..</box>

</warehouse> ``

NESTED would allow for XML looking like:

`` <warehouse>

<boxes>

<box>..box 1..</box> <box>..box 2..</box>

</boxes>

</warehouse> ``

FLAT = 1
NESTED = 2
class py_serializable.XmlStringSerializationType

Bases: enum.Enum

Enum to differentiate how string-type properties are serialized.

STRING = 1

as raw string. see https://www.w3.org/TR/xmlschema-2/#string

NORMALIZED_STRING = 2

as normalizedString. see http://www.w3.org/TR/xmlschema-2/#normalizedString

TOKEN = 3

as token. see http://www.w3.org/TR/xmlschema-2/#token

class py_serializable.ObjectMetadataLibrary

namespace-like

The core Class in py_serializable that is used to record all metadata about classes that you annotate for serialization and deserialization.

custom_enum_klasses: Set[Type[enum.Enum]]
klass_mappings: Dict[str, ObjectMetadataLibrary]
klass_property_mappings: Dict[str, Dict[str, ObjectMetadataLibrary]]
class SerializableClass(*, klass: type, custom_name: str | None = None, serialization_types: Iterable[SerializationType] | None = None, ignore_during_deserialization: Iterable[str] | None = None, ignore_unknown_during_deserialization: bool = False)

Internal model class used to represent metadata we hold about Classes that are being included in (de-)serialization.

property name: str
property klass: type
property custom_name: str | None
property serialization_types: Iterable[SerializationType]
property ignore_during_deserialization: Set[str]
property ignore_unknown_during_deserialization: bool
class SerializableProperty(*, prop_name: str, prop_type: Any, custom_names: Dict[SerializationType, str], custom_type: Any | None = None, include_none_config: Set[Tuple[Type[ViewType], Any]] | None = None, is_xml_attribute: bool = False, string_format_: str | None = None, views: Iterable[Type[ViewType]] | None = None, xml_array_config: Tuple[XmlArraySerializationType, str] | None = None, xml_string_config: XmlStringSerializationType | None = None, xml_sequence_: int | None = None)

Internal model class used to represent metadata we hold about Properties that are being included in (de-)serialization.

property name: str
property custom_names: Dict[SerializationType, str]
custom_name(serialization_type: SerializationType) str | None
property type_: Any
property concrete_type: Any
property custom_type: Any | None
property include_none: bool
property include_none_views: Set[Tuple[Type[ViewType], Any]]
include_none_for_view(view_: Type[ViewType]) bool
get_none_value_for_view(view_: Type[ViewType] | None) Any
property is_xml_attribute: bool
property string_format: str | None
property views: Set[Type[ViewType]]
property xml_array_config: Tuple[XmlArraySerializationType, str] | None
property is_array: bool
property xml_string_config: XmlStringSerializationType | None
property is_enum: bool
property is_optional: bool
property xml_sequence: int
get_none_value(view_: Type[ViewType] | None = None) Any
is_helper_type() bool
is_primitive_type() bool
parse_type_deferred() None
classmethod defer_property_type_parsing(prop: ObjectMetadataLibrary, klasses: Iterable[str]) None
classmethod is_klass_serializable(klass: Any) bool
classmethod is_property(o: Any) bool
classmethod register_enum(klass: Type[_E]) Type[_E]
classmethod register_klass(klass: Type[_T], custom_name: str | None, serialization_types: Iterable[SerializationType], ignore_during_deserialization: Iterable[str] | None = None, ignore_unknown_during_deserialization: bool = False) Type[_T] | Type[_JsonSerializable] | Type[_XmlSerializable]
classmethod register_custom_json_property_name(qual_name: str, json_property_name: str) None
classmethod register_custom_string_format(qual_name: str, string_format: str) None
classmethod register_custom_xml_property_name(qual_name: str, xml_property_name: str) None
classmethod register_klass_view(klass: Type[_T], view_: Type[ViewType]) Type[_T]
classmethod register_property_include_none(qual_name: str, view_: Type[ViewType] | None = None, none_value: Any | None = None) None
classmethod register_property_view(qual_name: str, view_: Type[ViewType]) None
classmethod register_xml_property_array_config(qual_name: str, array_type: XmlArraySerializationType, child_name: str) None
classmethod register_xml_property_string_config(qual_name: str, string_type: XmlStringSerializationType | None) None
classmethod register_xml_property_attribute(qual_name: str) None
classmethod register_xml_property_sequence(qual_name: str, sequence: int) None
classmethod register_property_type_mapping(qual_name: str, mapped_type: type) None
py_serializable.serializable_enum(cls: Literal[None] = None) Callable[[Type[_E]], Type[_E]]
py_serializable.serializable_enum(cls: Type[_E]) Type[_E]

Decorator

py_serializable.serializable_class(cls: Literal[None] = None, *, name: str | None = ..., serialization_types: Iterable[SerializationType] | None = ..., ignore_during_deserialization: Iterable[str] | None = ..., ignore_unknown_during_deserialization: bool = ...) Callable[[Type[_T]], Type[_T] | Type[_JsonSerializable] | Type[_XmlSerializable]]
py_serializable.serializable_class(cls: Type[_T], *, name: str | None = ..., serialization_types: Iterable[SerializationType] | None = ..., ignore_during_deserialization: Iterable[str] | None = ..., ignore_unknown_during_deserialization: bool = ...) Type[_T] | Type[_JsonSerializable] | Type[_XmlSerializable]

Decorator used to tell py_serializable that a class is to be included in (de-)serialization.

Parameters:
  • cls – Class

  • name – Alternative name to use for this Class

  • serialization_types – Serialization Types that are to be supported for this class.

  • ignore_during_deserialization – List of properties/elements to ignore during deserialization

  • ignore_unknown_during_deserialization – Whether to ignore all properties/elements/attributes that are unknown to the class during deserialization

Returns:

py_serializable.type_mapping(type_: type) Callable[[_F], _F]

Decorator

py_serializable.include_none(view_: Type[ViewType] | None = None, none_value: Any | None = None) Callable[[_F], _F]

Decorator

py_serializable.json_name(name: str) Callable[[_F], _F]

Decorator

py_serializable.string_format(format_: str) Callable[[_F], _F]

Decorator

py_serializable.view(view_: Type[ViewType]) Callable[[_F], _F]

Decorator

py_serializable.xml_attribute() Callable[[_F], _F]

Decorator

py_serializable.xml_array(array_type: XmlArraySerializationType, child_name: str) Callable[[_F], _F]

Decorator

py_serializable.xml_string(string_type: XmlStringSerializationType) Callable[[_F], _F]

Decorator

py_serializable.xml_name(name: str) Callable[[_F], _F]

Decorator

py_serializable.xml_sequence(sequence: int) Callable[[_F], _F]

Decorator