pecan.jsonify – Pecan JSON Support

The pecan.jsonify module includes support for JSON rule creation using generic functions.

class pecan.jsonify.GenericFunctionJSON(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: pecan.jsonify.GenericJSON

default(obj)

Converts an object and returns a JSON-friendly structure.

Parameters

obj – object or structure to be converted into a JSON-ifiable structure

Considers the following special cases in order:

  • object has a callable __json__() attribute defined

    returns the result of the call to __json__()

  • date and datetime objects

    returns the object cast to str

  • Decimal objects

    returns the object cast to float

  • SQLAlchemy objects

    returns a copy of the object.__dict__ with internal SQLAlchemy parameters removed

  • SQLAlchemy ResultProxy objects

    Casts the iterable ResultProxy into a list of tuples containing the entire resultset data, returns the list in a dictionary along with the resultset “row” count.

    Note

    {‘count’: 5, ‘rows’: [(‘Ed Jones’,), (‘Pete Jones’,), (‘Wendy Williams’,), (‘Mary Contrary’,), (‘Fred Smith’,)]}

  • SQLAlchemy RowProxy objects

    Casts the RowProxy cursor object into a dictionary, probably losing its ordered dictionary behavior in the process but making it JSON-friendly.

  • webob_dicts objects

    returns webob_dicts.mixed() dictionary, which is guaranteed to be JSON-friendly.

class pecan.jsonify.GenericJSON(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.encoder.JSONEncoder

Generic JSON encoder. Makes several attempts to correctly JSONify requested response objects.

default(obj)

Converts an object and returns a JSON-friendly structure.

Parameters

obj – object or structure to be converted into a JSON-ifiable structure

Considers the following special cases in order:

  • object has a callable __json__() attribute defined

    returns the result of the call to __json__()

  • date and datetime objects

    returns the object cast to str

  • Decimal objects

    returns the object cast to float

  • SQLAlchemy objects

    returns a copy of the object.__dict__ with internal SQLAlchemy parameters removed

  • SQLAlchemy ResultProxy objects

    Casts the iterable ResultProxy into a list of tuples containing the entire resultset data, returns the list in a dictionary along with the resultset “row” count.

    Note

    {‘count’: 5, ‘rows’: [(‘Ed Jones’,), (‘Pete Jones’,), (‘Wendy Williams’,), (‘Mary Contrary’,), (‘Fred Smith’,)]}

  • SQLAlchemy RowProxy objects

    Casts the RowProxy cursor object into a dictionary, probably losing its ordered dictionary behavior in the process but making it JSON-friendly.

  • webob_dicts objects

    returns webob_dicts.mixed() dictionary, which is guaranteed to be JSON-friendly.