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( const char* string) {
26 return json_parse_string( (char*) string );
29 jsonObject* legacy_jsonParseStringFmt( const 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);
103 status = json_parse_json_array(string, index, obj, current_strlen);
109 status = json_parse_json_object(string, index, obj, current_strlen);
115 status = json_parse_json_null(string, index, obj, current_strlen);
124 status = json_parse_json_bool(string, index, obj, current_strlen);
128 if(isdigit(c) || c == '.' || c == '-') { /* are we a number? */
129 status = json_parse_json_number(string, index, obj, current_strlen);
130 if(status) return status;
135 /* we should never get here */
136 return json_handle_error(string, index, "_json_parse_string() final switch clause");
139 if(status) return status;
141 json_eat_ws(string, index, 1, current_strlen);
143 if( *index < current_strlen ) {
144 /* remove any trailing comments */
148 status = json_eat_comment(string, index, NULL, 0, current_strlen);
149 if(status) return status;
154 jsonObjectSetClass(obj, classname);
162 int json_parse_json_null(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
164 if(*index >= (current_strlen - 3)) {
165 return json_handle_error(string, index,
166 "_parse_json_null(): invalid null" );
169 if(!strncasecmp(string + (*index), "null", 4)) {
171 obj->type = JSON_NULL;
174 return json_handle_error(string, index,
175 "_parse_json_null(): invalid null" );
179 /* should be at the first character of the bool at this point */
180 int json_parse_json_bool(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
181 if( ! string || ! obj || *index >= current_strlen ) return -1;
183 char* ret = "json_parse_json_bool(): truncated bool";
185 if( *index >= (current_strlen - 5))
186 return json_handle_error(string, index, ret);
188 if(!strncasecmp( string + (*index), "false", 5)) {
191 obj->type = JSON_BOOL;
195 if( *index >= (current_strlen - 4))
196 return json_handle_error(string, index, ret);
198 if(!strncasecmp( string + (*index), "true", 4)) {
201 obj->type = JSON_BOOL;
205 return json_handle_error(string, index, ret);
209 /* expecting the first character of the number */
210 int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj, int current_strlen) {
211 if( ! string || ! obj || *index >= current_strlen ) return -1;
213 growing_buffer* buf = buffer_init(64);
214 char c = string[*index];
219 /* negative number? */
220 if(c == '-') { buffer_add(buf, "-"); (*index)++; }
224 while(*index < current_strlen) {
227 buffer_add_char(buf, c);
230 else if( c == '.' ) {
233 return json_handle_error(string, index,
234 "json_parse_json_number(): malformed json number");
237 buffer_add_char(buf, c);
247 obj->type = JSON_NUMBER;
248 obj->value.s = buffer_release(buf);
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"); }
481 /* ----------------------------------------------------------------------- */
482 /* ----------------------------------------------------------------------- */
483 /* The following chunk was borrowed with permission from
484 json-c http://oss.metaparadigm.com/json-c/ */
485 unsigned char utf_out[3] = { '\0', '\0', '\0' };
487 #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
489 unsigned int ucs_char =
490 (hexdigit(string[*index] ) << 12) +
491 (hexdigit(string[*index + 1]) << 8) +
492 (hexdigit(string[*index + 2]) << 4) +
493 hexdigit(string[*index + 3]);
495 if (ucs_char < 0x80) {
496 utf_out[0] = ucs_char;
497 buffer_add(buf, (char*) utf_out);
499 } else if (ucs_char < 0x800) {
500 utf_out[0] = 0xc0 | (ucs_char >> 6);
501 utf_out[1] = 0x80 | (ucs_char & 0x3f);
502 buffer_add(buf, (char*) utf_out);
505 utf_out[0] = 0xe0 | (ucs_char >> 12);
506 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
507 utf_out[2] = 0x80 | (ucs_char & 0x3f);
508 buffer_add(buf, (char*) utf_out);
510 /* ----------------------------------------------------------------------- */
511 /* ----------------------------------------------------------------------- */
518 buffer_add_char(buf, c);
524 buffer_add_char(buf, c);
531 jsonObjectSetString(obj, buf->buf);
537 void json_eat_ws(char* string, unsigned long* index, int eat_all, int current_strlen) {
538 if( ! string || ! index ) return;
539 if(*index >= current_strlen)
542 if( eat_all ) { /* removes newlines, etc */
543 while(string[*index] == ' ' ||
544 string[*index] == '\n' ||
545 string[*index] == '\t')
550 while(string[*index] == ' ') (*index)++;
554 /* index should be at the '*' character at the beginning of the comment.
555 * when done, index will point to the first character after the final /
557 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class, int current_strlen) {
558 if( ! string || ! index || *index >= current_strlen ) return -1;
561 if(string[*index] != '*' && string[*index] != '/' )
562 return json_handle_error(string, index,
563 "json_eat_comment(): invalid character after /");
565 /* chop out any // style comments */
566 if(string[*index] == '/') {
568 char c = string[*index];
569 while(*index < current_strlen) {
580 int on_star = 0; /* true if we just saw a '*' character */
582 /* we're just past the '*' */
583 if(!parse_class) { /* we're not concerned with class hints */
584 while(*index < current_strlen) {
585 if(string[*index] == '/') {
592 if(string[*index] == '*') on_star = 1;
602 growing_buffer* buf = buffer_init(64);
612 /*--S hint--*/ /* <-- Hints look like this */
615 while(*index < current_strlen) {
616 char c = string[*index];
622 if(third_dash) fourth_dash = 1;
623 else if(in_hint) third_dash = 1;
624 else if(first_dash) second_dash = 1;
630 if(second_dash && !in_hint) {
632 json_eat_ws(string, index, 1, current_strlen);
633 (*index)--; /* this will get incremented at the bottom of the loop */
638 if(second_dash && in_hint) {
639 buffer_add_char(buf, c);
645 if(second_dash && !in_hint) {
647 json_eat_ws(string, index, 1, current_strlen);
648 (*index)--; /* this will get incremented at the bottom of the loop */
653 if(second_dash && in_hint) {
654 buffer_add_char(buf, c);
672 buffer_add_char(buf, c);
679 if( buf->n_used > 0 && buffer)
680 *buffer = buffer_data(buf);
686 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
689 osrf_clearbuf(buf, sizeof(buf));
692 strncpy( buf, string + (*index - 30), sizeof(buf) - 1 );
694 strncpy( buf, string, sizeof(buf) - 1 );
696 buf[ sizeof(buf) - 1 ] = '\0';
699 "\nError parsing json string at charracter %c "
700 "(code %d) and index %ld\nString length: %d\nMsg:\t%s\nNear:\t%s\nFull String:\t%s\n",
701 string[*index], string[*index], *index, current_strlen, err_msg, buf, string );
706 char* legacy_jsonObjectToJSON( const jsonObject* obj ) {
708 if(obj == NULL) return strdup("null");
710 growing_buffer* buf = buffer_init(64);
712 /* add class hints if we have a class name */
714 buffer_add(buf,"/*--S ");
715 buffer_add(buf,obj->classname);
716 buffer_add(buf, "--*/");
719 switch( obj->type ) {
722 if(obj->value.b) buffer_add(buf, "true");
723 else buffer_add(buf, "false");
727 buffer_add(buf, obj->value.s);
732 buffer_add(buf, "null");
736 buffer_add(buf, "\"");
737 char* data = obj->value.s;
738 int len = strlen(data);
740 char* output = uescape(data, len, 1);
741 buffer_add(buf, output);
743 buffer_add(buf, "\"");
747 buffer_add(buf, "[");
749 for( i = 0; i!= obj->size; i++ ) {
750 const jsonObject* x = jsonObjectGetIndex(obj,i);
751 char* data = legacy_jsonObjectToJSON(x);
752 buffer_add(buf, data);
754 if(i != obj->size - 1)
755 buffer_add(buf, ",");
757 buffer_add(buf, "]");
762 buffer_add(buf, "{");
763 jsonIterator* itr = jsonNewIterator(obj);
766 while( (tmp = jsonIteratorNext(itr)) ) {
768 buffer_add(buf, "\"");
770 char* key = itr->key;
771 int len = strlen(key);
772 char* output = uescape(key, len, 1);
773 buffer_add(buf, output);
776 buffer_add(buf, "\":");
777 char* data = legacy_jsonObjectToJSON(tmp);
778 buffer_add(buf, data);
779 if(jsonIteratorHasNext(itr))
780 buffer_add(buf, ",");
784 jsonIteratorFree(itr);
785 buffer_add(buf, "}");
789 fprintf(stderr, "Unknown object type %d\n", obj->type);
794 /* close out the object hint */
796 buffer_add(buf, "/*--E ");
797 buffer_add(buf, obj->classname);
798 buffer_add(buf, "--*/");
801 char* data = buffer_data(buf);
808 static jsonObjectNode* makeNode(jsonObject* obj, unsigned long index, char* key) {
809 jsonObjectNode* node = safe_malloc(sizeof(jsonObjectNode));
816 jsonObjectIterator* jsonNewObjectIterator(const jsonObject* obj) {
817 if(!obj) return NULL;
818 jsonObjectIterator* itr = safe_malloc(sizeof(jsonObjectIterator));
819 itr->iterator = jsonNewIterator(obj);
826 jsonObjectNode* jsonObjectIteratorNext( jsonObjectIterator* itr ) {
827 if(itr == NULL || itr->done) return NULL;
829 if(itr->current) free(itr->current);
830 jsonObject* next = jsonIteratorNext(itr->iterator);
836 itr->current = makeNode(next, itr->iterator->index, itr->iterator->key);
840 void jsonObjectIteratorFree(jsonObjectIterator* iter) {
841 if(iter->current) free(iter->current);
842 jsonIteratorFree(iter->iterator);
846 int jsonObjectIteratorHasNext(const jsonObjectIterator* itr) {
847 return (itr && itr->current);