typeddfs.utils.json_utils
ο
Tools that could possibly be used outside of typed-dfs.
Module Contentsο
- class typeddfs.utils.json_utils.JsonDecoderο
- from_bytes(self, data: ByteString) Any ο
- from_str(self, data: str) Any ο
- class typeddfs.utils.json_utils.JsonEncoderο
- bytes_options :intο
- default :Callable[[Any], Any]ο
- prep :Optional[Callable[[Any], Any]]ο
- str_options :intο
- as_bytes(self, data: Any) ByteString ο
- as_str(self, data: Any) str ο
- class typeddfs.utils.json_utils.JsonUtilsο
- classmethod decoder(cls) JsonDecoder ο
- classmethod encoder(cls, *fallbacks: Optional[Callable[[Any], Any]], indent: bool = True, sort: bool = False, preserve_inf: bool = True, last: Optional[Callable[[Any], Any]] = str) JsonEncoder ο
Serializes to string with orjson, indenting and adding a trailing newline. Uses
orjson_default()
to encode more types than orjson can.- Parameters
indent β Indent by 2 spaces
preserve_inf β Preserve infinite values with
orjson_preserve_inf()
sort β Sort keys with
orjson.OPT_SORT_KEYS
; only fortypeddfs.json_utils.JsonEncoder.as_str()
last β Last resort option to encode a value
- classmethod misc_types_default(cls) Callable[[Any], Any] ο
- classmethod new_default(cls, *fallbacks: Optional[Callable[[Any], Any]], first: Optional[Callable[[Any], Any]] = _misc_types_default, last: Optional[Callable[[Any], Any]] = str) Callable[[Any], Any] ο
Creates a new method to be passed as
default=
toorjson.dumps
. Tries, in order:orjson_default()
,fallbacks
, thenstr
.- Parameters
first β Try this first
fallbacks β Tries these, in order, after
first
, skipping any Nonelast β Use this as the last resort; consider
str
orrepr
- classmethod preserve_inf(cls, data: Any) Any ο
Recursively replaces infinite float and numpy values with strings. Orjson encodes NaN, inf, and +inf as JSON null. This function converts to string as needed to preserve infinite values. Any float scalar (
np.floating
andfloat
) will be replaced with a string. Anynp.ndarray
, whether it contains an infinite value or not, will be converted to an ndarray of strings. The returned result may still not be serializable with orjson ororjson_bytes()
. Trying those methods is the best way to test for serializablity.