From d84b11120b40891a9c84670f85873cbc59fff68e Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 18 Jul 2007 22:27:04 +0000 Subject: [PATCH] fixed bug in json encoding of hinted objects and some display bugs git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1049 9efc2488-bf62-4759-914b-345cdb29e865 --- src/python/osrf/json.py | 25 +++++++++++++++++++++++-- src/python/srfsh.py | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/python/osrf/json.py b/src/python/osrf/json.py index 8845b92..a972b09 100644 --- a/src/python/osrf/json.py +++ b/src/python/osrf/json.py @@ -4,10 +4,21 @@ from osrf.const import OSRF_JSON_PAYLOAD_KEY, OSRF_JSON_CLASS_KEY class osrfJSONNetworkEncoder(simplejson.JSONEncoder): def default(self, obj): + if isinstance(obj, osrfNetworkObject): + reg = obj.getRegistry() + data = obj.getData() + + # re-encode the object as an array if necessary + if reg.wireProtocol == 'array': + d = [] + for k in reg.keys: + d.append(data[k]) + data = d + return { - OSRF_JSON_CLASS_KEY: obj.getRegistry().hint, - OSRF_JSON_PAYLOAD_KEY: self.default(obj.getData()) + OSRF_JSON_CLASS_KEY: reg.hint, + OSRF_JSON_PAYLOAD_KEY: self.default(data) } return obj @@ -77,9 +88,15 @@ def osrfFormatJSON(json): instring = False inescape = False done = False + eatws = False for c in json: + if eatws: # simpljson adds a pesky after array and object items + if c == ' ': + continue + + eatws = False done = False if (c == '{' or c == '[') and not instring: t += 1 @@ -94,6 +111,10 @@ def osrfFormatJSON(json): if c == ',' and not instring: r += c + '\n' + __tabs(t) done = True + eatws = True + + if c == ':' and not instring: + eatws = True if c == '"' and not inescape: instring = not instring diff --git a/src/python/srfsh.py b/src/python/srfsh.py index 70a278e..bb058b1 100755 --- a/src/python/srfsh.py +++ b/src/python/srfsh.py @@ -129,7 +129,7 @@ def handle_request(parts): otp = get_var('SRFSH_OUTPUT') if otp == 'pretty': - print osrfDebugNetworkObject(resp.content()) + print "\n" + osrfDebugNetworkObject(resp.content()) else: print osrfFormatJSON(osrfObjectToJSON(resp.content())) -- 2.11.0