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.
17 #include <opensrf/osrf_legacy_json.h>
19 /* keep a copy of the length of the current json string so we don't
20 * have to calculate it in each function
25 jsonObject* legacy_jsonParseString( char* string) {
26 return json_parse_string( string );
29 jsonObject* legacy_jsonParseStringFmt( char* string, ... ) {
30 VA_LIST_TO_STRING(string);
31 return json_parse_string( VA_BUF );
35 jsonObject* json_parse_string(char* string) {
37 if(string == NULL) return NULL;
39 current_strlen = strlen(string);
41 if(current_strlen == 0)
44 unsigned long index = 0;
46 json_eat_ws(string, &index, 1, current_strlen); /* remove leading whitespace */
47 if(index == current_strlen) return NULL;
49 jsonObject* obj = jsonNewObject(NULL);
51 int status = _json_parse_string(string, &index, obj, current_strlen);
52 if(!status) return obj;
63 int _json_parse_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
64 if( !string || !index || *index >= current_strlen) return -2;
66 int status = 0; /* return code from parsing routines */
67 char* classname = NULL; /* object class hint */
68 json_eat_ws(string, index, 1, current_strlen); /* remove leading whitespace */
70 char c = string[*index];
72 /* remove any leading comments */
76 (*index)++; /* move to second comment char */
77 status = json_eat_comment(string, index, &classname, 1, current_strlen);
78 if(status) return status;
80 json_eat_ws(string, index, 1, current_strlen);
87 json_eat_ws(string, index, 1, current_strlen); /* remove leading whitespace */
89 if(*index >= current_strlen)
97 status = json_parse_json_string(string, index, obj, current_strlen); break;
102 status = json_parse_json_array(string, index, obj, current_strlen);
108 status = json_parse_json_object(string, index, obj, current_strlen);
114 status = json_parse_json_null(string, index, obj, current_strlen);
123 status = json_parse_json_bool(string, index, obj, current_strlen);
127 if(isdigit(c) || c == '.' || c == '-') { /* are we a number? */
128 status = json_parse_json_number(string, index, obj, current_strlen);
129 if(status) return status;
134 /* we should never get here */
135 return json_handle_error(string, index, "_json_parse_string() final switch clause");
138 if(status) return status;
140 json_eat_ws(string, index, 1, current_strlen);
142 if( *index < current_strlen ) {
143 /* remove any trailing comments */
147 status = json_eat_comment(string, index, NULL, 0, current_strlen);
148 if(status) return status;
153 jsonObjectSetClass(obj, classname);
161 int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
163 if(*index >= (current_strlen - 3)) {
164 return json_handle_error(string, index,
165 "_parse_json_null(): invalid null" );
168 if(!strncasecmp(string + (*index), "null", 4)) {
170 obj->type = JSON_NULL;
173 return json_handle_error(string, index,
174 "_parse_json_null(): invalid null" );
178 /* should be at the first character of the bool at this point */
179 int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
180 if( ! string || ! obj || *index >= current_strlen ) return -1;
182 char* ret = "json_parse_json_bool(): truncated bool";
184 if( *index >= (current_strlen - 5))
185 return json_handle_error(string, index, ret);
187 if(!strncasecmp( string + (*index), "false", 5)) {
190 obj->type = JSON_BOOL;
194 if( *index >= (current_strlen - 4))
195 return json_handle_error(string, index, ret);
197 if(!strncasecmp( string + (*index), "true", 4)) {
200 obj->type = JSON_BOOL;
204 return json_handle_error(string, index, ret);
208 /* expecting the first character of the number */
209 int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
210 if( ! string || ! obj || *index >= current_strlen ) return -1;
212 growing_buffer* buf = buffer_init(64);
213 char c = string[*index];
218 /* negative number? */
219 if(c == '-') { buffer_add(buf, "-"); (*index)++; }
223 while(*index < current_strlen) {
226 buffer_add_char(buf, c);
229 else if( c == '.' ) {
232 return json_handle_error(string, index,
233 "json_parse_json_number(): malformed json number");
236 buffer_add_char(buf, c);
246 obj->type = JSON_NUMBER;
247 obj->value.n = strtod(buf->buf, NULL);
252 /* index should point to the character directly following the '['. when done
253 * index will point to the character directly following the ']' character
255 int json_parse_json_array(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
257 if( ! string || ! obj || ! index || *index >= current_strlen ) return -1;
260 int in_parse = 0; /* true if this array already contains one item */
261 obj->type = JSON_ARRAY;
265 while(*index < current_strlen) {
267 json_eat_ws(string, index, 1, current_strlen);
269 if(string[*index] == ']') {
276 json_eat_ws(string, index, 1, current_strlen);
277 if(string[*index] != ',') {
278 return json_handle_error(string, index,
279 "json_parse_json_array(): array item not followed by a ','");
282 json_eat_ws(string, index, 1, current_strlen);
285 jsonObject* item = jsonNewObject(NULL);
287 #ifndef STRICT_JSON_READ
288 if(*index < current_strlen) {
289 if(string[*index] == ',' || string[*index] == ']') {
294 if(!set) status = _json_parse_string(string, index, item, current_strlen);
297 status = _json_parse_string(string, index, item, current_strlen);
300 if(status) { jsonObjectFree(item); return status; }
301 jsonObjectPush(obj, item);
307 return json_handle_error(string, index,
308 "json_parse_json_array(): array not closed");
314 /* index should point to the character directly following the '{'. when done
315 * index will point to the character directly following the '}'
317 int json_parse_json_object(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
318 if( ! string || !obj || ! index || *index >= current_strlen ) return -1;
320 obj->type = JSON_HASH;
322 int in_parse = 0; /* true if we've already added one item to this object */
326 while(*index < current_strlen) {
328 json_eat_ws(string, index, 1, current_strlen);
330 if(string[*index] == '}') {
337 if(string[*index] != ',') {
338 return json_handle_error(string, index,
339 "json_parse_json_object(): object missing ',' between elements" );
342 json_eat_ws(string, index, 1, current_strlen);
345 /* first we grab the hash key */
346 jsonObject* key_obj = jsonNewObject(NULL);
347 status = _json_parse_string(string, index, key_obj, current_strlen);
348 if(status) return status;
350 if(key_obj->type != JSON_STRING) {
351 return json_handle_error(string, index,
352 "_json_parse_json_object(): hash key not a string");
355 char* key = key_obj->value.s;
357 json_eat_ws(string, index, 1, current_strlen);
359 if(string[*index] != ':') {
360 return json_handle_error(string, index,
361 "json_parse_json_object(): hash key not followed by ':' character");
366 /* now grab the value object */
367 json_eat_ws(string, index, 1, current_strlen);
368 jsonObject* value_obj = jsonNewObject(NULL);
370 #ifndef STRICT_JSON_READ
371 if(*index < current_strlen) {
372 if(string[*index] == ',' || string[*index] == '}') {
378 status = _json_parse_string(string, index, value_obj, current_strlen);
381 status = _json_parse_string(string, index, value_obj, current_strlen);
384 if(status) return status;
386 /* put the data into the object and continue */
387 jsonObjectSetKey(obj, key, value_obj);
388 jsonObjectFree(key_obj);
394 return json_handle_error(string, index,
395 "json_parse_json_object(): object not closed");
402 /* when done, index will point to the character after the closing quote */
403 int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
404 if( ! string || ! index || *index >= current_strlen ) return -1;
408 growing_buffer* buf = buffer_init(64);
410 while(*index < current_strlen) {
412 char c = string[*index];
418 buffer_add(buf, "\\");
426 buffer_add(buf, "\"");
434 buffer_add(buf,"\t");
437 buffer_add_char(buf, c);
442 buffer_add(buf,"\b");
445 buffer_add_char(buf, c);
450 buffer_add(buf,"\f");
453 buffer_add_char(buf, c);
458 buffer_add(buf,"\r");
461 buffer_add_char(buf, c);
466 buffer_add(buf,"\n");
469 buffer_add_char(buf, c);
476 if(*index >= (current_strlen - 4)) {
478 return json_handle_error(string, index,
479 "json_parse_json_string(): truncated escaped unicode"); }
483 memcpy(buff, string + (*index), 4);
486 /* ----------------------------------------------------------------------- */
487 /* ----------------------------------------------------------------------- */
488 /* The following chunk was borrowed with permission from
489 json-c http://oss.metaparadigm.com/json-c/ */
490 unsigned char utf_out[3];
493 #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
495 unsigned int ucs_char =
496 (hexdigit(string[*index] ) << 12) +
497 (hexdigit(string[*index + 1]) << 8) +
498 (hexdigit(string[*index + 2]) << 4) +
499 hexdigit(string[*index + 3]);
501 if (ucs_char < 0x80) {
502 utf_out[0] = ucs_char;
503 buffer_add(buf, (char*) utf_out);
505 } else if (ucs_char < 0x800) {
506 utf_out[0] = 0xc0 | (ucs_char >> 6);
507 utf_out[1] = 0x80 | (ucs_char & 0x3f);
508 buffer_add(buf, (char*) utf_out);
511 utf_out[0] = 0xe0 | (ucs_char >> 12);
512 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
513 utf_out[2] = 0x80 | (ucs_char & 0x3f);
514 buffer_add(buf, (char*) utf_out);
516 /* ----------------------------------------------------------------------- */
517 /* ----------------------------------------------------------------------- */
524 buffer_add_char(buf, c);
530 buffer_add_char(buf, c);
537 jsonObjectSetString(obj, buf->buf);
543 void json_eat_ws(char* string, unsigned long* index, int eat_all, int current_strlen) {
544 if( ! string || ! index ) return;
545 if(*index >= current_strlen)
548 if( eat_all ) { /* removes newlines, etc */
549 while(string[*index] == ' ' ||
550 string[*index] == '\n' ||
551 string[*index] == '\t')
556 while(string[*index] == ' ') (*index)++;
560 /* index should be at the '*' character at the beginning of the comment.
561 * when done, index will point to the first character after the final /
563 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class, int current_strlen) {
564 if( ! string || ! index || *index >= current_strlen ) return -1;
567 if(string[*index] != '*' && string[*index] != '/' )
568 return json_handle_error(string, index,
569 "json_eat_comment(): invalid character after /");
571 /* chop out any // style comments */
572 if(string[*index] == '/') {
574 char c = string[*index];
575 while(*index < current_strlen) {
586 int on_star = 0; /* true if we just saw a '*' character */
588 /* we're just past the '*' */
589 if(!parse_class) { /* we're not concerned with class hints */
590 while(*index < current_strlen) {
591 if(string[*index] == '/') {
598 if(string[*index] == '*') on_star = 1;
608 growing_buffer* buf = buffer_init(64);
618 /*--S hint--*/ /* <-- Hints look like this */
621 while(*index < current_strlen) {
622 char c = string[*index];
628 if(third_dash) fourth_dash = 1;
629 else if(in_hint) third_dash = 1;
630 else if(first_dash) second_dash = 1;
636 if(second_dash && !in_hint) {
638 json_eat_ws(string, index, 1, current_strlen);
639 (*index)--; /* this will get incremented at the bottom of the loop */
644 if(second_dash && in_hint) {
645 buffer_add_char(buf, c);
651 if(second_dash && !in_hint) {
653 json_eat_ws(string, index, 1, current_strlen);
654 (*index)--; /* this will get incremented at the bottom of the loop */
659 if(second_dash && in_hint) {
660 buffer_add_char(buf, c);
678 buffer_add_char(buf, c);
685 if( buf->n_used > 0 && buffer)
686 *buffer = buffer_data(buf);
692 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
698 strncpy( buf, string + (*index - 30), 59 );
700 strncpy( buf, string, 59 );
703 "\nError parsing json string at charracter %c "
704 "(code %d) and index %ld\nString length: %d\nMsg:\t%s\nNear:\t%s\nFull String:\t%s\n",
705 string[*index], string[*index], *index, current_strlen, err_msg, buf, string );
711 jsonObject* legacy_jsonParseFile( const char* filename ) {
712 return json_parse_file( filename );
715 jsonObject* json_parse_file(const char* filename) {
716 if(!filename) return NULL;
717 char* data = file_to_string(filename);
718 jsonObject* o = json_parse_string(data);
725 char* legacy_jsonObjectToJSON( const jsonObject* obj ) {
727 if(obj == NULL) return strdup("null");
729 growing_buffer* buf = buffer_init(64);
731 /* add class hints if we have a class name */
733 buffer_add(buf,"/*--S ");
734 buffer_add(buf,obj->classname);
735 buffer_add(buf, "--*/");
738 switch( obj->type ) {
741 if(obj->value.b) buffer_add(buf, "true");
742 else buffer_add(buf, "false");
746 double x = obj->value.n;
748 /* if the number does not need to be a double,
749 turn it into an int on the way out */
751 INT_TO_STRING((int)x);
752 buffer_add(buf, INTSTR);
756 buffer_add(buf, DOUBLESTR);
762 buffer_add(buf, "null");
766 buffer_add(buf, "\"");
767 char* data = obj->value.s;
768 int len = strlen(data);
770 char* output = uescape(data, len, 1);
771 buffer_add(buf, output);
773 buffer_add(buf, "\"");
777 buffer_add(buf, "[");
779 for( i = 0; i!= obj->size; i++ ) {
780 const jsonObject* x = jsonObjectGetIndex(obj,i);
781 char* data = legacy_jsonObjectToJSON(x);
782 buffer_add(buf, data);
784 if(i != obj->size - 1)
785 buffer_add(buf, ",");
787 buffer_add(buf, "]");
792 buffer_add(buf, "{");
793 jsonIterator* itr = jsonNewIterator(obj);
796 while( (tmp = jsonIteratorNext(itr)) ) {
798 buffer_add(buf, "\"");
800 char* key = itr->key;
801 int len = strlen(key);
802 char* output = uescape(key, len, 1);
803 buffer_add(buf, output);
806 buffer_add(buf, "\":");
807 char* data = legacy_jsonObjectToJSON(tmp);
808 buffer_add(buf, data);
809 if(jsonIteratorHasNext(itr))
810 buffer_add(buf, ",");
814 jsonIteratorFree(itr);
815 buffer_add(buf, "}");
819 fprintf(stderr, "Unknown object type %d\n", obj->type);
824 /* close out the object hint */
826 buffer_add(buf, "/*--E ");
827 buffer_add(buf, obj->classname);
828 buffer_add(buf, "--*/");
831 char* data = buffer_data(buf);
838 static jsonObjectNode* makeNode(jsonObject* obj, unsigned long index, char* key) {
839 jsonObjectNode* node = safe_malloc(sizeof(jsonObjectNode));
846 jsonObjectIterator* jsonNewObjectIterator(const jsonObject* obj) {
847 if(!obj) return NULL;
848 jsonObjectIterator* itr = safe_malloc(sizeof(jsonObjectIterator));
849 itr->iterator = jsonNewIterator(obj);
856 jsonObjectNode* jsonObjectIteratorNext( jsonObjectIterator* itr ) {
857 if(itr == NULL || itr->done) return NULL;
859 if(itr->current) free(itr->current);
860 jsonObject* next = jsonIteratorNext(itr->iterator);
866 itr->current = makeNode(next, itr->iterator->index, itr->iterator->key);
870 void jsonObjectIteratorFree(jsonObjectIterator* iter) {
871 if(iter->current) free(iter->current);
872 jsonIteratorFree(iter->iterator);
876 int jsonObjectIteratorHasNext(const jsonObjectIterator* itr) {
877 return (itr && itr->current);