@@ -121,9 +121,9 @@ def __mod__(self, other):
121121
122122
123123class UnixDomainSocketRpc (object ):
124- def __init__ (self , socket_path , executor = None , logger = logging , encoder = json .JSONEncoder , decoder = json .JSONDecoder ):
124+ def __init__ (self , socket_path , executor = None , logger = logging , encoder_cls = json .JSONEncoder , decoder = json .JSONDecoder () ):
125125 self .socket_path = socket_path
126- self .encoder = encoder
126+ self .encoder_cls = encoder_cls
127127 self .decoder = decoder
128128 self .executor = executor
129129 self .logger = logger
@@ -133,7 +133,7 @@ def __init__(self, socket_path, executor=None, logger=logging, encoder=json.JSON
133133 self .next_id = 0
134134
135135 def _writeobj (self , sock , obj ):
136- s = json .dumps (obj , cls = self .encoder )
136+ s = json .dumps (obj , cls = self .encoder_cls )
137137 sock .sendall (bytearray (s , 'UTF-8' ))
138138
139139 def _readobj_compat (self , sock , buff = b'' ):
@@ -245,32 +245,39 @@ def default(self, o):
245245 pass
246246 return json .JSONEncoder .default (self , o )
247247
248- @staticmethod
249- def lightning_json_hook (json_object ):
250- return json_object
251-
252- @staticmethod
253- def replace_amounts (obj ):
254- """
255- Recursively replace _msat fields with appropriate values with Millisatoshi.
256- """
257- if isinstance (obj , dict ):
258- for k , v in obj .items ():
259- if k .endswith ('msat' ):
260- if isinstance (v , str ) and v .endswith ('msat' ):
261- obj [k ] = Millisatoshi (v )
262- # Special case for array of msat values
263- elif isinstance (v , list ) and all (isinstance (e , str ) and e .endswith ('msat' ) for e in v ):
264- obj [k ] = [Millisatoshi (e ) for e in v ]
265- else :
266- obj [k ] = LightningRpc .replace_amounts (v )
267- elif isinstance (obj , list ):
268- obj = [LightningRpc .replace_amounts (e ) for e in obj ]
269-
270- return obj
248+ class LightningJSONDecoder (json .JSONDecoder ):
249+ def __init__ (self , * , object_hook = None , parse_float = None , parse_int = None , parse_constant = None , strict = True , object_pairs_hook = None ):
250+ self .object_hook_next = object_hook
251+ super ().__init__ (object_hook = self .millisatoshi_hook , parse_float = parse_float , parse_int = parse_int , parse_constant = parse_constant , strict = strict , object_pairs_hook = object_pairs_hook )
252+
253+ @staticmethod
254+ def replace_amounts (obj ):
255+ """
256+ Recursively replace _msat fields with appropriate values with Millisatoshi.
257+ """
258+ if isinstance (obj , dict ):
259+ for k , v in obj .items ():
260+ if k .endswith ('msat' ):
261+ if isinstance (v , str ) and v .endswith ('msat' ):
262+ obj [k ] = Millisatoshi (v )
263+ # Special case for array of msat values
264+ elif isinstance (v , list ) and all (isinstance (e , str ) and e .endswith ('msat' ) for e in v ):
265+ obj [k ] = [Millisatoshi (e ) for e in v ]
266+ else :
267+ obj [k ] = LightningRpc .LightningJSONDecoder .replace_amounts (v )
268+ elif isinstance (obj , list ):
269+ obj = [LightningRpc .LightningJSONDecoder .replace_amounts (e ) for e in obj ]
270+
271+ return obj
272+
273+ def millisatoshi_hook (self , obj ):
274+ obj = LightningRpc .LightningJSONDecoder .replace_amounts (obj )
275+ if self .object_hook_next :
276+ obj = self .object_hook_next (obj )
277+ return obj
271278
272279 def __init__ (self , socket_path , executor = None , logger = logging ):
273- super ().__init__ (socket_path , executor , logging , self .LightningJSONEncoder , json . JSONDecoder ( object_hook = self .replace_amounts ))
280+ super ().__init__ (socket_path , executor , logging , self .LightningJSONEncoder , self .LightningJSONDecoder ( ))
274281
275282 def getpeer (self , peer_id , level = None ):
276283 """
0 commit comments