pycharmers.utils.json_utils module

class pycharmers.utils.json_utils.PythonCharmersJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

Json encoder for Python data structures.

Supports the following objects and types by default (json.JSONEncoder):

Python

JSON

dict

object

list, tuple

array

str

string

int, float

number

True

true

False

false

None

null

default(obj)[source]

Override this method to accommodate other types of data structures.

Currently, supports the following objects and types by overriding.

Python

JSON

np.integar

number(int)

np.float

number(float)

np.ndarray

array

np.random.RandomState

object

datetime.datetime

string

pycharmers.utils.json_utils.dumps_json(obj, ensure_ascii=False, indent=2, cls=<class 'pycharmers.utils.json_utils.PythonCharmersJSONEncoder'>, flatten_list=True, **kwargs)[source]

dumps Json object to String.

Parameters
  • obj (dict) – Serialize obj as a JSON formatted stream.

  • ensure_ascii (bool) – If ensure_ascii is false, then the strings written to fp can contain non-ASCII characters if they appear in strings contained in obj.

  • indent (int) – 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.

  • cls (json.JSONEncoder) – To use a custom JSONEncoder subclass (e.g. one that overrides the .default() method to serialize additional types), specify it with the cls kwarg; otherwise PythonCharmersJSONEncoder is used.

  • flatten_list (bool) – Whether you want to flatten the list or not.

Example

>>> import datetime
>>> from pycharmers.utils import dumps_json
>>> print(dumps_json(obj={
...    "date": datetime.datetime.now(),
...     "bool" : True,
...     "dual_list": [[1,2,3],[4,5,6]]
>>> }))
{
    "date": "2020-12-07T23:28:49.311962",
    "bool": true,
    "dual_list": [
        [1, 2, 3],
        [4, 5, 6]
    ]
}
pycharmers.utils.json_utils.save_json(obj, file, ensure_ascii=False, indent=2, cls=<class 'pycharmers.utils.json_utils.PythonCharmersJSONEncoder'>, flatten_list=True, **kwargs)[source]

Save the json file with easy-to-use arguments

Parameters
  • obj (dict) – Serialize obj as a JSON formatted stream.

  • file (str) – a text or byte string giving the path of the file to be opened.

  • ensure_ascii (bool) – If ensure_ascii is false, then the strings written to fp can contain non-ASCII characters if they appear in strings contained in obj.

  • indent (int) – 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.

  • cls (json.JSONEncoder) – To use a custom JSONEncoder subclass (e.g. one that overrides the .default() method to serialize additional types), specify it with the cls kwarg; otherwise PythonCharmersJSONEncoder is used.

  • flatten_list (bool) – Whether you want to flatten the list or not.

Example

>>> import datetime
>>> from pycharmers.utils import save_json
>>> save_json(obj={"date": datetime.datetime.now(), "bool" : True}, file="sample.json")
>>> with open("sample.json") as f:
>>>     for line in f.readlines():
>>>         print(line, end="")
{
    "date": "2020-09-13T20:45:56.614838",
    "bool": true
}