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 int status = _json_parse_string(string, &index, obj);
47 int _json_parse_string(char* string, unsigned long* index, object* obj) {
48 assert(string && index && *index < current_strlen);
50 int status = 0; /* return code from parsing routines */
51 char* classname = NULL; /* object class hint */
52 json_eat_ws(string, index, 1); /* remove leading whitespace */
54 char c = string[*index];
56 /* remove any leading comments */
60 (*index)++; /* move to second comment char */
61 status = json_eat_comment(string, index, &classname, 1);
62 if(status) return status;
64 json_eat_ws(string, index, 1);
71 json_eat_ws(string, index, 1); /* remove leading whitespace */
73 if(*index >= current_strlen)
81 status = json_parse_json_string(string, index, obj);
87 status = json_parse_json_array(string, index, obj);
93 status = json_parse_json_object(string, index, obj);
99 status = json_parse_json_null(string, index, obj);
108 status = json_parse_json_bool(string, index, obj);
112 if(is_number(c) || c == '.' || c == '-') { /* are we a number? */
113 status = json_parse_json_number(string, index, obj);
114 if(status) return status;
119 /* we should never get here */
120 return json_handle_error(string, index, "_json_parse_string() final switch clause");
123 if(status) return status;
125 json_eat_ws(string, index, 1);
127 if( *index < current_strlen ) {
128 /* remove any trailing comments */
132 status = json_eat_comment(string, index, NULL, 0);
133 if(status) return status;
138 obj->set_class(obj, classname);
146 int json_parse_json_null(char* string, unsigned long* index, object* obj) {
148 if(*index >= (current_strlen - 3)) {
149 return json_handle_error(string, index,
150 "_parse_json_string(): invalid null" );
153 if(!strncasecmp(string + (*index), "null", 4)) {
158 return json_handle_error(string, index,
159 "_parse_json_string(): invalid null" );
163 /* should be at the first character of the bool at this point */
164 int json_parse_json_bool(char* string, unsigned long* index, object* obj) {
165 assert(string && obj && *index < current_strlen);
167 char* ret = "json_parse_json_bool(): truncated bool";
169 if( *index >= (current_strlen - 5))
170 return json_handle_error(string, index, ret);
172 if(!strncasecmp( string + (*index), "false", 5)) {
180 if( *index >= (current_strlen - 4))
181 return json_handle_error(string, index, ret);
183 if(!strncasecmp( string + (*index), "true", 4)) {
191 return json_handle_error(string, index, ret);
195 /* expecting the first character of the number */
196 int json_parse_json_number(char* string, unsigned long* index, object* obj) {
197 assert(string && obj && *index < current_strlen);
199 growing_buffer* buf = buffer_init(64);
200 char c = string[*index];
205 /* negative number? */
206 if(c == '-') { buffer_add(buf, "-"); (*index)++; }
208 while(*index < current_strlen) {
211 buffer_add_char(buf, c);
213 else if( c == '.' ) {
215 return json_handle_error(string, index,
216 "json_parse_json_number(): malformed json number");
219 buffer_add_char(buf, c);
231 obj->double_value = strtod(buf->buf, NULL);
238 obj->num_value = atol(buf->buf);
244 /* index should point to the character directly following the '['. when done
245 * index will point to the character directly following the ']' character
247 int json_parse_json_array(char* string, unsigned long* index, object* obj) {
248 assert(string && obj && index && *index < current_strlen);
251 int in_parse = 0; /* true if this array already contains one item */
254 while(*index < current_strlen) {
256 json_eat_ws(string, index, 1);
258 if(string[*index] == ']') {
264 json_eat_ws(string, index, 1);
265 if(string[*index] != ',') {
266 return json_handle_error(string, index,
267 "json_parse_json_array(): array not followed by a ','");
270 json_eat_ws(string, index, 1);
273 object* item = new_object(NULL);
274 status = _json_parse_string(string, index, item);
276 if(status) return status;
277 obj->push(obj, item);
285 /* index should point to the character directly following the '{'. when done
286 * index will point to the character directly following the '}'
288 int json_parse_json_object(char* string, unsigned long* index, object* obj) {
289 assert(string && obj && index && *index < current_strlen);
294 int in_parse = 0; /* true if we've already added one item to this object */
296 while(*index < current_strlen) {
298 json_eat_ws(string, index, 1);
300 if(string[*index] == '}') {
306 if(string[*index] != ',') {
307 return json_handle_error(string, index,
308 "json_parse_json_object(): object missing ',' betweenn elements" );
311 json_eat_ws(string, index, 1);
314 /* first we grab the hash key */
315 object* key_obj = new_object(NULL);
316 status = _json_parse_string(string, index, key_obj);
317 if(status) return status;
319 if(!key_obj->is_string) {
320 return json_handle_error(string, index,
321 "_json_parse_json_object(): hash key not a string");
324 char* key = key_obj->string_data;
326 json_eat_ws(string, index, 1);
328 if(string[*index] != ':') {
329 return json_handle_error(string, index,
330 "json_parse_json_object(): hash key not followed by ':' character");
335 /* now grab the value object */
336 json_eat_ws(string, index, 1);
337 object* value_obj = new_object(NULL);
338 status = _json_parse_string(string, index, value_obj);
339 if(status) return status;
341 /* put the data into the object and continue */
342 obj->add_key(obj, key, value_obj);
343 free_object(key_obj);
352 /* when done, index will point to the character after the closing quote */
353 int json_parse_json_string(char* string, unsigned long* index, object* obj) {
354 assert(string && index && *index < current_strlen);
358 growing_buffer* buf = buffer_init(64);
360 while(*index < current_strlen) {
362 char c = string[*index];
368 buffer_add(buf, "\\");
376 buffer_add(buf, "\"");
384 buffer_add(buf,"\t");
387 buffer_add_char(buf, c);
392 buffer_add(buf,"\b");
395 buffer_add_char(buf, c);
400 buffer_add(buf,"\f");
403 buffer_add_char(buf, c);
408 buffer_add(buf,"\r");
411 buffer_add_char(buf, c);
416 buffer_add(buf,"\n");
419 buffer_add_char(buf, c);
426 if(*index >= (current_strlen - 4)) {
427 return json_handle_error(string, index,
428 "json_parse_json_string(): truncated escaped unicode"); }
432 memcpy(buff, string + (*index), 4);
435 /* ----------------------------------------------------------------------- */
436 /* ----------------------------------------------------------------------- */
437 /* The following chunk was borrowed with permission from
438 json-c http://oss.metaparadigm.com/json-c/ */
439 unsigned char utf_out[3];
442 #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
444 unsigned int ucs_char =
445 (hexdigit(string[*index] ) << 12) +
446 (hexdigit(string[*index + 1]) << 8) +
447 (hexdigit(string[*index + 2]) << 4) +
448 hexdigit(string[*index + 3]);
450 if (ucs_char < 0x80) {
451 utf_out[0] = ucs_char;
452 buffer_add(buf, utf_out);
454 } else if (ucs_char < 0x800) {
455 utf_out[0] = 0xc0 | (ucs_char >> 6);
456 utf_out[1] = 0x80 | (ucs_char & 0x3f);
457 buffer_add(buf, utf_out);
460 utf_out[0] = 0xe0 | (ucs_char >> 12);
461 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
462 utf_out[2] = 0x80 | (ucs_char & 0x3f);
463 buffer_add(buf, utf_out);
465 /* ----------------------------------------------------------------------- */
466 /* ----------------------------------------------------------------------- */
473 buffer_add_char(buf, c);
479 buffer_add_char(buf, c);
486 obj->set_string(obj, buf->buf);
492 void json_eat_ws(char* string, unsigned long* index, int eat_all) {
493 assert(string && index);
494 if(*index >= current_strlen)
497 if( eat_all ) { /* removes newlines, etc */
498 while(string[*index] == ' ' ||
499 string[*index] == '\n' ||
500 string[*index] == '\t')
505 while(string[*index] == ' ') (*index)++;
509 /* index should be at the '*' character at the beginning of the comment.
510 * when done, index will point to the first character after the final /
512 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
513 assert(string && index && *index < current_strlen);
515 if(string[*index] != '*' && string[*index] != '/' )
516 return json_handle_error(string, index,
517 "json_eat_comment(): invalid character after /");
519 /* chop out any // style comments */
520 if(string[*index] == '/') {
522 char c = string[*index];
523 while(*index < current_strlen) {
534 int on_star = 0; /* true if we just saw a '*' character */
536 /* we're just past the '*' */
537 if(!parse_class) { /* we're not concerned with class hints */
538 while(*index < current_strlen) {
539 if(string[*index] == '/') {
546 if(string[*index] == '*') on_star = 1;
556 growing_buffer* buf = buffer_init(64);
566 /*--S hint--*/ /* <-- Hints look like this */
569 while(*index < current_strlen) {
570 char c = string[*index];
576 if(third_dash) fourth_dash = 1;
577 else if(in_hint) third_dash = 1;
578 else if(first_dash) second_dash = 1;
584 if(second_dash && !in_hint) {
586 json_eat_ws(string, index, 1);
587 (*index)--; /* this will get incremented at the bottom of the loop */
594 if(second_dash && !in_hint) {
596 json_eat_ws(string, index, 1);
597 (*index)--; /* this will get incremented at the bottom of the loop */
616 buffer_add_char(buf, c);
623 if( buf->n_used > 0 && buffer)
624 *buffer = buffer_data(buf);
630 int is_number(char c) {
647 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
653 strncpy( buf, string + (*index - 30), 59 );
655 strncpy( buf, string, 59 );
658 "\nError parsing json string at charracter %c "
659 "(code %d) and index %ld\nMsg:\t%s\nNear:\t%s\n\n",
660 string[*index], string[*index], *index, err_msg, buf );