2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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.
28 #include "dynamic-string.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, ...)
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_string(struct json *json, const char *name, const char *value)
285 json_object_put(json, name, json_string_create(value));
289 json_string(const struct json *json)
291 assert(json->type == JSON_STRING);
292 return json->u.string;
296 json_array(const struct json *json)
298 assert(json->type == JSON_ARRAY);
299 return (struct json_array *) &json->u.array;
303 json_object(const struct json *json)
305 assert(json->type == JSON_OBJECT);
306 return (struct shash *) json->u.object;
310 json_boolean(const struct json *json)
312 assert(json->type == JSON_TRUE || json->type == JSON_FALSE);
313 return json->type == JSON_TRUE;
317 json_real(const struct json *json)
319 assert(json->type == JSON_REAL || json->type == JSON_INTEGER);
320 return json->type == JSON_REAL ? json->u.real : json->u.integer;
324 json_integer(const struct json *json)
326 assert(json->type == JSON_INTEGER);
327 return json->u.integer;
330 static void json_destroy_object(struct shash *object);
331 static void json_destroy_array(struct json_array *array);
333 /* Frees 'json' and everything it points to, recursively. */
335 json_destroy(struct json *json)
338 switch (json->type) {
340 json_destroy_object(json->u.object);
344 json_destroy_array(&json->u.array);
348 free(json->u.string);
366 json_destroy_object(struct shash *object)
368 struct shash_node *node, *next;
370 SHASH_FOR_EACH_SAFE (node, next, object) {
371 struct json *value = node->data;
374 shash_delete(object, node);
376 shash_destroy(object);
381 json_destroy_array(struct json_array *array)
385 for (i = 0; i < array->n; i++) {
386 json_destroy(array->elems[i]);
391 static struct json *json_clone_object(const struct shash *object);
392 static struct json *json_clone_array(const struct json_array *array);
394 /* Returns a deep copy of 'json'. */
396 json_clone(const struct json *json)
398 switch (json->type) {
400 return json_clone_object(json->u.object);
403 return json_clone_array(&json->u.array);
406 return json_string_create(json->u.string);
411 return json_create(json->type);
414 return json_integer_create(json->u.integer);
417 return json_real_create(json->u.real);
426 json_clone_object(const struct shash *object)
428 struct shash_node *node;
431 json = json_object_create();
432 SHASH_FOR_EACH (node, object) {
433 struct json *value = node->data;
434 json_object_put(json, node->name, json_clone(value));
440 json_clone_array(const struct json_array *array)
445 elems = xmalloc(array->n * sizeof *elems);
446 for (i = 0; i < array->n; i++) {
447 elems[i] = json_clone(array->elems[i]);
449 return json_array_create(elems, array->n);
453 json_hash_object(const struct shash *object, size_t basis)
455 const struct shash_node **nodes;
458 nodes = shash_sort(object);
459 n = shash_count(object);
460 for (i = 0; i < n; i++) {
461 const struct shash_node *node = nodes[i];
462 basis = hash_string(node->name, basis);
463 basis = json_hash(node->data, basis);
469 json_hash_array(const struct json_array *array, size_t basis)
473 basis = hash_int(array->n, basis);
474 for (i = 0; i < array->n; i++) {
475 basis = json_hash(array->elems[i], basis);
481 json_hash(const struct json *json, size_t basis)
483 switch (json->type) {
485 return json_hash_object(json->u.object, basis);
488 return json_hash_array(&json->u.array, basis);
491 return hash_string(json->u.string, basis);
496 return hash_int(json->type << 8, basis);
499 return hash_int(json->u.integer, basis);
502 return hash_double(json->u.real, basis);
511 json_equal_object(const struct shash *a, const struct shash *b)
513 struct shash_node *a_node;
515 if (shash_count(a) != shash_count(b)) {
519 SHASH_FOR_EACH (a_node, a) {
520 struct shash_node *b_node = shash_find(b, a_node->name);
521 if (!b_node || !json_equal(a_node->data, b_node->data)) {
530 json_equal_array(const struct json_array *a, const struct json_array *b)
538 for (i = 0; i < a->n; i++) {
539 if (!json_equal(a->elems[i], b->elems[i])) {
548 json_equal(const struct json *a, const struct json *b)
550 if (a->type != b->type) {
556 return json_equal_object(a->u.object, b->u.object);
559 return json_equal_array(&a->u.array, &b->u.array);
562 return !strcmp(a->u.string, b->u.string);
570 return a->u.integer == b->u.integer;
573 return a->u.real == b->u.real;
581 /* Lexical analysis. */
584 json_lex_keyword(struct json_parser *p)
586 struct json_token token;
589 s = ds_cstr(&p->buffer);
590 if (!strcmp(s, "false")) {
591 token.type = T_FALSE;
592 } else if (!strcmp(s, "true")) {
594 } else if (!strcmp(s, "null")) {
597 json_error(p, "invalid keyword '%s'", s);
600 json_parser_input(p, &token);
604 json_lex_number(struct json_parser *p)
606 const char *cp = ds_cstr(&p->buffer);
607 unsigned long long int significand = 0;
608 struct json_token token;
609 bool imprecise = false;
610 bool negative = false;
613 /* Leading minus sign. */
619 /* At least one integer digit, but 0 may not be used as a leading digit for
620 * a longer number. */
625 json_error(p, "leading zeros not allowed");
628 } else if (isdigit(*cp)) {
630 if (significand <= ULLONG_MAX / 10) {
631 significand = significand * 10 + (*cp - '0');
639 } while (isdigit(*cp));
641 json_error(p, "'-' must be followed by digit");
645 /* Optional fraction. */
649 json_error(p, "decimal point must be followed by digit");
653 if (significand <= ULLONG_MAX / 10) {
654 significand = significand * 10 + (*cp - '0');
656 } else if (*cp != '0') {
660 } while (isdigit(*cp));
663 /* Optional exponent. */
664 if (*cp == 'e' || *cp == 'E') {
665 bool negative_exponent = false;
671 } else if (*cp == '-') {
672 negative_exponent = true;
677 json_error(p, "exponent must contain at least one digit");
683 if (exponent >= INT_MAX / 10) {
684 json_error(p, "exponent outside valid range");
687 exponent = exponent * 10 + (*cp - '0');
689 } while (isdigit(*cp));
691 if (negative_exponent) {
699 json_error(p, "syntax error in number");
703 /* Figure out number.
705 * We suppress negative zeros as a matter of policy. */
707 token.type = T_INTEGER;
709 json_parser_input(p, &token);
714 while (pow10 > 0 && significand < ULLONG_MAX / 10) {
718 while (pow10 < 0 && significand % 10 == 0) {
723 && significand <= (negative
724 ? (unsigned long long int) LLONG_MAX + 1
726 token.type = T_INTEGER;
727 token.u.integer = negative ? -significand : significand;
728 json_parser_input(p, &token);
734 if (!str_to_double(ds_cstr(&p->buffer), &token.u.real)) {
735 json_error(p, "number outside valid range");
738 /* Suppress negative zero. */
739 if (token.u.real == 0) {
742 json_parser_input(p, &token);
746 json_lex_4hex(const char *cp, const char *end, int *valuep)
751 return "quoted string ends within \\u escape";
754 value = hexits_value(cp, 4, NULL);
755 if (value == UINT_MAX) {
756 return "malformed \\u escape";
759 return "null bytes not supported in quoted strings";
766 json_lex_unicode(const char *cp, const char *end, struct ds *out)
771 error = json_lex_4hex(cp, end, &c0);
774 ds_put_cstr(out, error);
778 if (!uc_is_leading_surrogate(c0)) {
779 ds_put_utf8(out, c0);
783 if (cp + 2 > end || *cp++ != '\\' || *cp++ != 'u') {
785 ds_put_cstr(out, "malformed escaped surrogate pair");
789 error = json_lex_4hex(cp, end, &c1);
792 ds_put_cstr(out, error);
796 if (!uc_is_trailing_surrogate(c1)) {
798 ds_put_cstr(out, "second half of escaped surrogate pair is not "
799 "trailing surrogate");
803 ds_put_utf8(out, utf16_decode_surrogate_pair(c0, c1));
808 json_string_unescape(const char *in, size_t in_len, char **outp)
810 const char *end = in + in_len;
815 ds_reserve(&out, in_len);
816 if (in_len > 0 && in[in_len - 1] == '\\') {
817 ds_put_cstr(&out, "quoted string may not end with backslash");
823 ds_put_cstr(&out, "quoted string may not include unescaped \"");
827 ds_put_char(&out, *in++);
833 case '"': case '\\': case '/':
834 ds_put_char(&out, in[-1]);
838 ds_put_char(&out, '\b');
842 ds_put_char(&out, '\f');
846 ds_put_char(&out, '\n');
850 ds_put_char(&out, '\r');
854 ds_put_char(&out, '\t');
858 in = json_lex_unicode(in, end, &out);
866 ds_put_format(&out, "bad escape \\%c", in[-1]);
873 *outp = ds_cstr(&out);
878 json_parser_input_string(struct json_parser *p, const char *s)
880 struct json_token token;
882 token.type = T_STRING;
884 json_parser_input(p, &token);
888 json_lex_string(struct json_parser *p)
890 const char *raw = ds_cstr(&p->buffer);
891 if (!strchr(raw, '\\')) {
892 json_parser_input_string(p, raw);
896 if (json_string_unescape(raw, strlen(raw), &cooked)) {
897 json_parser_input_string(p, cooked);
899 json_error(p, "%s", cooked);
907 json_lex_input(struct json_parser *p, unsigned char c)
909 struct json_token token;
913 p->column_number = 0;
919 switch (p->lex_state) {
922 case ' ': case '\t': case '\n': case '\r':
926 case 'a': case 'b': case 'c': case 'd': case 'e':
927 case 'f': case 'g': case 'h': case 'i': case 'j':
928 case 'k': case 'l': case 'm': case 'n': case 'o':
929 case 'p': case 'q': case 'r': case 's': case 't':
930 case 'u': case 'v': case 'w': case 'x': case 'y':
932 p->lex_state = JSON_LEX_KEYWORD;
935 case '[': case '{': case ']': case '}': case ':': case ',':
937 json_parser_input(p, &token);
941 case '0': case '1': case '2': case '3': case '4':
942 case '5': case '6': case '7': case '8': case '9':
943 p->lex_state = JSON_LEX_NUMBER;
947 p->lex_state = JSON_LEX_STRING;
952 json_error(p, "invalid character '%c'", c);
954 json_error(p, "invalid character U+%04x", c);
960 case JSON_LEX_KEYWORD:
961 if (!isalpha((unsigned char) c)) {
967 case JSON_LEX_NUMBER:
968 if (!strchr(".0123456789eE-+", c)) {
974 case JSON_LEX_STRING:
976 p->lex_state = JSON_LEX_ESCAPE;
977 } else if (c == '"') {
980 } else if (c < 0x20) {
981 json_error(p, "U+%04X must be escaped in quoted string", c);
986 case JSON_LEX_ESCAPE:
987 p->lex_state = JSON_LEX_STRING;
993 ds_put_char(&p->buffer, c);
999 /* Parses 'string' as a JSON object or array and returns a newly allocated
1000 * 'struct json'. The caller must free the returned structure with
1001 * json_destroy() when it is no longer needed.
1003 * 'string' must be encoded in UTF-8.
1005 * If 'string' is valid JSON, then the returned 'struct json' will be either an
1006 * object (JSON_OBJECT) or an array (JSON_ARRAY).
1008 * If 'string' is not valid JSON, then the returned 'struct json' will be a
1009 * string (JSON_STRING) that describes the particular error encountered during
1010 * parsing. (This is an acceptable means of error reporting because at its top
1011 * level JSON must be either an object or an array; a bare string is not
1014 json_from_string(const char *string)
1016 struct json_parser *p = json_parser_create(JSPF_TRAILER);
1017 json_parser_feed(p, string, strlen(string));
1018 return json_parser_finish(p);
1021 /* Reads the file named 'file_name', parses its contents as a JSON object or
1022 * array, and returns a newly allocated 'struct json'. The caller must free
1023 * the returned structure with json_destroy() when it is no longer needed.
1025 * The file must be encoded in UTF-8.
1027 * See json_from_string() for return value semantics.
1030 json_from_file(const char *file_name)
1035 stream = fopen(file_name, "r");
1037 return json_string_create_nocopy(
1038 xasprintf("error opening \"%s\": %s", file_name, strerror(errno)));
1040 json = json_from_stream(stream);
1046 /* Parses the contents of 'stream' as a JSON object or array, and returns a
1047 * newly allocated 'struct json'. The caller must free the returned structure
1048 * with json_destroy() when it is no longer needed.
1050 * The file must be encoded in UTF-8.
1052 * See json_from_string() for return value semantics.
1055 json_from_stream(FILE *stream)
1057 struct json_parser *p;
1060 p = json_parser_create(JSPF_TRAILER);
1062 char buffer[BUFSIZ];
1065 n = fread(buffer, 1, sizeof buffer, stream);
1066 if (!n || json_parser_feed(p, buffer, n) != n) {
1070 json = json_parser_finish(p);
1072 if (ferror(stream)) {
1074 json = json_string_create_nocopy(
1075 xasprintf("error reading JSON stream: %s", strerror(errno)));
1081 struct json_parser *
1082 json_parser_create(int flags)
1084 struct json_parser *p = xzalloc(sizeof *p);
1090 json_parser_feed(struct json_parser *p, const char *input, size_t n)
1093 for (i = 0; !p->done && i < n; ) {
1094 if (json_lex_input(p, input[i])) {
1102 json_parser_is_done(const struct json_parser *p)
1108 json_parser_finish(struct json_parser *p)
1112 switch (p->lex_state) {
1113 case JSON_LEX_START:
1116 case JSON_LEX_STRING:
1117 case JSON_LEX_ESCAPE:
1118 json_error(p, "unexpected end of input in quoted string");
1121 case JSON_LEX_NUMBER:
1122 case JSON_LEX_KEYWORD:
1123 json_lex_input(p, ' ');
1127 if (p->parse_state == JSON_PARSE_START) {
1128 json_error(p, "empty input stream");
1129 } else if (p->parse_state != JSON_PARSE_END) {
1130 json_error(p, "unexpected end of input");
1134 assert(p->height == 1);
1135 assert(p->stack[0].json != NULL);
1136 json = p->stack[--p->height].json;
1138 json = json_string_create_nocopy(p->error);
1142 json_parser_abort(p);
1148 json_parser_abort(struct json_parser *p)
1151 ds_destroy(&p->buffer);
1153 json_destroy(p->stack[0].json);
1156 free(p->member_name);
1162 static struct json_parser_node *
1163 json_parser_top(struct json_parser *p)
1165 return &p->stack[p->height - 1];
1169 json_parser_put_value(struct json_parser *p, struct json *value)
1171 struct json_parser_node *node = json_parser_top(p);
1172 if (node->json->type == JSON_OBJECT) {
1173 json_object_put(node->json, p->member_name, value);
1174 free(p->member_name);
1175 p->member_name = NULL;
1176 } else if (node->json->type == JSON_ARRAY) {
1177 json_array_add(node->json, value);
1184 json_parser_push(struct json_parser *p,
1185 struct json *new_json, enum json_parse_state new_state)
1187 if (p->height < JSON_MAX_HEIGHT) {
1188 struct json_parser_node *node;
1190 if (p->height >= p->allocated_height) {
1191 p->stack = x2nrealloc(p->stack, &p->allocated_height,
1195 if (p->height > 0) {
1196 json_parser_put_value(p, new_json);
1199 node = &p->stack[p->height++];
1200 node->json = new_json;
1201 p->parse_state = new_state;
1203 json_destroy(new_json);
1204 json_error(p, "input exceeds maximum nesting depth %d",
1210 json_parser_push_object(struct json_parser *p)
1212 json_parser_push(p, json_object_create(), JSON_PARSE_OBJECT_INIT);
1216 json_parser_push_array(struct json_parser *p)
1218 json_parser_push(p, json_array_create_empty(), JSON_PARSE_ARRAY_INIT);
1222 json_parse_value(struct json_parser *p, struct json_token *token,
1223 enum json_parse_state next_state)
1227 switch (token->type) {
1229 value = json_boolean_create(false);
1233 value = json_null_create();
1237 value = json_boolean_create(true);
1241 json_parser_push_object(p);
1245 json_parser_push_array(p);
1249 value = json_integer_create(token->u.integer);
1253 value = json_real_create(token->u.real);
1257 value = json_string_create(token->u.string);
1266 json_error(p, "syntax error expecting value");
1270 json_parser_put_value(p, value);
1271 p->parse_state = next_state;
1275 json_parser_pop(struct json_parser *p)
1277 struct json_parser_node *node;
1279 /* Conserve memory. */
1280 node = json_parser_top(p);
1281 if (node->json->type == JSON_ARRAY) {
1282 json_array_trim(node->json);
1285 /* Pop off the top-of-stack. */
1286 if (p->height == 1) {
1287 p->parse_state = JSON_PARSE_END;
1288 if (!(p->flags & JSPF_TRAILER)) {
1293 node = json_parser_top(p);
1294 if (node->json->type == JSON_ARRAY) {
1295 p->parse_state = JSON_PARSE_ARRAY_NEXT;
1296 } else if (node->json->type == JSON_OBJECT) {
1297 p->parse_state = JSON_PARSE_OBJECT_NEXT;
1305 json_parser_input(struct json_parser *p, struct json_token *token)
1307 switch (p->parse_state) {
1308 case JSON_PARSE_START:
1309 if (token->type == '{') {
1310 json_parser_push_object(p);
1311 } else if (token->type == '[') {
1312 json_parser_push_array(p);
1314 json_error(p, "syntax error at beginning of input");
1318 case JSON_PARSE_END:
1319 json_error(p, "trailing garbage at end of input");
1322 case JSON_PARSE_OBJECT_INIT:
1323 if (token->type == '}') {
1328 case JSON_PARSE_OBJECT_NAME:
1329 if (token->type == T_STRING) {
1330 p->member_name = xstrdup(token->u.string);
1331 p->parse_state = JSON_PARSE_OBJECT_COLON;
1333 json_error(p, "syntax error parsing object expecting string");
1337 case JSON_PARSE_OBJECT_COLON:
1338 if (token->type == ':') {
1339 p->parse_state = JSON_PARSE_OBJECT_VALUE;
1341 json_error(p, "syntax error parsing object expecting ':'");
1345 case JSON_PARSE_OBJECT_VALUE:
1346 json_parse_value(p, token, JSON_PARSE_OBJECT_NEXT);
1349 case JSON_PARSE_OBJECT_NEXT:
1350 if (token->type == ',') {
1351 p->parse_state = JSON_PARSE_OBJECT_NAME;
1352 } else if (token->type == '}') {
1355 json_error(p, "syntax error expecting '}' or ','");
1359 case JSON_PARSE_ARRAY_INIT:
1360 if (token->type == ']') {
1365 case JSON_PARSE_ARRAY_VALUE:
1366 json_parse_value(p, token, JSON_PARSE_ARRAY_NEXT);
1369 case JSON_PARSE_ARRAY_NEXT:
1370 if (token->type == ',') {
1371 p->parse_state = JSON_PARSE_ARRAY_VALUE;
1372 } else if (token->type == ']') {
1375 json_error(p, "syntax error expecting ']' or ','");
1383 p->lex_state = JSON_LEX_START;
1384 ds_clear(&p->buffer);
1387 static struct json *
1388 json_create(enum json_type type)
1390 struct json *json = xmalloc(sizeof *json);
1396 json_error(struct json_parser *p, const char *format, ...)
1403 ds_put_format(&msg, "line %d, column %d, byte %d: ",
1404 p->line_number, p->column_number, p->byte_number);
1405 va_start(args, format);
1406 ds_put_format_valist(&msg, format, args);
1409 p->error = ds_steal_cstr(&msg);
1415 #define SPACES_PER_LEVEL 2
1417 struct json_serializer {
1423 static void json_serialize(const struct json *, struct json_serializer *);
1424 static void json_serialize_object(const struct shash *object,
1425 struct json_serializer *);
1426 static void json_serialize_array(const struct json_array *,
1427 struct json_serializer *);
1428 static void json_serialize_string(const char *, struct ds *);
1430 /* Converts 'json' to a string in JSON format, encoded in UTF-8, and returns
1431 * that string. The caller is responsible for freeing the returned string,
1432 * with free(), when it is no longer needed.
1434 * If 'flags' contains JSSF_PRETTY, the output is pretty-printed with each
1435 * nesting level introducing an additional indentation. Otherwise, the
1436 * returned string does not contain any new-line characters.
1438 * If 'flags' contains JSSF_SORT, members of objects in the output are sorted
1439 * in bytewise lexicographic order for reproducibility. Otherwise, members of
1440 * objects are output in an indeterminate order.
1442 * The returned string is valid JSON only if 'json' represents an array or an
1443 * object, since a bare literal does not satisfy the JSON grammar. */
1445 json_to_string(const struct json *json, int flags)
1450 json_to_ds(json, flags, &ds);
1451 return ds_steal_cstr(&ds);
1454 /* Same as json_to_string(), but the output is appended to 'ds'. */
1456 json_to_ds(const struct json *json, int flags, struct ds *ds)
1458 struct json_serializer s;
1463 json_serialize(json, &s);
1467 json_serialize(const struct json *json, struct json_serializer *s)
1469 struct ds *ds = s->ds;
1471 switch (json->type) {
1473 ds_put_cstr(ds, "null");
1477 ds_put_cstr(ds, "false");
1481 ds_put_cstr(ds, "true");
1485 json_serialize_object(json->u.object, s);
1489 json_serialize_array(&json->u.array, s);
1493 ds_put_format(ds, "%lld", json->u.integer);
1497 ds_put_format(ds, "%.*g", DBL_DIG, json->u.real);
1501 json_serialize_string(json->u.string, ds);
1511 indent_line(struct json_serializer *s)
1513 if (s->flags & JSSF_PRETTY) {
1514 ds_put_char(s->ds, '\n');
1515 ds_put_char_multiple(s->ds, ' ', SPACES_PER_LEVEL * s->depth);
1520 json_serialize_object_member(size_t i, const struct shash_node *node,
1521 struct json_serializer *s)
1523 struct ds *ds = s->ds;
1526 ds_put_char(ds, ',');
1530 json_serialize_string(node->name, ds);
1531 ds_put_char(ds, ':');
1532 if (s->flags & JSSF_PRETTY) {
1533 ds_put_char(ds, ' ');
1535 json_serialize(node->data, s);
1539 json_serialize_object(const struct shash *object, struct json_serializer *s)
1541 struct ds *ds = s->ds;
1543 ds_put_char(ds, '{');
1548 if (s->flags & JSSF_SORT) {
1549 const struct shash_node **nodes;
1552 nodes = shash_sort(object);
1553 n = shash_count(object);
1554 for (i = 0; i < n; i++) {
1555 json_serialize_object_member(i, nodes[i], s);
1559 struct shash_node *node;
1563 SHASH_FOR_EACH (node, object) {
1564 json_serialize_object_member(i++, node, s);
1568 ds_put_char(ds, '}');
1573 json_serialize_array(const struct json_array *array, struct json_serializer *s)
1575 struct ds *ds = s->ds;
1578 ds_put_char(ds, '[');
1584 for (i = 0; i < array->n; i++) {
1586 ds_put_char(ds, ',');
1589 json_serialize(array->elems[i], s);
1594 ds_put_char(ds, ']');
1598 json_serialize_string(const char *string, struct ds *ds)
1602 ds_put_char(ds, '"');
1603 while ((c = *string++) != '\0') {
1606 ds_put_cstr(ds, "\\\"");
1610 ds_put_cstr(ds, "\\\\");
1614 ds_put_cstr(ds, "\\b");
1618 ds_put_cstr(ds, "\\f");
1622 ds_put_cstr(ds, "\\n");
1626 ds_put_cstr(ds, "\\r");
1630 ds_put_cstr(ds, "\\t");
1637 ds_put_format(ds, "\\u%04x", c);
1642 ds_put_char(ds, '"');