]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/json.py
implemented the majority of server-side python. still need to add settings server...
[OpenSRF.git] / src / python / osrf / json.py
1 import simplejson, types 
2 from osrf.net_obj import NetworkObject, parse_net_object
3 from osrf.const import OSRF_JSON_PAYLOAD_KEY, OSRF_JSON_CLASS_KEY
4 import osrf.log
5
6 try:
7     # if available, use the faster cjson module for encoding/decoding JSON
8     import cjson
9     _use_cjson = True
10 except ImportError:
11     _use_cjson = False
12
13 class NetworkEncoder(simplejson.JSONEncoder):
14     ''' Encoder used by simplejson '''
15     def default(self, obj):
16
17         if isinstance(obj, NetworkObject):
18             reg = obj.get_registry()
19             data = obj.get_data()
20
21             # re-encode the object as an array if necessary
22             if reg.protocol == 'array':
23                 objarray = []
24                 for key in reg.keys:
25                     objarray.append(data.get(key)) 
26                 data = objarray
27
28             return { 
29                 OSRF_JSON_CLASS_KEY: reg.hint,
30                 OSRF_JSON_PAYLOAD_KEY: self.default(data)
31             }   
32         return obj
33
34
35 def encode_object(obj):
36     ''' Generic opensrf object encoder, used by cjson '''
37
38     if isinstance(obj, dict):
39         newobj = {}
40         for k,v in obj.iteritems():
41             newobj[k] = encode_object(v)
42         return newobj
43
44     elif isinstance(obj, list):
45         return [encode_object(v) for v in obj]
46
47     elif isinstance(obj, NetworkObject):
48         reg = obj.get_registry()
49         data = obj.get_data()
50
51         if reg.protocol == 'array':
52             objarray = []
53             for key in reg.keys:
54                 objarray.append(data.get(key)) 
55             data = objarray
56
57         return {
58             OSRF_JSON_CLASS_KEY: reg.hint,
59             OSRF_JSON_PAYLOAD_KEY: encode_object(data)
60         }
61
62     return obj
63         
64
65
66 def to_json(obj):
67     """Turns a python object into a wrapped JSON object"""
68     if _use_cjson:
69         return cjson.encode(encode_object(obj))
70     return simplejson.dumps(obj, cls=NetworkEncoder)
71
72
73 def to_object(json):
74     """Turns a JSON string into python objects"""
75     if _use_cjson:
76         return parse_net_object(cjson.decode(json))
77     return parse_net_object(simplejson.loads(json))
78
79 def parse_json_raw(json):
80     """Parses JSON the old fashioned way."""
81     if _use_cjson:
82         return cjson.decode(json)
83     return simplejson.loads(json)
84
85 def to_json_raw(obj):
86     """Stringifies an object as JSON with no additional logic."""
87     if _use_cjson:
88         return cjson.encode(json)
89     return simplejson.dumps(obj)
90
91 def __tabs(depth):
92     space = ''
93     for i in range(depth):
94         space += '   '
95     return space
96
97 def debug_net_object(obj, depth=1):
98     """Returns a debug string for a given object.
99
100     If it's an NetworkObject and has registered keys, key/value pairs
101     are returned.  Otherwise formatted JSON is returned"""
102
103     debug_str = ''
104     if isinstance(obj, NetworkObject):
105         reg = obj.get_registry()
106         keys = list(reg.keys) # clone it, so sorting won't break the original
107         keys.sort()
108
109         for k in keys:
110
111             key = str(k)
112             while len(key) < 24:
113                 key += '.' # pad the names to make the values line up somewhat
114             val = getattr(obj, k)()
115
116             subobj = val and not (isinstance(val, unicode) or isinstance(val, str) or \
117                 isinstance(val, int) or isinstance(val, float) or isinstance(val, long))
118
119             debug_str += __tabs(depth) + key + ' = '
120
121             if subobj:
122                 debug_str += '\n'
123                 val = debug_net_object(val, depth+1)
124
125             debug_str += str(val)
126
127             if not subobj: debug_str += '\n'
128
129     else:
130         osrf.log.log_internal("Pretty-printing NetworkObject")
131         debug_str = pprint(to_json(obj))
132     return debug_str
133
134 def pprint(json):
135     """JSON pretty-printer"""
136     r = ''
137     t = 0
138     instring = False
139     inescape = False
140     done = False
141     eatws = False
142
143     for c in json:
144
145         if eatws and not _use_cjson: # simpljson adds a pesky space after array and object items
146             if c == ' ': 
147                 continue
148
149         eatws = False
150         done = False
151         if (c == '{' or c == '[') and not instring:
152             t += 1
153             r += c + '\n' + __tabs(t)
154             done = True
155
156         if (c == '}' or c == ']') and not instring:
157             t -= 1
158             r += '\n' + __tabs(t) + c
159             done = True
160
161         if c == ',' and not instring:
162             r += c + '\n' + __tabs(t)
163             done = True
164             eatws = True
165
166         if c == ':' and not instring:
167             eatws = True
168
169         if c == '"' and not inescape:
170             instring = not instring
171
172         if inescape: 
173             inescape = False
174
175         if c == '\\':
176             inescape = True
177
178         if not done:
179             r += c
180
181     return r