2 * Copyright (c) 2009-2012, 2014-2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "openvswitch/json.h"
27 #include "openvswitch/dynamic-string.h"
29 #include "openvswitch/shash.h"
34 /* The type of a JSON token. */
35 enum json_token_type
{
41 T_NAME_SEPARATOR
= ':',
42 T_VALUE_SEPARATOR
= ',',
43 T_FALSE
= UCHAR_MAX
+ 1,
53 * RFC 4627 doesn't define a lexical structure for JSON but I believe this to
54 * be compliant with the standard.
57 enum json_token_type type
;
60 long long int integer
;
66 JSON_LEX_START
, /* Not inside a token. */
67 JSON_LEX_NUMBER
, /* Reading a number. */
68 JSON_LEX_KEYWORD
, /* Reading a keyword. */
69 JSON_LEX_STRING
, /* Reading a quoted string. */
70 JSON_LEX_ESCAPE
/* In a quoted string just after a "\". */
73 enum json_parse_state
{
74 JSON_PARSE_START
, /* Beginning of input. */
75 JSON_PARSE_END
, /* End of input. */
78 JSON_PARSE_OBJECT_INIT
, /* Expecting '}' or an object name. */
79 JSON_PARSE_OBJECT_NAME
, /* Expecting an object name. */
80 JSON_PARSE_OBJECT_COLON
, /* Expecting ':'. */
81 JSON_PARSE_OBJECT_VALUE
, /* Expecting an object value. */
82 JSON_PARSE_OBJECT_NEXT
, /* Expecting ',' or '}'. */
85 JSON_PARSE_ARRAY_INIT
, /* Expecting ']' or a value. */
86 JSON_PARSE_ARRAY_VALUE
, /* Expecting a value. */
87 JSON_PARSE_ARRAY_NEXT
/* Expecting ',' or ']'. */
90 struct json_parser_node
{
98 /* Lexical analysis. */
99 enum json_lex_state lex_state
;
100 struct ds buffer
; /* Buffer for accumulating token text. */
106 enum json_parse_state parse_state
;
107 #define JSON_MAX_HEIGHT 1000
108 struct json_parser_node
*stack
;
109 size_t height
, allocated_height
;
114 char *error
; /* Error message, if any, null if none yet. */
117 static struct json
*json_create(enum json_type type
);
118 static void json_parser_input(struct json_parser
*, struct json_token
*);
120 static void json_error(struct json_parser
*p
, const char *format
, ...)
121 OVS_PRINTF_FORMAT(2, 3);
124 json_type_to_string(enum json_type type
)
155 /* Functions for manipulating struct json. */
158 json_null_create(void)
160 return json_create(JSON_NULL
);
164 json_boolean_create(bool b
)
166 return json_create(b
? JSON_TRUE
: JSON_FALSE
);
170 json_string_create_nocopy(char *s
)
172 struct json
*json
= json_create(JSON_STRING
);
178 json_string_create(const char *s
)
180 return json_string_create_nocopy(xstrdup(s
));
184 json_array_create_empty(void)
186 struct json
*json
= json_create(JSON_ARRAY
);
187 json
->u
.array
.elems
= NULL
;
189 json
->u
.array
.n_allocated
= 0;
194 json_array_add(struct json
*array_
, struct json
*element
)
196 struct json_array
*array
= json_array(array_
);
197 if (array
->n
>= array
->n_allocated
) {
198 array
->elems
= x2nrealloc(array
->elems
, &array
->n_allocated
,
199 sizeof *array
->elems
);
201 array
->elems
[array
->n
++] = element
;
205 json_array_trim(struct json
*array_
)
207 struct json_array
*array
= json_array(array_
);
208 if (array
->n
< array
->n_allocated
){
209 array
->n_allocated
= array
->n
;
210 array
->elems
= xrealloc(array
->elems
, array
->n
* sizeof *array
->elems
);
215 json_array_create(struct json
**elements
, size_t n
)
217 struct json
*json
= json_create(JSON_ARRAY
);
218 json
->u
.array
.elems
= elements
;
220 json
->u
.array
.n_allocated
= n
;
225 json_array_create_1(struct json
*elem0
)
227 struct json
**elems
= xmalloc(sizeof *elems
);
229 return json_array_create(elems
, 1);
233 json_array_create_2(struct json
*elem0
, struct json
*elem1
)
235 struct json
**elems
= xmalloc(2 * sizeof *elems
);
238 return json_array_create(elems
, 2);
242 json_array_create_3(struct json
*elem0
, struct json
*elem1
, struct json
*elem2
)
244 struct json
**elems
= xmalloc(3 * sizeof *elems
);
248 return json_array_create(elems
, 3);
252 json_object_create(void)
254 struct json
*json
= json_create(JSON_OBJECT
);
255 json
->u
.object
= xmalloc(sizeof *json
->u
.object
);
256 shash_init(json
->u
.object
);
261 json_integer_create(long long int integer
)
263 struct json
*json
= json_create(JSON_INTEGER
);
264 json
->u
.integer
= integer
;
269 json_real_create(double real
)
271 struct json
*json
= json_create(JSON_REAL
);
277 json_object_put(struct json
*json
, const char *name
, struct json
*value
)
279 json_destroy(shash_replace(json
->u
.object
, name
, value
));
283 json_object_put_nocopy(struct json
*json
, char *name
, struct json
*value
)
285 json_destroy(shash_replace_nocopy(json
->u
.object
, name
, value
));
289 json_object_put_string(struct json
*json
, const char *name
, const char *value
)
291 json_object_put(json
, name
, json_string_create(value
));
294 void OVS_PRINTF_FORMAT(3, 4)
295 json_object_put_format(struct json
*json
,
296 const char *name
, const char *format
, ...)
299 va_start(args
, format
);
300 json_object_put(json
, name
,
301 json_string_create_nocopy(xvasprintf(format
, args
)));
306 json_string(const struct json
*json
)
308 ovs_assert(json
->type
== JSON_STRING
);
309 return json
->u
.string
;
313 json_array(const struct json
*json
)
315 ovs_assert(json
->type
== JSON_ARRAY
);
316 return CONST_CAST(struct json_array
*, &json
->u
.array
);
320 json_object(const struct json
*json
)
322 ovs_assert(json
->type
== JSON_OBJECT
);
323 return CONST_CAST(struct shash
*, json
->u
.object
);
327 json_boolean(const struct json
*json
)
329 ovs_assert(json
->type
== JSON_TRUE
|| json
->type
== JSON_FALSE
);
330 return json
->type
== JSON_TRUE
;
334 json_real(const struct json
*json
)
336 ovs_assert(json
->type
== JSON_REAL
|| json
->type
== JSON_INTEGER
);
337 return json
->type
== JSON_REAL
? json
->u
.real
: json
->u
.integer
;
341 json_integer(const struct json
*json
)
343 ovs_assert(json
->type
== JSON_INTEGER
);
344 return json
->u
.integer
;
347 static void json_destroy_object(struct shash
*object
);
348 static void json_destroy_array(struct json_array
*array
);
350 /* Frees 'json' and everything it points to, recursively. */
352 json_destroy(struct json
*json
)
354 if (json
&& !--json
->count
) {
355 switch (json
->type
) {
357 json_destroy_object(json
->u
.object
);
361 json_destroy_array(&json
->u
.array
);
365 free(json
->u
.string
);
383 json_destroy_object(struct shash
*object
)
385 struct shash_node
*node
, *next
;
387 SHASH_FOR_EACH_SAFE (node
, next
, object
) {
388 struct json
*value
= node
->data
;
391 shash_delete(object
, node
);
393 shash_destroy(object
);
398 json_destroy_array(struct json_array
*array
)
402 for (i
= 0; i
< array
->n
; i
++) {
403 json_destroy(array
->elems
[i
]);
408 static struct json
*json_clone_object(const struct shash
*object
);
409 static struct json
*json_clone_array(const struct json_array
*array
);
411 /* Returns a deep copy of 'json'. */
413 json_deep_clone(const struct json
*json
)
415 switch (json
->type
) {
417 return json_clone_object(json
->u
.object
);
420 return json_clone_array(&json
->u
.array
);
423 return json_string_create(json
->u
.string
);
428 return json_create(json
->type
);
431 return json_integer_create(json
->u
.integer
);
434 return json_real_create(json
->u
.real
);
442 /* Returns 'json', with the reference count incremented. */
444 json_clone(const struct json
*json_
)
446 struct json
*json
= CONST_CAST(struct json
*, json_
);
452 json_nullable_clone(const struct json
*json
)
454 return json
? json_clone(json
) : NULL
;
458 json_clone_object(const struct shash
*object
)
460 struct shash_node
*node
;
463 json
= json_object_create();
464 SHASH_FOR_EACH (node
, object
) {
465 struct json
*value
= node
->data
;
466 json_object_put(json
, node
->name
, json_clone(value
));
472 json_clone_array(const struct json_array
*array
)
477 elems
= xmalloc(array
->n
* sizeof *elems
);
478 for (i
= 0; i
< array
->n
; i
++) {
479 elems
[i
] = json_clone(array
->elems
[i
]);
481 return json_array_create(elems
, array
->n
);
485 json_hash_object(const struct shash
*object
, size_t basis
)
487 const struct shash_node
**nodes
;
490 nodes
= shash_sort(object
);
491 n
= shash_count(object
);
492 for (i
= 0; i
< n
; i
++) {
493 const struct shash_node
*node
= nodes
[i
];
494 basis
= hash_string(node
->name
, basis
);
495 basis
= json_hash(node
->data
, basis
);
502 json_hash_array(const struct json_array
*array
, size_t basis
)
506 basis
= hash_int(array
->n
, basis
);
507 for (i
= 0; i
< array
->n
; i
++) {
508 basis
= json_hash(array
->elems
[i
], basis
);
514 json_hash(const struct json
*json
, size_t basis
)
516 switch (json
->type
) {
518 return json_hash_object(json
->u
.object
, basis
);
521 return json_hash_array(&json
->u
.array
, basis
);
524 return hash_string(json
->u
.string
, basis
);
529 return hash_int(json
->type
<< 8, basis
);
532 return hash_int(json
->u
.integer
, basis
);
535 return hash_double(json
->u
.real
, basis
);
544 json_equal_object(const struct shash
*a
, const struct shash
*b
)
546 struct shash_node
*a_node
;
548 if (shash_count(a
) != shash_count(b
)) {
552 SHASH_FOR_EACH (a_node
, a
) {
553 struct shash_node
*b_node
= shash_find(b
, a_node
->name
);
554 if (!b_node
|| !json_equal(a_node
->data
, b_node
->data
)) {
563 json_equal_array(const struct json_array
*a
, const struct json_array
*b
)
571 for (i
= 0; i
< a
->n
; i
++) {
572 if (!json_equal(a
->elems
[i
], b
->elems
[i
])) {
581 json_equal(const struct json
*a
, const struct json
*b
)
585 } else if (!a
|| !b
) {
587 } else if (a
->type
!= b
->type
) {
593 return json_equal_object(a
->u
.object
, b
->u
.object
);
596 return json_equal_array(&a
->u
.array
, &b
->u
.array
);
599 return !strcmp(a
->u
.string
, b
->u
.string
);
607 return a
->u
.integer
== b
->u
.integer
;
610 return a
->u
.real
== b
->u
.real
;
618 /* Lexical analysis. */
621 json_lex_keyword(struct json_parser
*p
)
623 struct json_token token
;
626 s
= ds_cstr(&p
->buffer
);
627 if (!strcmp(s
, "false")) {
628 token
.type
= T_FALSE
;
629 } else if (!strcmp(s
, "true")) {
631 } else if (!strcmp(s
, "null")) {
634 json_error(p
, "invalid keyword '%s'", s
);
637 json_parser_input(p
, &token
);
641 json_lex_number(struct json_parser
*p
)
643 const char *cp
= ds_cstr(&p
->buffer
);
644 unsigned long long int significand
= 0;
645 struct json_token token
;
646 bool imprecise
= false;
647 bool negative
= false;
650 /* Leading minus sign. */
656 /* At least one integer digit, but 0 may not be used as a leading digit for
657 * a longer number. */
661 if (isdigit((unsigned char) *cp
)) {
662 json_error(p
, "leading zeros not allowed");
665 } else if (isdigit((unsigned char) *cp
)) {
667 if (significand
<= ULLONG_MAX
/ 10) {
668 significand
= significand
* 10 + (*cp
- '0');
676 } while (isdigit((unsigned char) *cp
));
678 json_error(p
, "'-' must be followed by digit");
682 /* Optional fraction. */
685 if (!isdigit((unsigned char) *cp
)) {
686 json_error(p
, "decimal point must be followed by digit");
690 if (significand
<= ULLONG_MAX
/ 10) {
691 significand
= significand
* 10 + (*cp
- '0');
693 } else if (*cp
!= '0') {
697 } while (isdigit((unsigned char) *cp
));
700 /* Optional exponent. */
701 if (*cp
== 'e' || *cp
== 'E') {
702 bool negative_exponent
= false;
708 } else if (*cp
== '-') {
709 negative_exponent
= true;
713 if (!isdigit((unsigned char) *cp
)) {
714 json_error(p
, "exponent must contain at least one digit");
720 if (exponent
>= INT_MAX
/ 10) {
721 json_error(p
, "exponent outside valid range");
724 exponent
= exponent
* 10 + (*cp
- '0');
726 } while (isdigit((unsigned char) *cp
));
728 if (negative_exponent
) {
736 json_error(p
, "syntax error in number");
740 /* Figure out number.
742 * We suppress negative zeros as a matter of policy. */
744 token
.type
= T_INTEGER
;
746 json_parser_input(p
, &token
);
751 while (pow10
> 0 && significand
< ULLONG_MAX
/ 10) {
755 while (pow10
< 0 && significand
% 10 == 0) {
760 && significand
<= (negative
761 ? (unsigned long long int) LLONG_MAX
+ 1
763 token
.type
= T_INTEGER
;
764 token
.u
.integer
= negative
? -significand
: significand
;
765 json_parser_input(p
, &token
);
771 if (!str_to_double(ds_cstr(&p
->buffer
), &token
.u
.real
)) {
772 json_error(p
, "number outside valid range");
775 /* Suppress negative zero. */
776 if (token
.u
.real
== 0) {
779 json_parser_input(p
, &token
);
783 json_lex_4hex(const char *cp
, const char *end
, int *valuep
)
789 return "quoted string ends within \\u escape";
792 value
= hexits_value(cp
, 4, &ok
);
794 return "malformed \\u escape";
797 return "null bytes not supported in quoted strings";
804 json_lex_unicode(const char *cp
, const char *end
, struct ds
*out
)
809 error
= json_lex_4hex(cp
, end
, &c0
);
812 ds_put_cstr(out
, error
);
816 if (!uc_is_leading_surrogate(c0
)) {
817 ds_put_utf8(out
, c0
);
821 if (cp
+ 2 > end
|| *cp
++ != '\\' || *cp
++ != 'u') {
823 ds_put_cstr(out
, "malformed escaped surrogate pair");
827 error
= json_lex_4hex(cp
, end
, &c1
);
830 ds_put_cstr(out
, error
);
834 if (!uc_is_trailing_surrogate(c1
)) {
836 ds_put_cstr(out
, "second half of escaped surrogate pair is not "
837 "trailing surrogate");
841 ds_put_utf8(out
, utf16_decode_surrogate_pair(c0
, c1
));
846 json_string_unescape(const char *in
, size_t in_len
, char **outp
)
848 const char *end
= in
+ in_len
;
853 ds_reserve(&out
, in_len
);
857 ds_put_cstr(&out
, "quoted string may not include unescaped \"");
861 ds_put_char(&out
, *in
++);
867 /* The JSON parser will never trigger this message, because its
868 * lexer will never pass in a string that ends in a single
869 * backslash, but json_string_unescape() has other callers that
870 * are not as careful.*/
872 ds_put_cstr(&out
, "quoted string may not end with backslash");
876 case '"': case '\\': case '/':
877 ds_put_char(&out
, in
[-1]);
881 ds_put_char(&out
, '\b');
885 ds_put_char(&out
, '\f');
889 ds_put_char(&out
, '\n');
893 ds_put_char(&out
, '\r');
897 ds_put_char(&out
, '\t');
901 in
= json_lex_unicode(in
, end
, &out
);
909 ds_put_format(&out
, "bad escape \\%c", in
[-1]);
916 *outp
= ds_cstr(&out
);
921 json_string_escape(const char *in
, struct ds
*out
)
925 .u
.string
= CONST_CAST(char *, in
),
927 json_to_ds(&json
, 0, out
);
931 json_parser_input_string(struct json_parser
*p
, const char *s
)
933 struct json_token token
;
935 token
.type
= T_STRING
;
937 json_parser_input(p
, &token
);
941 json_lex_string(struct json_parser
*p
)
943 const char *raw
= ds_cstr(&p
->buffer
);
944 if (!strchr(raw
, '\\')) {
945 json_parser_input_string(p
, raw
);
949 if (json_string_unescape(raw
, strlen(raw
), &cooked
)) {
950 json_parser_input_string(p
, cooked
);
952 json_error(p
, "%s", cooked
);
960 json_lex_input(struct json_parser
*p
, unsigned char c
)
962 struct json_token token
;
964 switch (p
->lex_state
) {
967 case ' ': case '\t': case '\n': case '\r':
971 case 'a': case 'b': case 'c': case 'd': case 'e':
972 case 'f': case 'g': case 'h': case 'i': case 'j':
973 case 'k': case 'l': case 'm': case 'n': case 'o':
974 case 'p': case 'q': case 'r': case 's': case 't':
975 case 'u': case 'v': case 'w': case 'x': case 'y':
977 p
->lex_state
= JSON_LEX_KEYWORD
;
980 case '[': case '{': case ']': case '}': case ':': case ',':
982 json_parser_input(p
, &token
);
986 case '0': case '1': case '2': case '3': case '4':
987 case '5': case '6': case '7': case '8': case '9':
988 p
->lex_state
= JSON_LEX_NUMBER
;
992 p
->lex_state
= JSON_LEX_STRING
;
997 json_error(p
, "invalid character '%c'", c
);
999 json_error(p
, "invalid character U+%04x", c
);
1005 case JSON_LEX_KEYWORD
:
1006 if (!isalpha((unsigned char) c
)) {
1007 json_lex_keyword(p
);
1012 case JSON_LEX_NUMBER
:
1013 if (!strchr(".0123456789eE-+", c
)) {
1019 case JSON_LEX_STRING
:
1021 p
->lex_state
= JSON_LEX_ESCAPE
;
1022 } else if (c
== '"') {
1025 } else if (c
< 0x20) {
1026 json_error(p
, "U+%04X must be escaped in quoted string", c
);
1031 case JSON_LEX_ESCAPE
:
1032 p
->lex_state
= JSON_LEX_STRING
;
1038 ds_put_char(&p
->buffer
, c
);
1044 /* Parses 'string' as a JSON object or array and returns a newly allocated
1045 * 'struct json'. The caller must free the returned structure with
1046 * json_destroy() when it is no longer needed.
1048 * 'string' must be encoded in UTF-8.
1050 * If 'string' is valid JSON, then the returned 'struct json' will be either an
1051 * object (JSON_OBJECT) or an array (JSON_ARRAY).
1053 * If 'string' is not valid JSON, then the returned 'struct json' will be a
1054 * string (JSON_STRING) that describes the particular error encountered during
1055 * parsing. (This is an acceptable means of error reporting because at its top
1056 * level JSON must be either an object or an array; a bare string is not
1059 json_from_string(const char *string
)
1061 struct json_parser
*p
= json_parser_create(JSPF_TRAILER
);
1062 json_parser_feed(p
, string
, strlen(string
));
1063 return json_parser_finish(p
);
1066 /* Reads the file named 'file_name', parses its contents as a JSON object or
1067 * array, and returns a newly allocated 'struct json'. The caller must free
1068 * the returned structure with json_destroy() when it is no longer needed.
1070 * The file must be encoded in UTF-8.
1072 * See json_from_string() for return value semantics.
1075 json_from_file(const char *file_name
)
1080 stream
= fopen(file_name
, "r");
1082 return json_string_create_nocopy(
1083 xasprintf("error opening \"%s\": %s", file_name
,
1084 ovs_strerror(errno
)));
1086 json
= json_from_stream(stream
);
1092 /* Parses the contents of 'stream' as a JSON object or array, and returns a
1093 * newly allocated 'struct json'. The caller must free the returned structure
1094 * with json_destroy() when it is no longer needed.
1096 * The file must be encoded in UTF-8.
1098 * See json_from_string() for return value semantics.
1101 json_from_stream(FILE *stream
)
1103 struct json_parser
*p
;
1106 p
= json_parser_create(JSPF_TRAILER
);
1108 char buffer
[BUFSIZ
];
1111 n
= fread(buffer
, 1, sizeof buffer
, stream
);
1112 if (!n
|| json_parser_feed(p
, buffer
, n
) != n
) {
1116 json
= json_parser_finish(p
);
1118 if (ferror(stream
)) {
1120 json
= json_string_create_nocopy(
1121 xasprintf("error reading JSON stream: %s", ovs_strerror(errno
)));
1127 struct json_parser
*
1128 json_parser_create(int flags
)
1130 struct json_parser
*p
= xzalloc(sizeof *p
);
1136 json_parser_feed(struct json_parser
*p
, const char *input
, size_t n
)
1139 for (i
= 0; !p
->done
&& i
< n
; ) {
1140 if (json_lex_input(p
, input
[i
])) {
1142 if (input
[i
] == '\n') {
1143 p
->column_number
= 0;
1155 json_parser_is_done(const struct json_parser
*p
)
1161 json_parser_finish(struct json_parser
*p
)
1165 switch (p
->lex_state
) {
1166 case JSON_LEX_START
:
1169 case JSON_LEX_STRING
:
1170 case JSON_LEX_ESCAPE
:
1171 json_error(p
, "unexpected end of input in quoted string");
1174 case JSON_LEX_NUMBER
:
1175 case JSON_LEX_KEYWORD
:
1176 json_lex_input(p
, ' ');
1180 if (p
->parse_state
== JSON_PARSE_START
) {
1181 json_error(p
, "empty input stream");
1182 } else if (p
->parse_state
!= JSON_PARSE_END
) {
1183 json_error(p
, "unexpected end of input");
1187 ovs_assert(p
->height
== 1);
1188 ovs_assert(p
->stack
[0].json
!= NULL
);
1189 json
= p
->stack
[--p
->height
].json
;
1191 json
= json_string_create_nocopy(p
->error
);
1195 json_parser_abort(p
);
1201 json_parser_abort(struct json_parser
*p
)
1204 ds_destroy(&p
->buffer
);
1206 json_destroy(p
->stack
[0].json
);
1209 free(p
->member_name
);
1215 static struct json_parser_node
*
1216 json_parser_top(struct json_parser
*p
)
1218 return &p
->stack
[p
->height
- 1];
1222 json_parser_put_value(struct json_parser
*p
, struct json
*value
)
1224 struct json_parser_node
*node
= json_parser_top(p
);
1225 if (node
->json
->type
== JSON_OBJECT
) {
1226 json_object_put_nocopy(node
->json
, p
->member_name
, value
);
1227 p
->member_name
= NULL
;
1228 } else if (node
->json
->type
== JSON_ARRAY
) {
1229 json_array_add(node
->json
, value
);
1236 json_parser_push(struct json_parser
*p
,
1237 struct json
*new_json
, enum json_parse_state new_state
)
1239 if (p
->height
< JSON_MAX_HEIGHT
) {
1240 struct json_parser_node
*node
;
1242 if (p
->height
>= p
->allocated_height
) {
1243 p
->stack
= x2nrealloc(p
->stack
, &p
->allocated_height
,
1247 if (p
->height
> 0) {
1248 json_parser_put_value(p
, new_json
);
1251 node
= &p
->stack
[p
->height
++];
1252 node
->json
= new_json
;
1253 p
->parse_state
= new_state
;
1255 json_destroy(new_json
);
1256 json_error(p
, "input exceeds maximum nesting depth %d",
1262 json_parser_push_object(struct json_parser
*p
)
1264 json_parser_push(p
, json_object_create(), JSON_PARSE_OBJECT_INIT
);
1268 json_parser_push_array(struct json_parser
*p
)
1270 json_parser_push(p
, json_array_create_empty(), JSON_PARSE_ARRAY_INIT
);
1274 json_parse_value(struct json_parser
*p
, struct json_token
*token
,
1275 enum json_parse_state next_state
)
1279 switch (token
->type
) {
1281 value
= json_boolean_create(false);
1285 value
= json_null_create();
1289 value
= json_boolean_create(true);
1293 json_parser_push_object(p
);
1297 json_parser_push_array(p
);
1301 value
= json_integer_create(token
->u
.integer
);
1305 value
= json_real_create(token
->u
.real
);
1309 value
= json_string_create(token
->u
.string
);
1318 json_error(p
, "syntax error expecting value");
1322 json_parser_put_value(p
, value
);
1323 p
->parse_state
= next_state
;
1327 json_parser_pop(struct json_parser
*p
)
1329 struct json_parser_node
*node
;
1331 /* Conserve memory. */
1332 node
= json_parser_top(p
);
1333 if (node
->json
->type
== JSON_ARRAY
) {
1334 json_array_trim(node
->json
);
1337 /* Pop off the top-of-stack. */
1338 if (p
->height
== 1) {
1339 p
->parse_state
= JSON_PARSE_END
;
1340 if (!(p
->flags
& JSPF_TRAILER
)) {
1345 node
= json_parser_top(p
);
1346 if (node
->json
->type
== JSON_ARRAY
) {
1347 p
->parse_state
= JSON_PARSE_ARRAY_NEXT
;
1348 } else if (node
->json
->type
== JSON_OBJECT
) {
1349 p
->parse_state
= JSON_PARSE_OBJECT_NEXT
;
1357 json_parser_input(struct json_parser
*p
, struct json_token
*token
)
1359 switch (p
->parse_state
) {
1360 case JSON_PARSE_START
:
1361 if (token
->type
== '{') {
1362 json_parser_push_object(p
);
1363 } else if (token
->type
== '[') {
1364 json_parser_push_array(p
);
1366 json_error(p
, "syntax error at beginning of input");
1370 case JSON_PARSE_END
:
1371 json_error(p
, "trailing garbage at end of input");
1374 case JSON_PARSE_OBJECT_INIT
:
1375 if (token
->type
== '}') {
1380 case JSON_PARSE_OBJECT_NAME
:
1381 if (token
->type
== T_STRING
) {
1382 p
->member_name
= xstrdup(token
->u
.string
);
1383 p
->parse_state
= JSON_PARSE_OBJECT_COLON
;
1385 json_error(p
, "syntax error parsing object expecting string");
1389 case JSON_PARSE_OBJECT_COLON
:
1390 if (token
->type
== ':') {
1391 p
->parse_state
= JSON_PARSE_OBJECT_VALUE
;
1393 json_error(p
, "syntax error parsing object expecting ':'");
1397 case JSON_PARSE_OBJECT_VALUE
:
1398 json_parse_value(p
, token
, JSON_PARSE_OBJECT_NEXT
);
1401 case JSON_PARSE_OBJECT_NEXT
:
1402 if (token
->type
== ',') {
1403 p
->parse_state
= JSON_PARSE_OBJECT_NAME
;
1404 } else if (token
->type
== '}') {
1407 json_error(p
, "syntax error expecting '}' or ','");
1411 case JSON_PARSE_ARRAY_INIT
:
1412 if (token
->type
== ']') {
1417 case JSON_PARSE_ARRAY_VALUE
:
1418 json_parse_value(p
, token
, JSON_PARSE_ARRAY_NEXT
);
1421 case JSON_PARSE_ARRAY_NEXT
:
1422 if (token
->type
== ',') {
1423 p
->parse_state
= JSON_PARSE_ARRAY_VALUE
;
1424 } else if (token
->type
== ']') {
1427 json_error(p
, "syntax error expecting ']' or ','");
1435 p
->lex_state
= JSON_LEX_START
;
1436 ds_clear(&p
->buffer
);
1439 static struct json
*
1440 json_create(enum json_type type
)
1442 struct json
*json
= xmalloc(sizeof *json
);
1449 json_error(struct json_parser
*p
, const char *format
, ...)
1456 ds_put_format(&msg
, "line %d, column %d, byte %d: ",
1457 p
->line_number
, p
->column_number
, p
->byte_number
);
1458 va_start(args
, format
);
1459 ds_put_format_valist(&msg
, format
, args
);
1462 p
->error
= ds_steal_cstr(&msg
);
1468 #define SPACES_PER_LEVEL 2
1470 struct json_serializer
{
1476 static void json_serialize(const struct json
*, struct json_serializer
*);
1477 static void json_serialize_object(const struct shash
*object
,
1478 struct json_serializer
*);
1479 static void json_serialize_array(const struct json_array
*,
1480 struct json_serializer
*);
1481 static void json_serialize_string(const char *, struct ds
*);
1483 /* Converts 'json' to a string in JSON format, encoded in UTF-8, and returns
1484 * that string. The caller is responsible for freeing the returned string,
1485 * with free(), when it is no longer needed.
1487 * If 'flags' contains JSSF_PRETTY, the output is pretty-printed with each
1488 * nesting level introducing an additional indentation. Otherwise, the
1489 * returned string does not contain any new-line characters.
1491 * If 'flags' contains JSSF_SORT, members of objects in the output are sorted
1492 * in bytewise lexicographic order for reproducibility. Otherwise, members of
1493 * objects are output in an indeterminate order.
1495 * The returned string is valid JSON only if 'json' represents an array or an
1496 * object, since a bare literal does not satisfy the JSON grammar. */
1498 json_to_string(const struct json
*json
, int flags
)
1503 json_to_ds(json
, flags
, &ds
);
1504 return ds_steal_cstr(&ds
);
1507 /* Same as json_to_string(), but the output is appended to 'ds'. */
1509 json_to_ds(const struct json
*json
, int flags
, struct ds
*ds
)
1511 struct json_serializer s
;
1516 json_serialize(json
, &s
);
1520 json_serialize(const struct json
*json
, struct json_serializer
*s
)
1522 struct ds
*ds
= s
->ds
;
1524 switch (json
->type
) {
1526 ds_put_cstr(ds
, "null");
1530 ds_put_cstr(ds
, "false");
1534 ds_put_cstr(ds
, "true");
1538 json_serialize_object(json
->u
.object
, s
);
1542 json_serialize_array(&json
->u
.array
, s
);
1546 ds_put_format(ds
, "%lld", json
->u
.integer
);
1550 ds_put_format(ds
, "%.*g", DBL_DIG
, json
->u
.real
);
1554 json_serialize_string(json
->u
.string
, ds
);
1564 indent_line(struct json_serializer
*s
)
1566 if (s
->flags
& JSSF_PRETTY
) {
1567 ds_put_char(s
->ds
, '\n');
1568 ds_put_char_multiple(s
->ds
, ' ', SPACES_PER_LEVEL
* s
->depth
);
1573 json_serialize_object_member(size_t i
, const struct shash_node
*node
,
1574 struct json_serializer
*s
)
1576 struct ds
*ds
= s
->ds
;
1579 ds_put_char(ds
, ',');
1583 json_serialize_string(node
->name
, ds
);
1584 ds_put_char(ds
, ':');
1585 if (s
->flags
& JSSF_PRETTY
) {
1586 ds_put_char(ds
, ' ');
1588 json_serialize(node
->data
, s
);
1592 json_serialize_object(const struct shash
*object
, struct json_serializer
*s
)
1594 struct ds
*ds
= s
->ds
;
1596 ds_put_char(ds
, '{');
1601 if (s
->flags
& JSSF_SORT
) {
1602 const struct shash_node
**nodes
;
1605 nodes
= shash_sort(object
);
1606 n
= shash_count(object
);
1607 for (i
= 0; i
< n
; i
++) {
1608 json_serialize_object_member(i
, nodes
[i
], s
);
1612 struct shash_node
*node
;
1616 SHASH_FOR_EACH (node
, object
) {
1617 json_serialize_object_member(i
++, node
, s
);
1621 ds_put_char(ds
, '}');
1626 json_serialize_array(const struct json_array
*array
, struct json_serializer
*s
)
1628 struct ds
*ds
= s
->ds
;
1631 ds_put_char(ds
, '[');
1637 for (i
= 0; i
< array
->n
; i
++) {
1639 ds_put_char(ds
, ',');
1642 json_serialize(array
->elems
[i
], s
);
1647 ds_put_char(ds
, ']');
1650 static const char *chars_escaping
[256] = {
1651 "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007",
1652 "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f",
1653 "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014", "\\u0015", "\\u0016", "\\u0017",
1654 "\\u0018", "\\u0019", "\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f",
1655 " ", "!", "\\\"", "#", "$", "%", "&", "'",
1656 "(", ")", "*", "+", ",", "-", ".", "/",
1657 "0", "1", "2", "3", "4", "5", "6", "7",
1658 "8", "9", ":", ";", "<", "=", ">", "?",
1659 "@", "A", "B", "C", "D", "E", "F", "G",
1660 "H", "I", "J", "K", "L", "M", "N", "O",
1661 "P", "Q", "R", "S", "T", "U", "V", "W",
1662 "X", "Y", "Z", "[", "\\\\", "]", "^", "_",
1663 "`", "a", "b", "c", "d", "e", "f", "g",
1664 "h", "i", "j", "k", "l", "m", "n", "o",
1665 "p", "q", "r", "s", "t", "u", "v", "w",
1666 "x", "y", "z", "{", "|", "}", "~", "\x7f",
1667 "\x80", "\x81", "\x82", "\x83", "\x84", "\x85", "\x86", "\x87",
1668 "\x88", "\x89", "\x8a", "\x8b", "\x8c", "\x8d", "\x8e", "\x8f",
1669 "\x90", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96", "\x97",
1670 "\x98", "\x99", "\x9a", "\x9b", "\x9c", "\x9d", "\x9e", "\x9f",
1671 "\xa0", "\xa1", "\xa2", "\xa3", "\xa4", "\xa5", "\xa6", "\xa7",
1672 "\xa8", "\xa9", "\xaa", "\xab", "\xac", "\xad", "\xae", "\xaf",
1673 "\xb0", "\xb1", "\xb2", "\xb3", "\xb4", "\xb5", "\xb6", "\xb7",
1674 "\xb8", "\xb9", "\xba", "\xbb", "\xbc", "\xbd", "\xbe", "\xbf",
1675 "\xc0", "\xc1", "\xc2", "\xc3", "\xc4", "\xc5", "\xc6", "\xc7",
1676 "\xc8", "\xc9", "\xca", "\xcb", "\xcc", "\xcd", "\xce", "\xcf",
1677 "\xd0", "\xd1", "\xd2", "\xd3", "\xd4", "\xd5", "\xd6", "\xd7",
1678 "\xd8", "\xd9", "\xda", "\xdb", "\xdc", "\xdd", "\xde", "\xdf",
1679 "\xe0", "\xe1", "\xe2", "\xe3", "\xe4", "\xe5", "\xe6", "\xe7",
1680 "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef",
1681 "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7",
1682 "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff"
1686 json_serialize_string(const char *string
, struct ds
*ds
)
1692 ds_put_char(ds
, '"');
1693 while ((c
= *string
++) != '\0') {
1694 escape
= chars_escaping
[c
];
1695 while ((c2
= *escape
++) != '\0') {
1696 ds_put_char(ds
, c2
);
1699 ds_put_char(ds
, '"');