]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/objson/json_parser.c
removed all of the old libjson dependencies
[OpenSRF.git] / 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;
253         int in_parse = 0; /* true if this array already contains one item */
254         obj->is_array = 1;
255         obj->is_null = 0;
256         while(*index < current_strlen) {
257
258                 json_eat_ws(string, index, 1);
259
260                 if(string[*index] == ']') {
261                         (*index)++;
262                         break;
263                 }
264
265                 if(in_parse) {
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 ','");
270                         }
271                         (*index)++;
272                         json_eat_ws(string, index, 1);
273                 }
274
275                 object* item = new_object(NULL);
276                 status = _json_parse_string(string, index, item);
277
278                 if(status) return status;
279                 obj->push(obj, item);
280                 in_parse = 1;
281         }
282
283         return 0;
284 }
285
286
287 /* index should point to the character directly following the '{'.  when done
288  * index will point to the character directly following the '}'
289  */
290 int json_parse_json_object(char* string, unsigned long* index, object* obj) {
291         assert(string && obj && index && *index < current_strlen);
292
293         obj->is_hash = 1;
294         obj->is_null = 0;
295         int status;
296         int in_parse = 0; /* true if we've already added one item to this object */
297
298         while(*index < current_strlen) {
299
300                 json_eat_ws(string, index, 1);
301
302                 if(string[*index] == '}') {
303                         (*index)++;
304                         break;
305                 }
306
307                 if(in_parse) {
308                         if(string[*index] != ',') {
309                                 return json_handle_error(string, index,
310                                         "json_parse_json_object(): object missing ',' betweenn elements" );
311                         }
312                         (*index)++;
313                         json_eat_ws(string, index, 1);
314                 }
315
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;
320
321                 if(!key_obj->is_string) {
322                         return json_handle_error(string, index, 
323                                 "_json_parse_json_object(): hash key not a string");
324                 }
325
326                 char* key = key_obj->string_data;
327
328                 json_eat_ws(string, index, 1);
329
330                 if(string[*index] != ':') {
331                         return json_handle_error(string, index, 
332                                 "json_parse_json_object(): hash key not followed by ':' character");
333                 }
334
335                 (*index)++;
336
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;
342
343                 /* put the data into the object and continue */
344                 obj->add_key(obj, key, value_obj);
345                 free_object(key_obj);
346                 in_parse = 1;
347         }
348
349         return 0;
350 }
351
352
353
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);
357
358         int in_escape = 0;      
359         int done = 0;
360         growing_buffer* buf = buffer_init(64);
361
362         while(*index < current_strlen) {
363
364                 char c = string[*index]; 
365
366                 switch(c) {
367
368                         case '\\':
369                                 if(in_escape) {
370                                         buffer_add(buf, "\\");
371                                         in_escape = 0;
372                                 } else 
373                                         in_escape = 1;
374                                 break;
375
376                         case '"':
377                                 if(in_escape) {
378                                         buffer_add(buf, "\"");
379                                         in_escape = 0;
380                                 } else 
381                                         done = 1;
382                                 break;
383
384                         case 't':
385                                 if(in_escape) {
386                                         buffer_add(buf,"\t");
387                                         in_escape = 0;
388                                 } else 
389                                         buffer_add_char(buf, c);
390                                 break;
391
392                         case 'b':
393                                 if(in_escape) {
394                                         buffer_add(buf,"\b");
395                                         in_escape = 0;
396                                 } else 
397                                         buffer_add_char(buf, c);
398                                 break;
399
400                         case 'f':
401                                 if(in_escape) {
402                                         buffer_add(buf,"\f");
403                                         in_escape = 0;
404                                 } else 
405                                         buffer_add_char(buf, c);
406                                 break;
407
408                         case 'r':
409                                 if(in_escape) {
410                                         buffer_add(buf,"\r");
411                                         in_escape = 0;
412                                 } else 
413                                         buffer_add_char(buf, c);
414                                 break;
415
416                         case 'n':
417                                 if(in_escape) {
418                                         buffer_add(buf,"\n");
419                                         in_escape = 0;
420                                 } else 
421                                         buffer_add_char(buf, c);
422                                 break;
423
424                         case 'u':
425                                 if(in_escape) {
426                                         (*index)++;
427
428                                         if(*index >= (current_strlen - 4)) {
429                                                 return json_handle_error(string, index,
430                                                         "json_parse_json_string(): truncated escaped unicode"); }
431
432                                         char buff[5];
433                                         memset(buff,0,5);
434                                         memcpy(buff, string + (*index), 4);
435
436
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];
442                                         memset(utf_out,0,3);
443
444                                         #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
445
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]);
451         
452                                         if (ucs_char < 0x80) {
453                                                 utf_out[0] = ucs_char;
454                                                 buffer_add(buf, utf_out);
455
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);
460
461                                         } else {
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);
466                                         }
467                                         /* ----------------------------------------------------------------------- */
468                                         /* ----------------------------------------------------------------------- */
469
470                                         (*index) += 3;
471                                         in_escape = 0;
472
473                                 } else {
474
475                                         buffer_add_char(buf, c);
476                                 }
477
478                                 break;
479
480                         default:
481                                 buffer_add_char(buf, c);
482                 }
483
484                 (*index)++;
485                 if(done) break;
486         }
487
488         obj->set_string(obj, buf->buf);
489         buffer_free(buf);
490         return 0;
491 }
492
493
494 void json_eat_ws(char* string, unsigned long* index, int eat_all) {
495         assert(string && index);
496         if(*index >= current_strlen)
497                 return;
498
499         if( eat_all ) { /* removes newlines, etc */
500                 while(string[*index] == ' '     || 
501                                 string[*index] == '\n'  ||
502                                 string[*index] == '\t') 
503                         (*index)++;
504         }
505
506         else    
507                 while(string[*index] == ' ') (*index)++;
508 }
509
510
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 /
513  */
514 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
515         assert(string && index && *index < current_strlen);
516
517         if(string[*index] != '*' && string[*index] != '/' )
518                 return json_handle_error(string, index, 
519                         "json_eat_comment(): invalid character after /");
520
521         /* chop out any // style comments */
522         if(string[*index] == '/') {
523                 (*index)++;
524                 char c = string[*index];
525                 while(*index < current_strlen) {
526                         (*index)++;
527                         if(c == '\n') 
528                                 return 0;
529                         c = string[*index];
530                 }
531                 return 0;
532         }
533
534         (*index)++;
535
536         int on_star                     = 0; /* true if we just saw a '*' character */
537
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] == '/') {
542                                 if(on_star) {
543                                         (*index)++;
544                                         return 0;
545                                 }
546                         }
547
548                         if(string[*index] == '*') on_star = 1;
549                         else on_star = 0;
550
551                         (*index)++;
552                 }
553                 return 0;
554         }
555
556
557
558         growing_buffer* buf = buffer_init(64);
559
560         int first_dash          = 0;
561         int second_dash = 0;
562         int third_dash          = 0;
563         int fourth_dash = 0;
564
565         int in_hint                     = 0;
566         int done                                = 0;
567
568         /*--S hint--*/   /* <-- Hints  look like this */
569         /*--E hint--*/
570
571         while(*index < current_strlen) {
572                 char c = string[*index];
573
574                 switch(c) {
575
576                         case '-':
577                                 on_star = 0;
578                                 if(third_dash)                  fourth_dash = 1;
579                                 else if(in_hint)                third_dash      = 1;
580                                 else if(first_dash)     second_dash = 1;
581                                 else                                            first_dash = 1;
582                                 break;
583
584                         case 'S':
585                                 on_star = 0;
586                                 if(second_dash && !in_hint) {
587                                         (*index)++;
588                                         json_eat_ws(string, index, 1);
589                                         (*index)--; /* this will get incremented at the bottom of the loop */
590                                         in_hint = 1;
591                                         break;
592                                 }
593
594                         case 'E':
595                                 on_star = 0;
596                                 if(second_dash && !in_hint) {
597                                         (*index)++;
598                                         json_eat_ws(string, index, 1);
599                                         (*index)--; /* this will get incremented at the bottom of the loop */
600                                         in_hint = 1;
601                                         break;
602                                 }
603
604                         case '*':
605                                 on_star = 1;
606                                 break;
607
608                         case '/':
609                                 if(on_star) 
610                                         done = 1;
611                                 else
612                                 on_star = 0;
613                                 break;
614
615                         default:
616                                 on_star = 0;
617                                 if(in_hint)
618                                         buffer_add_char(buf, c);
619                 }
620
621                 (*index)++;
622                 if(done) break;
623         }
624
625         if( buf->n_used > 0 && buffer)
626                 *buffer = buffer_data(buf);
627
628         buffer_free(buf);
629         return 0;
630 }
631
632 int is_number(char c) {
633         switch(c) {
634                 case '0':
635                 case '1':
636                 case '2':
637                 case '3':
638                 case '4':
639                 case '5':
640                 case '6':
641                 case '7':
642                 case '8':
643                 case '9':
644                         return 1;
645         }
646         return 0;
647 }
648
649 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
650
651         char buf[60];
652         memset(buf, 0, 60);
653
654         if(*index > 30)
655                 strncpy( buf, string + (*index - 30), 59 );
656         else
657                 strncpy( buf, string, 59 );
658
659         fprintf(stderr, 
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 );
663         return -1;
664 }
665
666