1
0

custom json encoder for transactions

This commit is contained in:
ThomasV
2013-09-14 21:53:56 +02:00
parent 72d7d0b07a
commit f957837e21
3 changed files with 13 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
import os, sys, re
import os, sys, re, json
import platform
import shutil
from datetime import datetime
@@ -6,6 +6,14 @@ is_verbose = True
class MyEncoder(json.JSONEncoder):
def default(self, obj):
from transaction import Transaction
if isinstance(obj, Transaction):
return obj.as_dict()
return super(MyEncoder, self).default(obj)
def set_verbosity(b):
global is_verbose
is_verbose = b
@@ -23,9 +31,8 @@ def print_msg(*args):
sys.stdout.flush()
def print_json(obj):
import json
try:
s = json.dumps(obj,sort_keys = True, indent = 4)
s = json.dumps(obj, sort_keys = True, indent = 4, cls=MyEncoder)
except TypeError:
s = repr(obj)
sys.stdout.write(s + "\n")