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 for typeddfs.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= to orjson.dumps. Tries, in order: orjson_default(), fallbacks, then str.

Parameters
  • first – Try this first

  • fallbacks – Tries these, in order, after first, skipping any None

  • last – Use this as the last resort; consider str or repr

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 and float) will be replaced with a string. Any np.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 or orjson_bytes(). Trying those methods is the best way to test for serializablity.