xotl.tools.future.json - Encode and decode the JSON format

This module extends the standard library’s json. You may use it as a drop-in replacement in many cases.

Avoid importing * from this module since could be different in Python 2.7 and Python 3.3.

We added the following features.

class xotl.tools.future.json.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Extensible JSON <http://json.org> encoder for Python data structures.

Supports the following objects and types by default:

Python JSON
dict object
list, tuple array
str string
int, float number
True true
False false
None null

To extend this to recognize other objects, subclass and implement a .default() method with another method that returns a serializable object for o if possible, otherwise it should call the superclass implementation (to raise TypeError).

xotl.tools extends this class by supporting the following data-types (see default() method):

  • datetime, date and time values, which are translated to strings using ISO format.
  • Decimal values, which are represented as a string representation.
  • Iterables, which are represented as lists.
xotl.tools.future.json.encode_string(string, ensure_ascii=True)[source]

Return a JSON representation of a Python string.

Parameters:ensure_ascii – If True, the output is guaranteed to be of type str with all incoming non-ASCII characters escaped. If False, the output can contain non-ASCII characters.