Module ldict.customjson
Expand source code
# Copyright (c) 2021. Davi Pereira dos Santos
# This file is part of the ldict project.
# Please respect the license - more about this in the section (*) below.
#
# ldict is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ldict is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ldict. If not, see <http://www.gnu.org/licenses/>.
#
# (*) Removing authorship by any means, e.g. by distribution of derived
# works or verbatim, obfuscated, compiled or rewritten versions of any
# part of this work is illegal and unethical regarding the effort and
# time spent here.
from json import JSONEncoder
class CustomJSONEncoder(JSONEncoder):
"""
>>> from ldict.frozenlazydict import FrozenLazyDict
>>> ldict = FrozenLazyDict
>>> a = ldict(x=3)
>>> ldict(d=a, y=5)
{
"d": {
"x": 3
},
"y": 5
}
>>> from pandas.core.frame import DataFrame, Series
>>> df = DataFrame([[1,2],[3,4]])
>>> df
0 1
0 1 2
1 3 4
>>> b = ldict(d=a, y=5, df=df, ell=...)
>>> b
{
"d": {
"x": 3
},
"y": 5,
"df": {0: {0: 1, 1: 3}, 1: {0: 2, 1: 4}},
"ell": "..."
}
>>> from numpy import array
>>> ldict(b=b, z=9, c=(c:=array([1,2,3])), d=Series(c), dd=array([[1, 2], [3, 4]]))
{
"b": {
"d": {
"x": 3
},
"y": 5,
"df": {0: {0: 1, 1: 3}, 1: {0: 2, 1: 4}},
"ell": "..."
},
"z": 9,
"c": [1 2 3],
"d": {0: 1, 1: 2, 2: 3},
"dd": [[1 2] [3 4]]
}
"""
width = None
def default(self, obj):
if obj is not None:
from ldict.frozenlazydict import FrozenLazyDict
from ldict.lazyval import LazyVal
if obj is Ellipsis:
return "..."
if isinstance(obj, FrozenLazyDict):
return self.data
if isinstance(obj, LazyVal):
return str(obj)
# if isinstance(obj, FunctionType):
# return str(obj)
if not isinstance(obj, (list, set, str, int, float, bytearray, bool)):
try:
from pandas.core.frame import DataFrame, Series
if isinstance(obj, (DataFrame, Series)):
# «str()» is to avoid nested identation
return truncate("«" + str(obj.to_dict()) + "»", self.width)
from numpy import ndarray
if isinstance(obj, ndarray):
return truncate("«" + str(obj).replace("\n", "") + "»", self.width)
except ImportError: # pragma: no cover
print("Pandas or numpy may be missing.")
if hasattr(obj, "asdict"):
return obj.asdict
elif hasattr(obj, "aslist"):
return obj.aslist
else:
return str(obj)
return JSONEncoder.default(self, obj)
# class CustomJSONDecoder(JSONDecoder):
# def __init__(self, *args, **kwargs):
# JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
#
# def object_hook(self, obj):
# if obj is not None:
# if isinstance(obj, str) and len(obj) == digits:
# return
# return obj
def truncate(txt, width):
return txt[:width] + "..." if width and len(txt) > width else txt
Functions
def truncate(txt, width)
-
Expand source code
def truncate(txt, width): return txt[:width] + "..." if width and len(txt) > width else txt
Classes
class CustomJSONEncoder (*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
-
>>> from ldict.frozenlazydict import FrozenLazyDict >>> ldict = FrozenLazyDict >>> a = ldict(x=3) >>> ldict(d=a, y=5) { "d": { "x": 3 }, "y": 5 } >>> from pandas.core.frame import DataFrame, Series >>> df = DataFrame([[1,2],[3,4]]) >>> df 0 1 0 1 2 1 3 4 >>> b = ldict(d=a, y=5, df=df, ell=...) >>> b { "d": { "x": 3 }, "y": 5, "df": {0: {0: 1, 1: 3}, 1: {0: 2, 1: 4}}, "ell": "..." } >>> from numpy import array >>> ldict(b=b, z=9, c=(c:=array([1,2,3])), d=Series(c), dd=array([[1, 2], [3, 4]])) { "b": { "d": { "x": 3 }, "y": 5, "df": {0: {0: 1, 1: 3}, 1: {0: 2, 1: 4}}, "ell": "..." }, "z": 9, "c": [1 2 3], "d": {0: 1, 1: 2, 2: 3}, "dd": [[1 2] [3 4]] }
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
.Expand source code
class CustomJSONEncoder(JSONEncoder): """ >>> from ldict.frozenlazydict import FrozenLazyDict >>> ldict = FrozenLazyDict >>> a = ldict(x=3) >>> ldict(d=a, y=5) { "d": { "x": 3 }, "y": 5 } >>> from pandas.core.frame import DataFrame, Series >>> df = DataFrame([[1,2],[3,4]]) >>> df 0 1 0 1 2 1 3 4 >>> b = ldict(d=a, y=5, df=df, ell=...) >>> b { "d": { "x": 3 }, "y": 5, "df": {0: {0: 1, 1: 3}, 1: {0: 2, 1: 4}}, "ell": "..." } >>> from numpy import array >>> ldict(b=b, z=9, c=(c:=array([1,2,3])), d=Series(c), dd=array([[1, 2], [3, 4]])) { "b": { "d": { "x": 3 }, "y": 5, "df": {0: {0: 1, 1: 3}, 1: {0: 2, 1: 4}}, "ell": "..." }, "z": 9, "c": [1 2 3], "d": {0: 1, 1: 2, 2: 3}, "dd": [[1 2] [3 4]] } """ width = None def default(self, obj): if obj is not None: from ldict.frozenlazydict import FrozenLazyDict from ldict.lazyval import LazyVal if obj is Ellipsis: return "..." if isinstance(obj, FrozenLazyDict): return self.data if isinstance(obj, LazyVal): return str(obj) # if isinstance(obj, FunctionType): # return str(obj) if not isinstance(obj, (list, set, str, int, float, bytearray, bool)): try: from pandas.core.frame import DataFrame, Series if isinstance(obj, (DataFrame, Series)): # «str()» is to avoid nested identation return truncate("«" + str(obj.to_dict()) + "»", self.width) from numpy import ndarray if isinstance(obj, ndarray): return truncate("«" + str(obj).replace("\n", "") + "»", self.width) except ImportError: # pragma: no cover print("Pandas or numpy may be missing.") if hasattr(obj, "asdict"): return obj.asdict elif hasattr(obj, "aslist"): return obj.aslist else: return str(obj) return JSONEncoder.default(self, obj)
Ancestors
- json.encoder.JSONEncoder
Class variables
var width
Methods
def default(self, obj)
-
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).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 JSONEncoder.default(self, o)
Expand source code
def default(self, obj): if obj is not None: from ldict.frozenlazydict import FrozenLazyDict from ldict.lazyval import LazyVal if obj is Ellipsis: return "..." if isinstance(obj, FrozenLazyDict): return self.data if isinstance(obj, LazyVal): return str(obj) # if isinstance(obj, FunctionType): # return str(obj) if not isinstance(obj, (list, set, str, int, float, bytearray, bool)): try: from pandas.core.frame import DataFrame, Series if isinstance(obj, (DataFrame, Series)): # «str()» is to avoid nested identation return truncate("«" + str(obj.to_dict()) + "»", self.width) from numpy import ndarray if isinstance(obj, ndarray): return truncate("«" + str(obj).replace("\n", "") + "»", self.width) except ImportError: # pragma: no cover print("Pandas or numpy may be missing.") if hasattr(obj, "asdict"): return obj.asdict elif hasattr(obj, "aslist"): return obj.aslist else: return str(obj) return JSONEncoder.default(self, obj)