Serialization#

Tools that provide JSON serialization via Pickle for unserializable objects.

  • PickleEncodedValue:

    A field annotated with this will be pickled/unpickled during JSON-serialization/validation.

  • JSONSerializableDict:

    A dictionary field annotated with this will make all its items smart-serializable: If an item is serializable – nothing would change. Otherwise – it will be serialized via pickle.

  • JSONSerializableExtras:

    A pydantic base class that makes its extra fields a JSONSerializableDict.

_JSON_EXTRA_FIELDS_KEYS = '__pickled_extra_fields__'#

This key is used in JSONSerializableDict to remember pickled items.

Serializable#

Type annotation for objects supported by json_pickle_serializer().

alias of Dict[str, Union[JsonValue, List[Any], Dict[str, Any], Any]]

class _WrapperModel(root=PydanticUndefined, **data)[source]#

Bases: RootModel

Wrapper model for testing whether an object is serializable to JSON.

root: Any#
pickle_serializer(value)[source]#

Serializer function that serializes any pickle-serializable value into JSON-serializable. Serializes value with pickle and encodes bytes as base64 string.

Parameters:

value (Any) – Pickle-serializable object.

Return type:

str

Returns:

String-encoded object.

pickle_validator(value)[source]#

Validator function that validates base64 string encoded bytes as a pickle-serializable value. Decodes base64 string and validates value with pickle.

Parameters:

value (str) – String-encoded string.

Return type:

Any

Returns:

Pickle-serializable object.

json_pickle_serializer(model)[source]#

Serializer function that serializes a dictionary or Pydantic object to JSON. For every object field, it checks whether the field is JSON serializable, and if it’s not, serializes it using pickle. It also keeps track of pickle-serializable field names in a special list.

Parameters:

model (Dict[str, Union[TypeAliasType, List[Any], Dict[str, Any], Any]]) – Pydantic model object or a dictionary.

Original_serializer:

Original serializer function for model.

Return type:

Dict[str, Union[TypeAliasType, List[Any], Dict[str, Any], Any]]

Returns:

model with all the fields serialized to JSON.

json_pickle_validator(model)[source]#

Validator function that validates a JSON dictionary to a python dictionary. For every object field, it checks if the field is pickle-serialized, and if it is, validates it using pickle.

Parameters:

model (Dict[str, Union[TypeAliasType, List[Any], Dict[str, Any], Any]]) – Pydantic model object or a dictionary.

Return type:

Dict[str, Union[TypeAliasType, List[Any], Dict[str, Any], Any]]

Returns:

model with all the fields serialized to JSON.

class JSONSerializableExtras(**kwargs)[source]#

Bases: BaseModel

This model makes extra fields pickle-serializable. Do not use _JSON_EXTRA_FIELDS_KEYS as an extra field name.

extra_validator()[source]#

Validate model along with the extras field: i.e. all the fields not listed in the model.

Returns:

Validated model.

extra_serializer(original_serializer)[source]#

Serialize model along with the extras field: i.e. all the fields not listed in the model.

Parameters:

original_serializer – Function originally used for serialization by Pydantic.

Return type:

Dict[str, Any]

Returns:

Serialized model.