Module xtuples.json

Expand source code
# ---------------------------------------------------------------

import json

from .xtuples import REGISTRY, nTuple, iTuple, _Example

# -----

def cast_json_nTuple(obj):
    """
    >>> ex = _Example(1, "a")
    >>> ex.pipe(cast_json_nTuple)
    {'x': 1, 's': 'a', 'it': {'__t__': 'iTuple', 'data': []}, '__t__': '_Example'}
    """
    d = {
        k: cast_json(v)
        for k, v in obj._asdict().items()
        #
    }
    d["__t__"] = type(obj).__name__
    return d

def uncast_json_nTuple(cls, obj):
    """
    >>> ex = _Example(1, "a")
    >>> uncast_json_nTuple(_Example, ex.pipe(cast_json_nTuple))
    _Example(x=1, s='a', it=iTuple())
    """
    assert obj["__t__"] == cls.__name__
    return cls(
        *(
            uncast_json(v)
            for k, v in obj.items() if k != "__t__"
        )
    )

def cast_json_iTuple(self):
    """
    >>> iTuple.range(1).pipe(cast_json_iTuple)
    {'__t__': 'iTuple', 'data': [0]}
    """
    return dict(
        __t__ = type(self).__name__,
        data = list(self.map(cast_json)),
    )

def uncast_json_iTuple(cls, obj):
    """
    >>> uncast_json_iTuple(iTuple, iTuple.range(1).pipe(cast_json_iTuple))
    iTuple(0)
    """
    assert obj["__t__"] == cls.__name__
    return cls(data=obj["data"])


def cast_json(obj, default = lambda obj: obj):
    """
    >>> ex = _Example(1, "a")
    >>> ex.pipe(cast_json)
    {'x': 1, 's': 'a', 'it': {'__t__': 'iTuple', 'data': []}, '__t__': '_Example'}
    >>> iTuple.range(1).pipe(cast_json)
    {'__t__': 'iTuple', 'data': [0]}
    """
    if nTuple.is_instance(obj):
        return cast_json_nTuple(obj)
    elif isinstance(obj, iTuple):
        return cast_json_iTuple(obj)
    return default(obj)

def uncast_json(obj):
    """
    >>> ex = _Example(1, "a")
    >>> uncast_json(ex.pipe(cast_json))
    _Example(x=1, s='a', it=iTuple())
    >>> uncast_json(iTuple.range(1).pipe(cast_json))
    iTuple(0)
    """
    if not isinstance(obj, dict):
        return obj
    __t__ = obj.get("__t__", None)
    if __t__ is None:
        return obj
    cls = iTuple if __t__ == "iTuple" else REGISTRY[__t__]
    if cls is iTuple or issubclass(cls, iTuple):
        return uncast_json_iTuple(cls, obj)
    return uncast_json_nTuple(cls, obj)

# ---------------------------------------------------------------

class JSONEncoder(json.JSONEncoder):

    def iterencode(self, o, *args, **kwargs):
        for chunk in super().iterencode(
            cast_json(o), *args, **kwargs
        ):
            yield chunk

    # def meta_default(self, obj):
    #     return json.JSONEncoder.default(self, obj)

    # def default(self, obj):
    #     if isinstance(obj, fDict):
    #         return self.meta_default(obj.data)
    #     return cast_json(obj, default=self.meta_default)

# -----

class JSONDecoder(json.JSONDecoder):

    def __init__(self, *args, **kwargs):
        json.JSONDecoder.__init__(
            self,
            object_hook=self.object_hook,
            *args,
            **kwargs
            #
        )

    @classmethod
    def xtuple_object_hook(cls, d):
        return uncast_json(d)

    def object_hook(self, d):
        return self.xtuple_object_hook(d)

# ---------------------------------------------------------------

# TODO: fString so can do .pipe ?
def to_json(v, **kwargs):
    """
    >>> print(iTuple([_Example(1, "a")]).pipe(to_json, indent=2))
    {
      "__t__": "iTuple",
      "data": [
        {
          "x": 1,
          "s": "a",
          "it": {
            "__t__": "iTuple",
            "data": []
          },
          "__t__": "_Example"
        }
      ]
    }
    >>> print(iTuple([
    ...     iTuple([_Example(1, "a")])
    ... ]).pipe(to_json, indent=2))
    {
      "__t__": "iTuple",
      "data": [
        {
          "__t__": "iTuple",
          "data": [
            {
              "x": 1,
              "s": "a",
              "it": {
                "__t__": "iTuple",
                "data": []
              },
              "__t__": "_Example"
            }
          ]
        }
      ]
    }
    >>> print(_Example(2, "b", iTuple([
    ...     iTuple([_Example(1, "a")])
    ... ])).pipe(to_json, indent=2))
    {
      "x": 2,
      "s": "b",
      "it": {
        "__t__": "iTuple",
        "data": [
          {
            "__t__": "iTuple",
            "data": [
              {
                "x": 1,
                "s": "a",
                "it": {
                  "__t__": "iTuple",
                  "data": []
                },
                "__t__": "_Example"
              }
            ]
          }
        ]
      },
      "__t__": "_Example"
    }
    """
    return json.dumps(v, cls=JSONEncoder, **kwargs)

