tcustom json encoder for transactions - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit f957837e218770531502d995556bc34925a6b733 DIR parent 72d7d0b07ae49785591c571137f5090b38729b36 HTML Author: ThomasV <thomasv@gitorious> Date: Sat, 14 Sep 2013 21:53:56 +0200 custom json encoder for transactions Diffstat: M lib/commands.py | 6 +++--- M lib/transaction.py | 4 ---- M lib/util.py | 13 ++++++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) --- DIR diff --git a/lib/commands.py b/lib/commands.py t@@ -115,12 +115,12 @@ class Commands: i['index'] = i['vout'] outputs = map(lambda x: (x[0],int(1e8*x[1])), outputs.items()) tx = Transaction.from_io(inputs, outputs) - return tx.as_dict() + return tx def signrawtransaction(self, raw_tx, input_info, private_keys): tx = Transaction(raw_tx) self.wallet.signrawtransaction(tx, input_info, private_keys, self.password) - return tx.as_dict() + return tx def decoderawtransaction(self, raw): tx = Transaction(raw) t@@ -251,7 +251,7 @@ class Commands: def mksendmanytx(self, outputs, fee = None, change_addr = None, domain = None): tx = self._mktx(outputs, fee, change_addr, domain) - return tx.as_dict() + return tx def payto(self, to_address, amount, fee = None, change_addr = None, domain = None): DIR diff --git a/lib/transaction.py b/lib/transaction.py t@@ -379,10 +379,6 @@ class Transaction: self.input_info = None self.is_complete = True - - def __repr__(self): - return "Transaction('"+self.raw+"')" - def __str__(self): return self.raw DIR diff --git a/lib/util.py b/lib/util.py t@@ -1,4 +1,4 @@ -import os, sys, re +import os, sys, re, json import platform import shutil from datetime import datetime t@@ -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 t@@ -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")