custom json encoder for transactions
authorThomasV <thomasv@gitorious>
Sat, 14 Sep 2013 19:53:56 +0000 (21:53 +0200)
committerThomasV <thomasv@gitorious>
Sat, 14 Sep 2013 19:53:56 +0000 (21:53 +0200)
lib/commands.py
lib/transaction.py
lib/util.py

index 53a829d..db11eb7 100644 (file)
@@ -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)
@@ -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):
index 892ec5a..a248532 100644 (file)
@@ -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
 
index 9503b88..15b6b85 100644 (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")