2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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.
27 #include "dynamic-string.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, ...)
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)
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_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);
425 json_clone_object(const struct shash *object)
427 struct shash_node *node;
430 json = json_object_create();
431 SHASH_FOR_EACH (node, object) {
432 struct json *value = node->data;
433 json_object_put(json, node->name, json_clone(value));
439 json_clone_array(const struct json_array *array)
444 elems = xmalloc(array->n * sizeof *elems);
445 for (i = 0; i < array->n; i++) {
446 elems[i] = json_clone(array->elems[i]);
448 return json_array_create(elems, array->n);
452 json_hash_object(const struct shash *object, size_t basis)
454 const struct shash_node **nodes;
457 nodes = shash_sort(object);
458 n = shash_count(object);
459 for (i = 0; i < n; i++) {
460 const struct shash_node *node = nodes[i];
461 basis = hash_string(node->name, basis);
462 basis = json_hash(node->data, basis);
468 json_hash_array(const struct json_array *array, size_t basis)
472 basis = hash_int(array->n, basis);
473 for (i = 0; i < array->n; i++) {
474 basis = json_hash(array->elems[i], basis);
480 json_hash(const struct json *json, size_t basis)
482 switch (json->type) {
484 return json_hash_object(json->u.object, basis);
487 return json_hash_array(&json->u.array, basis);
490 return hash_string(json->u.string, basis);
495 return hash_int(json->type << 8, basis);
498 return hash_int(json->u.integer, basis);
501 return hash_double(json->u.real, basis);
510 json_equal_object(const struct shash *a, const struct shash *b)
512 struct shash_node *a_node;
514 if (shash_count(a) != shash_count(b)) {
518 SHASH_FOR_EACH (a_node, a) {
519 struct shash_node *b_node = shash_find(b, a_node->name);
520 if (!b_node || !json_equal(a_node->data, b_node->data)) {
529 json_equal_array(const struct json_array *a, const struct json_array *b)
537 for (i = 0; i < a->n; i++) {
538 if (!json_equal(a->elems[i], b->elems[i])) {
547 json_equal(const struct json *a, const struct json *b)
549 if (a->type != b->type) {
555 return json_equal_object(a->u.object, b->u.object);
558 return json_equal_array(&a->u.array, &b->u.array);
561 return !strcmp(a->u.string, b->u.string);
569 return a->u.integer == b->u.integer;
572 return a->u.real == b->u.real;
580 /* Lexical analysis. */
583 json_lex_keyword(struct json_parser *p)
585 struct json_token token;
588 s = ds_cstr(&p->buffer);
589 if (!strcmp(s, "false")) {
590 token.type = T_FALSE;
591 } else if (!strcmp(s, "true")) {
593 } else if (!strcmp(s, "null")) {
596 json_error(p, "invalid keyword '%s'", s);
599 json_parser_input(p, &token);
603 json_lex_number(struct json_parser *p)
605 const char *cp = ds_cstr(&p->buffer);
606 unsigned long long int significand = 0;
607 struct json_token token;
608 bool imprecise = false;
609 bool negative = false;
612 /* Leading minus sign. */
618 /* At least one integer digit, but 0 may not be used as a leading digit for
619 * a longer number. */
623 if (isdigit((unsigned char) *cp)) {
624 json_error(p, "leading zeros not allowed");
627 } else if (isdigit((unsigned char) *cp)) {
629 if (significand <= ULLONG_MAX / 10) {
630 significand = significand * 10 + (*cp - '0');
638 } while (isdigit((unsigned char) *cp));
640 json_error(p, "'-' must be followed by digit");
644 /* Optional fraction. */
647 if (!isdigit((unsigned char) *cp)) {
648 json_error(p, "decimal point must be followed by digit");
652 if (significand <= ULLONG_MAX / 10) {
653 significand = significand * 10 + (*cp - '0');
655 } else if (*cp != '0') {
659 } while (isdigit((unsigned char) *cp));
662 /* Optional exponent. */
663 if (*cp == 'e' || *cp == 'E') {
664 bool negative_exponent = false;
670 } else if (*cp == '-') {
671 negative_exponent = true;
675 if (!isdigit((unsigned char) *cp)) {
676 json_error(p, "exponent must contain at least one digit");
682 if (exponent >= INT_MAX / 10) {
683 json_error(p, "exponent outside valid range");
686 exponent = exponent * 10 + (*cp - '0');
688 } while (isdigit((unsigned char) *cp));
690 if (negative_exponent) {
698 json_error(p, "syntax error in number");
702 /* Figure out number.
704 * We suppress negative zeros as a matter of policy. */
706 token.type = T_INTEGER;
708 json_parser_input(p, &token);
713 while (pow10 > 0 && significand < ULLONG_MAX / 10) {
717 while (pow10 < 0 && significand % 10 == 0) {
722 && significand <= (negative
723 ? (unsigned long long int) LLONG_MAX + 1
725 token.type = T_INTEGER;
726 token.u.integer = negative ? -significand : significand;
727 json_parser_input(p, &token);
733 if (!str_to_double(ds_cstr(&p->buffer), &token.u.real)) {
734 json_error(p, "number outside valid range");
737 /* Suppress negative zero. */
738 if (token.u.real == 0) {
741 json_parser_input(p, &token);
745 json_lex_4hex(const char *cp, const char *end, int *valuep)
750 return "quoted string ends within \\u escape";
753 value = hexits_value(cp, 4, NULL);
754 if (value == UINT_MAX) {
755 return "malformed \\u escape";
758 return "null bytes not supported in quoted strings";
765 json_lex_unicode(const char *cp, const char *end, struct ds *out)
770 error = json_lex_4hex(cp, end, &c0);
773 ds_put_cstr(out, error);
777 if (!uc_is_leading_surrogate(c0)) {
778 ds_put_utf8(out, c0);
782 if (cp + 2 > end || *cp++ != '\\' || *cp++ != 'u') {
784 ds_put_cstr(out, "malformed escaped surrogate pair");
788 error = json_lex_4hex(cp, end, &c1);
791 ds_put_cstr(out, error);
795 if (!uc_is_trailing_surrogate(c1)) {
797 ds_put_cstr(out, "second half of escaped surrogate pair is not "
798 "trailing surrogate");
802 ds_put_utf8(out, utf16_decode_surrogate_pair(c0, c1));
807 json_string_unescape(const char *in, size_t in_len, char **outp)
809 const char *end = in + in_len;
814 ds_reserve(&out, in_len);
818 ds_put_cstr(&out, "quoted string may not include unescaped \"");
822 ds_put_char(&out, *in++);
828 /* The JSON parser will never trigger this message, because its
829 * lexer will never pass in a string that ends in a single
830 * backslash, but json_string_unescape() has other callers that
831 * are not as careful.*/
832 ds_put_cstr(&out, "quoted string may not end with backslash");
836 case '"': case '\\': case '/':
837 ds_put_char(&out, in[-1]);
841 ds_put_char(&out, '\b');
845 ds_put_char(&out, '\f');
849 ds_put_char(&out, '\n');
853 ds_put_char(&out, '\r');
857 ds_put_char(&out, '\t');
861 in = json_lex_unicode(in, end, &out);
869 ds_put_format(&out, "bad escape \\%c", in[-1]);
876 *outp = ds_cstr(&out);
881 json_parser_input_string(struct json_parser *p, const char *s)
883 struct json_token token;
885 token.type = T_STRING;
887 json_parser_input(p, &token);
891 json_lex_string(struct json_parser *p)
893 const char *raw = ds_cstr(&p->buffer);
894 if (!strchr(raw, '\\')) {
895 json_parser_input_string(p, raw);
899 if (json_string_unescape(raw, strlen(raw), &cooked)) {
900 json_parser_input_string(p, cooked);
902 json_error(p, "%s", cooked);
910 json_lex_input(struct json_parser *p, unsigned char c)
912 struct json_token token;
914 switch (p->lex_state) {
917 case ' ': case '\t': case '\n': case '\r':
921 case 'a': case 'b': case 'c': case 'd': case 'e':
922 case 'f': case 'g': case 'h': case 'i': case 'j':
923 case 'k': case 'l': case 'm': case 'n': case 'o':
924 case 'p': case 'q': case 'r': case 's': case 't':
925 case 'u': case 'v': case 'w': case 'x': case 'y':
927 p->lex_state = JSON_LEX_KEYWORD;
930 case '[': case '{': case ']': case '}': case ':': case ',':
932 json_parser_input(p, &token);
936 case '0': case '1': case '2': case '3': case '4':
937 case '5': case '6': case '7': case '8': case '9':
938 p->lex_state = JSON_LEX_NUMBER;
942 p->lex_state = JSON_LEX_STRING;
947 json_error(p, "invalid character '%c'", c);
949 json_error(p, "invalid character U+%04x", c);
955 case JSON_LEX_KEYWORD:
956 if (!isalpha((unsigned char) c)) {
962 case JSON_LEX_NUMBER:
963 if (!strchr(".0123456789eE-+", c)) {
969 case JSON_LEX_STRING:
971 p->lex_state = JSON_LEX_ESCAPE;
972 } else if (c == '"') {
975 } else if (c < 0x20) {
976 json_error(p, "U+%04X must be escaped in quoted string", c);
981 case JSON_LEX_ESCAPE:
982 p->lex_state = JSON_LEX_STRING;
988 ds_put_char(&p->buffer, c);
994 /* Parses 'string' as a JSON object or array and returns a newly allocated
995 * 'struct json'. The caller must free the returned structure with
996 * json_destroy() when it is no longer needed.
998 * 'string' must be encoded in UTF-8.
1000 * If 'string' is valid JSON, then the returned 'struct json' will be either an
1001 * object (JSON_OBJECT) or an array (JSON_ARRAY).
1003 * If 'string' is not valid JSON, then the returned 'struct json' will be a
1004 * string (JSON_STRING) that describes the particular error encountered during
1005 * parsing. (This is an acceptable means of error reporting because at its top
1006 * level JSON must be either an object or an array; a bare string is not
1009 json_from_string(const char *string)
1011 struct json_parser *p = json_parser_create(JSPF_TRAILER);
1012 json_parser_feed(p, string, strlen(string));
1013 return json_parser_finish(p);
1016 /* Reads the file named 'file_name', parses its contents as a JSON object or
1017 * array, and returns a newly allocated 'struct json'. The caller must free
1018 * the returned structure with json_destroy() when it is no longer needed.
1020 * The file must be encoded in UTF-8.
1022 * See json_from_string() for return value semantics.
1025 json_from_file(const char *file_name)
1030 stream = fopen(file_name, "r");
1032 return json_string_create_nocopy(
1033 xasprintf("error opening \"%s\": %s", file_name,
1034 ovs_strerror(errno)));
1036 json = json_from_stream(stream);
1042 /* Parses the contents of 'stream' as a JSON object or array, and returns a
1043 * newly allocated 'struct json'. The caller must free the returned structure
1044 * 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_stream(FILE *stream)
1053 struct json_parser *p;
1056 p = json_parser_create(JSPF_TRAILER);
1058 char buffer[BUFSIZ];
1061 n = fread(buffer, 1, sizeof buffer, stream);
1062 if (!n || json_parser_feed(p, buffer, n) != n) {
1066 json = json_parser_finish(p);
1068 if (ferror(stream)) {
1070 json = json_string_create_nocopy(
1071 xasprintf("error reading JSON stream: %s", ovs_strerror(errno)));
1077 struct json_parser *
1078 json_parser_create(int flags)
1080 struct json_parser *p = xzalloc(sizeof *p);
1086 json_parser_feed(struct json_parser *p, const char *input, size_t n)
1089 for (i = 0; !p->done && i < n; ) {
1090 if (json_lex_input(p, input[i])) {
1092 if (input[i] == '\n') {
1093 p->column_number = 0;
1105 json_parser_is_done(const struct json_parser *p)
1111 json_parser_finish(struct json_parser *p)
1115 switch (p->lex_state) {
1116 case JSON_LEX_START:
1119 case JSON_LEX_STRING:
1120 case JSON_LEX_ESCAPE:
1121 json_error(p, "unexpected end of input in quoted string");
1124 case JSON_LEX_NUMBER:
1125 case JSON_LEX_KEYWORD:
1126 json_lex_input(p, ' ');
1130 if (p->parse_state == JSON_PARSE_START) {
1131 json_error(p, "empty input stream");
1132 } else if (p->parse_state != JSON_PARSE_END) {
1133 json_error(p, "unexpected end of input");
1137 ovs_assert(p->height == 1);
1138 ovs_assert(p->stack[0].json != NULL);
1139 json = p->stack[--p->height].json;
1141 json = json_string_create_nocopy(p->error);
1145 json_parser_abort(p);
1151 json_parser_abort(struct json_parser *p)
1154 ds_destroy(&p->buffer);
1156 json_destroy(p->stack[0].json);
1159 free(p->member_name);
1165 static struct json_parser_node *
1166 json_parser_top(struct json_parser *p)
1168 return &p->stack[p->height - 1];
1172 json_parser_put_value(struct json_parser *p, struct json *value)
1174 struct json_parser_node *node = json_parser_top(p);
1175 if (node->json->type == JSON_OBJECT) {
1176 json_object_put(node->json, p->member_name, value);
1177 free(p->member_name);
1178 p->member_name = NULL;
1179 } else if (node->json->type == JSON_ARRAY) {
1180 json_array_add(node->json, value);
1187 json_parser_push(struct json_parser *p,
1188 struct json *new_json, enum json_parse_state new_state)
1190 if (p->height < JSON_MAX_HEIGHT) {
1191 struct json_parser_node *node;
1193 if (p->height >= p->allocated_height) {
1194 p->stack = x2nrealloc(p->stack, &p->allocated_height,
1198 if (p->height > 0) {
1199 json_parser_put_value(p, new_json);
1202 node = &p->stack[p->height++];
1203 node->json = new_json;
1204 p->parse_state = new_state;
1206 json_destroy(new_json);
1207 json_error(p, "input exceeds maximum nesting depth %d",
1213 json_parser_push_object(struct json_parser *p)
1215 json_parser_push(p, json_object_create(), JSON_PARSE_OBJECT_INIT);
1219 json_parser_push_array(struct json_parser *p)
1221 json_parser_push(p, json_array_create_empty(), JSON_PARSE_ARRAY_INIT);
1225 json_parse_value(struct json_parser *p, struct json_token *token,
1226 enum json_parse_state next_state)
1230 switch (token->type) {
1232 value = json_boolean_create(false);
1236 value = json_null_create();
1240 value = json_boolean_create(true);
1244 json_parser_push_object(p);
1248 json_parser_push_array(p);
1252 value = json_integer_create(token->u.integer);
1256 value = json_real_create(token->u.real);
1260 value = json_string_create(token->u.string);
1269 json_error(p, "syntax error expecting value");
1273 json_parser_put_value(p, value);
1274 p->parse_state = next_state;
1278 json_parser_pop(struct json_parser *p)
1280 struct json_parser_node *node;
1282 /* Conserve memory. */
1283 node = json_parser_top(p);
1284 if (node->json->type == JSON_ARRAY) {
1285 json_array_trim(node->json);
1288 /* Pop off the top-of-stack. */
1289 if (p->height == 1) {
1290 p->parse_state = JSON_PARSE_END;
1291 if (!(p->flags & JSPF_TRAILER)) {
1296 node = json_parser_top(p);
1297 if (node->json->type == JSON_ARRAY) {
1298 p->parse_state = JSON_PARSE_ARRAY_NEXT;
1299 } else if (node->json->type == JSON_OBJECT) {
1300 p->parse_state = JSON_PARSE_OBJECT_NEXT;
1308 json_parser_input(struct json_parser *p, struct json_token *token)
1310 switch (p->parse_state) {
1311 case JSON_PARSE_START:
1312 if (token->type == '{') {
1313 json_parser_push_object(p);
1314 } else if (token->type == '[') {
1315 json_parser_push_array(p);
1317 json_error(p, "syntax error at beginning of input");
1321 case JSON_PARSE_END:
1322 json_error(p, "trailing garbage at end of input");
1325 case JSON_PARSE_OBJECT_INIT:
1326 if (token->type == '}') {
1331 case JSON_PARSE_OBJECT_NAME:
1332 if (token->type == T_STRING) {
1333 p->member_name = xstrdup(token->u.string);
1334 p->parse_state = JSON_PARSE_OBJECT_COLON;
1336 json_error(p, "syntax error parsing object expecting string");
1340 case JSON_PARSE_OBJECT_COLON:
1341 if (token->type == ':') {
1342 p->parse_state = JSON_PARSE_OBJECT_VALUE;
1344 json_error(p, "syntax error parsing object expecting ':'");
1348 case JSON_PARSE_OBJECT_VALUE:
1349 json_parse_value(p, token, JSON_PARSE_OBJECT_NEXT);
1352 case JSON_PARSE_OBJECT_NEXT:
1353 if (token->type == ',') {
1354 p->parse_state = JSON_PARSE_OBJECT_NAME;
1355 } else if (token->type == '}') {
1358 json_error(p, "syntax error expecting '}' or ','");
1362 case JSON_PARSE_ARRAY_INIT:
1363 if (token->type == ']') {
1368 case JSON_PARSE_ARRAY_VALUE:
1369 json_parse_value(p, token, JSON_PARSE_ARRAY_NEXT);
1372 case JSON_PARSE_ARRAY_NEXT:
1373 if (token->type == ',') {
1374 p->parse_state = JSON_PARSE_ARRAY_VALUE;
1375 } else if (token->type == ']') {
1378 json_error(p, "syntax error expecting ']' or ','");
1386 p->lex_state = JSON_LEX_START;
1387 ds_clear(&p->buffer);
1390 static struct json *
1391 json_create(enum json_type type)
1393 struct json *json = xmalloc(sizeof *json);
1399 json_error(struct json_parser *p, const char *format, ...)
1406 ds_put_format(&msg, "line %d, column %d, byte %d: ",
1407 p->line_number, p->column_number, p->byte_number);
1408 va_start(args, format);
1409 ds_put_format_valist(&msg, format, args);
1412 p->error = ds_steal_cstr(&msg);
1418 #define SPACES_PER_LEVEL 2
1420 struct json_serializer {
1426 static void json_serialize(const struct json *, struct json_serializer *);
1427 static void json_serialize_object(const struct shash *object,
1428 struct json_serializer *);
1429 static void json_serialize_array(const struct json_array *,
1430 struct json_serializer *);
1431 static void json_serialize_string(const char *, struct ds *);
1433 /* Converts 'json' to a string in JSON format, encoded in UTF-8, and returns
1434 * that string. The caller is responsible for freeing the returned string,
1435 * with free(), when it is no longer needed.
1437 * If 'flags' contains JSSF_PRETTY, the output is pretty-printed with each
1438 * nesting level introducing an additional indentation. Otherwise, the
1439 * returned string does not contain any new-line characters.
1441 * If 'flags' contains JSSF_SORT, members of objects in the output are sorted
1442 * in bytewise lexicographic order for reproducibility. Otherwise, members of
1443 * objects are output in an indeterminate order.
1445 * The returned string is valid JSON only if 'json' represents an array or an
1446 * object, since a bare literal does not satisfy the JSON grammar. */
1448 json_to_string(const struct json *json, int flags)
1453 json_to_ds(json, flags, &ds);
1454 return ds_steal_cstr(&ds);
1457 /* Same as json_to_string(), but the output is appended to 'ds'. */
1459 json_to_ds(const struct json *json, int flags, struct ds *ds)
1461 struct json_serializer s;
1466 json_serialize(json, &s);
1470 json_serialize(const struct json *json, struct json_serializer *s)
1472 struct ds *ds = s->ds;
1474 switch (json->type) {
1476 ds_put_cstr(ds, "null");
1480 ds_put_cstr(ds, "false");
1484 ds_put_cstr(ds, "true");
1488 json_serialize_object(json->u.object, s);
1492 json_serialize_array(&json->u.array, s);
1496 ds_put_format(ds, "%lld", json->u.integer);
1500 ds_put_format(ds, "%.*g", DBL_DIG, json->u.real);
1504 json_serialize_string(json->u.string, ds);
1514 indent_line(struct json_serializer *s)
1516 if (s->flags & JSSF_PRETTY) {
1517 ds_put_char(s->ds, '\n');
1518 ds_put_char_multiple(s->ds, ' ', SPACES_PER_LEVEL * s->depth);
1523 json_serialize_object_member(size_t i, const struct shash_node *node,
1524 struct json_serializer *s)
1526 struct ds *ds = s->ds;
1529 ds_put_char(ds, ',');
1533 json_serialize_string(node->name, ds);
1534 ds_put_char(ds, ':');
1535 if (s->flags & JSSF_PRETTY) {
1536 ds_put_char(ds, ' ');
1538 json_serialize(node->data, s);
1542 json_serialize_object(const struct shash *object, struct json_serializer *s)
1544 struct ds *ds = s->ds;
1546 ds_put_char(ds, '{');
1551 if (s->flags & JSSF_SORT) {
1552 const struct shash_node **nodes;
1555 nodes = shash_sort(object);
1556 n = shash_count(object);
1557 for (i = 0; i < n; i++) {
1558 json_serialize_object_member(i, nodes[i], s);
1562 struct shash_node *node;
1566 SHASH_FOR_EACH (node, object) {
1567 json_serialize_object_member(i++, node, s);
1571 ds_put_char(ds, '}');
1576 json_serialize_array(const struct json_array *array, struct json_serializer *s)
1578 struct ds *ds = s->ds;
1581 ds_put_char(ds, '[');
1587 for (i = 0; i < array->n; i++) {
1589 ds_put_char(ds, ',');
1592 json_serialize(array->elems[i], s);
1597 ds_put_char(ds, ']');
1601 json_serialize_string(const char *string, struct ds *ds)
1605 ds_put_char(ds, '"');
1606 while ((c = *string++) != '\0') {
1609 ds_put_cstr(ds, "\\\"");
1613 ds_put_cstr(ds, "\\\\");
1617 ds_put_cstr(ds, "\\b");
1621 ds_put_cstr(ds, "\\f");
1625 ds_put_cstr(ds, "\\n");
1629 ds_put_cstr(ds, "\\r");
1633 ds_put_cstr(ds, "\\t");
1640 ds_put_format(ds, "\\u%04x", c);
1645 ds_put_char(ds, '"');
1649 json_string_serialized_length(const char *string)
1654 length = strlen("\"\"");
1656 while ((c = *string++) != '\0') {
1683 json_object_serialized_length(const struct shash *object)
1685 size_t length = strlen("{}");
1687 if (!shash_is_empty(object)) {
1688 struct shash_node *node;
1690 /* Commas and colons. */
1691 length += 2 * shash_count(object) - 1;
1693 SHASH_FOR_EACH (node, object) {
1694 const struct json *value = node->data;
1696 length += json_string_serialized_length(node->name);
1697 length += json_serialized_length(value);
1705 json_array_serialized_length(const struct json_array *array)
1707 size_t length = strlen("[]");
1713 length += array->n - 1;
1715 for (i = 0; i < array->n; i++) {
1716 length += json_serialized_length(array->elems[i]);
1723 /* Returns strlen(json_to_string(json, 0)), that is, the number of bytes in the
1724 * JSON output by json_to_string() for 'json' when JSSF_PRETTY is not
1725 * requested. (JSSF_SORT does not affect the length of json_to_string()'s
1728 json_serialized_length(const struct json *json)
1730 switch (json->type) {
1732 return strlen("null");
1735 return strlen("false");
1738 return strlen("true");
1741 return json_object_serialized_length(json->u.object);
1744 return json_array_serialized_length(&json->u.array);
1747 return snprintf(NULL, 0, "%lld", json->u.integer);
1750 return snprintf(NULL, 0, "%.*g", DBL_DIG, json->u.real);
1753 return json_string_serialized_length(json->u.string);