2 Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com)
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #define _CRT_SECURE_NO_WARNINGS
26 #define _CRT_NONSTDC_NO_DEPRECATE
31 // include utf8 library used by libsass
32 // ToDo: replace internal json utf8 code
41 #if defined(_MSC_VER) && _MSC_VER < 1900
46 extern "C" int snprintf(char *, size_t, const char *, ...);
49 #define out_of_memory() do { \
50 fprintf(stderr, "Out of memory.\n"); \
54 /* Sadly, strdup is not portable. */
55 static char *json_strdup(const char *str)
57 char *ret = (char*) malloc(strlen(str) + 1);
73 static void sb_init(SB *sb)
75 sb->start = (char*) malloc(17);
76 if (sb->start == NULL)
79 sb->end = sb->start + 16;
82 /* sb and need may be evaluated multiple times. */
83 #define sb_need(sb, need) do { \
84 if ((sb)->end - (sb)->cur < (need)) \
88 static void sb_grow(SB *sb, int need)
90 size_t length = sb->cur - sb->start;
91 size_t alloc = sb->end - sb->start;
95 } while (alloc < length + need);
97 sb->start = (char*) realloc(sb->start, alloc + 1);
98 if (sb->start == NULL)
100 sb->cur = sb->start + length;
101 sb->end = sb->start + alloc;
104 static void sb_put(SB *sb, const char *bytes, int count)
107 memcpy(sb->cur, bytes, count);
111 #define sb_putc(sb, c) do { \
112 if ((sb)->cur >= (sb)->end) \
114 *(sb)->cur++ = (c); \
117 static void sb_puts(SB *sb, const char *str)
119 sb_put(sb, str, (int)strlen(str));
122 static char *sb_finish(SB *sb)
125 assert(sb->start <= sb->cur && strlen(sb->start) == (size_t)(sb->cur - sb->start));
129 static void sb_free(SB *sb)
135 * Unicode helper functions
137 * These are taken from the ccan/charset module and customized a bit.
138 * Putting them here means the compiler can (choose to) inline them,
139 * and it keeps ccan/json from having a dependency.
141 * We use uint32_t Type for Unicode codepoints.
142 * We need our own because wchar_t might be 16 bits.
146 * Validate a single UTF-8 character starting at @s.
147 * The string must be null-terminated.
149 * If it's valid, return its length (1 thru 4).
150 * If it's invalid or clipped, return 0.
152 * This function implements the syntax given in RFC3629, which is
153 * the same as that given in The Unicode Standard, Version 6.0.
155 * It has the following properties:
157 * * All codepoints U+0000..U+10FFFF may be encoded,
158 * except for U+D800..U+DFFF, which are reserved
159 * for UTF-16 surrogate pair encoding.
160 * * UTF-8 byte sequences longer than 4 bytes are not permitted,
161 * as they exceed the range of Unicode.
162 * * The sixty-six Unicode "non-characters" are permitted
163 * (namely, U+FDD0..U+FDEF, U+xxFFFE, and U+xxFFFF).
165 static int utf8_validate_cz(const char *s)
167 unsigned char c = *s++;
169 if (c <= 0x7F) { /* 00..7F */
171 } else if (c <= 0xC1) { /* 80..C1 */
172 /* Disallow overlong 2-byte sequence. */
174 } else if (c <= 0xDF) { /* C2..DF */
175 /* Make sure subsequent byte is in the range 0x80..0xBF. */
176 if (((unsigned char)*s++ & 0xC0) != 0x80)
180 } else if (c <= 0xEF) { /* E0..EF */
181 /* Disallow overlong 3-byte sequence. */
182 if (c == 0xE0 && (unsigned char)*s < 0xA0)
185 /* Disallow U+D800..U+DFFF. */
186 if (c == 0xED && (unsigned char)*s > 0x9F)
189 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
190 if (((unsigned char)*s++ & 0xC0) != 0x80)
192 if (((unsigned char)*s++ & 0xC0) != 0x80)
196 } else if (c <= 0xF4) { /* F0..F4 */
197 /* Disallow overlong 4-byte sequence. */
198 if (c == 0xF0 && (unsigned char)*s < 0x90)
201 /* Disallow codepoints beyond U+10FFFF. */
202 if (c == 0xF4 && (unsigned char)*s > 0x8F)
205 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
206 if (((unsigned char)*s++ & 0xC0) != 0x80)
208 if (((unsigned char)*s++ & 0xC0) != 0x80)
210 if (((unsigned char)*s++ & 0xC0) != 0x80)
214 } else { /* F5..FF */
219 /* Validate a null-terminated UTF-8 string. */
220 static bool utf8_validate(const char *s)
224 for (; *s != 0; s += len) {
225 len = utf8_validate_cz(s);
234 * Read a single UTF-8 character starting at @s,
235 * returning the length, in bytes, of the character read.
237 * This function assumes input is valid UTF-8,
238 * and that there are enough characters in front of @s.
240 static int utf8_read_char(const char *s, uint32_t *out)
242 const unsigned char *c = (const unsigned char*) s;
244 assert(utf8_validate_cz(s));
250 } else if (c[0] <= 0xDF) {
251 /* C2..DF (unless input is invalid) */
252 *out = ((uint32_t)c[0] & 0x1F) << 6 |
253 ((uint32_t)c[1] & 0x3F);
255 } else if (c[0] <= 0xEF) {
257 *out = ((uint32_t)c[0] & 0xF) << 12 |
258 ((uint32_t)c[1] & 0x3F) << 6 |
259 ((uint32_t)c[2] & 0x3F);
262 /* F0..F4 (unless input is invalid) */
263 *out = ((uint32_t)c[0] & 0x7) << 18 |
264 ((uint32_t)c[1] & 0x3F) << 12 |
265 ((uint32_t)c[2] & 0x3F) << 6 |
266 ((uint32_t)c[3] & 0x3F);
272 * Write a single UTF-8 character to @s,
273 * returning the length, in bytes, of the character written.
275 * @unicode must be U+0000..U+10FFFF, but not U+D800..U+DFFF.
277 * This function will write up to 4 bytes to @out.
279 static int utf8_write_char(uint32_t unicode, char *out)
281 unsigned char *o = (unsigned char*) out;
283 assert(unicode <= 0x10FFFF && !(unicode >= 0xD800 && unicode <= 0xDFFF));
285 if (unicode <= 0x7F) {
289 } else if (unicode <= 0x7FF) {
291 *o++ = 0xC0 | unicode >> 6;
292 *o++ = 0x80 | (unicode & 0x3F);
294 } else if (unicode <= 0xFFFF) {
296 *o++ = 0xE0 | unicode >> 12;
297 *o++ = 0x80 | (unicode >> 6 & 0x3F);
298 *o++ = 0x80 | (unicode & 0x3F);
301 /* U+10000..U+10FFFF */
302 *o++ = 0xF0 | unicode >> 18;
303 *o++ = 0x80 | (unicode >> 12 & 0x3F);
304 *o++ = 0x80 | (unicode >> 6 & 0x3F);
305 *o++ = 0x80 | (unicode & 0x3F);
311 * Compute the Unicode codepoint of a UTF-16 surrogate pair.
313 * @uc should be 0xD800..0xDBFF, and @lc should be 0xDC00..0xDFFF.
314 * If they aren't, this function returns false.
316 static bool from_surrogate_pair(uint16_t uc, uint16_t lc, uint32_t *unicode)
318 if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF) {
319 *unicode = 0x10000 + ((((uint32_t)uc & 0x3FF) << 10) | (lc & 0x3FF));
327 * Construct a UTF-16 surrogate pair given a Unicode codepoint.
329 * @unicode must be U+10000..U+10FFFF.
331 static void to_surrogate_pair(uint32_t unicode, uint16_t *uc, uint16_t *lc)
335 assert(unicode >= 0x10000 && unicode <= 0x10FFFF);
337 n = unicode - 0x10000;
338 *uc = ((n >> 10) & 0x3FF) | 0xD800;
339 *lc = (n & 0x3FF) | 0xDC00;
342 static bool is_space (const char *c);
343 static bool is_digit (const char *c);
344 static bool parse_value (const char **sp, JsonNode **out);
345 static bool parse_string (const char **sp, char **out);
346 static bool parse_number (const char **sp, double *out);
347 static bool parse_array (const char **sp, JsonNode **out);
348 static bool parse_object (const char **sp, JsonNode **out);
349 static bool parse_hex16 (const char **sp, uint16_t *out);
351 static bool expect_literal (const char **sp, const char *str);
352 static void skip_space (const char **sp);
354 static void emit_value (SB *out, const JsonNode *node);
355 static void emit_value_indented (SB *out, const JsonNode *node, const char *space, int indent_level);
356 static void emit_string (SB *out, const char *str);
357 static void emit_number (SB *out, double num);
358 static void emit_array (SB *out, const JsonNode *array);
359 static void emit_array_indented (SB *out, const JsonNode *array, const char *space, int indent_level);
360 static void emit_object (SB *out, const JsonNode *object);
361 static void emit_object_indented (SB *out, const JsonNode *object, const char *space, int indent_level);
363 static int write_hex16(char *out, uint16_t val);
365 static JsonNode *mknode(JsonTag tag);
366 static void append_node(JsonNode *parent, JsonNode *child);
367 static void prepend_node(JsonNode *parent, JsonNode *child);
368 static void append_member(JsonNode *object, char *key, JsonNode *value);
370 /* Assertion-friendly validity checks */
371 static bool tag_is_valid(unsigned int tag);
372 static bool number_is_valid(const char *num);
374 JsonNode *json_decode(const char *json)
376 const char *s = json;
380 if (!parse_value(&s, &ret))
392 char *json_encode(const JsonNode *node)
394 return json_stringify(node, NULL);
397 char *json_encode_string(const char *str)
403 emit_string(&sb, str);
405 catch (std::exception) {
410 return sb_finish(&sb);
413 char *json_stringify(const JsonNode *node, const char *space)
420 emit_value_indented(&sb, node, space, 0);
422 emit_value(&sb, node);
424 catch (std::exception) {
429 return sb_finish(&sb);
432 void json_delete(JsonNode *node)
435 json_remove_from_parent(node);
444 JsonNode *child, *next;
445 for (child = node->children.head; child != NULL; child = next) {
458 bool json_validate(const char *json)
460 const char *s = json;
463 if (!parse_value(&s, NULL))
473 JsonNode *json_find_element(JsonNode *array, int index)
478 if (array == NULL || array->tag != JSON_ARRAY)
481 json_foreach(element, array) {
490 JsonNode *json_find_member(JsonNode *object, const char *name)
494 if (object == NULL || object->tag != JSON_OBJECT)
497 json_foreach(member, object)
498 if (strcmp(member->key, name) == 0)
504 JsonNode *json_first_child(const JsonNode *node)
506 if (node != NULL && (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT))
507 return node->children.head;
511 static JsonNode *mknode(JsonTag tag)
513 JsonNode *ret = (JsonNode*) calloc(1, sizeof(JsonNode));
520 JsonNode *json_mknull(void)
522 return mknode(JSON_NULL);
525 JsonNode *json_mkbool(bool b)
527 JsonNode *ret = mknode(JSON_BOOL);
532 static JsonNode *mkstring(char *s)
534 JsonNode *ret = mknode(JSON_STRING);
539 JsonNode *json_mkstring(const char *s)
541 return mkstring(json_strdup(s));
544 JsonNode *json_mknumber(double n)
546 JsonNode *node = mknode(JSON_NUMBER);
551 JsonNode *json_mkarray(void)
553 return mknode(JSON_ARRAY);
556 JsonNode *json_mkobject(void)
558 return mknode(JSON_OBJECT);
561 static void append_node(JsonNode *parent, JsonNode *child)
563 if (child != NULL && parent != NULL) {
564 child->parent = parent;
565 child->prev = parent->children.tail;
568 if (parent->children.tail != NULL)
569 parent->children.tail->next = child;
571 parent->children.head = child;
572 parent->children.tail = child;
576 static void prepend_node(JsonNode *parent, JsonNode *child)
578 if (child != NULL && parent != NULL) {
579 child->parent = parent;
581 child->next = parent->children.head;
583 if (parent->children.head != NULL)
584 parent->children.head->prev = child;
586 parent->children.tail = child;
587 parent->children.head = child;
591 static void append_member(JsonNode *object, char *key, JsonNode *value)
593 if (value != NULL && object != NULL) {
595 append_node(object, value);
599 void json_append_element(JsonNode *array, JsonNode *element)
601 if (array != NULL && element !=NULL) {
602 assert(array->tag == JSON_ARRAY);
603 assert(element->parent == NULL);
605 append_node(array, element);
609 void json_prepend_element(JsonNode *array, JsonNode *element)
611 assert(array->tag == JSON_ARRAY);
612 assert(element->parent == NULL);
614 prepend_node(array, element);
617 void json_append_member(JsonNode *object, const char *key, JsonNode *value)
619 if (object != NULL && key != NULL && value != NULL) {
620 assert(object->tag == JSON_OBJECT);
621 assert(value->parent == NULL);
623 append_member(object, json_strdup(key), value);
627 void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
629 if (object != NULL && key != NULL && value != NULL) {
630 assert(object->tag == JSON_OBJECT);
631 assert(value->parent == NULL);
633 value->key = json_strdup(key);
634 prepend_node(object, value);
638 void json_remove_from_parent(JsonNode *node)
641 JsonNode *parent = node->parent;
643 if (parent != NULL) {
644 if (node->prev != NULL)
645 node->prev->next = node->next;
647 parent->children.head = node->next;
649 if (node->next != NULL)
650 node->next->prev = node->prev;
652 parent->children.tail = node->prev;
657 node->prev = node->next = NULL;
663 static bool parse_value(const char **sp, JsonNode **out)
669 if (expect_literal(&s, "null")) {
671 *out = json_mknull();
678 if (expect_literal(&s, "false")) {
680 *out = json_mkbool(false);
687 if (expect_literal(&s, "true")) {
689 *out = json_mkbool(true);
697 if (parse_string(&s, out ? &str : NULL)) {
699 *out = mkstring(str);
707 if (parse_array(&s, out)) {
714 if (parse_object(&s, out)) {
722 if (parse_number(&s, out ? &num : NULL)) {
724 *out = json_mknumber(num);
733 static bool parse_array(const char **sp, JsonNode **out)
736 JsonNode *ret = out ? json_mkarray() : NULL;
737 JsonNode *element = NULL;
749 if (!parse_value(&s, out ? &element : NULL))
754 json_append_element(ret, element);
777 static bool parse_object(const char **sp, JsonNode **out)
780 JsonNode *ret = out ? json_mkobject() : NULL;
782 JsonNode *value = NULL;
794 if (!parse_string(&s, out ? &key : NULL))
799 goto failure_free_key;
802 if (!parse_value(&s, out ? &value : NULL))
803 goto failure_free_key;
807 append_member(ret, key, value);
833 bool parse_string(const char **sp, char **out)
837 char throwaway_buffer[4];
838 /* enough space for a UTF-8 character */
849 b = throwaway_buffer;
853 unsigned char c = *s++;
855 /* Parse next character, and write it to b. */
884 if (!parse_hex16(&s, &uc))
887 if (uc >= 0xD800 && uc <= 0xDFFF) {
888 /* Handle UTF-16 surrogate pair. */
889 if (*s++ != '\\' || *s++ != 'u' || !parse_hex16(&s, &lc))
890 goto failed; /* Incomplete surrogate pair. */
891 if (!from_surrogate_pair(uc, lc, &unicode))
892 goto failed; /* Invalid surrogate pair. */
893 } else if (uc == 0) {
894 /* Disallow "\u0000". */
900 b += utf8_write_char(unicode, b);
907 } else if (c <= 0x1F) {
908 /* Control characters are not allowed in string literals. */
911 /* Validate and echo a UTF-8 character. */
915 len = utf8_validate_cz(s);
917 goto failed; /* Invalid UTF-8 character. */
924 * Update sb to know about the new bytes,
925 * and set up b to write another character.
932 b = throwaway_buffer;
938 *out = sb_finish(&sb);
948 bool is_space(const char *c) {
949 return ((*c) == '\t' || (*c) == '\n' || (*c) == '\r' || (*c) == ' ');
952 bool is_digit(const char *c){
953 return ((*c) >= '0' && (*c) <= '9');
957 * The JSON spec says that a number shall follow this precise pattern
958 * (spaces and quotes added for readability):
959 * '-'? (0 | [1-9][0-9]*) ('.' [0-9]+)? ([Ee] [+-]? [0-9]+)?
961 * However, some JSON parsers are more liberal. For instance, PHP accepts
962 * '.5' and '1.'. JSON.parse accepts '+3'.
964 * This function takes the strict approach.
966 bool parse_number(const char **sp, double *out)
974 /* (0 | [1-9][0-9]*) */
982 } while (is_digit(s));
992 } while (is_digit(s));
995 /* ([Ee] [+-]? [0-9]+)? */
996 if (*s == 'E' || *s == 'e') {
998 if (*s == '+' || *s == '-')
1004 } while (is_digit(s));
1008 *out = strtod(*sp, NULL);
1014 static void skip_space(const char **sp)
1016 const char *s = *sp;
1022 static void emit_value(SB *out, const JsonNode *node)
1024 assert(tag_is_valid(node->tag));
1025 switch (node->tag) {
1027 sb_puts(out, "null");
1030 sb_puts(out, node->bool_ ? "true" : "false");
1033 emit_string(out, node->string_);
1036 emit_number(out, node->number_);
1039 emit_array(out, node);
1042 emit_object(out, node);
1049 void emit_value_indented(SB *out, const JsonNode *node, const char *space, int indent_level)
1051 assert(tag_is_valid(node->tag));
1052 switch (node->tag) {
1054 sb_puts(out, "null");
1057 sb_puts(out, node->bool_ ? "true" : "false");
1060 emit_string(out, node->string_);
1063 emit_number(out, node->number_);
1066 emit_array_indented(out, node, space, indent_level);
1069 emit_object_indented(out, node, space, indent_level);
1076 static void emit_array(SB *out, const JsonNode *array)
1078 const JsonNode *element;
1081 json_foreach(element, array) {
1082 emit_value(out, element);
1083 if (element->next != NULL)
1089 static void emit_array_indented(SB *out, const JsonNode *array, const char *space, int indent_level)
1091 const JsonNode *element = array->children.head;
1094 if (element == NULL) {
1099 sb_puts(out, "[\n");
1100 while (element != NULL) {
1101 for (i = 0; i < indent_level + 1; i++)
1102 sb_puts(out, space);
1103 emit_value_indented(out, element, space, indent_level + 1);
1105 element = element->next;
1106 sb_puts(out, element != NULL ? ",\n" : "\n");
1108 for (i = 0; i < indent_level; i++)
1109 sb_puts(out, space);
1113 static void emit_object(SB *out, const JsonNode *object)
1115 const JsonNode *member;
1118 json_foreach(member, object) {
1119 emit_string(out, member->key);
1121 emit_value(out, member);
1122 if (member->next != NULL)
1128 static void emit_object_indented(SB *out, const JsonNode *object, const char *space, int indent_level)
1130 const JsonNode *member = object->children.head;
1133 if (member == NULL) {
1138 sb_puts(out, "{\n");
1139 while (member != NULL) {
1140 for (i = 0; i < indent_level + 1; i++)
1141 sb_puts(out, space);
1142 emit_string(out, member->key);
1144 emit_value_indented(out, member, space, indent_level + 1);
1146 member = member->next;
1147 sb_puts(out, member != NULL ? ",\n" : "\n");
1149 for (i = 0; i < indent_level; i++)
1150 sb_puts(out, space);
1154 void emit_string(SB *out, const char *str)
1156 bool escape_unicode = false;
1157 const char *s = str;
1160 // make assertion catchable
1162 if (!utf8_validate(str)) {
1163 throw utf8::invalid_utf8(0);
1167 assert(utf8_validate(str));
1170 * 14 bytes is enough space to write up to two
1171 * \uXXXX escapes and two quotation marks.
1178 unsigned char c = *s++;
1180 /* Encode the next character, and write it to b. */
1214 len = utf8_validate_cz(s);
1218 * Handle invalid UTF-8 character gracefully in production
1219 * by writing a replacement character (U+FFFD)
1220 * and skipping a single byte.
1222 * This should never happen when assertions are enabled
1223 * due to the assertion at the beginning of this function.
1226 if (escape_unicode) {
1227 strcpy(b, "\\uFFFD");
1235 } else if (c < 0x1F || (c >= 0x80 && escape_unicode)) {
1236 /* Encode using \u.... */
1239 s += utf8_read_char(s, &unicode);
1241 if (unicode <= 0xFFFF) {
1244 b += write_hex16(b, unicode);
1246 /* Produce a surrogate pair. */
1248 assert(unicode <= 0x10FFFF);
1249 to_surrogate_pair(unicode, &uc, &lc);
1252 b += write_hex16(b, uc);
1255 b += write_hex16(b, lc);
1258 /* Write the character directly. */
1268 * Update *out to know about the new bytes,
1269 * and set up b to write another encoded character.
1280 static void emit_number(SB *out, double num)
1283 * This isn't exactly how JavaScript renders numbers,
1284 * but it should produce valid JSON for reasonable numbers
1285 * preserve precision well enough, and avoid some oddities
1286 * like 0.3 -> 0.299999999999999988898 .
1289 sprintf(buf, "%.16g", num);
1291 if (number_is_valid(buf))
1294 sb_puts(out, "null");
1297 static bool tag_is_valid(unsigned int tag)
1299 return (/* tag >= JSON_NULL && */ tag <= JSON_OBJECT);
1302 static bool number_is_valid(const char *num)
1304 return (parse_number(&num, NULL) && *num == '\0');
1307 static bool expect_literal(const char **sp, const char *str)
1309 const char *s = *sp;
1311 while (*str != '\0')
1320 * Parses exactly 4 hex characters (capital or lowercase).
1321 * Fails if any input chars are not [0-9A-Fa-f].
1323 static bool parse_hex16(const char **sp, uint16_t *out)
1325 const char *s = *sp;
1331 for (i = 0; i < 4; i++) {
1333 if (c >= '0' && c <= '9')
1335 else if (c >= 'A' && c <= 'F')
1337 else if (c >= 'a' && c <= 'f')
1353 * Encodes a 16-bit number into hexadecimal,
1354 * writing exactly 4 hex chars.
1356 static int write_hex16(char *out, uint16_t val)
1358 const char *hex = "0123456789ABCDEF";
1360 *out++ = hex[(val >> 12) & 0xF];
1361 *out++ = hex[(val >> 8) & 0xF];
1362 *out++ = hex[(val >> 4) & 0xF];
1363 *out++ = hex[ val & 0xF];
1368 bool json_check(const JsonNode *node, char errmsg[256])
1370 #define problem(...) do { \
1371 if (errmsg != NULL) \
1372 snprintf(errmsg, 256, __VA_ARGS__); \
1376 if (node->key != NULL && !utf8_validate(node->key))
1377 problem("key contains invalid UTF-8");
1379 if (!tag_is_valid(node->tag))
1380 problem("tag is invalid (%u)", node->tag);
1382 if (node->tag == JSON_BOOL) {
1383 if (node->bool_ != false && node->bool_ != true)
1384 problem("bool_ is neither false (%d) nor true (%d)", (int)false, (int)true);
1385 } else if (node->tag == JSON_STRING) {
1386 if (node->string_ == NULL)
1387 problem("string_ is NULL");
1388 if (!utf8_validate(node->string_))
1389 problem("string_ contains invalid UTF-8");
1390 } else if (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT) {
1391 JsonNode *head = node->children.head;
1392 JsonNode *tail = node->children.tail;
1394 if (head == NULL || tail == NULL) {
1396 problem("tail is NULL, but head is not");
1398 problem("head is NULL, but tail is not");
1401 JsonNode *last = NULL;
1403 if (head->prev != NULL)
1404 problem("First child's prev pointer is not NULL");
1406 for (child = head; child != NULL; last = child, child = child->next) {
1408 problem("node is its own child");
1409 if (child->next == child)
1410 problem("child->next == child (cycle)");
1411 if (child->next == head)
1412 problem("child->next == head (cycle)");
1414 if (child->parent != node)
1415 problem("child does not point back to parent");
1416 if (child->next != NULL && child->next->prev != child)
1417 problem("child->next does not point back to child");
1419 if (node->tag == JSON_ARRAY && child->key != NULL)
1420 problem("Array element's key is not NULL");
1421 if (node->tag == JSON_OBJECT && child->key == NULL)
1422 problem("Object member's key is NULL");
1424 if (!json_check(child, errmsg))
1429 problem("tail does not match pointer found by starting at head and following next links");