2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@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 "json_parser.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
22 int current_strlen; /* XXX need to move this into the function params for thread support */
24 object* json_parse_string(char* string) {
26 if(string == NULL) return NULL;
28 current_strlen = strlen(string);
30 if(current_strlen == 0)
33 object* obj = new_object(NULL);
34 unsigned long index = 0;
36 json_eat_ws(string, &index, 1); /* remove leading whitespace */
37 if(index == current_strlen) return NULL;
39 int status = _json_parse_string(string, &index, obj);
40 if(!status) return obj;
49 int _json_parse_string(char* string, unsigned long* index, object* obj) {
50 assert(string && index && *index < current_strlen);
52 int status = 0; /* return code from parsing routines */
53 char* classname = NULL; /* object class hint */
54 json_eat_ws(string, index, 1); /* remove leading whitespace */
56 char c = string[*index];
58 /* remove any leading comments */
62 (*index)++; /* move to second comment char */
63 status = json_eat_comment(string, index, &classname, 1);
64 if(status) return status;
66 json_eat_ws(string, index, 1);
73 json_eat_ws(string, index, 1); /* remove leading whitespace */
75 if(*index >= current_strlen)
83 status = json_parse_json_string(string, index, obj);
89 status = json_parse_json_array(string, index, obj);
95 status = json_parse_json_object(string, index, obj);
101 status = json_parse_json_null(string, index, obj);
110 status = json_parse_json_bool(string, index, obj);
114 if(is_number(c) || c == '.' || c == '-') { /* are we a number? */
115 status = json_parse_json_number(string, index, obj);
116 if(status) return status;
121 /* we should never get here */
122 return json_handle_error(string, index, "_json_parse_string() final switch clause");
125 if(status) return status;
127 json_eat_ws(string, index, 1);
129 if( *index < current_strlen ) {
130 /* remove any trailing comments */
134 status = json_eat_comment(string, index, NULL, 0);
135 if(status) return status;
140 obj->set_class(obj, classname);
148 int json_parse_json_null(char* string, unsigned long* index, object* obj) {
150 if(*index >= (current_strlen - 3)) {
151 return json_handle_error(string, index,
152 "_parse_json_string(): invalid null" );
155 if(!strncasecmp(string + (*index), "null", 4)) {
160 return json_handle_error(string, index,
161 "_parse_json_string(): invalid null" );
165 /* should be at the first character of the bool at this point */
166 int json_parse_json_bool(char* string, unsigned long* index, object* obj) {
167 assert(string && obj && *index < current_strlen);
169 char* ret = "json_parse_json_bool(): truncated bool";
171 if( *index >= (current_strlen - 5))
172 return json_handle_error(string, index, ret);
174 if(!strncasecmp( string + (*index), "false", 5)) {
182 if( *index >= (current_strlen - 4))
183 return json_handle_error(string, index, ret);
185 if(!strncasecmp( string + (*index), "true", 4)) {
193 return json_handle_error(string, index, ret);
197 /* expecting the first character of the number */
198 int json_parse_json_number(char* string, unsigned long* index, object* obj) {
199 assert(string && obj && *index < current_strlen);
201 growing_buffer* buf = buffer_init(64);
202 char c = string[*index];
207 /* negative number? */
208 if(c == '-') { buffer_add(buf, "-"); (*index)++; }
212 while(*index < current_strlen) {
215 buffer_add_char(buf, c);
218 else if( c == '.' ) {
220 return json_handle_error(string, index,
221 "json_parse_json_number(): malformed json number");
224 buffer_add_char(buf, c);
237 obj->double_value = strtod(buf->buf, NULL);
244 obj->num_value = atol(buf->buf);
250 /* index should point to the character directly following the '['. when done
251 * index will point to the character directly following the ']' character
253 int json_parse_json_array(char* string, unsigned long* index, object* obj) {
254 assert(string && obj && index && *index < current_strlen);
257 int in_parse = 0; /* true if this array already contains one item */
263 while(*index < current_strlen) {
265 json_eat_ws(string, index, 1);
267 if(string[*index] == ']') {
274 json_eat_ws(string, index, 1);
275 if(string[*index] != ',') {
276 return json_handle_error(string, index,
277 "json_parse_json_array(): array not followed by a ','");
280 json_eat_ws(string, index, 1);
283 object* item = new_object(NULL);
285 #ifndef STRICT_JSON_READ
286 if(*index < current_strlen) {
287 if(string[*index] == ',' || string[*index] == ']') {
293 status = _json_parse_string(string, index, item);
296 status = _json_parse_string(string, index, item);
299 if(status) return status;
300 obj->push(obj, item);
306 return json_handle_error(string, index,
307 "json_parse_json_array(): array not closed");
313 /* index should point to the character directly following the '{'. when done
314 * index will point to the character directly following the '}'
316 int json_parse_json_object(char* string, unsigned long* index, object* obj) {
317 assert(string && obj && index && *index < current_strlen);
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);
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);
345 /* first we grab the hash key */
346 object* key_obj = new_object(NULL);
347 status = _json_parse_string(string, index, key_obj);
348 if(status) return status;
350 if(!key_obj->is_string) {
351 return json_handle_error(string, index,
352 "_json_parse_json_object(): hash key not a string");
355 char* key = key_obj->string_data;
357 json_eat_ws(string, index, 1);
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);
368 object* value_obj = new_object(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);
381 status = _json_parse_string(string, index, value_obj);
384 if(status) return status;
386 /* put the data into the object and continue */
387 obj->add_key(obj, key, value_obj);
388 free_object(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, object* obj) {
404 assert(string && index && *index < current_strlen);
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)) {
477 return json_handle_error(string, index,
478 "json_parse_json_string(): truncated escaped unicode"); }
482 memcpy(buff, string + (*index), 4);
485 /* ----------------------------------------------------------------------- */
486 /* ----------------------------------------------------------------------- */
487 /* The following chunk was borrowed with permission from
488 json-c http://oss.metaparadigm.com/json-c/ */
489 unsigned char utf_out[3];
492 #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
494 unsigned int ucs_char =
495 (hexdigit(string[*index] ) << 12) +
496 (hexdigit(string[*index + 1]) << 8) +
497 (hexdigit(string[*index + 2]) << 4) +
498 hexdigit(string[*index + 3]);
500 if (ucs_char < 0x80) {
501 utf_out[0] = ucs_char;
502 buffer_add(buf, utf_out);
504 } else if (ucs_char < 0x800) {
505 utf_out[0] = 0xc0 | (ucs_char >> 6);
506 utf_out[1] = 0x80 | (ucs_char & 0x3f);
507 buffer_add(buf, utf_out);
510 utf_out[0] = 0xe0 | (ucs_char >> 12);
511 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
512 utf_out[2] = 0x80 | (ucs_char & 0x3f);
513 buffer_add(buf, utf_out);
515 /* ----------------------------------------------------------------------- */
516 /* ----------------------------------------------------------------------- */
523 buffer_add_char(buf, c);
529 buffer_add_char(buf, c);
536 obj->set_string(obj, buf->buf);
542 void json_eat_ws(char* string, unsigned long* index, int eat_all) {
543 assert(string && index);
544 if(*index >= current_strlen)
547 if( eat_all ) { /* removes newlines, etc */
548 while(string[*index] == ' ' ||
549 string[*index] == '\n' ||
550 string[*index] == '\t')
555 while(string[*index] == ' ') (*index)++;
559 /* index should be at the '*' character at the beginning of the comment.
560 * when done, index will point to the first character after the final /
562 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
563 assert(string && index && *index < current_strlen);
565 if(string[*index] != '*' && string[*index] != '/' )
566 return json_handle_error(string, index,
567 "json_eat_comment(): invalid character after /");
569 /* chop out any // style comments */
570 if(string[*index] == '/') {
572 char c = string[*index];
573 while(*index < current_strlen) {
584 int on_star = 0; /* true if we just saw a '*' character */
586 /* we're just past the '*' */
587 if(!parse_class) { /* we're not concerned with class hints */
588 while(*index < current_strlen) {
589 if(string[*index] == '/') {
596 if(string[*index] == '*') on_star = 1;
606 growing_buffer* buf = buffer_init(64);
616 /*--S hint--*/ /* <-- Hints look like this */
619 while(*index < current_strlen) {
620 char c = string[*index];
626 if(third_dash) fourth_dash = 1;
627 else if(in_hint) third_dash = 1;
628 else if(first_dash) second_dash = 1;
634 if(second_dash && !in_hint) {
636 json_eat_ws(string, index, 1);
637 (*index)--; /* this will get incremented at the bottom of the loop */
642 if(second_dash && in_hint) {
643 buffer_add_char(buf, c);
649 if(second_dash && !in_hint) {
651 json_eat_ws(string, index, 1);
652 (*index)--; /* this will get incremented at the bottom of the loop */
657 if(second_dash && in_hint) {
658 buffer_add_char(buf, c);
676 buffer_add_char(buf, c);
683 if( buf->n_used > 0 && buffer)
684 *buffer = buffer_data(buf);
690 int is_number(char c) {
707 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
713 strncpy( buf, string + (*index - 30), 59 );
715 strncpy( buf, string, 59 );
718 "\nError parsing json string at charracter %c "
719 "(code %d) and index %ld\nMsg:\t%s\nNear:\t%s\n\n",
720 string[*index], string[*index], *index, err_msg, buf );
725 object* json_parse_file(char* filename) {
726 if(!filename) return NULL;
727 char* data = file_to_string(filename);
728 object* o = json_parse_string(data);