2 Copyright (C) 2006 Georgia Public Library Service
3 Bill Erickson <billserickson@gmail.com>
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.
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.
16 #include <opensrf/osrf_json.h>
17 #include <opensrf/osrf_json_utils.h>
19 jsonObject* jsonNewObject(const char* data) {
22 OSRF_MALLOC(o, sizeof(jsonObject));
26 o->type = JSON_STRING;
27 o->value.s = strdup(data);
33 jsonObject* jsonNewObjectFmt(const char* data, ...) {
36 OSRF_MALLOC(o, sizeof(jsonObject));
40 VA_LIST_TO_STRING(data);
41 o->type = JSON_STRING;
42 o->value.s = strdup(VA_BUF);
48 jsonObject* jsonNewNumberObject( double num ) {
49 jsonObject* o = jsonNewObject(NULL);
50 o->type = JSON_NUMBER;
55 jsonObject* jsonNewBoolObject(int val) {
56 jsonObject* o = jsonNewObject(NULL);
62 jsonObject* jsonNewObjectType(int type) {
63 jsonObject* o = jsonNewObject(NULL);
68 void jsonObjectFree( jsonObject* o ) {
70 if(!o || o->parent) return;
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;
81 static void _jsonFreeHashItem(char* key, void* item){
83 jsonObject* o = (jsonObject*) item;
84 o->parent = NULL; /* detach the item */
87 static void _jsonFreeListItem(void* item){
89 jsonObject* o = (jsonObject*) item;
90 o->parent = NULL; /* detach the item */
94 void jsonSetBool(jsonObject* bl, int val) {
96 JSON_INIT_CLEAR(bl, JSON_BOOL);
100 unsigned long jsonObjectPush(jsonObject* o, jsonObject* newo) {
102 if(!newo) newo = jsonNewObject(NULL);
103 JSON_INIT_CLEAR(o, JSON_ARRAY);
105 osrfListPush( o->value.l, newo );
106 o->size = o->value.l->size;
110 unsigned long jsonObjectSetIndex(jsonObject* dest, unsigned long index, jsonObject* newObj) {
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;
120 unsigned long jsonObjectSetKey( jsonObject* o, const char* key, jsonObject* newo) {
122 if(!newo) newo = jsonNewObject(NULL);
123 JSON_INIT_CLEAR(o, JSON_HASH);
125 osrfHashSet( o->value.h, newo, key );
126 o->size = o->value.h->size;
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);
135 char* jsonObjectToJSON( const jsonObject* obj ) {
136 jsonObject* obj2 = jsonObjectEncodeClass( (jsonObject*) obj);
137 char* json = jsonObjectToJSONRaw(obj2);
138 jsonObjectFree(obj2);
142 char* jsonObjectToJSONRaw( const jsonObject* obj ) {
143 if(!obj) return NULL;
144 growing_buffer* buf = buffer_init(32);
151 if(obj->value.b) OSRF_BUFFER_ADD(buf, "true");
152 else OSRF_BUFFER_ADD(buf, "false");
156 double x = obj->value.n;
158 INT_TO_STRING((int)x);
159 OSRF_BUFFER_ADD(buf, INTSTR);
163 OSRF_BUFFER_ADD(buf, DOUBLESTR);
169 OSRF_BUFFER_ADD(buf, "null");
173 OSRF_BUFFER_ADD_CHAR(buf, '"');
174 char* data = obj->value.s;
175 int len = strlen(data);
177 char* output = uescape(data, len, 1);
178 OSRF_BUFFER_ADD(buf, output);
180 OSRF_BUFFER_ADD_CHAR(buf, '"');
184 OSRF_BUFFER_ADD_CHAR(buf, '[');
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);
193 OSRF_BUFFER_ADD_CHAR(buf, ']');
199 OSRF_BUFFER_ADD_CHAR(buf, '{');
200 osrfHashIterator* itr = osrfNewHashIterator(obj->value.h);
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);
212 osrfHashIteratorFree(itr);
213 OSRF_BUFFER_ADD_CHAR(buf, '}');
218 return buffer_release(buf);
222 jsonIterator* jsonNewIterator(const jsonObject* obj) {
223 if(!obj) return NULL;
225 OSRF_MALLOC(itr, sizeof(jsonIterator));
227 itr->obj = (jsonObject*) obj;
231 if( obj->type == JSON_HASH )
232 itr->hashItr = osrfNewHashIterator(obj->value.h);
237 void jsonIteratorFree(jsonIterator* itr) {
240 osrfHashIteratorFree(itr->hashItr);
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);
250 itr->key = strdup(itr->hashItr->current);
253 return jsonObjectGetIndex( itr->obj, itr->index++ );
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;
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;
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;
281 unsigned long jsonObjectRemoveKey( jsonObject* dest, const char* key) {
282 if( dest && key && dest->type == JSON_HASH ) {
283 osrfHashRemove(dest->value.h, key);
289 char* jsonObjectGetString(const jsonObject* obj) {
290 return (obj && obj->type == JSON_STRING) ? obj->value.s : NULL;
293 double jsonObjectGetNumber( const jsonObject* obj ) {
294 return (obj && obj->type == JSON_NUMBER) ? obj->value.n : 0;
297 void jsonObjectSetString(jsonObject* dest, const char* string) {
298 if(!(dest && string)) return;
299 JSON_INIT_CLEAR(dest, JSON_STRING);
301 dest->value.s = strdup(string);
304 void jsonObjectSetNumber(jsonObject* dest, double num) {
306 JSON_INIT_CLEAR(dest, JSON_NUMBER);
310 void jsonObjectSetClass(jsonObject* dest, const char* classname ) {
311 if(!(dest && classname)) return;
312 free(dest->classname);
313 dest->classname = strdup(classname);
315 const char* jsonObjectGetClass(const jsonObject* dest) {
316 if(!dest) return NULL;
317 return dest->classname;
320 jsonObject* jsonObjectClone( const jsonObject* o ) {
321 if(!o) return jsonNewObject(NULL);
328 jsonObject* result = NULL;
332 result = jsonNewObject(NULL);
335 result = jsonNewObject(jsonObjectGetString(o));
338 result = jsonNewNumberObject(jsonObjectGetNumber(o));
341 result = jsonNewBoolObject(jsonBoolIsTrue((jsonObject*) o));
344 arr = jsonNewObject(NULL);
345 arr->type = JSON_ARRAY;
346 for(i=0; i < o->size; i++)
347 jsonObjectPush(arr, jsonObjectClone(jsonObjectGetIndex(o, i)));
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);
361 jsonObjectSetClass(result, jsonObjectGetClass(o));
365 int jsonBoolIsTrue( jsonObject* boolObj ) {
366 if( boolObj && boolObj->type == JSON_BOOL && boolObj->value.b )
372 char* jsonObjectToSimpleString( const jsonObject* o ) {
381 if( o->value.n == (int) o->value.n ) {
382 INT_TO_STRING((int) o->value.n);
383 value = strdup(INTSTR);
386 DOUBLE_TO_STRING(o->value.n);
387 value = strdup(DOUBLESTR);
394 value = strdup(o->value.s);