Module data

Functions

def add_detected_types(types: dict[str, type])

Load the detected types into the JSONDecoder. Can be useful to restor the state of the module

Args:

types: A dictionary of types to add to the JSONDecoder [str: class]

def add_types(*types: type)

Add new types supported by extended json encoder/decoder. The type will be serialized using the tojson method. (Must exist in the object) and deserialized using the fromjson class method.

Args:

types: A dictionary of types to add to the JSONEncoder [str: class]

def get_detected_types() ‑> dict[str, type]

Get the types that have been automatically detected by the JSONEncoder.

Returns:

A dictionary of detected types [str: class]
def remove_types(*types)

Remove supported types from the extended json encoder/decoder. The type will be serialized using the tojson method. (Must exist in the object) and deserialized using the fromjson class method.

Args:

types: A list of types to remove from the JSONEncoder [str]

def set_detected_types(types: dict[str, type])

Load the detected types into the JSONDecoder. Can be useful to restor the state of the module

Args:

types: A dictionary of types to add to the JSONDecoder [str: class]

Classes

class JSONEncoder (*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

This class extends the JSONEncoder class from the json module to handle the serialization of a variety of objects. If your object isn't serialized properly, you can add a json method to your object to handle the serialization and return a jsonable dictionary.

The types supported by default are:

  • int
  • float
  • str
  • list
  • tuple
  • dict
  • bool
  • None
  • pd.DataFrame
  • np.ndarray
  • np.int64
  • np.float64
  • np.bool_
  • np.datetime64
  • np.timedelta64
  • datetime
  • timedelta
  • date
  • time
  • pd.Series
  • Enum
  • Any object with a dict attribute
  • Any object that is iterable

If you want to add a custom serializer to your object, you can add a tojson method to your object that returns a dictionary that can be serialized. To be able to deserialize the object, you must also add a fromjson class method that takes the dictionary and returns the object. In order fo the Serializer to use those method, you must first register the class using the add_types function.

Example

>>> from backtest.data import json_extension as je
>>> class MyClass:
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
...     def __tojson__(self):
...         return {"a": self.a, "b": self.b}
...     @classmethod
...     def __fromjson__(cls, d):
...         return cls(d["a"], d["b"])
...     def __repr__(self):
...         return f"MyClass({self.a}, {self.b})"
...
>>> je.add_types(MyClass)
>>> obj = MyClass(1, 2)
>>> print(json.dumps(obj, cls=je.JSONEncoder))
{"__TYPE__": "MyClass", "data": {"a": 1, "b": 2}}
>>> # To deserialize the object
>>> d = '{"__TYPE__": "MyClass", "data": {"a": 1, "b": 2}}'
>>> print(json.loads(d, cls=je.JSONDecoder))
MyClass(1, 2)
>>> # To unregister a type, use the remove_types function
>>> je.remove_types(MyClass)

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.

If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

Parent

  • json.encoder.JSONEncoder

Methods

def default(self, o)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this::

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)