JSON Serializer
json-based packet serializer module.
- class easynetwork.serializers.json.JSONSerializer
Bases:
AbstractIncrementalPacketSerializer[Any,Any]A serializer built on top of the
jsonmodule.- __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 toDeserializeErrorvia theerror_infoattribute.
See also
- 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}'
- 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'
- 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
UnicodeErrororJSONDecodeErrorhave been raised.- Returns:
the deserialized Python object.
- Return type:
- 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:
- Raises:
LimitOverrunError – Reached buffer size limit.
IncrementalDeserializeError – A
UnicodeErrororJSONDecodeErrorhave been raised.
- Returns:
a tuple with the deserialized Python object and the unused trailing data.
- Return type:
Configuration
- class easynetwork.serializers.json.JSONEncoderConfig
Bases:
objectA dataclass with the JSON encoder options.
See
json.JSONEncoderfor details.
- class easynetwork.serializers.json.JSONDecoderConfig
Bases:
objectA dataclass with the JSON decoder options.
See
json.JSONDecoderfor details.- __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