mt940.json module

JSON serialization for MT940 models.

This module exposes JSONEncoder, a json.JSONEncoder subclass that knows how to serialize the model types returned by the parser (balances, amounts, dates and the transaction collections).

Example

>>> import json
>>> import mt940
>>> transactions = mt940.models.Transactions()
>>> json.dumps(transactions, cls=mt940.JSONEncoder)
'{"transactions": []}'
class mt940.json.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

Serialize MT940 model objects to JSON-compatible primitives.

Dates, datetimes, timedeltas, timezones and decimals are rendered as strings; Transactions, Transaction, Balance and Amount are rendered as their data/__dict__ mappings. Pass it as the cls argument to json.dumps().

default(o)[source]

Return a JSON-serializable representation of o.

Parameters:

o (Any) – The object to serialize. o keeps the permissive Any type of the overridden json.JSONEncoder.default().

Returns:

The serialized form of the object.

Return type:

Any