json

Utility functions related to json.

Module Contents

Classes

JsonData

An object to represent JSON data content.

Functions

dump_to_json_file(→ None)

Dump a Python object to a json file.

load_from_json_file(→ Any)

Load a Python object from a JSON file.

Attributes

JsonDataSelf

json.JsonDataSelf
exception json.JsonError(message: str | list[str], origin: str | None = None)

Bases: e3.error.E3Error

Exception raised by functions defined in E3.

exception json.JsonDataInvalidJsonError(message: str | list[str], origin: str | None = None)

Bases: e3.error.E3Error

An error thrown when input data string does not represent a dictionary.

class json.JsonData

Bases: abc.ABC

An object to represent JSON data content.

abstract as_dict() dict[str, object]

Return the dict representation of this JSON data object.

__eq__(other: object) bool

Check if this JSON data is identical to other.

Parameters:

other – The object to compare this JSON data with.

Returns:

A bool set to True if both JSON data are identical, False if they are not, or if other is not a JsonData object.

as_json() str

Return a JSON string representing this JSON data.

See also

python:json.dumps()

classmethod from_dict(obj: dict) JsonDataSelf

Load a dictionary as a JSON data object.

Parameters:

obj – The dictionary to initialize the JSON data object with.

Returns:

A new JsonData object initialized with values from the input dictionary.

classmethod from_json(content: str) JsonDataSelf

Load a JSON string as a JSON data object.

As this method calls for from_dict(), the input content string MUST represent a dictionary. If that’s not the case, a JsonDataInvalidJsonError is thrown.

Parameters:

content – The JSON string to initialize the JSON data object with.

Returns:

A new JsonData object initialized with values from the input dictionary.

Raise:

JsonDataInvalidJsonError when content string does not represent a dictionary.

json.dump_to_json_file(path: str, obj: Any) None

Dump a Python object to a json file.

Parameters:
  • path – path to the json file

  • obj – a Python object that can serialized to JSON

json.load_from_json_file(path: str, default: Any = None, ignore_non_existing: bool = True) Any

Load a Python object from a JSON file.

Parameters:
  • path – json file path

  • default – default value returned if ignore_non_existing is True and the specified file does not exist.

  • ignore_non_existing – if False raise JsonError if the file does not exist, otherwise return default value

Returns:

a Python object