JSONAble

A generic object which has a JSON object/string representation.

This class is “abstract”, assigning the task of implementing the abstract function yael.jsonable.JSONAble.json_object() to each concrete subclass, according to the suitable element semantics.

class yael.jsonable.JSONAble[source]

A generic object which has a JSON object/string representation.

static clean(obj)[source]

Recursively “clean” the given object by removing:

  1. None values,
  2. empty dictionaries, and
  3. empty lists.

Note that this function works on the given object, altering it in place. Pass a copy of the original object if you want to avoid side effects.

Parameters:obj (value, list or dict) – the object to clean
Returns:a cleaned version (possibly, None) of the given object
Return type:object
json_object(recursive=True)[source]

To be implemented in concrete subclasses.

Parameters:recursive (bool) – if True, append JSON sub-objects
Returns:object that can be output as a JSON string
Return type:dict
json_string(recursive=True, pretty=False, indent=4, sort=False, clean=False)[source]

Format a JSON string representation of the object.

Parameters:
  • recursive (bool) – if True, append JSON sub-objects
  • pretty (bool) – if True, pretty print the string
  • indent (integer) – the number of spaces for each indentation level
  • sort (bool) – if True, sort the keys
  • clean (bool) – if True, remove None values and empty lists/dictionaries
Returns:

a JSON representation of the object

Return type:

str

static safe(obj)[source]

Return a JSON-safe representation of the given object.

If obj is a list, return a list whose elements are the safe(...) version of the original elements, otherwise return the result of yael.jsonable.JSONAble.json_object().

The result might be None, if obj is invalid.

Parameters:obj ((list of) yael.jsonable.JSONAble) – the object to represent
Returs:a JSON-safe representation of the object
Return type:object