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)) {
179 if( *index >= (current_strlen - 4))
180 return json_handle_error(string, index, ret);
182 if(!strncasecmp( string + (*index), "true", 4)) {
189 return json_handle_error(string, index, ret);
193 /* expecting the first character of the number */
194 int json_parse_json_number(char* string, unsigned long* index, object* obj) {
195 assert(string && obj && *index < current_strlen);
197 growing_buffer* buf = buffer_init(64);
198 char c = string[*index];
203 /* negative number? */
204 if(c == '-') { buffer_add(buf, "-"); (*index)++; }
206 while(*index < current_strlen) {
209 buffer_add_char(buf, c);
211 else if( c == '.' ) {
213 return json_handle_error(string, index,
214 "json_parse_json_number(): malformed json number");
217 buffer_add_char(buf, c);
228 obj->double_value = strtod(buf->buf, NULL);
234 obj->num_value = atol(buf->buf);
240 /* index should point to the character directly following the '['. when done
241 * index will point to the character directly following the ']' character
243 int json_parse_json_array(char* string, unsigned long* index, object* obj) {
244 assert(string && obj && index && *index < current_strlen);
247 int in_parse = 0; /* true if this array already contains one item */
249 while(*index < current_strlen) {
251 json_eat_ws(string, index, 1);
253 if(string[*index] == ']') {
259 json_eat_ws(string, index, 1);
260 if(string[*index] != ',') {
261 return json_handle_error(string, index,
262 "json_parse_json_array(): array not followed by a ','");
265 json_eat_ws(string, index, 1);
268 object* item = new_object(NULL);
269 status = _json_parse_string(string, index, item);
271 if(status) return status;
272 obj->push(obj, item);
280 /* index should point to the character directly following the '{'. when done
281 * index will point to the character directly following the '}'
283 int json_parse_json_object(char* string, unsigned long* index, object* obj) {
284 assert(string && obj && index && *index < current_strlen);
288 int in_parse = 0; /* true if we've already added one item to this object */
290 while(*index < current_strlen) {
292 json_eat_ws(string, index, 1);
294 if(string[*index] == '}') {
300 if(string[*index] != ',') {
301 return json_handle_error(string, index,
302 "json_parse_json_object(): object missing ',' betweenn elements" );
305 json_eat_ws(string, index, 1);
308 /* first we grab the hash key */
309 object* key_obj = new_object(NULL);
310 status = _json_parse_string(string, index, key_obj);
311 if(status) return status;
313 if(!key_obj->is_string) {
314 return json_handle_error(string, index,
315 "_json_parse_json_object(): hash key not a string");
318 char* key = key_obj->string_data;
320 json_eat_ws(string, index, 1);
322 if(string[*index] != ':') {
323 return json_handle_error(string, index,
324 "json_parse_json_object(): hash key not followed by ':' character");
329 /* now grab the value object */
330 json_eat_ws(string, index, 1);
331 object* value_obj = new_object(NULL);
332 status = _json_parse_string(string, index, value_obj);
333 if(status) return status;
335 /* put the data into the object and continue */
336 obj->add_key(obj, key, value_obj);
337 free_object(key_obj);
345 /* when done, index will point to the character after the closing quote */
346 int json_parse_json_string(char* string, unsigned long* index, object* obj) {
347 assert(string && index && *index < current_strlen);
351 growing_buffer* buf = buffer_init(64);
353 while(*index < current_strlen) {
355 char c = string[*index];
361 buffer_add(buf, "\\");
369 buffer_add(buf, "\"");
377 buffer_add(buf,"\t");
380 buffer_add_char(buf, c);
385 buffer_add(buf,"\b");
388 buffer_add_char(buf, c);
393 buffer_add(buf,"\f");
396 buffer_add_char(buf, c);
401 buffer_add(buf,"\r");
404 buffer_add_char(buf, c);
409 buffer_add(buf,"\n");
412 buffer_add_char(buf, c);
419 if(*index >= (current_strlen - 4)) {
420 return json_handle_error(string, index,
421 "json_parse_json_string(): truncated escaped unicode"); }
425 memcpy(buff, string + (*index), 4);
428 /* ----------------------------------------------------------------------- */
429 /* ----------------------------------------------------------------------- */
430 /* The following chunk was borrowed with permission from
431 json-c http://oss.metaparadigm.com/json-c/ */
432 unsigned char utf_out[3];
435 #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
437 unsigned int ucs_char =
438 (hexdigit(string[*index] ) << 12) +
439 (hexdigit(string[*index + 1]) << 8) +
440 (hexdigit(string[*index + 2]) << 4) +
441 hexdigit(string[*index + 3]);
443 if (ucs_char < 0x80) {
444 utf_out[0] = ucs_char;
445 buffer_add(buf, utf_out);
447 } else if (ucs_char < 0x800) {
448 utf_out[0] = 0xc0 | (ucs_char >> 6);
449 utf_out[1] = 0x80 | (ucs_char & 0x3f);
450 buffer_add(buf, utf_out);
453 utf_out[0] = 0xe0 | (ucs_char >> 12);
454 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
455 utf_out[2] = 0x80 | (ucs_char & 0x3f);
456 buffer_add(buf, utf_out);
458 /* ----------------------------------------------------------------------- */
459 /* ----------------------------------------------------------------------- */
466 buffer_add_char(buf, c);
472 buffer_add_char(buf, c);
479 obj->set_string(obj, buf->buf);
485 void json_eat_ws(char* string, unsigned long* index, int eat_all) {
486 assert(string && index);
487 if(*index >= current_strlen)
490 if( eat_all ) { /* removes newlines, etc */
491 while(string[*index] == ' ' ||
492 string[*index] == '\n' ||
493 string[*index] == '\t')
498 while(string[*index] == ' ') (*index)++;
502 /* index should be at the '*' character at the beginning of the comment.
503 * when done, index will point to the first character after the final /
505 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
506 assert(string && index && *index < current_strlen);
508 if(string[*index] != '*' && string[*index] != '/' )
509 return json_handle_error(string, index,
510 "json_eat_comment(): invalid character after /");
512 /* chop out any // style comments */
513 if(string[*index] == '/') {
515 char c = string[*index];
516 while(*index < current_strlen) {
527 int on_star = 0; /* true if we just saw a '*' character */
529 /* we're just past the '*' */
530 if(!parse_class) { /* we're not concerned with class hints */
531 while(*index < current_strlen) {
532 if(string[*index] == '/') {
539 if(string[*index] == '*') on_star = 1;
549 growing_buffer* buf = buffer_init(64);
559 /*--S hint--*/ /* <-- Hints look like this */
562 while(*index < current_strlen) {
563 char c = string[*index];
569 if(third_dash) fourth_dash = 1;
570 else if(in_hint) third_dash = 1;
571 else if(first_dash) second_dash = 1;
577 if(second_dash && !in_hint) {
579 json_eat_ws(string, index, 1);
580 (*index)--; /* this will get incremented at the bottom of the loop */
587 if(second_dash && !in_hint) {
589 json_eat_ws(string, index, 1);
590 (*index)--; /* this will get incremented at the bottom of the loop */
609 buffer_add_char(buf, c);
616 if( buf->n_used > 0 && buffer)
617 *buffer = buffer_data(buf);
623 int is_number(char c) {
640 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
646 strncpy( buf, string + (*index - 30), 59 );
648 strncpy( buf, string, 59 );
651 "\nError parsing json string at charracter %c "
652 "(code %d) and index %ld\nMsg:\t%s\nNear:\t%s\n\n",
653 string[*index], string[*index], *index, err_msg, buf );