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
39 typedef struct JsonNode JsonNode;
43 /* only if parent is an object or array (NULL otherwise) */
45 JsonNode *prev, *next;
47 /* only if parent is an object (NULL otherwise) */
48 char *key; /* Must be valid UTF-8. */
56 char *string_; /* Must be valid UTF-8. */
64 JsonNode *head, *tail;
69 /*** Encoding, decoding, and validation ***/
71 JsonNode *json_decode (const char *json);
72 char *json_encode (const JsonNode *node);
73 char *json_encode_string (const char *str);
74 char *json_stringify (const JsonNode *node, const char *space);
75 void json_delete (JsonNode *node);
77 bool json_validate (const char *json);
79 /*** Lookup and traversal ***/
81 JsonNode *json_find_element (JsonNode *array, int index);
82 JsonNode *json_find_member (JsonNode *object, const char *key);
84 JsonNode *json_first_child (const JsonNode *node);
86 #define json_foreach(i, object_or_array) \
87 for ((i) = json_first_child(object_or_array); \
91 /*** Construction and manipulation ***/
93 JsonNode *json_mknull(void);
94 JsonNode *json_mkbool(bool b);
95 JsonNode *json_mkstring(const char *s);
96 JsonNode *json_mknumber(double n);
97 JsonNode *json_mkarray(void);
98 JsonNode *json_mkobject(void);
100 void json_append_element(JsonNode *array, JsonNode *element);
101 void json_prepend_element(JsonNode *array, JsonNode *element);
102 void json_append_member(JsonNode *object, const char *key, JsonNode *value);
103 void json_prepend_member(JsonNode *object, const char *key, JsonNode *value);
105 void json_remove_from_parent(JsonNode *node);
110 * Look for structure and encoding problems in a JsonNode or its descendents.
112 * If a problem is detected, return false, writing a description of the problem
113 * to errmsg (unless errmsg is NULL).
115 bool json_check(const JsonNode *node, char errmsg[256]);