Encoder module

The encoder module provides custom JSON serialization functionality specifically designed to handle Object instances. This module extends Python’s built-in json module to ensure that Object instances are correctly converted into their original dictionary representation during the serialization process.

The module features the ObjectEncoder class, which overrides the default JSON encoding behavior to accommodate Object instances. Additionally, it provides custom dump and dumps functions that leverage this encoder, allowing seamless integration with standard JSON serialization workflows.

You must import this module if you want to use serialization done by the json module.

class jsify.encoder.ObjectEncoder(omit_undefined, *args, **kwargs)[source]

Bases: JSONEncoder

Custom JSON encoder for Object instances.

This encoder converts Object instances to their original dictionary representation for JSON serialization. It also provides an option to omit fields with the Undefined value during serialization.

default(o)[source]

Override the default method to handle Object and Undefined instances.

This method converts Object instances to their original representation and handles Undefined values by converting them to None. It also supports serializing SimpleNamespace instances by returning their dictionary representation.

Parameters:

o (Any) – The object to encode.

Returns:

The encoded object, or the result of calling the superclass’s default method if the object type is not explicitly handled.

Return type:

Any

iterencode(o, _one_shot=False)[source]

Encode the object into a JSON string.

This method handles the serialization of Object instances by converting them to their original representation using unjsify. If omit_undefined is set to True, it omits fields with the Undefined value.

Parameters:
  • o (Any) – The object to encode into JSON format.

  • _one_shot (bool, optional) – A flag for one-shot encoding, passed to the parent JSONEncoder. Default is False.

Returns:

An iterator that generates the encoded JSON string.

Return type:

Iterator[str]

jsify.encoder.dump(o, *args, omit_undefined=True, **kwargs)[source]

Serialize o as a JSON formatted stream to fp using ObjectEncoder.

This function wraps json.dump, providing custom serialization for Object instances and optionally omitting fields with the Undefined value.

Parameters:
  • o (Any) – The object to serialize.

  • fp (file-like object) – The file-like object to which the JSON formatted stream is written.

  • omit_undefined (bool, optional) – If True, fields with the Undefined value are omitted from the serialized output. Default is True.

  • **kwargs (dict) – Additional keyword arguments passed to json.dump.

Return type:

None

jsify.encoder.dumps(o, *args, omit_undefined=True, **kwargs)[source]

Serialize o to a JSON formatted str using ObjectEncoder.

This function wraps json.dumps, providing custom serialization for Object instances and optionally omitting fields with the Undefined value.

Parameters:
  • o (Any) – The object to serialize.

  • omit_undefined (bool, optional) – If True, fields with the Undefined value are omitted from the serialized output. Default is True.

  • **kwargs (dict) – Additional keyword arguments passed to json.dumps.

Returns:

The JSON formatted string.

Return type:

str