]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/objson/json_parser.c
ac0de5f3d8153ddde6c39e7a5645f89c775379b3
[working/Evergreen.git] / OpenSRF / src / objson / json_parser.c
1 /*
2 Copyright (C) 2005  Georgia Public Library Service 
3 Bill Erickson <highfalutin@gmail.com>
4
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.
9
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.
14 */
15
16
17 #include "json_parser.h"
18
19 /* keep a copy of the length of the current json string so we don't 
20  * have to calculate it in each function
21  */
22 int current_strlen; /* XXX need to move this into the function params for thread support */
23
24 object* json_parse_string(char* string) {
25
26         if(string == NULL) return NULL;
27
28         current_strlen = strlen(string);
29
30         if(current_strlen == 0) 
31                 return NULL;
32
33         object* obj = new_object(NULL);
34         unsigned long index = 0;
35
36         json_eat_ws(string, &index, 1); /* remove leading whitespace */
37         if(index == current_strlen) return NULL;
38
39         int status = _json_parse_string(string, &index, obj);
40         if(!status) return obj;
41
42         if(status == -2)
43                 return NULL;
44
45         return NULL;
46 }
47
48
49 int _json_parse_string(char* string, unsigned long* index, object* obj) {
50         assert(string && index && *index < current_strlen);
51
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 */
55
56         char c = string[*index];
57
58         /* remove any leading comments */
59         if( c == '/' ) { 
60
61                 while(1) {
62                         (*index)++; /* move to second comment char */
63                         status = json_eat_comment(string, index, &classname, 1);
64                         if(status) return status;
65
66                         json_eat_ws(string, index, 1);
67                         c = string[*index];
68                         if(c != '/')
69                                 break;
70                 }
71         }
72
73         json_eat_ws(string, index, 1); /* remove leading whitespace */
74
75         if(*index >= current_strlen)
76                 return -2;
77
78         switch(c) {
79                                 
80                 /* json string */
81                 case '"': 
82                         (*index)++;
83                         status = json_parse_json_string(string, index, obj);
84                         break;
85
86                 /* json array */
87                 case '[':
88                         (*index)++;
89                         status = json_parse_json_array(string, index, obj);                     
90                         break;
91
92                 /* json object */
93                 case '{':
94                         (*index)++;
95                         status = json_parse_json_object(string, index, obj);
96                         break;
97
98                 /* NULL */
99                 case 'n':
100                 case 'N':
101                         status = json_parse_json_null(string, index, obj);
102                         break;
103                         
104
105                 /* true, false */
106                 case 'f':
107                 case 'F':
108                 case 't':
109                 case 'T':
110                         status = json_parse_json_bool(string, index, obj);
111                         break;
112
113                 default:
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;
117                                 break;
118                         }
119
120                         (*index)--;
121                         /* we should never get here */
122                         return json_handle_error(string, index, "_json_parse_string() final switch clause");
123         }       
124
125         if(status) return status;
126
127         json_eat_ws(string, index, 1);
128
129         if( *index < current_strlen ) {
130                 /* remove any trailing comments */
131                 c = string[*index];
132                 if( c == '/' ) { 
133                         (*index)++;
134                         status = json_eat_comment(string, index, NULL, 0);
135                         if(status) return status;
136                 }
137         }
138
139         if(classname){
140                 obj->set_class(obj, classname);
141                 free(classname);
142         }
143
144         return 0;
145 }
146
147
148 int json_parse_json_null(char* string, unsigned long* index, object* obj) {
149
150         if(*index >= (current_strlen - 3)) {
151                 return json_handle_error(string, index, 
152                         "_parse_json_string(): invalid null" );
153         }
154
155         if(!strncasecmp(string + (*index), "null", 4)) {
156                 (*index) += 4;
157                 obj->is_null = 1;
158                 return 0;
159         } else {
160                 return json_handle_error(string, index,
161                         "_parse_json_string(): invalid null" );
162         }
163 }
164
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);
168
169         char* ret = "json_parse_json_bool(): truncated bool";
170
171         if( *index >= (current_strlen - 5))
172                 return json_handle_error(string, index, ret);
173         
174         if(!strncasecmp( string + (*index), "false", 5)) {
175                 (*index) += 5;
176                 obj->bool_value = 0;
177                 obj->is_bool = 1;
178                 obj->is_null = 0;
179                 return 0;
180         }
181
182         if( *index >= (current_strlen - 4))
183                 return json_handle_error(string, index, ret);
184
185         if(!strncasecmp( string + (*index), "true", 4)) {
186                 (*index) += 4;
187                 obj->bool_value = 1;
188                 obj->is_bool = 1;
189                 obj->is_null = 0;
190                 return 0;
191         }
192
193         return json_handle_error(string, index, ret);
194 }
195
196
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);
200
201         growing_buffer* buf = buffer_init(64);
202         char c = string[*index];
203
204         int done = 0;
205         int dot_seen = 0;
206
207         /* negative number? */
208         if(c == '-') { buffer_add(buf, "-"); (*index)++; }
209
210         while(*index < current_strlen) {
211
212                 if(is_number(c))
213                         buffer_add_char(buf, c);
214
215                 else if( c == '.' ) {
216                         if(dot_seen) {
217                                 return json_handle_error(string, index, 
218                                         "json_parse_json_number(): malformed json number");
219                         }
220                         dot_seen = 1;
221                         buffer_add_char(buf, c);
222                 } else {
223                         done = 1; break;
224                 }
225                 (*index)++;
226                 c = string[*index];
227                 if(done) break;
228         }
229
230         if(dot_seen) {
231                 obj->is_double = 1;
232                 obj->is_null = 0;
233                 obj->double_value = strtod(buf->buf, NULL);
234                 buffer_free(buf);
235                 return 0;
236
237         } else {
238                 obj->is_number = 1;
239                 obj->is_null = 0;
240                 obj->num_value = atol(buf->buf);
241                 buffer_free(buf);
242                 return 0;
243         }
244 }
245
246 /* index should point to the character directly following the '['.  when done
247  * index will point to the character directly following the ']' character
248  */
249 int json_parse_json_array(char* string, unsigned long* index, object* obj) {
250         assert(string && obj && index && *index < current_strlen);
251
252         int status = 0;
253         int in_parse = 0; /* true if this array already contains one item */
254         obj->is_array = 1;
255         obj->is_null = 0;
256         int set = 0;
257         int done = 0;
258
259         while(*index < current_strlen) {
260
261                 json_eat_ws(string, index, 1);
262
263                 if(string[*index] == ']') {
264                         (*index)++;
265                         done = 1;
266                         break;
267                 }
268
269                 if(in_parse) {
270                         json_eat_ws(string, index, 1);
271                         if(string[*index] != ',') {
272                                 return json_handle_error(string, index,
273                                         "json_parse_json_array(): array not followed by a ','");
274                         }
275                         (*index)++;
276                         json_eat_ws(string, index, 1);
277                 }
278
279                 object* item = new_object(NULL);
280
281 #ifndef STRICT_JSON_READ
282                 if(*index < current_strlen) {
283                         if(string[*index] == ',' || string[*index] == ']') {
284                                 status = 0;
285                                 set = 1;
286                         }
287                 }
288                 if(!set)
289                         status = _json_parse_string(string, index, item);
290
291 #else
292                  status = _json_parse_string(string, index, item);
293 #endif
294
295                 if(status) return status;
296                 obj->push(obj, item);
297                 in_parse = 1;
298                 set = 0;
299         }
300
301         if(!done)
302                 return json_handle_error(string, index,
303                         "json_parse_json_array(): array not closed");
304
305         return 0;
306 }
307
308
309 /* index should point to the character directly following the '{'.  when done
310  * index will point to the character directly following the '}'
311  */
312 int json_parse_json_object(char* string, unsigned long* index, object* obj) {
313         assert(string && obj && index && *index < current_strlen);
314
315         obj->is_hash = 1;
316         obj->is_null = 0;
317         int status;
318         int in_parse = 0; /* true if we've already added one item to this object */
319         int set = 0;
320         int done = 0;
321
322         while(*index < current_strlen) {
323
324                 json_eat_ws(string, index, 1);
325
326                 if(string[*index] == '}') {
327                         (*index)++;
328                         done = 1;
329                         break;
330                 }
331
332                 if(in_parse) {
333                         if(string[*index] != ',') {
334                                 return json_handle_error(string, index,
335                                         "json_parse_json_object(): object missing ',' between elements" );
336                         }
337                         (*index)++;
338                         json_eat_ws(string, index, 1);
339                 }
340
341                 /* first we grab the hash key */
342                 object* key_obj = new_object(NULL);
343                 status = _json_parse_string(string, index, key_obj);
344                 if(status) return status;
345
346                 if(!key_obj->is_string) {
347                         return json_handle_error(string, index, 
348                                 "_json_parse_json_object(): hash key not a string");
349                 }
350
351                 char* key = key_obj->string_data;
352
353                 json_eat_ws(string, index, 1);
354
355                 if(string[*index] != ':') {
356                         return json_handle_error(string, index, 
357                                 "json_parse_json_object(): hash key not followed by ':' character");
358                 }
359
360                 (*index)++;
361
362                 /* now grab the value object */
363                 json_eat_ws(string, index, 1);
364                 object* value_obj = new_object(NULL);
365
366 #ifndef STRICT_JSON_READ
367                 if(*index < current_strlen) {
368                         if(string[*index] == ',' || string[*index] == '}') {
369                                 status = 0;
370                                 set = 1;
371                         }
372                 }
373                 if(!set)
374                         status = _json_parse_string(string, index, value_obj);
375
376 #else
377                  status = _json_parse_string(string, index, value_obj);
378 #endif
379
380                 if(status) return status;
381
382                 /* put the data into the object and continue */
383                 obj->add_key(obj, key, value_obj);
384                 free_object(key_obj);
385                 in_parse = 1;
386                 set = 0;
387         }
388
389         if(!done)
390                 return json_handle_error(string, index,
391                         "json_parse_json_object(): object not closed");
392
393         return 0;
394 }
395
396
397
398 /* when done, index will point to the character after the closing quote */
399 int json_parse_json_string(char* string, unsigned long* index, object* obj) {
400         assert(string && index && *index < current_strlen);
401
402         int in_escape = 0;      
403         int done = 0;
404         growing_buffer* buf = buffer_init(64);
405
406         while(*index < current_strlen) {
407
408                 char c = string[*index]; 
409
410                 switch(c) {
411
412                         case '\\':
413                                 if(in_escape) {
414                                         buffer_add(buf, "\\");
415                                         in_escape = 0;
416                                 } else 
417                                         in_escape = 1;
418                                 break;
419
420                         case '"':
421                                 if(in_escape) {
422                                         buffer_add(buf, "\"");
423                                         in_escape = 0;
424                                 } else 
425                                         done = 1;
426                                 break;
427
428                         case 't':
429                                 if(in_escape) {
430                                         buffer_add(buf,"\t");
431                                         in_escape = 0;
432                                 } else 
433                                         buffer_add_char(buf, c);
434                                 break;
435
436                         case 'b':
437                                 if(in_escape) {
438                                         buffer_add(buf,"\b");
439                                         in_escape = 0;
440                                 } else 
441                                         buffer_add_char(buf, c);
442                                 break;
443
444                         case 'f':
445                                 if(in_escape) {
446                                         buffer_add(buf,"\f");
447                                         in_escape = 0;
448                                 } else 
449                                         buffer_add_char(buf, c);
450                                 break;
451
452                         case 'r':
453                                 if(in_escape) {
454                                         buffer_add(buf,"\r");
455                                         in_escape = 0;
456                                 } else 
457                                         buffer_add_char(buf, c);
458                                 break;
459
460                         case 'n':
461                                 if(in_escape) {
462                                         buffer_add(buf,"\n");
463                                         in_escape = 0;
464                                 } else 
465                                         buffer_add_char(buf, c);
466                                 break;
467
468                         case 'u':
469                                 if(in_escape) {
470                                         (*index)++;
471
472                                         if(*index >= (current_strlen - 4)) {
473                                                 return json_handle_error(string, index,
474                                                         "json_parse_json_string(): truncated escaped unicode"); }
475
476                                         char buff[5];
477                                         memset(buff,0,5);
478                                         memcpy(buff, string + (*index), 4);
479
480
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];
486                                         memset(utf_out,0,3);
487
488                                         #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
489
490                                         unsigned int ucs_char =
491                                                 (hexdigit(string[*index] ) << 12) +
492                                                 (hexdigit(string[*index + 1]) << 8) +
493                                                 (hexdigit(string[*index + 2]) << 4) +
494                                                 hexdigit(string[*index + 3]);
495         
496                                         if (ucs_char < 0x80) {
497                                                 utf_out[0] = ucs_char;
498                                                 buffer_add(buf, utf_out);
499
500                                         } else if (ucs_char < 0x800) {
501                                                 utf_out[0] = 0xc0 | (ucs_char >> 6);
502                                                 utf_out[1] = 0x80 | (ucs_char & 0x3f);
503                                                 buffer_add(buf, utf_out);
504
505                                         } else {
506                                                 utf_out[0] = 0xe0 | (ucs_char >> 12);
507                                                 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
508                                                 utf_out[2] = 0x80 | (ucs_char & 0x3f);
509                                                 buffer_add(buf, utf_out);
510                                         }
511                                         /* ----------------------------------------------------------------------- */
512                                         /* ----------------------------------------------------------------------- */
513
514                                         (*index) += 3;
515                                         in_escape = 0;
516
517                                 } else {
518
519                                         buffer_add_char(buf, c);
520                                 }
521
522                                 break;
523
524                         default:
525                                 buffer_add_char(buf, c);
526                 }
527
528                 (*index)++;
529                 if(done) break;
530         }
531
532         obj->set_string(obj, buf->buf);
533         buffer_free(buf);
534         return 0;
535 }
536
537
538 void json_eat_ws(char* string, unsigned long* index, int eat_all) {
539         assert(string && index);
540         if(*index >= current_strlen)
541                 return;
542
543         if( eat_all ) { /* removes newlines, etc */
544                 while(string[*index] == ' '     || 
545                                 string[*index] == '\n'  ||
546                                 string[*index] == '\t') 
547                         (*index)++;
548         }
549
550         else    
551                 while(string[*index] == ' ') (*index)++;
552 }
553
554
555 /* index should be at the '*' character at the beginning of the comment.
556  * when done, index will point to the first character after the final /
557  */
558 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
559         assert(string && index && *index < current_strlen);
560
561         if(string[*index] != '*' && string[*index] != '/' )
562                 return json_handle_error(string, index, 
563                         "json_eat_comment(): invalid character after /");
564
565         /* chop out any // style comments */
566         if(string[*index] == '/') {
567                 (*index)++;
568                 char c = string[*index];
569                 while(*index < current_strlen) {
570                         (*index)++;
571                         if(c == '\n') 
572                                 return 0;
573                         c = string[*index];
574                 }
575                 return 0;
576         }
577
578         (*index)++;
579
580         int on_star                     = 0; /* true if we just saw a '*' character */
581
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] == '/') {
586                                 if(on_star) {
587                                         (*index)++;
588                                         return 0;
589                                 }
590                         }
591
592                         if(string[*index] == '*') on_star = 1;
593                         else on_star = 0;
594
595                         (*index)++;
596                 }
597                 return 0;
598         }
599
600
601
602         growing_buffer* buf = buffer_init(64);
603
604         int first_dash          = 0;
605         int second_dash = 0;
606         int third_dash          = 0;
607         int fourth_dash = 0;
608
609         int in_hint                     = 0;
610         int done                                = 0;
611
612         /*--S hint--*/   /* <-- Hints  look like this */
613         /*--E hint--*/
614
615         while(*index < current_strlen) {
616                 char c = string[*index];
617
618                 switch(c) {
619
620                         case '-':
621                                 on_star = 0;
622                                 if(third_dash)                  fourth_dash = 1;
623                                 else if(in_hint)                third_dash      = 1;
624                                 else if(first_dash)     second_dash = 1;
625                                 else                                            first_dash = 1;
626                                 break;
627
628                         case 'S':
629                                 on_star = 0;
630                                 if(second_dash && !in_hint) {
631                                         (*index)++;
632                                         json_eat_ws(string, index, 1);
633                                         (*index)--; /* this will get incremented at the bottom of the loop */
634                                         in_hint = 1;
635                                         break;
636                                 } 
637
638                                 if(second_dash && in_hint) {
639                                         buffer_add_char(buf, c);
640                                         break;
641                                 }
642
643                         case 'E':
644                                 on_star = 0;
645                                 if(second_dash && !in_hint) {
646                                         (*index)++;
647                                         json_eat_ws(string, index, 1);
648                                         (*index)--; /* this will get incremented at the bottom of the loop */
649                                         in_hint = 1;
650                                         break;
651                                 }
652
653                                 if(second_dash && in_hint) {
654                                         buffer_add_char(buf, c);
655                                         break;
656                                 }
657
658                         case '*':
659                                 on_star = 1;
660                                 break;
661
662                         case '/':
663                                 if(on_star) 
664                                         done = 1;
665                                 else
666                                 on_star = 0;
667                                 break;
668
669                         default:
670                                 on_star = 0;
671                                 if(in_hint)
672                                         buffer_add_char(buf, c);
673                 }
674
675                 (*index)++;
676                 if(done) break;
677         }
678
679         if( buf->n_used > 0 && buffer)
680                 *buffer = buffer_data(buf);
681
682         buffer_free(buf);
683         return 0;
684 }
685
686 int is_number(char c) {
687         switch(c) {
688                 case '0':
689                 case '1':
690                 case '2':
691                 case '3':
692                 case '4':
693                 case '5':
694                 case '6':
695                 case '7':
696                 case '8':
697                 case '9':
698                         return 1;
699         }
700         return 0;
701 }
702
703 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
704
705         char buf[60];
706         memset(buf, 0, 60);
707
708         if(*index > 30)
709                 strncpy( buf, string + (*index - 30), 59 );
710         else
711                 strncpy( buf, string, 59 );
712
713         fprintf(stderr, 
714                         "\nError parsing json string at charracter %c "
715                         "(code %d) and index %ld\nMsg:\t%s\nNear:\t%s\n\n", 
716                         string[*index], string[*index], *index, err_msg, buf );
717         return -1;
718 }
719
720