]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/json.py
Cleaned up the network hint / object registration code in net_obj
[OpenSRF.git] / src / python / osrf / json.py
1 import simplejson, types 
2 from osrf.net_obj import *
3 from osrf.const import OSRF_JSON_PAYLOAD_KEY, OSRF_JSON_CLASS_KEY
4
5 class osrfJSONNetworkEncoder(simplejson.JSONEncoder):
6     def default(self, obj):
7         if isinstance(obj, osrfNetworkObject):
8             return { 
9                 OSRF_JSON_CLASS_KEY: obj.getHint(),
10                 OSRF_JSON_PAYLOAD_KEY: self.default(obj.getData())
11             }   
12         return obj
13
14
15 def osrfObjectToJSON(obj):
16     """Turns a python object into a wrapped JSON object"""
17     return simplejson.dumps(obj, cls=osrfJSONNetworkEncoder)
18
19
20 def osrfJSONToObject(json):
21     """Turns a JSON string into python objects"""
22     obj = simplejson.loads(json)
23     return parseNetObject(obj)
24
25 def osrfParseJSONRaw(json):
26     """Parses JSON the old fashioned way."""
27     return simplejson.loads(json)
28
29 def osrfToJSONRaw(obj):
30     """Stringifies an object as JSON with no additional logic."""
31     return simplejson.dumps(obj)
32
33 def __tabs(t):
34     r=''
35     for i in range(t): r += '   '
36     return r
37
38 def osrfDebugNetworkObject(obj, t=1):
39     """Returns a debug string for a given object.
40
41     If it's an osrfNetworkObject and has registered keys, key/value p
42     pairs are returned.  Otherwise formatted JSON is returned"""
43
44     s = ''
45     if isinstance(obj, osrfNetworkObject):
46         reg = obj.getRegistry()
47         keys = list(reg.keys) # clone it, so sorting won't break the original
48         keys.sort()
49
50         for k in keys:
51
52             key = k
53             while len(key) < 24: key += '.' # pad the names to make the values line up somewhat
54             val = getattr(obj, k)()
55
56             subobj = val and not (isinstance(val,unicode) or \
57                 isinstance(val, int) or isinstance(val, float) or isinstance(val, long))
58
59             s += __tabs(t) + key + ' = '
60
61             if subobj:
62                 s += '\n'
63                 val = osrfDebugNetworkObject(val, t+1)
64
65             s += str(val)
66
67             if not subobj: s += '\n'
68
69     else:
70         s = osrfFormatJSON(osrfObjectToJSON(obj))
71     return s
72
73 def osrfFormatJSON(json):
74     """JSON pretty-printer"""
75     r = ''
76     t = 0
77     instring = False
78     inescape = False
79     done = False
80
81     for c in json:
82
83         done = False
84         if (c == '{' or c == '[') and not instring:
85             t += 1
86             r += c + '\n' + __tabs(t)
87             done = True
88
89         if (c == '}' or c == ']') and not instring:
90             t -= 1
91             r += '\n' + __tabs(t) + c
92             done = True
93
94         if c == ',' and not instring:
95             r += c + '\n' + __tabs(t)
96             done = True
97
98         if c == '"' and not inescape:
99             instring = not instring
100
101         if inescape: 
102             inescape = False
103
104         if c == '\\':
105             inescape = True
106
107         if not done:
108             r += c
109
110     return r
111
112
113
114
115
116