def from_json(v: str, **kwargs):
    """
    >>> ex = iTuple([_Example(1, "a")])
    >>> from_json(ex.pipe(to_json))
    iTuple(_Example(x=1, s='a', it=iTuple()))
    >>> from_json(
    ...     iTuple([iTuple([_Example(1, "a")])]).pipe(to_json)
    ... )
    iTuple(iTuple(_Example(x=1, s='a', it=iTuple())))
    >>> from_json(
    ...     _Example(2, "b", iTuple([
    ...         iTuple([_Example(1, "a")])
    ...     ])).pipe(to_json)
    ... )
    _Example(x=2, s='b', it=iTuple(iTuple(_Example(x=1, s='a', it=iTuple()))))
    """
    return json.loads(v, cls=JSONDecoder, **kwargs)

def load_json(f):
    return json.load(f, cls=JSONDecoder)

def dump_json(f, v):
    return json.dump(f, v, cls=JSONEncoder)

# ---------------------------------------------------------------

__all__ = [
    "JSONEncoder",
    "JSONDecoder",
]

# ---------------------------------------------------------------

Classes

class JSONDecoder (*args, **kwargs)

Simple JSON http://json.org decoder

Performs the following translations in decoding by default:

+---------------+-------------------+ | JSON | Python | +===============+===================+ | object | dict | +---------------+-------------------+ | array | list | +---------------+-------------------+ | string | str | +---------------+-------------------+ | number (int) | int | +---------------+-------------------+ | number (real) | float | +---------------+-------------------+ | true | True | +---------------+-------------------+ | false | False | +---------------+-------------------+ | null | None | +---------------+-------------------+

It also understands NaN, Infinity, and -Infinity as their corresponding float values, which is outside the JSON spec.

object_hook, if specified, will be called with the result of every JSON object decoded and its return value will be used in place of the given dict. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting).

object_pairs_hook, if specified will be called with the result of every JSON object decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders. If object_hook is also defined, the object_pairs_hook takes priority.

parse_float, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).

parse_int, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).

parse_constant, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered.

If strict is false (true is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0-31 range, including '\t' (tab), '\n', '\r' and '\0'.

Expand source code
class JSONDecoder(json.JSONDecoder):

    def __init__(self, *args, **kwargs):
        json.JSONDecoder.__init__(
            self,
            object_hook=self.object_hook,
            *args,
            **kwargs
            #
        )

    @classmethod
    def xtuple_object_hook(cls, d):
        return uncast_json(d)

    def object_hook(self, d):
        return self.xtuple_object_hook(d)

Ancestors

  • json.decoder.JSONDecoder

Static methods

def xtuple_object_hook(d)
Expand source code
@classmethod
def xtuple_object_hook(cls, d):
    return uncast_json(d)

Methods

def object_hook(self, d)
Expand source code
def object_hook(self, d):
    return self.xtuple_object_hook(d)
class JSONEncoder (*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Extensible JSON http://json.org encoder for Python data structures.

Supports the following objects and types by default:

+-------------------+---------------+ | Python | JSON | +===================+===============+ | dict | object | +-------------------+---------------+ | list, tuple | array | +-------------------+---------------+ | str | string | +-------------------+---------------+ | int, float | number | +-------------------+---------------+ | True | true | +-------------------+---------------+ | False | false | +-------------------+---------------+ | None | null | +-------------------+---------------+

To extend this to recognize other objects, subclass and implement a .default() method with another method that returns a serializable object for o if possible, otherwise it should call the superclass implementation (to raise TypeError).

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 OverflowError). 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.

Expand source code
class JSONEncoder(json.JSONEncoder):

    def iterencode(self, o, *args, **kwargs):
        for chunk in super().iterencode(
            cast_json(o), *args, **kwargs
        ):
            yield chunk

Ancestors

  • json.encoder.JSONEncoder

Methods

def iterencode(self, o, *args, **kwargs)

Encode the given object and yield each string representation as available.

For example::

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
Expand source code
def iterencode(self, o, *args, **kwargs):
    for chunk in super().iterencode(
        cast_json(o), *args, **kwargs
    ):
        yield chunk