JSON Serializer

json-based packet serializer module.

class easynetwork.serializers.json.JSONSerializer

Bases: AbstractIncrementalPacketSerializer[Any, Any]

A serializer built on top of the json module.

__init__(encoder_config: JSONEncoderConfig | None = None, decoder_config: JSONDecoderConfig | None = None, *, encoding: str = 'utf-8', unicode_errors: str = 'strict', limit: int = DEFAULT_SERIALIZER_LIMIT, use_lines: bool = True, debug: bool = False) None
Parameters:
  • encoder_config (JSONEncoderConfig | None) – Parameter object to configure the JSONEncoder.

  • decoder_config (JSONDecoderConfig | None) – Parameter object to configure the JSONDecoder.

  • encoding (str) – String encoding.

  • unicode_errors (str) – Controls how encoding errors are handled.

  • limit (int) – Maximum buffer size. Used in incremental serialization context.

  • use_lines (bool) – If True (the default), each ASCII lines is considered a JSON object.

  • debug (bool) – If True, add information to DeserializeError via the error_info attribute.

final serialize(packet: Any) bytes

Returns the JSON representation of the Python object packet.

Roughly equivalent to:

def serialize(self, packet):
    return json.dumps(packet)

Example

>>> s = JSONSerializer()
>>> s.serialize({"key": [1, 2, 3], "data": None})
b'{"key":[1,2,3],"data":null}'
Parameters:

packet (Any) – The Python object to serialize.

Returns:

a byte sequence.

Return type:

bytes

final incremental_serialize(packet: Any) Generator[bytes]

Returns the JSON representation of the Python object packet.

Example

>>> s = JSONSerializer()
>>> b"".join(s.incremental_serialize({"key": [1, 2, 3], "data": None}))
b'{"key":[1,2,3],"data":null}\n'
Parameters:

packet (Any) – The Python object to serialize.

Yields:

all the parts of the JSON packet.

Return type:

Generator[bytes]

final deserialize(data: bytes) Any

Creates a Python object representing the raw JSON packet from data.

Roughly equivalent to:

def deserialize(self, data):
    return json.loads(data)

Example

>>> s = JSONSerializer()
>>> s.deserialize(b'{"key":[1,2,3],"data":null}')
{'key': [1, 2, 3], 'data': None}
Parameters:

data (bytes) – The byte sequence to deserialize.

Raises:

DeserializeError – A UnicodeError or JSONDecodeError have been raised.

Returns:

the deserialized Python object.

Return type:

Any

final incremental_deserialize() Generator[None, bytes, tuple[Any, bytes]]

Creates a Python object representing the raw JSON packet.

Example

>>> s = JSONSerializer(use_lines=False)
>>> consumer = s.incremental_deserialize()
>>> next(consumer)
>>> consumer.send(b'{"key":[1,2,3]')
>>> consumer.send(b',"data":null}{"something":"remaining"}')
Traceback (most recent call last):
...
StopIteration: ({'key': [1, 2, 3], 'data': None}, b'{"something":"remaining"}')
Yields:

None until the whole packet has been deserialized.

Raises:
Returns:

a tuple with the deserialized Python object and the unused trailing data.

Return type:

Generator[None, bytes, tuple[Any, bytes]]

property debug: bool

The debug mode flag. Read-only attribute.

property buffer_limit: int

Maximum buffer size. Read-only attribute.

Configuration

class easynetwork.serializers.json.JSONEncoderConfig

Bases: object

A dataclass with the JSON encoder options.

See json.JSONEncoder for details.

skipkeys: bool = False
check_circular: bool = True
ensure_ascii: bool = True
allow_nan: bool = True
default: Callable[[...], Any] | None = None
__init__(*, skipkeys: bool = False, check_circular: bool = True, ensure_ascii: bool = True, allow_nan: bool = True, default: Callable[[...], Any] | None = None) None
class easynetwork.serializers.json.JSONDecoderConfig

Bases: object

A dataclass with the JSON decoder options.

See json.JSONDecoder for details.

object_hook: Callable[[...], Any] | None = None
parse_int: Callable[[str], Any] | None = None
parse_float: Callable[[str], Any] | None = None
parse_constant: Callable[[str], Any] | None = None
object_pairs_hook: Callable[[list[tuple[str, Any]]], Any] | None = None
strict: bool = True
__init__(*, object_hook: Callable[[...], Any] | None = None, parse_int: Callable[[str], Any] | None = None, parse_float: Callable[[str], Any] | None = None, parse_constant: Callable[[str], Any] | None = None, object_pairs_hook: Callable[[list[tuple[str, Any]]], Any] | None = None, strict: bool = True) None