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)++; }
210 while(*index < current_strlen) {
213 buffer_add_char(buf, c);
215 else if( c == '.' ) {
217 return json_handle_error(string, index,
218 "json_parse_json_number(): malformed json number");
221 buffer_add_char(buf, c);
233 obj->double_value = strtod(buf->buf, NULL);
240 obj->num_value = atol(buf->buf);
246 /* index should point to the character directly following the '['. when done
247 * index will point to the character directly following the ']' character
249 int json_parse_json_array(char* string, unsigned long* index, object* obj) {
250 assert(string && obj && index && *index < current_strlen);
253 int in_parse = 0; /* true if this array already contains one item */
256 while(*index < current_strlen) {
258 json_eat_ws(string, index, 1);
260 if(string[*index] == ']') {
266 json_eat_ws(string, index, 1);
267 if(string[*index] != ',') {
268 return json_handle_error(string, index,
269 "json_parse_json_array(): array not followed by a ','");
272 json_eat_ws(string, index, 1);
275 object* item = new_object(NULL);
276 status = _json_parse_string(string, index, item);
278 if(status) return status;
279 obj->push(obj, item);
287 /* index should point to the character directly following the '{'. when done
288 * index will point to the character directly following the '}'
290 int json_parse_json_object(char* string, unsigned long* index, object* obj) {
291 assert(string && obj && index && *index < current_strlen);
296 int in_parse = 0; /* true if we've already added one item to this object */
298 while(*index < current_strlen) {
300 json_eat_ws(string, index, 1);
302 if(string[*index] == '}') {
308 if(string[*index] != ',') {
309 return json_handle_error(string, index,
310 "json_parse_json_object(): object missing ',' betweenn elements" );
313 json_eat_ws(string, index, 1);
316 /* first we grab the hash key */
317 object* key_obj = new_object(NULL);
318 status = _json_parse_string(string, index, key_obj);
319 if(status) return status;
321 if(!key_obj->is_string) {
322 return json_handle_error(string, index,
323 "_json_parse_json_object(): hash key not a string");
326 char* key = key_obj->string_data;
328 json_eat_ws(string, index, 1);
330 if(string[*index] != ':') {
331 return json_handle_error(string, index,
332 "json_parse_json_object(): hash key not followed by ':' character");
337 /* now grab the value object */
338 json_eat_ws(string, index, 1);
339 object* value_obj = new_object(NULL);
340 status = _json_parse_string(string, index, value_obj);
341 if(status) return status;
343 /* put the data into the object and continue */
344 obj->add_key(obj, key, value_obj);
345 free_object(key_obj);
354 /* when done, index will point to the character after the closing quote */
355 int json_parse_json_string(char* string, unsigned long* index, object* obj) {
356 assert(string && index && *index < current_strlen);
360 growing_buffer* buf = buffer_init(64);
362 while(*index < current_strlen) {
364 char c = string[*index];
370 buffer_add(buf, "\\");
378 buffer_add(buf, "\"");
386 buffer_add(buf,"\t");
389 buffer_add_char(buf, c);
394 buffer_add(buf,"\b");
397 buffer_add_char(buf, c);
402 buffer_add(buf,"\f");
405 buffer_add_char(buf, c);
410 buffer_add(buf,"\r");
413 buffer_add_char(buf, c);
418 buffer_add(buf,"\n");
421 buffer_add_char(buf, c);
428 if(*index >= (current_strlen - 4)) {
429 return json_handle_error(string, index,
430 "json_parse_json_string(): truncated escaped unicode"); }
434 memcpy(buff, string + (*index), 4);
437 /* ----------------------------------------------------------------------- */
438 /* ----------------------------------------------------------------------- */
439 /* The following chunk was borrowed with permission from
440 json-c http://oss.metaparadigm.com/json-c/ */
441 unsigned char utf_out[3];
444 #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
446 unsigned int ucs_char =
447 (hexdigit(string[*index] ) << 12) +
448 (hexdigit(string[*index + 1]) << 8) +
449 (hexdigit(string[*index + 2]) << 4) +
450 hexdigit(string[*index + 3]);
452 if (ucs_char < 0x80) {
453 utf_out[0] = ucs_char;
454 buffer_add(buf, utf_out);
456 } else if (ucs_char < 0x800) {
457 utf_out[0] = 0xc0 | (ucs_char >> 6);
458 utf_out[1] = 0x80 | (ucs_char & 0x3f);
459 buffer_add(buf, utf_out);
462 utf_out[0] = 0xe0 | (ucs_char >> 12);
463 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
464 utf_out[2] = 0x80 | (ucs_char & 0x3f);
465 buffer_add(buf, utf_out);
467 /* ----------------------------------------------------------------------- */
468 /* ----------------------------------------------------------------------- */
475 buffer_add_char(buf, c);
481 buffer_add_char(buf, c);
488 obj->set_string(obj, buf->buf);
494 void json_eat_ws(char* string, unsigned long* index, int eat_all) {
495 assert(string && index);
496 if(*index >= current_strlen)
499 if( eat_all ) { /* removes newlines, etc */
500 while(string[*index] == ' ' ||
501 string[*index] == '\n' ||
502 string[*index] == '\t')
507 while(string[*index] == ' ') (*index)++;
511 /* index should be at the '*' character at the beginning of the comment.
512 * when done, index will point to the first character after the final /
514 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
515 assert(string && index && *index < current_strlen);
517 if(string[*index] != '*' && string[*index] != '/' )
518 return json_handle_error(string, index,
519 "json_eat_comment(): invalid character after /");
521 /* chop out any // style comments */
522 if(string[*index] == '/') {
524 char c = string[*index];
525 while(*index < current_strlen) {
536 int on_star = 0; /* true if we just saw a '*' character */
538 /* we're just past the '*' */
539 if(!parse_class) { /* we're not concerned with class hints */
540 while(*index < current_strlen) {
541 if(string[*index] == '/') {
548 if(string[*index] == '*') on_star = 1;
558 growing_buffer* buf = buffer_init(64);
568 /*--S hint--*/ /* <-- Hints look like this */
571 while(*index < current_strlen) {
572 char c = string[*index];
578 if(third_dash) fourth_dash = 1;
579 else if(in_hint) third_dash = 1;
580 else if(first_dash) second_dash = 1;
586 if(second_dash && !in_hint) {
588 json_eat_ws(string, index, 1);
589 (*index)--; /* this will get incremented at the bottom of the loop */
596 if(second_dash && !in_hint) {
598 json_eat_ws(string, index, 1);
599 (*index)--; /* this will get incremented at the bottom of the loop */
618 buffer_add_char(buf, c);
625 if( buf->n_used > 0 && buffer)
626 *buffer = buffer_data(buf);
632 int is_number(char c) {
649 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
655 strncpy( buf, string + (*index - 30), 59 );
657 strncpy( buf, string, 59 );
660 "\nError parsing json string at charracter %c "
661 "(code %d) and index %ld\nMsg:\t%s\nNear:\t%s\n\n",
662 string[*index], string[*index], *index, err_msg, buf );