2 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3 * Copyright (c) 2011-2012 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
5 * Jansson is free software; you can redistribute it and/or modify
6 * it under the terms of the MIT license. See LICENSE for details.
11 #include "jansson_private.h"
35 #define token(scanner) ((scanner)->token.token)
37 static const char * const type_names
[] = {
48 #define type_name(x) type_names[json_typeof(x)]
50 static const char unpack_value_starters
[] = "{[siIbfFOon";
52 static void scanner_init(scanner_t
*s
, json_error_t
*error
,
53 size_t flags
, const char *fmt
)
57 s
->fmt
= s
->start
= fmt
;
58 memset(&s
->prev_token
, 0, sizeof(token_t
));
59 memset(&s
->token
, 0, sizeof(token_t
));
60 memset(&s
->next_token
, 0, sizeof(token_t
));
67 static void next_token(scanner_t
*s
)
70 s
->prev_token
= s
->token
;
72 if(s
->next_token
.line
) {
73 s
->token
= s
->next_token
;
74 s
->next_token
.line
= 0;
78 if (!token(s
) && !*s
->fmt
)
85 /* skip space and ignored chars */
86 while(*t
== ' ' || *t
== '\t' || *t
== '\n' || *t
== ',' || *t
== ':') {
99 s
->token
.line
= s
->line
;
100 s
->token
.column
= s
->column
;
101 s
->token
.pos
= s
->pos
;
107 static void prev_token(scanner_t
*s
)
109 s
->next_token
= s
->token
;
110 s
->token
= s
->prev_token
;
113 static void set_error(scanner_t
*s
, const char *source
, enum json_error_code code
,
114 const char *fmt
, ...)
119 jsonp_error_vset(s
->error
, s
->token
.line
, s
->token
.column
, s
->token
.pos
,
122 jsonp_error_set_source(s
->error
, source
);
127 static json_t
*pack(scanner_t
*s
, va_list *ap
);
130 /* ours will be set to 1 if jsonp_free() must be called for the result
132 static char *read_string(scanner_t
*s
, va_list *ap
,
133 const char *purpose
, size_t *out_len
, int *ours
, int optional
)
145 if(t
!= '#' && t
!= '%' && t
!= '+') {
146 /* Optimize the simple case */
147 str
= va_arg(*ap
, const char *);
151 set_error(s
, "<args>", json_error_null_value
, "NULL %s", purpose
);
157 length
= strlen(str
);
159 if(!utf8_check_string(str
, length
)) {
160 set_error(s
, "<args>", json_error_invalid_utf8
, "Invalid UTF-8 %s", purpose
);
167 } else if (optional
) {
168 set_error(s
, "<format>", json_error_invalid_format
, "Cannot use '%c' on optional strings", t
);
174 if(strbuffer_init(&strbuff
)) {
175 set_error(s
, "<internal>", json_error_out_of_memory
, "Out of memory");
180 str
= va_arg(*ap
, const char *);
182 set_error(s
, "<args>", json_error_null_value
, "NULL %s", purpose
);
188 if(token(s
) == '#') {
189 length
= va_arg(*ap
, int);
191 else if(token(s
) == '%') {
192 length
= va_arg(*ap
, size_t);
196 length
= s
->has_error
? 0 : strlen(str
);
199 if(!s
->has_error
&& strbuffer_append_bytes(&strbuff
, str
, length
) == -1) {
200 set_error(s
, "<internal>", json_error_out_of_memory
, "Out of memory");
205 if(token(s
) != '+') {
212 strbuffer_close(&strbuff
);
216 if(!utf8_check_string(strbuff
.value
, strbuff
.length
)) {
217 set_error(s
, "<args>", json_error_invalid_utf8
, "Invalid UTF-8 %s", purpose
);
218 strbuffer_close(&strbuff
);
223 *out_len
= strbuff
.length
;
225 return strbuffer_steal_value(&strbuff
);
228 static json_t
*pack_object(scanner_t
*s
, va_list *ap
)
230 json_t
*object
= json_object();
233 while(token(s
) != '}') {
241 set_error(s
, "<format>", json_error_invalid_format
, "Unexpected end of format string");
245 if(token(s
) != 's') {
246 set_error(s
, "<format>", json_error_invalid_format
, "Expected format 's', got '%c'", token(s
));
250 key
= read_string(s
, ap
, "object key", &len
, &ours
, 0);
255 valueOptional
= token(s
);
263 if(valueOptional
!= '*') {
264 set_error(s
, "<args>", json_error_null_value
, "NULL object value");
275 if(!s
->has_error
&& json_object_set_new_nocheck(object
, key
, value
)) {
276 set_error(s
, "<internal>", json_error_out_of_memory
, "Unable to add key \"%s\"", key
);
294 static json_t
*pack_array(scanner_t
*s
, va_list *ap
)
296 json_t
*array
= json_array();
299 while(token(s
) != ']') {
304 set_error(s
, "<format>", json_error_invalid_format
, "Unexpected end of format string");
305 /* Format string errors are unrecoverable. */
310 valueOptional
= token(s
);
315 if(valueOptional
!= '*') {
326 if(!s
->has_error
&& json_array_append_new(array
, value
)) {
327 set_error(s
, "<internal>", json_error_out_of_memory
, "Unable to append to array");
342 static json_t
*pack_string(scanner_t
*s
, va_list *ap
)
352 optional
= t
== '?' || t
== '*';
356 str
= read_string(s
, ap
, "string", &len
, &ours
, optional
);
359 return t
== '?' && !s
->has_error
? json_null() : NULL
;
362 /* It's impossible to reach this point if ours != 0, do not free str. */
367 return jsonp_stringn_nocheck_own(str
, len
);
369 return json_stringn_nocheck(str
, len
);
372 static json_t
*pack_object_inter(scanner_t
*s
, va_list *ap
, int need_incref
)
380 if (ntoken
!= '?' && ntoken
!= '*')
383 json
= va_arg(*ap
, json_t
*);
386 return need_incref
? json_incref(json
) : json
;
397 set_error(s
, "<args>", json_error_null_value
, "NULL object");
402 static json_t
*pack_integer(scanner_t
*s
, json_int_t value
)
404 json_t
*json
= json_integer(value
);
407 set_error(s
, "<internal>", json_error_out_of_memory
, "Out of memory");
414 static json_t
*pack_real(scanner_t
*s
, double value
)
416 /* Allocate without setting value so we can identify OOM error. */
417 json_t
*json
= json_real(0.0);
420 set_error(s
, "<internal>", json_error_out_of_memory
, "Out of memory");
426 if (json_real_set(json
, value
)) {
429 set_error(s
, "<args>", json_error_numeric_overflow
, "Invalid floating point value");
438 static json_t
*pack(scanner_t
*s
, va_list *ap
)
442 return pack_object(s
, ap
);
445 return pack_array(s
, ap
);
447 case 's': /* string */
448 return pack_string(s
, ap
);
453 case 'b': /* boolean */
454 return va_arg(*ap
, int) ? json_true() : json_false();
456 case 'i': /* integer from int */
457 return pack_integer(s
, va_arg(*ap
, int));
459 case 'I': /* integer from json_int_t */
460 return pack_integer(s
, va_arg(*ap
, json_int_t
));
463 return pack_real(s
, va_arg(*ap
, double));
465 case 'O': /* a json_t object; increments refcount */
466 return pack_object_inter(s
, ap
, 1);
468 case 'o': /* a json_t object; doesn't increment refcount */
469 return pack_object_inter(s
, ap
, 0);
472 set_error(s
, "<format>", json_error_invalid_format
, "Unexpected format character '%c'",
479 static int unpack(scanner_t
*s
, json_t
*root
, va_list *ap
);
481 static int unpack_object(scanner_t
*s
, json_t
*root
, va_list *ap
)
487 /* Use a set (emulated by a hashtable) to check that all object
488 keys are accessed. Checking that the correct number of keys
489 were accessed is not enough, as the same key can be unpacked
494 if(hashtable_init(&key_set
)) {
495 set_error(s
, "<internal>", json_error_out_of_memory
, "Out of memory");
499 if(root
&& !json_is_object(root
)) {
500 set_error(s
, "<validation>", json_error_wrong_type
, "Expected object, got %s",
506 while(token(s
) != '}') {
512 set_error(s
, "<format>", json_error_invalid_format
, "Expected '}' after '%c', got '%c'",
513 (strict
== 1 ? '!' : '*'), token(s
));
518 set_error(s
, "<format>", json_error_invalid_format
, "Unexpected end of format string");
522 if(token(s
) == '!' || token(s
) == '*') {
523 strict
= (token(s
) == '!' ? 1 : -1);
528 if(token(s
) != 's') {
529 set_error(s
, "<format>", json_error_invalid_format
, "Expected format 's', got '%c'", token(s
));
533 key
= va_arg(*ap
, const char *);
535 set_error(s
, "<args>", json_error_null_value
, "NULL object key");
541 if(token(s
) == '?') {
551 value
= json_object_get(root
, key
);
553 set_error(s
, "<validation>", json_error_item_not_found
, "Object item not found: %s", key
);
558 if(unpack(s
, value
, ap
))
561 hashtable_set(&key_set
, key
, json_null());
565 if(strict
== 0 && (s
->flags
& JSON_STRICT
))
568 if(root
&& strict
== 1) {
569 /* We need to check that all non optional items have been parsed */
571 /* keys_res is 1 for uninitialized, 0 for success, -1 for error. */
573 strbuffer_t unrecognized_keys
;
577 if (gotopt
|| json_object_size(root
) != key_set
.size
) {
578 json_object_foreach(root
, key
, value
) {
579 if(!hashtable_get(&key_set
, key
)) {
582 /* Save unrecognized keys for the error message */
584 keys_res
= strbuffer_init(&unrecognized_keys
);
585 } else if (!keys_res
) {
586 keys_res
= strbuffer_append_bytes(&unrecognized_keys
, ", ", 2);
590 keys_res
= strbuffer_append_bytes(&unrecognized_keys
, key
, strlen(key
));
595 set_error(s
, "<validation>", json_error_end_of_input_expected
,
596 "%li object item(s) left unpacked: %s",
598 keys_res
? "<unknown>" : strbuffer_value(&unrecognized_keys
));
599 strbuffer_close(&unrecognized_keys
);
607 hashtable_close(&key_set
);
611 static int unpack_array(scanner_t
*s
, json_t
*root
, va_list *ap
)
616 if(root
&& !json_is_array(root
)) {
617 set_error(s
, "<validation>", json_error_wrong_type
, "Expected array, got %s", type_name(root
));
622 while(token(s
) != ']') {
626 set_error(s
, "<format>", json_error_invalid_format
, "Expected ']' after '%c', got '%c'",
627 (strict
== 1 ? '!' : '*'),
633 set_error(s
, "<format>", json_error_invalid_format
, "Unexpected end of format string");
637 if(token(s
) == '!' || token(s
) == '*') {
638 strict
= (token(s
) == '!' ? 1 : -1);
643 if(!strchr(unpack_value_starters
, token(s
))) {
644 set_error(s
, "<format>", json_error_invalid_format
, "Unexpected format character '%c'",
654 value
= json_array_get(root
, i
);
656 set_error(s
, "<validation>", json_error_index_out_of_range
, "Array index %lu out of range",
662 if(unpack(s
, value
, ap
))
669 if(strict
== 0 && (s
->flags
& JSON_STRICT
))
672 if(root
&& strict
== 1 && i
!= json_array_size(root
)) {
673 long diff
= (long)json_array_size(root
) - (long)i
;
674 set_error(s
, "<validation>", json_error_end_of_input_expected
, "%li array item(s) left unpacked", diff
);
681 static int unpack(scanner_t
*s
, json_t
*root
, va_list *ap
)
686 return unpack_object(s
, root
, ap
);
689 return unpack_array(s
, root
, ap
);
692 if(root
&& !json_is_string(root
)) {
693 set_error(s
, "<validation>", json_error_wrong_type
, "Expected string, got %s",
698 if(!(s
->flags
& JSON_VALIDATE_ONLY
)) {
699 const char **str_target
;
700 size_t *len_target
= NULL
;
702 str_target
= va_arg(*ap
, const char **);
704 set_error(s
, "<args>", json_error_null_value
, "NULL string argument");
710 if(token(s
) == '%') {
711 len_target
= va_arg(*ap
, size_t *);
713 set_error(s
, "<args>", json_error_null_value
, "NULL string length argument");
721 *str_target
= json_string_value(root
);
723 *len_target
= json_string_length(root
);
729 if(root
&& !json_is_integer(root
)) {
730 set_error(s
, "<validation>", json_error_wrong_type
, "Expected integer, got %s",
735 if(!(s
->flags
& JSON_VALIDATE_ONLY
)) {
736 int *target
= va_arg(*ap
, int*);
738 *target
= (int)json_integer_value(root
);
744 if(root
&& !json_is_integer(root
)) {
745 set_error(s
, "<validation>", json_error_wrong_type
, "Expected integer, got %s",
750 if(!(s
->flags
& JSON_VALIDATE_ONLY
)) {
751 json_int_t
*target
= va_arg(*ap
, json_int_t
*);
753 *target
= json_integer_value(root
);
759 if(root
&& !json_is_boolean(root
)) {
760 set_error(s
, "<validation>", json_error_wrong_type
, "Expected true or false, got %s",
765 if(!(s
->flags
& JSON_VALIDATE_ONLY
)) {
766 int *target
= va_arg(*ap
, int*);
768 *target
= json_is_true(root
);
774 if(root
&& !json_is_real(root
)) {
775 set_error(s
, "<validation>", json_error_wrong_type
, "Expected real, got %s",
780 if(!(s
->flags
& JSON_VALIDATE_ONLY
)) {
781 double *target
= va_arg(*ap
, double*);
783 *target
= json_real_value(root
);
789 if(root
&& !json_is_number(root
)) {
790 set_error(s
, "<validation>", json_error_wrong_type
, "Expected real or integer, got %s",
795 if(!(s
->flags
& JSON_VALIDATE_ONLY
)) {
796 double *target
= va_arg(*ap
, double*);
798 *target
= json_number_value(root
);
804 if(root
&& !(s
->flags
& JSON_VALIDATE_ONLY
))
809 if(!(s
->flags
& JSON_VALIDATE_ONLY
)) {
810 json_t
**target
= va_arg(*ap
, json_t
**);
818 /* Never assign, just validate */
819 if(root
&& !json_is_null(root
)) {
820 set_error(s
, "<validation>", json_error_wrong_type
, "Expected null, got %s",
827 set_error(s
, "<format>", json_error_invalid_format
, "Unexpected format character '%c'",
833 json_t
*json_vpack_ex(json_error_t
*error
, size_t flags
,
834 const char *fmt
, va_list ap
)
841 jsonp_error_init(error
, "<format>");
842 jsonp_error_set(error
, -1, -1, 0, json_error_invalid_argument
, "NULL or empty format string");
845 jsonp_error_init(error
, NULL
);
847 scanner_init(&s
, error
, flags
, fmt
);
850 va_copy(ap_copy
, ap
);
851 value
= pack(&s
, &ap_copy
);
854 /* This will cover all situations where s.has_error is true */
861 set_error(&s
, "<format>", json_error_invalid_format
, "Garbage after format string");
868 json_t
*json_pack_ex(json_error_t
*error
, size_t flags
, const char *fmt
, ...)
874 value
= json_vpack_ex(error
, flags
, fmt
, ap
);
880 json_t
*json_pack(const char *fmt
, ...)
886 value
= json_vpack_ex(NULL
, 0, fmt
, ap
);
892 int json_vunpack_ex(json_t
*root
, json_error_t
*error
, size_t flags
,
893 const char *fmt
, va_list ap
)
899 jsonp_error_init(error
, "<root>");
900 jsonp_error_set(error
, -1, -1, 0, json_error_null_value
, "NULL root value");
905 jsonp_error_init(error
, "<format>");
906 jsonp_error_set(error
, -1, -1, 0, json_error_invalid_argument
, "NULL or empty format string");
909 jsonp_error_init(error
, NULL
);
911 scanner_init(&s
, error
, flags
, fmt
);
914 va_copy(ap_copy
, ap
);
915 if(unpack(&s
, root
, &ap_copy
)) {
923 set_error(&s
, "<format>", json_error_invalid_format
, "Garbage after format string");
930 int json_unpack_ex(json_t
*root
, json_error_t
*error
, size_t flags
, const char *fmt
, ...)
936 ret
= json_vunpack_ex(root
, error
, flags
, fmt
, ap
);
942 int json_unpack(json_t
*root
, const char *fmt
, ...)
948 ret
= json_vunpack_ex(root
, NULL
, 0, fmt
, ap
);