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
32 #define out_of_memory() do { \
33 fprintf(stderr, "Out of memory.\n"); \
37 /* Sadly, strdup is not portable. */
38 static char *json_strdup(const char *str)
40 char *ret = (char*) malloc(strlen(str) + 1);
56 static void sb_init(SB *sb)
58 sb->start = (char*) malloc(17);
59 if (sb->start == NULL)
62 sb->end = sb->start + 16;
65 /* sb and need may be evaluated multiple times. */
66 #define sb_need(sb, need) do { \
67 if ((sb)->end - (sb)->cur < (need)) \
71 static void sb_grow(SB *sb, int need)
73 size_t length = sb->cur - sb->start;
74 size_t alloc = sb->end - sb->start;
78 } while (alloc < length + need);
80 sb->start = (char*) realloc(sb->start, alloc + 1);
81 if (sb->start == NULL)
83 sb->cur = sb->start + length;
84 sb->end = sb->start + alloc;
87 static void sb_put(SB *sb, const char *bytes, int count)
90 memcpy(sb->cur, bytes, count);
94 #define sb_putc(sb, c) do { \
95 if ((sb)->cur >= (sb)->end) \
100 static void sb_puts(SB *sb, const char *str)
102 sb_put(sb, str, strlen(str));
105 static char *sb_finish(SB *sb)
108 assert(sb->start <= sb->cur && strlen(sb->start) == (size_t)(sb->cur - sb->start));
112 static void sb_free(SB *sb)
118 * Unicode helper functions
120 * These are taken from the ccan/charset module and customized a bit.
121 * Putting them here means the compiler can (choose to) inline them,
122 * and it keeps ccan/json from having a dependency.
126 * Type for Unicode codepoints.
127 * We need our own because wchar_t might be 16 bits.
129 typedef uint32_t uchar_t;
132 * Validate a single UTF-8 character starting at @s.
133 * The string must be null-terminated.
135 * If it's valid, return its length (1 thru 4).
136 * If it's invalid or clipped, return 0.
138 * This function implements the syntax given in RFC3629, which is
139 * the same as that given in The Unicode Standard, Version 6.0.
141 * It has the following properties:
143 * * All codepoints U+0000..U+10FFFF may be encoded,
144 * except for U+D800..U+DFFF, which are reserved
145 * for UTF-16 surrogate pair encoding.
146 * * UTF-8 byte sequences longer than 4 bytes are not permitted,
147 * as they exceed the range of Unicode.
148 * * The sixty-six Unicode "non-characters" are permitted
149 * (namely, U+FDD0..U+FDEF, U+xxFFFE, and U+xxFFFF).
151 static int utf8_validate_cz(const char *s)
153 unsigned char c = *s++;
155 if (c <= 0x7F) { /* 00..7F */
157 } else if (c <= 0xC1) { /* 80..C1 */
158 /* Disallow overlong 2-byte sequence. */
160 } else if (c <= 0xDF) { /* C2..DF */
161 /* Make sure subsequent byte is in the range 0x80..0xBF. */
162 if (((unsigned char)*s++ & 0xC0) != 0x80)
166 } else if (c <= 0xEF) { /* E0..EF */
167 /* Disallow overlong 3-byte sequence. */
168 if (c == 0xE0 && (unsigned char)*s < 0xA0)
171 /* Disallow U+D800..U+DFFF. */
172 if (c == 0xED && (unsigned char)*s > 0x9F)
175 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
176 if (((unsigned char)*s++ & 0xC0) != 0x80)
178 if (((unsigned char)*s++ & 0xC0) != 0x80)
182 } else if (c <= 0xF4) { /* F0..F4 */
183 /* Disallow overlong 4-byte sequence. */
184 if (c == 0xF0 && (unsigned char)*s < 0x90)
187 /* Disallow codepoints beyond U+10FFFF. */
188 if (c == 0xF4 && (unsigned char)*s > 0x8F)
191 /* Make sure subsequent bytes are in the range 0x80..0xBF. */
192 if (((unsigned char)*s++ & 0xC0) != 0x80)
194 if (((unsigned char)*s++ & 0xC0) != 0x80)
196 if (((unsigned char)*s++ & 0xC0) != 0x80)
200 } else { /* F5..FF */
205 /* Validate a null-terminated UTF-8 string. */
206 static bool utf8_validate(const char *s)
210 for (; *s != 0; s += len) {
211 len = utf8_validate_cz(s);
220 * Read a single UTF-8 character starting at @s,
221 * returning the length, in bytes, of the character read.
223 * This function assumes input is valid UTF-8,
224 * and that there are enough characters in front of @s.
226 static int utf8_read_char(const char *s, uchar_t *out)
228 const unsigned char *c = (const unsigned char*) s;
230 assert(utf8_validate_cz(s));
236 } else if (c[0] <= 0xDF) {
237 /* C2..DF (unless input is invalid) */
238 *out = ((uchar_t)c[0] & 0x1F) << 6 |
239 ((uchar_t)c[1] & 0x3F);
241 } else if (c[0] <= 0xEF) {
243 *out = ((uchar_t)c[0] & 0xF) << 12 |
244 ((uchar_t)c[1] & 0x3F) << 6 |
245 ((uchar_t)c[2] & 0x3F);
248 /* F0..F4 (unless input is invalid) */
249 *out = ((uchar_t)c[0] & 0x7) << 18 |
250 ((uchar_t)c[1] & 0x3F) << 12 |
251 ((uchar_t)c[2] & 0x3F) << 6 |
252 ((uchar_t)c[3] & 0x3F);
258 * Write a single UTF-8 character to @s,
259 * returning the length, in bytes, of the character written.
261 * @unicode must be U+0000..U+10FFFF, but not U+D800..U+DFFF.
263 * This function will write up to 4 bytes to @out.
265 static int utf8_write_char(uchar_t unicode, char *out)
267 unsigned char *o = (unsigned char*) out;
269 assert(unicode <= 0x10FFFF && !(unicode >= 0xD800 && unicode <= 0xDFFF));
271 if (unicode <= 0x7F) {
275 } else if (unicode <= 0x7FF) {
277 *o++ = 0xC0 | unicode >> 6;
278 *o++ = 0x80 | (unicode & 0x3F);
280 } else if (unicode <= 0xFFFF) {
282 *o++ = 0xE0 | unicode >> 12;
283 *o++ = 0x80 | (unicode >> 6 & 0x3F);
284 *o++ = 0x80 | (unicode & 0x3F);
287 /* U+10000..U+10FFFF */
288 *o++ = 0xF0 | unicode >> 18;
289 *o++ = 0x80 | (unicode >> 12 & 0x3F);
290 *o++ = 0x80 | (unicode >> 6 & 0x3F);
291 *o++ = 0x80 | (unicode & 0x3F);
297 * Compute the Unicode codepoint of a UTF-16 surrogate pair.
299 * @uc should be 0xD800..0xDBFF, and @lc should be 0xDC00..0xDFFF.
300 * If they aren't, this function returns false.
302 static bool from_surrogate_pair(uint16_t uc, uint16_t lc, uchar_t *unicode)
304 if (uc >= 0xD800 && uc <= 0xDBFF && lc >= 0xDC00 && lc <= 0xDFFF) {
305 *unicode = 0x10000 + ((((uchar_t)uc & 0x3FF) << 10) | (lc & 0x3FF));
313 * Construct a UTF-16 surrogate pair given a Unicode codepoint.
315 * @unicode must be U+10000..U+10FFFF.
317 static void to_surrogate_pair(uchar_t unicode, uint16_t *uc, uint16_t *lc)
321 assert(unicode >= 0x10000 && unicode <= 0x10FFFF);
323 n = unicode - 0x10000;
324 *uc = ((n >> 10) & 0x3FF) | 0xD800;
325 *lc = (n & 0x3FF) | 0xDC00;
328 #define is_space(c) ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ')
329 #define is_digit(c) ((c) >= '0' && (c) <= '9')
331 static bool parse_value (const char **sp, JsonNode **out);
332 static bool parse_string (const char **sp, char **out);
333 static bool parse_number (const char **sp, double *out);
334 static bool parse_array (const char **sp, JsonNode **out);
335 static bool parse_object (const char **sp, JsonNode **out);
336 static bool parse_hex16 (const char **sp, uint16_t *out);
338 static bool expect_literal (const char **sp, const char *str);
339 static void skip_space (const char **sp);
341 static void emit_value (SB *out, const JsonNode *node);
342 static void emit_value_indented (SB *out, const JsonNode *node, const char *space, int indent_level);
343 static void emit_string (SB *out, const char *str);
344 static void emit_number (SB *out, double num);
345 static void emit_array (SB *out, const JsonNode *array);
346 static void emit_array_indented (SB *out, const JsonNode *array, const char *space, int indent_level);
347 static void emit_object (SB *out, const JsonNode *object);
348 static void emit_object_indented (SB *out, const JsonNode *object, const char *space, int indent_level);
350 static int write_hex16(char *out, uint16_t val);
352 static JsonNode *mknode(JsonTag tag);
353 static void append_node(JsonNode *parent, JsonNode *child);
354 static void prepend_node(JsonNode *parent, JsonNode *child);
355 static void append_member(JsonNode *object, char *key, JsonNode *value);
357 /* Assertion-friendly validity checks */
358 static bool tag_is_valid(unsigned int tag);
359 static bool number_is_valid(const char *num);
361 JsonNode *json_decode(const char *json)
363 const char *s = json;
367 if (!parse_value(&s, &ret))
379 char *json_encode(const JsonNode *node)
381 return json_stringify(node, NULL);
384 char *json_encode_string(const char *str)
389 emit_string(&sb, str);
391 return sb_finish(&sb);
394 char *json_stringify(const JsonNode *node, const char *space)
400 emit_value_indented(&sb, node, space, 0);
402 emit_value(&sb, node);
404 return sb_finish(&sb);
407 void json_delete(JsonNode *node)
410 json_remove_from_parent(node);
419 JsonNode *child, *next;
420 for (child = node->children.head; child != NULL; child = next) {
433 bool json_validate(const char *json)
435 const char *s = json;
438 if (!parse_value(&s, NULL))
448 JsonNode *json_find_element(JsonNode *array, int index)
453 if (array == NULL || array->tag != JSON_ARRAY)
456 json_foreach(element, array) {
465 JsonNode *json_find_member(JsonNode *object, const char *name)
469 if (object == NULL || object->tag != JSON_OBJECT)
472 json_foreach(member, object)
473 if (strcmp(member->key, name) == 0)
479 JsonNode *json_first_child(const JsonNode *node)
481 if (node != NULL && (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT))
482 return node->children.head;
486 static JsonNode *mknode(JsonTag tag)
488 JsonNode *ret = (JsonNode*) calloc(1, sizeof(JsonNode));
495 JsonNode *json_mknull(void)
497 return mknode(JSON_NULL);
500 JsonNode *json_mkbool(bool b)
502 JsonNode *ret = mknode(JSON_BOOL);
507 static JsonNode *mkstring(char *s)
509 JsonNode *ret = mknode(JSON_STRING);
514 JsonNode *json_mkstring(const char *s)
516 return mkstring(json_strdup(s));
519 JsonNode *json_mknumber(double n)
521 JsonNode *node = mknode(JSON_NUMBER);
526 JsonNode *json_mkarray(void)
528 return mknode(JSON_ARRAY);
531 JsonNode *json_mkobject(void)
533 return mknode(JSON_OBJECT);
536 static void append_node(JsonNode *parent, JsonNode *child)
538 child->parent = parent;
539 child->prev = parent->children.tail;
542 if (parent->children.tail != NULL)
543 parent->children.tail->next = child;
545 parent->children.head = child;
546 parent->children.tail = child;
549 static void prepend_node(JsonNode *parent, JsonNode *child)
551 child->parent = parent;
553 child->next = parent->children.head;
555 if (parent->children.head != NULL)
556 parent->children.head->prev = child;
558 parent->children.tail = child;
559 parent->children.head = child;
562 static void append_member(JsonNode *object, char *key, JsonNode *value)
565 append_node(object, value);
568 void json_append_element(JsonNode *array, JsonNode *element)
570 assert(array->tag == JSON_ARRAY);
571 assert(element->parent == NULL);
573 append_node(array, element);
576 void json_prepend_element(JsonNode *array, JsonNode *element)
578 assert(array->tag == JSON_ARRAY);
579 assert(element->parent == NULL);
581 prepend_node(array, element);
584 void json_append_member(JsonNode *object, const char *key, JsonNode *value)
586 assert(object->tag == JSON_OBJECT);
587 assert(value->parent == NULL);
589 append_member(object, json_strdup(key), value);
592 void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
594 assert(object->tag == JSON_OBJECT);
595 assert(value->parent == NULL);
597 value->key = json_strdup(key);
598 prepend_node(object, value);
601 void json_remove_from_parent(JsonNode *node)
603 JsonNode *parent = node->parent;
605 if (parent != NULL) {
606 if (node->prev != NULL)
607 node->prev->next = node->next;
609 parent->children.head = node->next;
610 if (node->next != NULL)
611 node->next->prev = node->prev;
613 parent->children.tail = node->prev;
618 node->prev = node->next = NULL;
623 static bool parse_value(const char **sp, JsonNode **out)
629 if (expect_literal(&s, "null")) {
631 *out = json_mknull();
638 if (expect_literal(&s, "false")) {
640 *out = json_mkbool(false);
647 if (expect_literal(&s, "true")) {
649 *out = json_mkbool(true);
657 if (parse_string(&s, out ? &str : NULL)) {
659 *out = mkstring(str);
667 if (parse_array(&s, out)) {
674 if (parse_object(&s, out)) {
682 if (parse_number(&s, out ? &num : NULL)) {
684 *out = json_mknumber(num);
693 static bool parse_array(const char **sp, JsonNode **out)
696 JsonNode *ret = out ? json_mkarray() : NULL;
709 if (!parse_value(&s, out ? &element : NULL))
714 json_append_element(ret, element);
737 static bool parse_object(const char **sp, JsonNode **out)
740 JsonNode *ret = out ? json_mkobject() : NULL;
754 if (!parse_string(&s, out ? &key : NULL))
759 goto failure_free_key;
762 if (!parse_value(&s, out ? &value : NULL))
763 goto failure_free_key;
767 append_member(ret, key, value);
793 bool parse_string(const char **sp, char **out)
797 char throwaway_buffer[4];
798 /* enough space for a UTF-8 character */
809 b = throwaway_buffer;
813 unsigned char c = *s++;
815 /* Parse next character, and write it to b. */
844 if (!parse_hex16(&s, &uc))
847 if (uc >= 0xD800 && uc <= 0xDFFF) {
848 /* Handle UTF-16 surrogate pair. */
849 if (*s++ != '\\' || *s++ != 'u' || !parse_hex16(&s, &lc))
850 goto failed; /* Incomplete surrogate pair. */
851 if (!from_surrogate_pair(uc, lc, &unicode))
852 goto failed; /* Invalid surrogate pair. */
853 } else if (uc == 0) {
854 /* Disallow "\u0000". */
860 b += utf8_write_char(unicode, b);
867 } else if (c <= 0x1F) {
868 /* Control characters are not allowed in string literals. */
871 /* Validate and echo a UTF-8 character. */
875 len = utf8_validate_cz(s);
877 goto failed; /* Invalid UTF-8 character. */
884 * Update sb to know about the new bytes,
885 * and set up b to write another character.
892 b = throwaway_buffer;
898 *out = sb_finish(&sb);
909 * The JSON spec says that a number shall follow this precise pattern
910 * (spaces and quotes added for readability):
911 * '-'? (0 | [1-9][0-9]*) ('.' [0-9]+)? ([Ee] [+-]? [0-9]+)?
913 * However, some JSON parsers are more liberal. For instance, PHP accepts
914 * '.5' and '1.'. JSON.parse accepts '+3'.
916 * This function takes the strict approach.
918 bool parse_number(const char **sp, double *out)
926 /* (0 | [1-9][0-9]*) */
934 } while (is_digit(*s));
944 } while (is_digit(*s));
947 /* ([Ee] [+-]? [0-9]+)? */
948 if (*s == 'E' || *s == 'e') {
950 if (*s == '+' || *s == '-')
956 } while (is_digit(*s));
960 *out = strtod(*sp, NULL);
966 static void skip_space(const char **sp)
974 static void emit_value(SB *out, const JsonNode *node)
976 assert(tag_is_valid(node->tag));
979 sb_puts(out, "null");
982 sb_puts(out, node->bool_ ? "true" : "false");
985 emit_string(out, node->string_);
988 emit_number(out, node->number_);
991 emit_array(out, node);
994 emit_object(out, node);
1001 void emit_value_indented(SB *out, const JsonNode *node, const char *space, int indent_level)
1003 assert(tag_is_valid(node->tag));
1004 switch (node->tag) {
1006 sb_puts(out, "null");
1009 sb_puts(out, node->bool_ ? "true" : "false");
1012 emit_string(out, node->string_);
1015 emit_number(out, node->number_);
1018 emit_array_indented(out, node, space, indent_level);
1021 emit_object_indented(out, node, space, indent_level);
1028 static void emit_array(SB *out, const JsonNode *array)
1030 const JsonNode *element;
1033 json_foreach(element, array) {
1034 emit_value(out, element);
1035 if (element->next != NULL)
1041 static void emit_array_indented(SB *out, const JsonNode *array, const char *space, int indent_level)
1043 const JsonNode *element = array->children.head;
1046 if (element == NULL) {
1051 sb_puts(out, "[\n");
1052 while (element != NULL) {
1053 for (i = 0; i < indent_level + 1; i++)
1054 sb_puts(out, space);
1055 emit_value_indented(out, element, space, indent_level + 1);
1057 element = element->next;
1058 sb_puts(out, element != NULL ? ",\n" : "\n");
1060 for (i = 0; i < indent_level; i++)
1061 sb_puts(out, space);
1065 static void emit_object(SB *out, const JsonNode *object)
1067 const JsonNode *member;
1070 json_foreach(member, object) {
1071 emit_string(out, member->key);
1073 emit_value(out, member);
1074 if (member->next != NULL)
1080 static void emit_object_indented(SB *out, const JsonNode *object, const char *space, int indent_level)
1082 const JsonNode *member = object->children.head;
1085 if (member == NULL) {
1090 sb_puts(out, "{\n");
1091 while (member != NULL) {
1092 for (i = 0; i < indent_level + 1; i++)
1093 sb_puts(out, space);
1094 emit_string(out, member->key);
1096 emit_value_indented(out, member, space, indent_level + 1);
1098 member = member->next;
1099 sb_puts(out, member != NULL ? ",\n" : "\n");
1101 for (i = 0; i < indent_level; i++)
1102 sb_puts(out, space);
1106 void emit_string(SB *out, const char *str)
1108 bool escape_unicode = false;
1109 const char *s = str;
1112 assert(utf8_validate(str));
1115 * 14 bytes is enough space to write up to two
1116 * \uXXXX escapes and two quotation marks.
1123 unsigned char c = *s++;
1125 /* Encode the next character, and write it to b. */
1159 len = utf8_validate_cz(s);
1163 * Handle invalid UTF-8 character gracefully in production
1164 * by writing a replacement character (U+FFFD)
1165 * and skipping a single byte.
1167 * This should never happen when assertions are enabled
1168 * due to the assertion at the beginning of this function.
1171 if (escape_unicode) {
1172 strcpy(b, "\\uFFFD");
1180 } else if (c < 0x1F || (c >= 0x80 && escape_unicode)) {
1181 /* Encode using \u.... */
1184 s += utf8_read_char(s, &unicode);
1186 if (unicode <= 0xFFFF) {
1189 b += write_hex16(b, unicode);
1191 /* Produce a surrogate pair. */
1193 assert(unicode <= 0x10FFFF);
1194 to_surrogate_pair(unicode, &uc, &lc);
1197 b += write_hex16(b, uc);
1200 b += write_hex16(b, lc);
1203 /* Write the character directly. */
1213 * Update *out to know about the new bytes,
1214 * and set up b to write another encoded character.
1225 static void emit_number(SB *out, double num)
1228 * This isn't exactly how JavaScript renders numbers,
1229 * but it should produce valid JSON for reasonable numbers
1230 * preserve precision well enough, and avoid some oddities
1231 * like 0.3 -> 0.299999999999999988898 .
1234 sprintf(buf, "%.16g", num);
1236 if (number_is_valid(buf))
1239 sb_puts(out, "null");
1242 static bool tag_is_valid(unsigned int tag)
1244 return (/* tag >= JSON_NULL && */ tag <= JSON_OBJECT);
1247 static bool number_is_valid(const char *num)
1249 return (parse_number(&num, NULL) && *num == '\0');
1252 static bool expect_literal(const char **sp, const char *str)
1254 const char *s = *sp;
1256 while (*str != '\0')
1265 * Parses exactly 4 hex characters (capital or lowercase).
1266 * Fails if any input chars are not [0-9A-Fa-f].
1268 static bool parse_hex16(const char **sp, uint16_t *out)
1270 const char *s = *sp;
1276 for (i = 0; i < 4; i++) {
1278 if (c >= '0' && c <= '9')
1280 else if (c >= 'A' && c <= 'F')
1282 else if (c >= 'a' && c <= 'f')
1298 * Encodes a 16-bit number into hexadecimal,
1299 * writing exactly 4 hex chars.
1301 static int write_hex16(char *out, uint16_t val)
1303 const char *hex = "0123456789ABCDEF";
1305 *out++ = hex[(val >> 12) & 0xF];
1306 *out++ = hex[(val >> 8) & 0xF];
1307 *out++ = hex[(val >> 4) & 0xF];
1308 *out++ = hex[ val & 0xF];
1313 bool json_check(const JsonNode *node, char errmsg[256])
1315 #define problem(...) do { \
1316 if (errmsg != NULL) \
1317 snprintf(errmsg, 256, __VA_ARGS__); \
1321 if (node->key != NULL && !utf8_validate(node->key))
1322 problem("key contains invalid UTF-8");
1324 if (!tag_is_valid(node->tag))
1325 problem("tag is invalid (%u)", node->tag);
1327 if (node->tag == JSON_BOOL) {
1328 if (node->bool_ != false && node->bool_ != true)
1329 problem("bool_ is neither false (%d) nor true (%d)", (int)false, (int)true);
1330 } else if (node->tag == JSON_STRING) {
1331 if (node->string_ == NULL)
1332 problem("string_ is NULL");
1333 if (!utf8_validate(node->string_))
1334 problem("string_ contains invalid UTF-8");
1335 } else if (node->tag == JSON_ARRAY || node->tag == JSON_OBJECT) {
1336 JsonNode *head = node->children.head;
1337 JsonNode *tail = node->children.tail;
1339 if (head == NULL || tail == NULL) {
1341 problem("tail is NULL, but head is not");
1343 problem("head is NULL, but tail is not");
1346 JsonNode *last = NULL;
1348 if (head->prev != NULL)
1349 problem("First child's prev pointer is not NULL");
1351 for (child = head; child != NULL; last = child, child = child->next) {
1353 problem("node is its own child");
1354 if (child->next == child)
1355 problem("child->next == child (cycle)");
1356 if (child->next == head)
1357 problem("child->next == head (cycle)");
1359 if (child->parent != node)
1360 problem("child does not point back to parent");
1361 if (child->next != NULL && child->next->prev != child)
1362 problem("child->next does not point back to child");
1364 if (node->tag == JSON_ARRAY && child->key != NULL)
1365 problem("Array element's key is not NULL");
1366 if (node->tag == JSON_OBJECT && child->key == NULL)
1367 problem("Object member's key is NULL");
1369 if (!json_check(child, errmsg))
1374 problem("tail does not match pointer found by starting at head and following next links");