serde.json
Serialize and Deserialize in JSON format.
1""" 2Serialize and Deserialize in JSON format. 3""" 4 5from typing import Any, AnyStr, overload, Optional, Union 6 7from .compat import T 8from .de import Deserializer, from_dict 9from .se import Serializer, to_dict 10from .numpy import encode_numpy 11 12try: # pragma: no cover 13 import orjson 14 15 def json_dumps(obj: Any, **opts: Any) -> str: 16 if "option" not in opts: 17 opts["option"] = orjson.OPT_SERIALIZE_NUMPY 18 return orjson.dumps(obj, **opts).decode() # type: ignore 19 20 def json_loads(s: Union[str, bytes], **opts: Any) -> Any: 21 return orjson.loads(s, **opts) 22 23except ImportError: 24 import json 25 26 def json_dumps(obj: Any, **opts: Any) -> str: 27 if "default" not in opts: 28 opts["default"] = encode_numpy 29 # compact output 30 ensure_ascii = opts.pop("ensure_ascii", False) 31 separators = opts.pop("separators", (",", ":")) 32 return json.dumps(obj, ensure_ascii=ensure_ascii, separators=separators, **opts) 33 34 def json_loads(s: Union[str, bytes], **opts: Any) -> Any: 35 return json.loads(s, **opts) 36 37 38__all__ = ["from_json", "to_json"] 39 40 41class JsonSerializer(Serializer[str]): 42 @classmethod 43 def serialize(cls, obj: Any, **opts: Any) -> str: 44 return json_dumps(obj, **opts) 45 46 47class JsonDeserializer(Deserializer[AnyStr]): 48 @classmethod 49 def deserialize(cls, data: AnyStr, **opts: Any) -> Any: 50 return json_loads(data, **opts) 51 52 53def to_json( 54 obj: Any, 55 cls: Optional[Any] = None, 56 se: type[Serializer[str]] = JsonSerializer, 57 reuse_instances: bool = False, 58 convert_sets: bool = True, 59 **opts: Any, 60) -> str: 61 """ 62 Serialize the object into JSON str. [orjson](https://github.com/ijl/orjson) 63 will be used if installed. 64 65 You can pass any serializable `obj`. If you supply other keyword arguments, 66 they will be passed in `dumps` function. 67 By default, numpy objects are serialized, this behaviour can be customized with the `option` 68 argument with [orjson](https://github.com/ijl/orjson#numpy), or the `default` argument with 69 Python standard json library. 70 71 If you want to use another json package, you can subclass `JsonSerializer` and implement 72 your own logic. 73 """ 74 return se.serialize( 75 to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts 76 ) 77 78 79@overload 80def from_json( 81 c: type[T], s: AnyStr, de: type[Deserializer[AnyStr]] = JsonDeserializer, **opts: Any 82) -> T: ... 83 84 85# For Union, Optional etc. 86@overload 87def from_json( 88 c: Any, s: AnyStr, de: type[Deserializer[AnyStr]] = JsonDeserializer, **opts: Any 89) -> Any: ... 90 91 92def from_json( 93 c: Any, s: AnyStr, de: type[Deserializer[AnyStr]] = JsonDeserializer, **opts: Any 94) -> Any: 95 """ 96 Deserialize from JSON into the object. [orjson](https://github.com/ijl/orjson) will be used 97 if installed. 98 99 `c` is a class obejct and `s` is JSON bytes or str. If you supply other keyword arguments, 100 they will be passed in `loads` function. 101 102 If you want to use another json package, you can subclass `JsonDeserializer` and implement 103 your own logic. 104 """ 105 return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)
93def from_json( 94 c: Any, s: AnyStr, de: type[Deserializer[AnyStr]] = JsonDeserializer, **opts: Any 95) -> Any: 96 """ 97 Deserialize from JSON into the object. [orjson](https://github.com/ijl/orjson) will be used 98 if installed. 99 100 `c` is a class obejct and `s` is JSON bytes or str. If you supply other keyword arguments, 101 they will be passed in `loads` function. 102 103 If you want to use another json package, you can subclass `JsonDeserializer` and implement 104 your own logic. 105 """ 106 return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)
Deserialize from JSON into the object. orjson will be used if installed.
c
is a class obejct and s
is JSON bytes or str. If you supply other keyword arguments,
they will be passed in loads
function.
If you want to use another json package, you can subclass JsonDeserializer
and implement
your own logic.
54def to_json( 55 obj: Any, 56 cls: Optional[Any] = None, 57 se: type[Serializer[str]] = JsonSerializer, 58 reuse_instances: bool = False, 59 convert_sets: bool = True, 60 **opts: Any, 61) -> str: 62 """ 63 Serialize the object into JSON str. [orjson](https://github.com/ijl/orjson) 64 will be used if installed. 65 66 You can pass any serializable `obj`. If you supply other keyword arguments, 67 they will be passed in `dumps` function. 68 By default, numpy objects are serialized, this behaviour can be customized with the `option` 69 argument with [orjson](https://github.com/ijl/orjson#numpy), or the `default` argument with 70 Python standard json library. 71 72 If you want to use another json package, you can subclass `JsonSerializer` and implement 73 your own logic. 74 """ 75 return se.serialize( 76 to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts 77 )
Serialize the object into JSON str. orjson will be used if installed.
You can pass any serializable obj
. If you supply other keyword arguments,
they will be passed in dumps
function.
By default, numpy objects are serialized, this behaviour can be customized with the option
argument with orjson, or the default
argument with
Python standard json library.
If you want to use another json package, you can subclass JsonSerializer
and implement
your own logic.