17#include "jansson_private.h"
33#define STREAM_STATE_OK 0
34#define STREAM_STATE_EOF -1
35#define STREAM_STATE_ERROR -2
37#define TOKEN_INVALID -1
39#define TOKEN_STRING 256
40#define TOKEN_INTEGER 257
43#define TOKEN_FALSE 260
47#define l_isupper(c) ('A' <= (c) && (c) <= 'Z')
48#define l_islower(c) ('a' <= (c) && (c) <= 'z')
49#define l_isalpha(c) (l_isupper(c) || l_islower(c))
50#define l_isdigit(c) ('0' <= (c) && (c) <= '9')
51#define l_isxdigit(c) \
52 (l_isdigit(c) || ('A' <= (c) && (c) <= 'F') || ('a' <= (c) && (c) <= 'f'))
57typedef int (*get_func)(
68 int column, last_column;
74 strbuffer_t saved_text;
88#define stream_to_lex(stream) container_of(stream, lex_t, stream)
96 enum json_error_code code,
102 char msg_text[JSON_ERROR_TEXT_LENGTH];
103 char msg_with_context[JSON_ERROR_TEXT_LENGTH];
105 int line = -1, col = -1;
107 const char *result = msg_text;
114 vsnprintf (msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap);
115 msg_text[JSON_ERROR_TEXT_LENGTH - 1] =
'\0';
119 const char *saved_text = strbuffer_value (&lex->saved_text);
121 line = lex->stream.line;
122 col = lex->stream.column;
123 pos = lex->stream.position;
125 if (saved_text && saved_text[0]) {
126 if (lex->saved_text.length <= 20) {
129 JSON_ERROR_TEXT_LENGTH,
134 msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] =
'\0';
135 result = msg_with_context;
138 if (code == json_error_invalid_syntax) {
140 code = json_error_premature_end_of_input;
143 if (lex->stream.state == STREAM_STATE_ERROR) {
149 JSON_ERROR_TEXT_LENGTH,
150 "%s near end of file",
153 msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] =
'\0';
154 result = msg_with_context;
159 jsonp_error_set (error, line, col, pos, code,
"%s", result);
173 stream->buffer[0] =
'\0';
174 stream->buffer_pos = 0;
176 stream->state = STREAM_STATE_OK;
179 stream->position = 0;
190 if (stream->state != STREAM_STATE_OK) {
191 return stream->state;
194 if (!stream->buffer[stream->buffer_pos]) {
195 c = stream->get (stream->data);
197 stream->state = STREAM_STATE_EOF;
198 return STREAM_STATE_EOF;
201 stream->buffer[0] = c;
202 stream->buffer_pos = 0;
204 if ((0x80 <= c) && (c <= 0xFF)) {
208 count = utf8_check_first (c);
215 for (i = 1; i < count; i++) {
216 stream->buffer[i] = stream->get (stream->data);
219 if (!utf8_check_full (stream->buffer, count,
NULL)) {
223 stream->buffer[count] =
'\0';
225 stream->buffer[1] =
'\0';
229 c = stream->buffer[stream->buffer_pos++];
234 stream->last_column = stream->column;
236 }
else if (utf8_check_first (c)) {
245 stream->state = STREAM_STATE_ERROR;
248 stream_to_lex (stream),
249 json_error_invalid_utf8,
250 "unable to decode byte 0x%x",
253 return STREAM_STATE_ERROR;
262 if ((c == STREAM_STATE_EOF) || (c == STREAM_STATE_ERROR)) {
269 stream->column = stream->last_column;
270 }
else if (utf8_check_first (c)) {
274 assert (stream->buffer_pos > 0);
275 stream->buffer_pos--;
276 assert (stream->buffer[stream->buffer_pos] == c);
285 return stream_get (&lex->stream, error);
294 strbuffer_append_byte (&lex->saved_text, c);
303 int c = stream_get (&lex->stream, error);
305 if ((c != STREAM_STATE_EOF) && (c != STREAM_STATE_ERROR)) {
318 stream_unget (&lex->stream, c);
327 if ((c != STREAM_STATE_EOF) && (c != STREAM_STATE_ERROR)) {
335 stream_unget (&lex->stream, c);
339 strbuffer_pop (&lex->saved_text);
349 while (lex->stream.buffer[lex->stream.buffer_pos] !=
'\0') {
350 lex_save (lex, lex->stream.buffer[lex->stream.buffer_pos]);
351 lex->stream.buffer_pos++;
352 lex->stream.position++;
361 jsonp_free (lex->value.string.val);
362 lex->value.string.val =
NULL;
363 lex->value.string.len = 0;
368decode_unicode_escape (
375 assert (str[0] ==
'u');
377 for (i = 1; i <= 4; i++) {
382 }
else if (l_islower (c)) {
383 value += c -
'a' + 10;
384 }
else if (l_isupper (c)) {
385 value += c -
'A' + 10;
405 lex->value.string.val =
NULL;
406 lex->token = TOKEN_INVALID;
408 c = lex_get_save (lex, error);
411 if (c == STREAM_STATE_ERROR) {
413 }
else if (c == STREAM_STATE_EOF) {
417 json_error_premature_end_of_input,
418 "premature end of input"
421 }
else if ((0 <= c) && (c <= 0x1F)) {
423 lex_unget_unsave (lex, c);
425 error_set (error, lex, json_error_invalid_syntax,
"unexpected newline");
430 json_error_invalid_syntax,
431 "control character 0x%x",
437 }
else if (c ==
'\\') {
438 c = lex_get_save (lex, error);
440 c = lex_get_save (lex, error);
441 for (i = 0; i < 4; i++) {
442 if (!l_isxdigit (c)) {
446 json_error_invalid_syntax,
452 c = lex_get_save (lex, error);
454 }
else if ((c ==
'"') || (c ==
'\\') || (c ==
'/') || (c ==
'b') || (c ==
'f') ||
455 (c ==
'n') || (c ==
'r') || (c ==
't'))
457 c = lex_get_save (lex, error);
459 error_set (error, lex, json_error_invalid_syntax,
"invalid escape");
463 c = lex_get_save (lex, error);
474 t = jsonp_malloc (lex->saved_text.length + 1);
480 lex->value.string.val = t;
483 p = strbuffer_value (&lex->saved_text) + 1;
492 value = decode_unicode_escape (p);
497 json_error_invalid_syntax,
498 "invalid Unicode escape '%.6s'",
506 if ((0xD800 <= value) && (value <= 0xDBFF)) {
508 if ((*p ==
'\\') && (*(p + 1) ==
'u')) {
509 int32_t value2 = decode_unicode_escape (++p);
514 json_error_invalid_syntax,
515 "invalid Unicode escape '%.6s'",
523 if ((0xDC00 <= value2) && (value2 <= 0xDFFF)) {
526 ((value - 0xD800) << 10) + (value2 - 0xDC00) + 0x10000;
532 json_error_invalid_syntax,
533 "invalid Unicode '\\u%04X\\u%04X'",
544 json_error_invalid_syntax,
545 "invalid Unicode '\\u%04X'",
550 }
else if ((0xDC00 <= value) && (value <= 0xDFFF)) {
554 json_error_invalid_syntax,
555 "invalid Unicode '\\u%04X'",
561 if (utf8_encode (value, t, &length)) {
601 lex->value.string.len = t - lex->value.string.val;
602 lex->token = TOKEN_STRING;
606 lex_free_string (lex);
609#ifndef JANSSON_USING_CMAKE
610 #if JSON_INTEGER_IS_LONG_LONG
612#define json_strtoint _strtoi64
614#define json_strtoint strtoll
617#define json_strtoint strtol
628 const char *saved_text;
632 lex->token = TOKEN_INVALID;
635 c = lex_get_save (lex, error);
639 c = lex_get_save (lex, error);
641 lex_unget_unsave (lex, c);
644 }
else if (l_isdigit (c)) {
646 c = lex_get_save (lex, error);
647 }
while (l_isdigit (c));
649 lex_unget_unsave (lex, c);
653 if (!(lex->flags & JSON_DECODE_INT_AS_REAL) && (c !=
'.') && (c !=
'E') && (c !=
'e')) {
656 lex_unget_unsave (lex, c);
658 saved_text = strbuffer_value (&lex->saved_text);
661 intval = json_strtoint (saved_text, &end, 10);
662 if (errno == ERANGE) {
667 json_error_numeric_overflow,
668 "too big negative integer"
671 error_set (error, lex, json_error_numeric_overflow,
"too big integer");
677 assert (end == saved_text + lex->saved_text.length);
679 lex->token = TOKEN_INTEGER;
680 lex->value.integer = intval;
685 c = lex_get (lex, error);
686 if (!l_isdigit (c)) {
694 c = lex_get_save (lex, error);
695 }
while (l_isdigit (c));
698 if ((c ==
'E') || (c ==
'e')) {
699 c = lex_get_save (lex, error);
700 if ((c ==
'+') || (c ==
'-')) {
701 c = lex_get_save (lex, error);
704 if (!l_isdigit (c)) {
705 lex_unget_unsave (lex, c);
710 c = lex_get_save (lex, error);
711 }
while (l_isdigit (c));
714 lex_unget_unsave (lex, c);
716 if (jsonp_strtod (&lex->saved_text, &doubleval)) {
717 error_set (error, lex, json_error_numeric_overflow,
"real number overflow");
721 lex->token = TOKEN_INTEGER;
722 lex->value.integer = doubleval;
737 strbuffer_clear (&lex->saved_text);
739 if (lex->token == TOKEN_STRING) {
740 lex_free_string (lex);
744 c = lex_get (lex, error);
745 }
while (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r');
747 if (c == STREAM_STATE_EOF) {
748 lex->token = TOKEN_EOF;
752 if (c == STREAM_STATE_ERROR) {
753 lex->token = TOKEN_INVALID;
759 if ((c ==
'{') || (c ==
'}') || (c ==
'[') || (c ==
']') || (c ==
':') || (c ==
',')) {
761 }
else if (c ==
'"') {
762 lex_scan_string (lex, error);
763 }
else if (l_isdigit (c) || (c ==
'-')) {
764 if (lex_scan_number (lex, c, error)) {
767 }
else if (l_isalpha (c)) {
769 const char *saved_text;
772 c = lex_get_save (lex, error);
773 }
while (l_isalpha (c));
775 lex_unget_unsave (lex, c);
777 saved_text = strbuffer_value (&lex->saved_text);
779 if (strcmp (saved_text,
"true") == 0) {
780 lex->token = TOKEN_TRUE;
781 }
else if (strcmp (saved_text,
"false") == 0) {
782 lex->token = TOKEN_FALSE;
783 }
else if (strcmp (saved_text,
"null") == 0) {
784 lex->token = TOKEN_NULL;
786 lex->token = TOKEN_INVALID;
791 lex_save_cached (lex);
792 lex->token = TOKEN_INVALID;
807 if (lex->token == TOKEN_STRING) {
808 result = lex->value.string.val;
809 *out_len = lex->value.string.len;
810 lex->value.string.val =
NULL;
811 lex->value.string.len = 0;
825 stream_init (&lex->stream, get, data);
826 if (strbuffer_init (&lex->saved_text)) {
831 lex->token = TOKEN_INVALID;
840 if (lex->token == TOKEN_STRING) {
841 lex_free_string (lex);
844 strbuffer_close (&lex->saved_text);
863 json_t *
object = json_object ();
869 lex_scan (lex, error);
870 if (lex->token ==
'}') {
879 if (lex->token != TOKEN_STRING) {
880 error_set (error, lex, json_error_invalid_syntax,
"string or '}' expected");
884 key = lex_steal_string (lex, &len);
889 if (memchr (key,
'\0', len)) {
894 json_error_null_byte_in_key,
895 "NUL byte in object key not supported"
900 if (flags & JSON_REJECT_DUPLICATES) {
901 if (json_object_get (
object, key)) {
903 error_set (error, lex, json_error_duplicate_key,
"duplicate object key");
908 lex_scan (lex, error);
909 if (lex->token !=
':') {
911 error_set (error, lex, json_error_invalid_syntax,
"':' expected");
915 lex_scan (lex, error);
916 value = parse_value (lex, flags, error);
922 if (json_object_set_new_nocheck (
object, key, value)) {
929 lex_scan (lex, error);
930 if (lex->token !=
',') {
934 lex_scan (lex, error);
937 if (lex->token !=
'}') {
938 error_set (error, lex, json_error_invalid_syntax,
"'}' expected");
945 json_decref (
object);
956 json_t *array = json_array ();
962 lex_scan (lex, error);
963 if (lex->token ==
']') {
968 json_t *elem = parse_value (lex, flags, error);
973 if (json_array_append_new (array, elem)) {
977 lex_scan (lex, error);
978 if (lex->token !=
',') {
982 lex_scan (lex, error);
985 if (lex->token !=
']') {
986 error_set (error, lex, json_error_invalid_syntax,
"']' expected");
1007 if (lex->depth > JSON_PARSER_MAX_DEPTH) {
1008 error_set (error, lex, json_error_stack_overflow,
"maximum parsing depth reached");
1012 switch (lex->token) {
1015 const char *value = lex->value.string.val;
1016 size_t len = lex->value.string.len;
1018 if (!(flags & JSON_ALLOW_NUL)) {
1019 if (memchr (value,
'\0', len)) {
1023 json_error_null_character,
1024 "\\u0000 is not allowed without JSON_ALLOW_NUL"
1030 json = jsonp_stringn_nocheck_own (value, len);
1031 lex->value.string.val =
NULL;
1032 lex->value.string.len = 0;
1038 json = json_integer (lex->value.integer);
1044 json = json_real (lex->value.real);
1049 json = json_true ();
1053 json = json_false ();
1057 json = json_null ();
1061 json = parse_object (lex, flags, error);
1065 json = parse_array (lex, flags, error);
1069 error_set (error, lex, json_error_invalid_syntax,
"invalid token");
1073 error_set (error, lex, json_error_invalid_syntax,
"unexpected token");
1096 lex_scan (lex, error);
1097 if (!(flags & JSON_DECODE_ANY)) {
1098 if ((lex->token !=
'[') && (lex->token !=
'{')) {
1099 error_set (error, lex, json_error_invalid_syntax,
"'[' or '{' expected");
1104 result = parse_value (lex, flags, error);
1109 if (!(flags & JSON_DISABLE_EOF_CHECK)) {
1110 lex_scan (lex, error);
1111 if (lex->token != TOKEN_EOF) {
1115 json_error_end_of_input_expected,
1116 "end of file expected"
1118 json_decref (result);
1125 error->position = (int)lex->stream.position;
1144 c = stream->data[stream->pos];
1149 return (
unsigned char)c;
1164 jsonp_error_init (error,
"<string>");
1166 if (
string ==
NULL) {
1167 error_set (error,
NULL, json_error_invalid_argument,
"wrong arguments");
1171 stream_data.data = string;
1172 stream_data.pos = 0;
1174 if (lex_init (&lex, string_get, flags, (
void *)&stream_data)) {
1178 result = parse_json (&lex, flags, error);
1198 if (stream->pos >= stream->len) {
1202 c = stream->data[stream->pos];
1204 return (
unsigned char)c;
1219 jsonp_error_init (error,
"<buffer>");
1221 if (buffer ==
NULL) {
1222 error_set (error,
NULL, json_error_invalid_argument,
"wrong arguments");
1226 stream_data.data = buffer;
1227 stream_data.pos = 0;
1228 stream_data.len = buflen;
1230 if (lex_init (&lex, buffer_get, flags, (
void *)&stream_data)) {
1234 result = parse_json (&lex, flags, error);
1251 #ifdef HAVE_UNISTD_H
1252 if (input == stdin) {
1256 source =
"<stream>";
1258 jsonp_error_init (error, source);
1260 if (input ==
NULL) {
1261 error_set (error,
NULL, json_error_invalid_argument,
"wrong arguments");
1265 if (lex_init (&lex, (get_func)
fgetc, flags, input)) {
1269 result = parse_json (&lex, flags, error);
1280 #ifdef HAVE_UNISTD_H
1282 if (read (*fd, &c, 1) == 1) {
1301 #ifdef HAVE_UNISTD_H
1302 if (input == STDIN_FILENO) {
1306 source =
"<stream>";
1308 jsonp_error_init (error, source);
1311 error_set (error,
NULL, json_error_invalid_argument,
"wrong arguments");
1315 if (lex_init (&lex, (get_func)fd_get_func, flags, &input)) {
1319 result = parse_json (&lex, flags, error);
1335 jsonp_error_init (error, path);
1338 error_set (error,
NULL, json_error_invalid_argument,
"wrong arguments");
1342 fp = fopen (path,
"rb");
1347 json_error_cannot_open_file,
1348 "unable to open %s: %s",
1355 result = json_loadf (fp, flags, error);
1361#define MAX_BUF_LEN 1024
1364 char data[MAX_BUF_LEN];
1367 json_load_callback_t callback;
1379 if (stream->pos >= stream->len) {
1381 stream->len = stream->callback (stream->data, MAX_BUF_LEN, stream->arg);
1382 if ((stream->len == 0) || (stream->len == (
size_t)-1)) {
1387 c = stream->data[stream->pos];
1389 return (
unsigned char)c;
1394 json_load_callback_t callback,
1405 memset (&stream_data, 0,
sizeof (stream_data));
1406 stream_data.callback = callback;
1407 stream_data.arg = arg;
1409 jsonp_error_init (error,
"<callback>");
1411 if (callback ==
NULL) {
1412 error_set (error,
NULL, json_error_invalid_argument,
"wrong arguments");
1416 if (lex_init (&lex, (get_func)callback_get, flags, &stream_data)) {
1420 result = parse_json (&lex, flags, error);