Skip to content

Commit

Permalink
updated json readme
Browse files Browse the repository at this point in the history
Signed-off-by: Rick van Hattem <Wolph@wol.ph>
  • Loading branch information
wolph committed Mar 6, 2019
1 parent 0f6253c commit 2e3f0cf
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,41 +113,12 @@ Simple json encoding:
.. code-block:: python
import json
import decimal
import mt940
transactions = mt940.parse('tests/jejik/abnamro.sta')
def default(value):
# Handle decimal values so we get the transaction amounts
if isinstance(value, decimal.Decimal):
return str(value)
# Handling of the Transaction objects to include the actual transactions
if isinstance(value, mt940.models.Transactions):
data = value.data.copy()
data['transactions'] = value.transactions
return data
# If an object has a `data` attribute, return that instead of the
# `__dict__` ro prevent loops
elif hasattr(value, 'data'):
return value.data
# The rest of the models should work just fine by returning the `__dict__`
elif isinstance(value, mt940.models.Model):
return value.__dict__
# Actually raise an error if unknown types are found. Better than silently
# ignoring ;)
else:
raise TypeError('Unknown type %s: %r' % (type(value), value))
print(json.dumps(transactions, default=default, indent=4))
print(json.dumps(transactions, indent=4, cls=mt940.JSONEncoder))
Contributing
------------
Expand Down

0 comments on commit 2e3f0cf

Please sign in to comment.