]> git.ozlabs.org Git - ccan/blob - ccan/json/json.h
ttxml: removed cruft from tests
[ccan] / ccan / json / json.h
1 /*
2   Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com)
3   All rights reserved.
4
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:
11
12   The above copyright notice and this permission notice shall be included in
13   all copies or substantial portions of the Software.
14
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
21   THE SOFTWARE.
22 */
23
24 #ifndef CCAN_JSON_H
25 #define CCAN_JSON_H
26
27 #include <stdbool.h>
28 #include <stddef.h>
29
30 typedef enum {
31         JSON_NULL,
32         JSON_BOOL,
33         JSON_STRING,
34         JSON_NUMBER,
35         JSON_ARRAY,
36         JSON_OBJECT,
37 } JsonTag;
38
39 typedef struct JsonNode JsonNode;
40
41 struct JsonNode
42 {
43         /* only if parent is an object or array (NULL otherwise) */
44         JsonNode *parent;
45         JsonNode *prev, *next;
46         
47         /* only if parent is an object (NULL otherwise) */
48         char *key; /* Must be valid UTF-8. */
49         
50         JsonTag tag;
51         union {
52                 /* JSON_BOOL */
53                 bool bool_;
54                 
55                 /* JSON_STRING */
56                 char *string_; /* Must be valid UTF-8. */
57                 
58                 /* JSON_NUMBER */
59                 double number_;
60                 
61                 /* JSON_ARRAY */
62                 /* JSON_OBJECT */
63                 struct {
64                         JsonNode *head, *tail;
65                 } children;
66         };
67 };
68
69 /*** Encoding, decoding, and validation ***/
70
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);
76
77 bool        json_validate       (const char *json);
78
79 /*** Lookup and traversal ***/
80
81 JsonNode   *json_find_element   (JsonNode *array, int index);
82 JsonNode   *json_find_member    (JsonNode *object, const char *key);
83
84 JsonNode   *json_first_child    (const JsonNode *node);
85
86 #define json_foreach(i, object_or_array)            \
87         for ((i) = json_first_child(object_or_array);   \
88                  (i) != NULL;                               \
89                  (i) = (i)->next)
90
91 /*** Construction and manipulation ***/
92
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);
99
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);
104
105 void json_remove_from_parent(JsonNode *node);
106
107 /*** Debugging ***/
108
109 /*
110  * Look for structure and encoding problems in a JsonNode or its descendents.
111  *
112  * If a problem is detected, return false, writing a description of the problem
113  * to errmsg (unless errmsg is NULL).
114  */
115 bool json_check(const JsonNode *node, char errmsg[256]);
116
117 #endif