2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015 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"
33 /* The type of a JSON token. */
34 enum json_token_type
{
40 T_NAME_SEPARATOR
= ':',
41 T_VALUE_SEPARATOR
= ',',
42 T_FALSE
= UCHAR_MAX
+ 1,
52 * RFC 4627 doesn't define a lexical structure for JSON but I believe this to
53 * be compliant with the standard.
56 enum json_token_type type
;
59 long long int integer
;
65 JSON_LEX_START
, /* Not inside a token. */
66 JSON_LEX_NUMBER
, /* Reading a number. */
67 JSON_LEX_KEYWORD
, /* Reading a keyword. */
68 JSON_LEX_STRING
, /* Reading a quoted string. */
69 JSON_LEX_ESCAPE
/* In a quoted string just after a "\". */
72 enum json_parse_state
{
73 JSON_PARSE_START
, /* Beginning of input. */
74 JSON_PARSE_END
, /* End of input. */
77 JSON_PARSE_OBJECT_INIT
, /* Expecting '}' or an object name. */
78 JSON_PARSE_OBJECT_NAME
, /* Expecting an object name. */
79 JSON_PARSE_OBJECT_COLON
, /* Expecting ':'. */
80 JSON_PARSE_OBJECT_VALUE
, /* Expecting an object value. */
81 JSON_PARSE_OBJECT_NEXT
, /* Expecting ',' or '}'. */
84 JSON_PARSE_ARRAY_INIT
, /* Expecting ']' or a value. */
85 JSON_PARSE_ARRAY_VALUE
, /* Expecting a value. */
86 JSON_PARSE_ARRAY_NEXT
/* Expecting ',' or ']'. */
89 struct json_parser_node
{
97 /* Lexical analysis. */
98 enum json_lex_state lex_state
;
99 struct ds buffer
; /* Buffer for accumulating token text. */
105 enum json_parse_state parse_state
;
106 #define JSON_MAX_HEIGHT 1000
107 struct json_parser_node
*stack
;
108 size_t height
, allocated_height
;
113 char *error
; /* Error message, if any, null if none yet. */
116 static struct json
*json_create(enum json_type type
);
117 static void json_parser_input(struct json_parser
*, struct json_token
*);
119 static void json_error(struct json_parser
*p
, const char *format
, ...)
120 OVS_PRINTF_FORMAT(2, 3);
123 json_type_to_string(enum json_type type
)
154 /* Functions for manipulating struct json. */
157 json_null_create(void)
159 return json_create(JSON_NULL
);
163 json_boolean_create(bool b
)
165 return json_create(b
? JSON_TRUE
: JSON_FALSE
);
169 json_string_create_nocopy(char *s
)
171 struct json
*json
= json_create(JSON_STRING
);
177 json_string_create(const char *s
)
179 return json_string_create_nocopy(xstrdup(s
));
183 json_array_create_empty(void)
185 struct json
*json
= json_create(JSON_ARRAY
);
186 json
->u
.array
.elems
= NULL
;
188 json
->u
.array
.n_allocated
= 0;
193 json_array_add(struct json
*array_
, struct json
*element
)
195 struct json_array
*array
= json_array(array_
);
196 if (array
->n
>= array
->n_allocated
) {
197 array
->elems
= x2nrealloc(array
->elems
, &array
->n_allocated
,
198 sizeof *array
->elems
);
200 array
->elems
[array
->n
++] = element
;
204 json_array_trim(struct json
*array_
)
206 struct json_array
*array
= json_array(array_
);
207 if (array
->n
< array
->n_allocated
){
208 array
->n_allocated
= array
->n
;
209 array
->elems
= xrealloc(array
->elems
, array
->n
* sizeof *array
->elems
);
214 json_array_create(struct json
**elements
, size_t n
)
216 struct json
*json
= json_create(JSON_ARRAY
);
217 json
->u
.array
.elems
= elements
;
219 json
->u
.array
.n_allocated
= n
;
224 json_array_create_1(struct json
*elem0
)
226 struct json
**elems
= xmalloc(sizeof *elems
);
228 return json_array_create(elems
, 1);
232 json_array_create_2(struct json
*elem0
, struct json
*elem1
)
234 struct json
**elems
= xmalloc(2 * sizeof *elems
);
237 return json_array_create(elems
, 2);
241 json_array_create_3(struct json
*elem0
, struct json
*elem1
, struct json
*elem2
)
243 struct json
**elems
= xmalloc(3 * sizeof *elems
);
247 return json_array_create(elems
, 3);
251 json_object_create(void)
253 struct json
*json
= json_create(JSON_OBJECT
);
254 json
->u
.object
= xmalloc(sizeof *json
->u
.object
);
255 shash_init(json
->u
.object
);
260 json_integer_create(long long int integer
)
262 struct json
*json
= json_create(JSON_INTEGER
);
263 json
->u
.integer
= integer
;
268 json_real_create(double real
)
270 struct json
*json
= json_create(JSON_REAL
);
276 json_object_put(struct json
*json
, const char *name
, struct json
*value
)
278 json_destroy(shash_replace(json
->u
.object
, name
, value
));
282 json_object_put_string(struct json
*json
, const char *name
, const char *value
)
284 json_object_put(json
, name
, json_string_create(value
));
288 json_string(const struct json
*json
)
290 ovs_assert(json
->type
== JSON_STRING
);
291 return json
->u
.string
;
295 json_array(const struct json
*json
)
297 ovs_assert(json
->type
== JSON_ARRAY
);
298 return CONST_CAST(struct json_array
*, &json
->u
.array
);
302 json_object(const struct json
*json
)
304 ovs_assert(json
->type
== JSON_OBJECT
);
305 return CONST_CAST(struct shash
*, json
->u
.object
);
309 json_boolean(const struct json
*json
)
311 ovs_assert(json
->type
== JSON_TRUE
|| json
->type
== JSON_FALSE
);
312 return json
->type
== JSON_TRUE
;
316 json_real(const struct json
*json
)
318 ovs_assert(json
->type
== JSON_REAL
|| json
->type
== JSON_INTEGER
);
319 return json
->type
== JSON_REAL
? json
->u
.real
: json
->u
.integer
;
323 json_integer(const struct json
*json
)
325 ovs_assert(json
->type
== JSON_INTEGER
);
326 return json
->u
.integer
;
329 static void json_destroy_object(struct shash
*object
);
330 static void json_destroy_array(struct json_array
*array
);
332 /* Frees 'json' and everything it points to, recursively. */
334 json_destroy(struct json
*json
)
336 if (json
&& !--json
->count
) {
337 switch (json
->type
) {
339 json_destroy_object(json
->u
.object
);
343 json_destroy_array(&json
->u
.array
);
347 free(json
->u
.string
);
365 json_destroy_object(struct shash
*object
)
367 struct shash_node
*node
, *next
;
369 SHASH_FOR_EACH_SAFE (node
, next
, object
) {
370 struct json
*value
= node
->data
;
373 shash_delete(object
, node
);
375 shash_destroy(object
);
380 json_destroy_array(struct json_array
*array
)
384 for (i
= 0; i
< array
->n
; i
++) {
385 json_destroy(array
->elems
[i
]);
390 static struct json
*json_clone_object(const struct shash
*object
);
391 static struct json
*json_clone_array(const struct json_array
*array
);
393 /* Returns a deep copy of 'json'. */
395 json_deep_clone(const struct json
*json
)
397 switch (json
->type
) {
399 return json_clone_object(json
->u
.object
);
402 return json_clone_array(&json
->u
.array
);
405 return json_string_create(json
->u
.string
);
410 return json_create(json
->type
);
413 return json_integer_create(json
->u
.integer
);
416 return json_real_create(json
->u
.real
);
424 /* Returns 'json', with the reference count incremented. */
426 json_clone(const struct json
*json_
)
428 struct json
*json
= CONST_CAST(struct json
*, json_
);
434 json_clone_object(const struct shash
*object
)
436 struct shash_node
*node
;
439 json
= json_object_create();
440 SHASH_FOR_EACH (node
, object
) {
441 struct json
*value
= node
->data
;
442 json_object_put(json
, node
->name
, json_clone(value
));
448 json_clone_array(const struct json_array
*array
)
453 elems
= xmalloc(array
->n
* sizeof *elems
);
454 for (i
= 0; i
< array
->n
; i
++) {
455 elems
[i
] = json_clone(array
->elems
[i
]);
457 return json_array_create(elems
, array
->n
);
461 json_hash_object(const struct shash
*object
, size_t basis
)
463 const struct shash_node
**nodes
;
466 nodes
= shash_sort(object
);
467 n
= shash_count(object
);
468 for (i
= 0; i
< n
; i
++) {
469 const struct shash_node
*node
= nodes
[i
];
470 basis
= hash_string(node
->name
, basis
);
471 basis
= json_hash(node
->data
, basis
);
478 json_hash_array(const struct json_array
*array
, size_t basis
)
482 basis
= hash_int(array
->n
, basis
);
483 for (i
= 0; i
< array
->n
; i
++) {
484 basis
= json_hash(array
->elems
[i
], basis
);
490 json_hash(const struct json
*json
, size_t basis
)
492 switch (json
->type
) {
494 return json_hash_object(json
->u
.object
, basis
);
497 return json_hash_array(&json
->u
.array
, basis
);
500 return hash_string(json
->u
.string
, basis
);
505 return hash_int(json
->type
<< 8, basis
);
508 return hash_int(json
->u
.integer
, basis
);
511 return hash_double(json
->u
.real
, basis
);
520 json_equal_object(const struct shash
*a
, const struct shash
*b
)
522 struct shash_node
*a_node
;
524 if (shash_count(a
) != shash_count(b
)) {
528 SHASH_FOR_EACH (a_node
, a
) {
529 struct shash_node
*b_node
= shash_find(b
, a_node
->name
);
530 if (!b_node
|| !json_equal(a_node
->data
, b_node
->data
)) {
539 json_equal_array(const struct json_array
*a
, const struct json_array
*b
)
547 for (i
= 0; i
< a
->n
; i
++) {
548 if (!json_equal(a
->elems
[i
], b
->elems
[i
])) {
557 json_equal(const struct json
*a
, const struct json
*b
)
563 if (a
->type
!= b
->type
) {
569 return json_equal_object(a
->u
.object
, b
->u
.object
);
572 return json_equal_array(&a
->u
.array
, &b
->u
.array
);
575 return !strcmp(a
->u
.string
, b
->u
.string
);
583 return a
->u
.integer
== b
->u
.integer
;
586 return a
->u
.real
== b
->u
.real
;
594 /* Lexical analysis. */
597 json_lex_keyword(struct json_parser
*p
)
599 struct json_token token
;
602 s
= ds_cstr(&p
->buffer
);
603 if (!strcmp(s
, "false")) {
604 token
.type
= T_FALSE
;
605 } else if (!strcmp(s
, "true")) {
607 } else if (!strcmp(s
, "null")) {
610 json_error(p
, "invalid keyword '%s'", s
);
613 json_parser_input(p
, &token
);
617 json_lex_number(struct json_parser
*p
)
619 const char *cp
= ds_cstr(&p
->buffer
);
620 unsigned long long int significand
= 0;
621 struct json_token token
;
622 bool imprecise
= false;
623 bool negative
= false;
626 /* Leading minus sign. */
632 /* At least one integer digit, but 0 may not be used as a leading digit for
633 * a longer number. */
637 if (isdigit((unsigned char) *cp
)) {
638 json_error(p
, "leading zeros not allowed");
641 } else if (isdigit((unsigned char) *cp
)) {
643 if (significand
<= ULLONG_MAX
/ 10) {
644 significand
= significand
* 10 + (*cp
- '0');
652 } while (isdigit((unsigned char) *cp
));
654 json_error(p
, "'-' must be followed by digit");
658 /* Optional fraction. */
661 if (!isdigit((unsigned char) *cp
)) {
662 json_error(p
, "decimal point must be followed by digit");
666 if (significand
<= ULLONG_MAX
/ 10) {
667 significand
= significand
* 10 + (*cp
- '0');
669 } else if (*cp
!= '0') {
673 } while (isdigit((unsigned char) *cp
));
676 /* Optional exponent. */
677 if (*cp
== 'e' || *cp
== 'E') {
678 bool negative_exponent
= false;
684 } else if (*cp
== '-') {
685 negative_exponent
= true;
689 if (!isdigit((unsigned char) *cp
)) {
690 json_error(p
, "exponent must contain at least one digit");
696 if (exponent
>= INT_MAX
/ 10) {
697 json_error(p
, "exponent outside valid range");
700 exponent
= exponent
* 10 + (*cp
- '0');
702 } while (isdigit((unsigned char) *cp
));
704 if (negative_exponent
) {
712 json_error(p
, "syntax error in number");
716 /* Figure out number.
718 * We suppress negative zeros as a matter of policy. */
720 token
.type
= T_INTEGER
;
722 json_parser_input(p
, &token
);
727 while (pow10
> 0 && significand
< ULLONG_MAX
/ 10) {
731 while (pow10
< 0 && significand
% 10 == 0) {
736 && significand
<= (negative
737 ? (unsigned long long int) LLONG_MAX
+ 1
739 token
.type
= T_INTEGER
;
740 token
.u
.integer
= negative
? -significand
: significand
;
741 json_parser_input(p
, &token
);
747 if (!str_to_double(ds_cstr(&p
->buffer
), &token
.u
.real
)) {
748 json_error(p
, "number outside valid range");
751 /* Suppress negative zero. */
752 if (token
.u
.real
== 0) {
755 json_parser_input(p
, &token
);
759 json_lex_4hex(const char *cp
, const char *end
, int *valuep
)
765 return "quoted string ends within \\u escape";
768 value
= hexits_value(cp
, 4, &ok
);
770 return "malformed \\u escape";
773 return "null bytes not supported in quoted strings";
780 json_lex_unicode(const char *cp
, const char *end
, struct ds
*out
)
785 error
= json_lex_4hex(cp
, end
, &c0
);
788 ds_put_cstr(out
, error
);
792 if (!uc_is_leading_surrogate(c0
)) {
793 ds_put_utf8(out
, c0
);
797 if (cp
+ 2 > end
|| *cp
++ != '\\' || *cp
++ != 'u') {
799 ds_put_cstr(out
, "malformed escaped surrogate pair");
803 error
= json_lex_4hex(cp
, end
, &c1
);
806 ds_put_cstr(out
, error
);
810 if (!uc_is_trailing_surrogate(c1
)) {
812 ds_put_cstr(out
, "second half of escaped surrogate pair is not "
813 "trailing surrogate");
817 ds_put_utf8(out
, utf16_decode_surrogate_pair(c0
, c1
));
822 json_string_unescape(const char *in
, size_t in_len
, char **outp
)
824 const char *end
= in
+ in_len
;
829 ds_reserve(&out
, in_len
);
833 ds_put_cstr(&out
, "quoted string may not include unescaped \"");
837 ds_put_char(&out
, *in
++);
843 /* The JSON parser will never trigger this message, because its
844 * lexer will never pass in a string that ends in a single
845 * backslash, but json_string_unescape() has other callers that
846 * are not as careful.*/
848 ds_put_cstr(&out
, "quoted string may not end with backslash");
852 case '"': case '\\': case '/':
853 ds_put_char(&out
, in
[-1]);
857 ds_put_char(&out
, '\b');
861 ds_put_char(&out
, '\f');
865 ds_put_char(&out
, '\n');
869 ds_put_char(&out
, '\r');
873 ds_put_char(&out
, '\t');
877 in
= json_lex_unicode(in
, end
, &out
);
885 ds_put_format(&out
, "bad escape \\%c", in
[-1]);
892 *outp
= ds_cstr(&out
);
897 json_string_escape(const char *in
, struct ds
*out
)
901 .u
.string
= CONST_CAST(char *, in
),
903 json_to_ds(&json
, 0, out
);
907 json_parser_input_string(struct json_parser
*p
, const char *s
)
909 struct json_token token
;
911 token
.type
= T_STRING
;
913 json_parser_input(p
, &token
);
917 json_lex_string(struct json_parser
*p
)
919 const char *raw
= ds_cstr(&p
->buffer
);
920 if (!strchr(raw
, '\\')) {
921 json_parser_input_string(p
, raw
);
925 if (json_string_unescape(raw
, strlen(raw
), &cooked
)) {
926 json_parser_input_string(p
, cooked
);
928 json_error(p
, "%s", cooked
);
936 json_lex_input(struct json_parser
*p
, unsigned char c
)
938 struct json_token token
;
940 switch (p
->lex_state
) {
943 case ' ': case '\t': case '\n': case '\r':
947 case 'a': case 'b': case 'c': case 'd': case 'e':
948 case 'f': case 'g': case 'h': case 'i': case 'j':
949 case 'k': case 'l': case 'm': case 'n': case 'o':
950 case 'p': case 'q': case 'r': case 's': case 't':
951 case 'u': case 'v': case 'w': case 'x': case 'y':
953 p
->lex_state
= JSON_LEX_KEYWORD
;
956 case '[': case '{': case ']': case '}': case ':': case ',':
958 json_parser_input(p
, &token
);
962 case '0': case '1': case '2': case '3': case '4':
963 case '5': case '6': case '7': case '8': case '9':
964 p
->lex_state
= JSON_LEX_NUMBER
;
968 p
->lex_state
= JSON_LEX_STRING
;
973 json_error(p
, "invalid character '%c'", c
);
975 json_error(p
, "invalid character U+%04x", c
);
981 case JSON_LEX_KEYWORD
:
982 if (!isalpha((unsigned char) c
)) {
988 case JSON_LEX_NUMBER
:
989 if (!strchr(".0123456789eE-+", c
)) {
995 case JSON_LEX_STRING
:
997 p
->lex_state
= JSON_LEX_ESCAPE
;
998 } else if (c
== '"') {
1001 } else if (c
< 0x20) {
1002 json_error(p
, "U+%04X must be escaped in quoted string", c
);
1007 case JSON_LEX_ESCAPE
:
1008 p
->lex_state
= JSON_LEX_STRING
;
1014 ds_put_char(&p
->buffer
, c
);
1020 /* Parses 'string' as a JSON object or array and returns a newly allocated
1021 * 'struct json'. The caller must free the returned structure with
1022 * json_destroy() when it is no longer needed.
1024 * 'string' must be encoded in UTF-8.
1026 * If 'string' is valid JSON, then the returned 'struct json' will be either an
1027 * object (JSON_OBJECT) or an array (JSON_ARRAY).
1029 * If 'string' is not valid JSON, then the returned 'struct json' will be a
1030 * string (JSON_STRING) that describes the particular error encountered during
1031 * parsing. (This is an acceptable means of error reporting because at its top
1032 * level JSON must be either an object or an array; a bare string is not
1035 json_from_string(const char *string
)
1037 struct json_parser
*p
= json_parser_create(JSPF_TRAILER
);
1038 json_parser_feed(p
, string
, strlen(string
));
1039 return json_parser_finish(p
);
1042 /* Reads the file named 'file_name', parses its contents as a JSON object or
1043 * array, and returns a newly allocated 'struct json'. The caller must free
1044 * the returned structure with json_destroy() when it is no longer needed.
1046 * The file must be encoded in UTF-8.
1048 * See json_from_string() for return value semantics.
1051 json_from_file(const char *file_name
)
1056 stream
= fopen(file_name
, "r");
1058 return json_string_create_nocopy(
1059 xasprintf("error opening \"%s\": %s", file_name
,
1060 ovs_strerror(errno
)));
1062 json
= json_from_stream(stream
);
1068 /* Parses the contents of 'stream' as a JSON object or array, and returns a
1069 * newly allocated 'struct json'. The caller must free the returned structure
1070 * with json_destroy() when it is no longer needed.
1072 * The file must be encoded in UTF-8.
1074 * See json_from_string() for return value semantics.
1077 json_from_stream(FILE *stream
)
1079 struct json_parser
*p
;
1082 p
= json_parser_create(JSPF_TRAILER
);
1084 char buffer
[BUFSIZ
];
1087 n
= fread(buffer
, 1, sizeof buffer
, stream
);
1088 if (!n
|| json_parser_feed(p
, buffer
, n
) != n
) {
1092 json
= json_parser_finish(p
);
1094 if (ferror(stream
)) {
1096 json
= json_string_create_nocopy(
1097 xasprintf("error reading JSON stream: %s", ovs_strerror(errno
)));
1103 struct json_parser
*
1104 json_parser_create(int flags
)
1106 struct json_parser
*p
= xzalloc(sizeof *p
);
1112 json_parser_feed(struct json_parser
*p
, const char *input
, size_t n
)
1115 for (i
= 0; !p
->done
&& i
< n
; ) {
1116 if (json_lex_input(p
, input
[i
])) {
1118 if (input
[i
] == '\n') {
1119 p
->column_number
= 0;
1131 json_parser_is_done(const struct json_parser
*p
)
1137 json_parser_finish(struct json_parser
*p
)
1141 switch (p
->lex_state
) {
1142 case JSON_LEX_START
:
1145 case JSON_LEX_STRING
:
1146 case JSON_LEX_ESCAPE
:
1147 json_error(p
, "unexpected end of input in quoted string");
1150 case JSON_LEX_NUMBER
:
1151 case JSON_LEX_KEYWORD
:
1152 json_lex_input(p
, ' ');
1156 if (p
->parse_state
== JSON_PARSE_START
) {
1157 json_error(p
, "empty input stream");
1158 } else if (p
->parse_state
!= JSON_PARSE_END
) {
1159 json_error(p
, "unexpected end of input");
1163 ovs_assert(p
->height
== 1);
1164 ovs_assert(p
->stack
[0].json
!= NULL
);
1165 json
= p
->stack
[--p
->height
].json
;
1167 json
= json_string_create_nocopy(p
->error
);
1171 json_parser_abort(p
);
1177 json_parser_abort(struct json_parser
*p
)
1180 ds_destroy(&p
->buffer
);
1182 json_destroy(p
->stack
[0].json
);
1185 free(p
->member_name
);
1191 static struct json_parser_node
*
1192 json_parser_top(struct json_parser
*p
)
1194 return &p
->stack
[p
->height
- 1];
1198 json_parser_put_value(struct json_parser
*p
, struct json
*value
)
1200 struct json_parser_node
*node
= json_parser_top(p
);
1201 if (node
->json
->type
== JSON_OBJECT
) {
1202 json_object_put(node
->json
, p
->member_name
, value
);
1203 free(p
->member_name
);
1204 p
->member_name
= NULL
;
1205 } else if (node
->json
->type
== JSON_ARRAY
) {
1206 json_array_add(node
->json
, value
);
1213 json_parser_push(struct json_parser
*p
,
1214 struct json
*new_json
, enum json_parse_state new_state
)
1216 if (p
->height
< JSON_MAX_HEIGHT
) {
1217 struct json_parser_node
*node
;
1219 if (p
->height
>= p
->allocated_height
) {
1220 p
->stack
= x2nrealloc(p
->stack
, &p
->allocated_height
,
1224 if (p
->height
> 0) {
1225 json_parser_put_value(p
, new_json
);
1228 node
= &p
->stack
[p
->height
++];
1229 node
->json
= new_json
;
1230 p
->parse_state
= new_state
;
1232 json_destroy(new_json
);
1233 json_error(p
, "input exceeds maximum nesting depth %d",
1239 json_parser_push_object(struct json_parser
*p
)
1241 json_parser_push(p
, json_object_create(), JSON_PARSE_OBJECT_INIT
);
1245 json_parser_push_array(struct json_parser
*p
)
1247 json_parser_push(p
, json_array_create_empty(), JSON_PARSE_ARRAY_INIT
);
1251 json_parse_value(struct json_parser
*p
, struct json_token
*token
,
1252 enum json_parse_state next_state
)
1256 switch (token
->type
) {
1258 value
= json_boolean_create(false);
1262 value
= json_null_create();
1266 value
= json_boolean_create(true);
1270 json_parser_push_object(p
);
1274 json_parser_push_array(p
);
1278 value
= json_integer_create(token
->u
.integer
);
1282 value
= json_real_create(token
->u
.real
);
1286 value
= json_string_create(token
->u
.string
);
1295 json_error(p
, "syntax error expecting value");
1299 json_parser_put_value(p
, value
);
1300 p
->parse_state
= next_state
;
1304 json_parser_pop(struct json_parser
*p
)
1306 struct json_parser_node
*node
;
1308 /* Conserve memory. */
1309 node
= json_parser_top(p
);
1310 if (node
->json
->type
== JSON_ARRAY
) {
1311 json_array_trim(node
->json
);
1314 /* Pop off the top-of-stack. */
1315 if (p
->height
== 1) {
1316 p
->parse_state
= JSON_PARSE_END
;
1317 if (!(p
->flags
& JSPF_TRAILER
)) {
1322 node
= json_parser_top(p
);
1323 if (node
->json
->type
== JSON_ARRAY
) {
1324 p
->parse_state
= JSON_PARSE_ARRAY_NEXT
;
1325 } else if (node
->json
->type
== JSON_OBJECT
) {
1326 p
->parse_state
= JSON_PARSE_OBJECT_NEXT
;
1334 json_parser_input(struct json_parser
*p
, struct json_token
*token
)
1336 switch (p
->parse_state
) {
1337 case JSON_PARSE_START
:
1338 if (token
->type
== '{') {
1339 json_parser_push_object(p
);
1340 } else if (token
->type
== '[') {
1341 json_parser_push_array(p
);
1343 json_error(p
, "syntax error at beginning of input");
1347 case JSON_PARSE_END
:
1348 json_error(p
, "trailing garbage at end of input");
1351 case JSON_PARSE_OBJECT_INIT
:
1352 if (token
->type
== '}') {
1357 case JSON_PARSE_OBJECT_NAME
:
1358 if (token
->type
== T_STRING
) {
1359 p
->member_name
= xstrdup(token
->u
.string
);
1360 p
->parse_state
= JSON_PARSE_OBJECT_COLON
;
1362 json_error(p
, "syntax error parsing object expecting string");
1366 case JSON_PARSE_OBJECT_COLON
:
1367 if (token
->type
== ':') {
1368 p
->parse_state
= JSON_PARSE_OBJECT_VALUE
;
1370 json_error(p
, "syntax error parsing object expecting ':'");
1374 case JSON_PARSE_OBJECT_VALUE
:
1375 json_parse_value(p
, token
, JSON_PARSE_OBJECT_NEXT
);
1378 case JSON_PARSE_OBJECT_NEXT
:
1379 if (token
->type
== ',') {
1380 p
->parse_state
= JSON_PARSE_OBJECT_NAME
;
1381 } else if (token
->type
== '}') {
1384 json_error(p
, "syntax error expecting '}' or ','");
1388 case JSON_PARSE_ARRAY_INIT
:
1389 if (token
->type
== ']') {
1394 case JSON_PARSE_ARRAY_VALUE
:
1395 json_parse_value(p
, token
, JSON_PARSE_ARRAY_NEXT
);
1398 case JSON_PARSE_ARRAY_NEXT
:
1399 if (token
->type
== ',') {
1400 p
->parse_state
= JSON_PARSE_ARRAY_VALUE
;
1401 } else if (token
->type
== ']') {
1404 json_error(p
, "syntax error expecting ']' or ','");
1412 p
->lex_state
= JSON_LEX_START
;
1413 ds_clear(&p
->buffer
);
1416 static struct json
*
1417 json_create(enum json_type type
)
1419 struct json
*json
= xmalloc(sizeof *json
);
1426 json_error(struct json_parser
*p
, const char *format
, ...)
1433 ds_put_format(&msg
, "line %d, column %d, byte %d: ",
1434 p
->line_number
, p
->column_number
, p
->byte_number
);
1435 va_start(args
, format
);
1436 ds_put_format_valist(&msg
, format
, args
);
1439 p
->error
= ds_steal_cstr(&msg
);
1445 #define SPACES_PER_LEVEL 2
1447 struct json_serializer
{
1453 static void json_serialize(const struct json
*, struct json_serializer
*);
1454 static void json_serialize_object(const struct shash
*object
,
1455 struct json_serializer
*);
1456 static void json_serialize_array(const struct json_array
*,
1457 struct json_serializer
*);
1458 static void json_serialize_string(const char *, struct ds
*);
1460 /* Converts 'json' to a string in JSON format, encoded in UTF-8, and returns
1461 * that string. The caller is responsible for freeing the returned string,
1462 * with free(), when it is no longer needed.
1464 * If 'flags' contains JSSF_PRETTY, the output is pretty-printed with each
1465 * nesting level introducing an additional indentation. Otherwise, the
1466 * returned string does not contain any new-line characters.
1468 * If 'flags' contains JSSF_SORT, members of objects in the output are sorted
1469 * in bytewise lexicographic order for reproducibility. Otherwise, members of
1470 * objects are output in an indeterminate order.
1472 * The returned string is valid JSON only if 'json' represents an array or an
1473 * object, since a bare literal does not satisfy the JSON grammar. */
1475 json_to_string(const struct json
*json
, int flags
)
1480 json_to_ds(json
, flags
, &ds
);
1481 return ds_steal_cstr(&ds
);
1484 /* Same as json_to_string(), but the output is appended to 'ds'. */
1486 json_to_ds(const struct json
*json
, int flags
, struct ds
*ds
)
1488 struct json_serializer s
;
1493 json_serialize(json
, &s
);
1497 json_serialize(const struct json
*json
, struct json_serializer
*s
)
1499 struct ds
*ds
= s
->ds
;
1501 switch (json
->type
) {
1503 ds_put_cstr(ds
, "null");
1507 ds_put_cstr(ds
, "false");
1511 ds_put_cstr(ds
, "true");
1515 json_serialize_object(json
->u
.object
, s
);
1519 json_serialize_array(&json
->u
.array
, s
);
1523 ds_put_format(ds
, "%lld", json
->u
.integer
);
1527 ds_put_format(ds
, "%.*g", DBL_DIG
, json
->u
.real
);
1531 json_serialize_string(json
->u
.string
, ds
);
1541 indent_line(struct json_serializer
*s
)
1543 if (s
->flags
& JSSF_PRETTY
) {
1544 ds_put_char(s
->ds
, '\n');
1545 ds_put_char_multiple(s
->ds
, ' ', SPACES_PER_LEVEL
* s
->depth
);
1550 json_serialize_object_member(size_t i
, const struct shash_node
*node
,
1551 struct json_serializer
*s
)
1553 struct ds
*ds
= s
->ds
;
1556 ds_put_char(ds
, ',');
1560 json_serialize_string(node
->name
, ds
);
1561 ds_put_char(ds
, ':');
1562 if (s
->flags
& JSSF_PRETTY
) {
1563 ds_put_char(ds
, ' ');
1565 json_serialize(node
->data
, s
);
1569 json_serialize_object(const struct shash
*object
, struct json_serializer
*s
)
1571 struct ds
*ds
= s
->ds
;
1573 ds_put_char(ds
, '{');
1578 if (s
->flags
& JSSF_SORT
) {
1579 const struct shash_node
**nodes
;
1582 nodes
= shash_sort(object
);
1583 n
= shash_count(object
);
1584 for (i
= 0; i
< n
; i
++) {
1585 json_serialize_object_member(i
, nodes
[i
], s
);
1589 struct shash_node
*node
;
1593 SHASH_FOR_EACH (node
, object
) {
1594 json_serialize_object_member(i
++, node
, s
);
1598 ds_put_char(ds
, '}');
1603 json_serialize_array(const struct json_array
*array
, struct json_serializer
*s
)
1605 struct ds
*ds
= s
->ds
;
1608 ds_put_char(ds
, '[');
1614 for (i
= 0; i
< array
->n
; i
++) {
1616 ds_put_char(ds
, ',');
1619 json_serialize(array
->elems
[i
], s
);
1624 ds_put_char(ds
, ']');
1627 static const char *chars_escaping
[256] = {
1628 "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007",
1629 "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f",
1630 "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014", "\\u0015", "\\u0016", "\\u0017",
1631 "\\u0018", "\\u0019", "\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f",
1632 " ", "!", "\\\"", "#", "$", "%", "&", "'",
1633 "(", ")", "*", "+", ",", "-", ".", "/",
1634 "0", "1", "2", "3", "4", "5", "6", "7",
1635 "8", "9", ":", ";", "<", "=", ">", "?",
1636 "@", "A", "B", "C", "D", "E", "F", "G",
1637 "H", "I", "J", "K", "L", "M", "N", "O",
1638 "P", "Q", "R", "S", "T", "U", "V", "W",
1639 "X", "Y", "Z", "[", "\\\\", "]", "^", "_",
1640 "`", "a", "b", "c", "d", "e", "f", "g",
1641 "h", "i", "j", "k", "l", "m", "n", "o",
1642 "p", "q", "r", "s", "t", "u", "v", "w",
1643 "x", "y", "z", "{", "|", "}", "~", "\x7f",
1644 "\x80", "\x81", "\x82", "\x83", "\x84", "\x85", "\x86", "\x87",
1645 "\x88", "\x89", "\x8a", "\x8b", "\x8c", "\x8d", "\x8e", "\x8f",
1646 "\x90", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96", "\x97",
1647 "\x98", "\x99", "\x9a", "\x9b", "\x9c", "\x9d", "\x9e", "\x9f",
1648 "\xa0", "\xa1", "\xa2", "\xa3", "\xa4", "\xa5", "\xa6", "\xa7",
1649 "\xa8", "\xa9", "\xaa", "\xab", "\xac", "\xad", "\xae", "\xaf",
1650 "\xb0", "\xb1", "\xb2", "\xb3", "\xb4", "\xb5", "\xb6", "\xb7",
1651 "\xb8", "\xb9", "\xba", "\xbb", "\xbc", "\xbd", "\xbe", "\xbf",
1652 "\xc0", "\xc1", "\xc2", "\xc3", "\xc4", "\xc5", "\xc6", "\xc7",
1653 "\xc8", "\xc9", "\xca", "\xcb", "\xcc", "\xcd", "\xce", "\xcf",
1654 "\xd0", "\xd1", "\xd2", "\xd3", "\xd4", "\xd5", "\xd6", "\xd7",
1655 "\xd8", "\xd9", "\xda", "\xdb", "\xdc", "\xdd", "\xde", "\xdf",
1656 "\xe0", "\xe1", "\xe2", "\xe3", "\xe4", "\xe5", "\xe6", "\xe7",
1657 "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef",
1658 "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7",
1659 "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff"
1663 json_serialize_string(const char *string
, struct ds
*ds
)
1669 ds_put_char(ds
, '"');
1670 while ((c
= *string
++) != '\0') {
1671 escape
= chars_escaping
[c
];
1672 while ((c2
= *escape
++) != '\0') {
1673 ds_put_char(ds
, c2
);
1676 ds_put_char(ds
, '"');