]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_json_object.c
Merging changes from branches/new-json2
[OpenSRF.git] / src / libopensrf / osrf_json_object.c
1 /*
2 Copyright (C) 2006  Georgia Public Library Service 
3 Bill Erickson <billserickson@gmail.com>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 */
15
16 #include <opensrf/osrf_json.h>
17 #include <opensrf/osrf_json_utils.h>
18
19 jsonObject* jsonNewObject(const char* data) {
20
21         jsonObject* o;
22         OSRF_MALLOC(o, sizeof(jsonObject));
23         o->type = JSON_NULL;
24
25         if(data) {
26                 o->type = JSON_STRING;
27                 o->value.s = strdup(data);
28         }
29
30         return o;
31 }
32
33 jsonObject* jsonNewObjectFmt(const char* data, ...) {
34
35         jsonObject* o;
36         OSRF_MALLOC(o, sizeof(jsonObject));
37         o->type = JSON_NULL;
38
39         if(data) {
40                 VA_LIST_TO_STRING(data);
41                 o->type = JSON_STRING;
42                 o->value.s = strdup(VA_BUF);
43         }
44
45         return o;
46 }
47
48 jsonObject* jsonNewNumberObject( double num ) {
49         jsonObject* o = jsonNewObject(NULL);
50         o->type = JSON_NUMBER;
51         o->value.n = num;
52         return o;
53 }
54
55 jsonObject* jsonNewBoolObject(int val) {
56     jsonObject* o = jsonNewObject(NULL);
57     o->type = JSON_BOOL;
58     jsonSetBool(o, val);
59     return o;
60 }
61
62 jsonObject* jsonNewObjectType(int type) {
63         jsonObject* o = jsonNewObject(NULL);
64         o->type = type;
65         return o;
66 }
67
68 void jsonObjectFree( jsonObject* o ) {
69
70         if(!o || o->parent) return;
71         free(o->classname);
72
73         switch(o->type) {
74                 case JSON_HASH          : osrfHashFree(o->value.h); break;
75                 case JSON_ARRAY : osrfListFree(o->value.l); break;
76                 case JSON_STRING        : free(o->value.s); break;
77         }
78         free(o);
79 }
80
81 static void _jsonFreeHashItem(char* key, void* item){
82         if(!item) return;
83         jsonObject* o = (jsonObject*) item;
84         o->parent = NULL; /* detach the item */
85         jsonObjectFree(o);
86 }
87 static void _jsonFreeListItem(void* item){
88         if(!item) return;
89         jsonObject* o = (jsonObject*) item;
90         o->parent = NULL; /* detach the item */
91         jsonObjectFree(o);
92 }
93
94 void jsonSetBool(jsonObject* bl, int val) {
95     if(!bl) return;
96     JSON_INIT_CLEAR(bl, JSON_BOOL);
97     bl->value.b = val;
98 }
99
100 unsigned long jsonObjectPush(jsonObject* o, jsonObject* newo) {
101     if(!o) return -1;
102     if(!newo) newo = jsonNewObject(NULL);
103         JSON_INIT_CLEAR(o, JSON_ARRAY);
104         newo->parent = o;
105         osrfListPush( o->value.l, newo );
106         o->size = o->value.l->size;
107         return o->size;
108 }
109
110 unsigned long jsonObjectSetIndex(jsonObject* dest, unsigned long index, jsonObject* newObj) {
111     if(!dest) return -1;
112     if(!newObj) newObj = jsonNewObject(NULL);
113         JSON_INIT_CLEAR(dest, JSON_ARRAY);
114         newObj->parent = dest;
115         osrfListSet( dest->value.l, newObj, index );
116         dest->size = dest->value.l->size;
117         return dest->value.l->size;
118 }
119
120 unsigned long jsonObjectSetKey( jsonObject* o, const char* key, jsonObject* newo) {
121     if(!o) return -1;
122     if(!newo) newo = jsonNewObject(NULL);
123         JSON_INIT_CLEAR(o, JSON_HASH);
124         newo->parent = o;
125         osrfHashSet( o->value.h, newo, key );
126         o->size = o->value.h->size;
127         return o->size;
128 }
129
130 jsonObject* jsonObjectGetKey( const jsonObject* obj, const char* key ) {
131         if(!(obj && obj->type == JSON_HASH && obj->value.h && key)) return NULL;
132         return osrfHashGet( obj->value.h, key);
133 }
134
135 char* jsonObjectToJSON( const jsonObject* obj ) {
136         jsonObject* obj2 = jsonObjectEncodeClass( (jsonObject*) obj);
137         char* json = jsonObjectToJSONRaw(obj2);
138         jsonObjectFree(obj2);
139         return json;
140 }
141
142 char* jsonObjectToJSONRaw( const jsonObject* obj ) {
143         if(!obj) return NULL;
144         growing_buffer* buf = buffer_init(32);
145         int i;
146     char* json;
147
148         switch(obj->type) {
149
150                 case JSON_BOOL :
151                         if(obj->value.b) OSRF_BUFFER_ADD(buf, "true"); 
152                         else OSRF_BUFFER_ADD(buf, "false"); 
153                         break;
154
155                 case JSON_NUMBER: {
156                         double x = obj->value.n;
157                         if( x == (int) x ) {
158                                 INT_TO_STRING((int)x);  
159                                 OSRF_BUFFER_ADD(buf, INTSTR);
160
161                         } else {
162                                 DOUBLE_TO_STRING(x);
163                                 OSRF_BUFFER_ADD(buf, DOUBLESTR);
164                         }
165                         break;
166                 }
167
168                 case JSON_NULL:
169                         OSRF_BUFFER_ADD(buf, "null");
170                         break;
171
172                 case JSON_STRING:
173                         OSRF_BUFFER_ADD_CHAR(buf, '"');
174                         char* data = obj->value.s;
175                         int len = strlen(data);
176                         
177                         char* output = uescape(data, len, 1);
178                         OSRF_BUFFER_ADD(buf, output);
179                         free(output);
180                         OSRF_BUFFER_ADD_CHAR(buf, '"');
181                         break;
182                         
183                 case JSON_ARRAY: {
184                         OSRF_BUFFER_ADD_CHAR(buf, '[');
185                         if( obj->value.l ) {
186                                 for( i = 0; i != obj->value.l->size; i++ ) {
187                                         json = jsonObjectToJSONRaw(OSRF_LIST_GET_INDEX(obj->value.l, i));
188                                         if(i > 0) OSRF_BUFFER_ADD(buf, ",");
189                                         OSRF_BUFFER_ADD(buf, json);
190                                         free(json);
191                                 }
192                         } 
193                         OSRF_BUFFER_ADD_CHAR(buf, ']');
194                         break;
195                 }
196
197                 case JSON_HASH: {
198
199                         OSRF_BUFFER_ADD_CHAR(buf, '{');
200                         osrfHashIterator* itr = osrfNewHashIterator(obj->value.h);
201                         jsonObject* item;
202                         int i = 0;
203
204                         while( (item = osrfHashIteratorNext(itr)) ) {
205                                 if(i++ > 0) OSRF_BUFFER_ADD(buf, ",");
206                                 buffer_fadd(buf, "\"%s\":", itr->current);
207                                 char* json = jsonObjectToJSONRaw(item);
208                                 OSRF_BUFFER_ADD(buf, json);
209                                 free(json);
210                         }
211
212                         osrfHashIteratorFree(itr);
213                         OSRF_BUFFER_ADD_CHAR(buf, '}');
214                         break;
215                 }
216         }
217
218     return buffer_release(buf);
219 }
220
221
222 jsonIterator* jsonNewIterator(const jsonObject* obj) {
223         if(!obj) return NULL;
224         jsonIterator* itr;
225         OSRF_MALLOC(itr, sizeof(jsonIterator));
226
227         itr->obj                = (jsonObject*) obj;
228         itr->index      = 0;
229         itr->key                = NULL;
230
231         if( obj->type == JSON_HASH )
232                 itr->hashItr = osrfNewHashIterator(obj->value.h);
233         
234         return itr;
235 }
236
237 void jsonIteratorFree(jsonIterator* itr) {
238         if(!itr) return;
239         free(itr->key);
240         osrfHashIteratorFree(itr->hashItr);
241         free(itr);
242 }
243
244 jsonObject* jsonIteratorNext(jsonIterator* itr) {
245         if(!(itr && itr->obj)) return NULL;
246         if( itr->obj->type == JSON_HASH ) {
247                 if(!itr->hashItr) return NULL;
248                 jsonObject* item = osrfHashIteratorNext(itr->hashItr);
249                 free(itr->key);
250                 itr->key = strdup(itr->hashItr->current);
251                 return item;
252         } else {
253                 return jsonObjectGetIndex( itr->obj, itr->index++ );
254         }
255 }
256
257 int jsonIteratorHasNext(const jsonIterator* itr) {
258         if(!(itr && itr->obj)) return 0;
259         if( itr->obj->type == JSON_HASH )
260                 return osrfHashIteratorHasNext( itr->hashItr );
261         return (itr->index < itr->obj->size) ? 1 : 0;
262 }
263
264 jsonObject* jsonObjectGetIndex( const jsonObject* obj, unsigned long index ) {
265         if(!obj) return NULL;
266         return (obj->type == JSON_ARRAY) ? 
267         (OSRF_LIST_GET_INDEX(obj->value.l, index)) : NULL;
268 }
269
270
271
272 unsigned long jsonObjectRemoveIndex(jsonObject* dest, unsigned long index) {
273         if( dest && dest->type == JSON_ARRAY ) {
274                 osrfListRemove(dest->value.l, index);
275                 return dest->value.l->size;
276         }
277         return -1;
278 }
279
280
281 unsigned long jsonObjectRemoveKey( jsonObject* dest, const char* key) {
282         if( dest && key && dest->type == JSON_HASH ) {
283                 osrfHashRemove(dest->value.h, key);
284                 return 1;
285         }
286         return -1;
287 }
288
289 char* jsonObjectGetString(const jsonObject* obj) {
290         return (obj && obj->type == JSON_STRING) ? obj->value.s : NULL;
291 }
292
293 double jsonObjectGetNumber( const jsonObject* obj ) {
294         return (obj && obj->type == JSON_NUMBER) ? obj->value.n : 0;
295 }
296
297 void jsonObjectSetString(jsonObject* dest, const char* string) {
298         if(!(dest && string)) return;
299         JSON_INIT_CLEAR(dest, JSON_STRING);
300         free(dest->value.s);
301         dest->value.s = strdup(string);
302 }
303
304 void jsonObjectSetNumber(jsonObject* dest, double num) {
305         if(!dest) return;
306         JSON_INIT_CLEAR(dest, JSON_NUMBER);
307         dest->value.n = num;
308 }
309
310 void jsonObjectSetClass(jsonObject* dest, const char* classname ) {
311         if(!(dest && classname)) return;
312         free(dest->classname);
313         dest->classname = strdup(classname);
314 }
315 const char* jsonObjectGetClass(const jsonObject* dest) {
316     if(!dest) return NULL;
317     return dest->classname;
318 }
319
320 jsonObject* jsonObjectClone( const jsonObject* o ) {
321     if(!o) return jsonNewObject(NULL);
322
323     int i;
324     jsonObject* arr; 
325     jsonObject* hash; 
326     jsonIterator* itr;
327     jsonObject* tmp;
328     jsonObject* result = NULL;
329
330     switch(o->type) {
331         case JSON_NULL:
332             result = jsonNewObject(NULL);
333             break;
334         case JSON_STRING:
335             result = jsonNewObject(jsonObjectGetString(o));
336             break;
337         case JSON_NUMBER:
338             result = jsonNewNumberObject(jsonObjectGetNumber(o));
339             break;
340         case JSON_BOOL:
341             result = jsonNewBoolObject(jsonBoolIsTrue((jsonObject*) o));
342             break;
343         case JSON_ARRAY:
344             arr = jsonNewObject(NULL);
345             arr->type = JSON_ARRAY;
346             for(i=0; i < o->size; i++) 
347                 jsonObjectPush(arr, jsonObjectClone(jsonObjectGetIndex(o, i)));
348             result = arr;
349             break;
350         case JSON_HASH:
351             hash = jsonNewObject(NULL);
352             hash->type = JSON_HASH;
353             itr = jsonNewIterator(o);
354             while( (tmp = jsonIteratorNext(itr)) )
355                 jsonObjectSetKey(hash, itr->key, jsonObjectClone(tmp));
356             jsonIteratorFree(itr);
357             result = hash;
358             break;
359     }
360
361     jsonObjectSetClass(result, jsonObjectGetClass(o));
362     return result;
363 }
364
365 int jsonBoolIsTrue( jsonObject* boolObj ) {
366     if( boolObj && boolObj->type == JSON_BOOL && boolObj->value.b )
367         return 1;
368     return 0;
369 }
370
371
372 char* jsonObjectToSimpleString( const jsonObject* o ) {
373         if(!o) return NULL;
374
375         char* value = NULL;
376
377         switch( o->type ) {
378
379                 case JSON_NUMBER: {
380
381                         if( o->value.n == (int) o->value.n ) {
382                                 INT_TO_STRING((int) o->value.n);        
383                                 value = strdup(INTSTR);
384
385                         } else {
386                                 DOUBLE_TO_STRING(o->value.n);
387                                 value = strdup(DOUBLESTR);
388                         }
389
390                         break;
391                 }
392
393                 case JSON_STRING:
394                         value = strdup(o->value.s);
395         }
396
397         return value;
398 }
399
400