Class JsonEncoder

class ooodev.io.json.JsonEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

General JSON Encoder class.

This class can be used to encode objects to JSON and is generally used as a base class for other classes that need to encode objects to JSON.

Any object that has a to_json() method will be encoded using that method.

See ooodev.gui.menu.popup.PopupCreator for an example of a class that uses this class.

default(o)[source]

JsonEncoder default method.

Parameters:

o (Any) – Data to be encoded.

Returns:

Encoded data.

Return type:

Any

Note

This method or the on_json_encode() can be overridden by subclasses to encode objects to JSON. If this class is a subclass of EventsPartial, the json_encoding event is triggered before encoding. The event data is a dictionary with the key obj containing the object to be encoded. If the event data result key is set then the value is returned as the result of the encoding.

on_json_encode(obj)[source]

Protected method to encode object to JSON. Can be overridden by subclasses.

Parameters:

obj (Any) – Object to encode. This is the object that json is currently encoding.

Returns:

The result of the encoding. The default is NULL_OBJ which means that the encoding is not handled.

Return type:

Any