dlpt.json module

Read and write JSON files, JSON files with comments and (un)picklable JSON data.

dlpt.json.remove_comments(data_str: str) str

Return given string with removed C/C++ style comments.

Parameters:

data_str – string to remove comments from.

Returns:

Input string without C/C++ style comments.

dlpt.json.read(file_path: str) Dict[str, Any]

Open the given JSON file, strip comments and return dictionary data.

Parameters:

file_path – path to a file that needs to be parsed.

Returns:

Data (dictionary) of a given JSON file.

dlpt.json.write(data: Dict[str, Any], file_path: str, indent: int = 2, sort_keys: bool = True, *args, **kwargs)

Write given data to a file in a JSON format.

Parameters:
  • data – serializable object to store to a file in JSON format.

  • file_path – destination file path.

  • indent – number of spaces to use while building file line indentation.

  • sort_keys – if True, data keys are sorted alphabetically, else left unchanged.

  • *argsjson.dump() additional arguments.

  • **kwargsjson.dump() additional keyword arguments.

dlpt.json.read_jsonpickle(file_path: str, classes: object | List[object] | None = None, *args) Any

Read given file and return unpicklable data - python objects with jsonpickle module.

Parameters:
  • file_path – path to a file that needs to be read.

  • classes – see jsonpickle decode() docstring. TLDR: if un-picklable objects are from modules which are not globally available, use classes arg to specify them.

  • *argsjsonpickle.decode() additional arguments.

Returns:

Python object(s) of unpickled JSON data.

dlpt.json.write_jsonpickle(data: Any, file_path: str, indent: int = 2, *args)

Write given data to a file in a JSON format with jsonpickle module, which adds data type info for unpickling with read_jsonpickle().

Parameters:
  • data – serializable object to store to a file in JSON format.

  • file_path – destination file path.

  • indent – number of spaces for line indentation.

  • *argsjsonpickle.encode() additional arguments.