Debian Codegraph @

libmowgli-2/src/libmowgli/ext/json.h

raw
1 /*
2 * Copyright (C) 2012 Alex Iadicicco
3 * Rights to this code are as documented in COPYING.
4 *
5 * Structs and functions for interacting with JSON documents from C
6 */
7
8 /* A note about refcounting:
9
10 Since JSON cannot represent recursive structures, it makes sense
11 to use refcounting. If a recursive structure is created then it's
12 guaranteed to be the programmer's fault, and the programmer deserves
13 every memory leak he gets.
14
15 When cared for and fed daily, refcounting is a very nifty tool. Entire
16 structures can be destroyed cleanly by a single mowgli_json_decref. To
17 fully take advantage of this, know the refcounting rules:
18
19 o JSON objects as arguments should NEVER have a reference for
20 the callee. If the callee wants to keep the object somewhere,
21 they will incref themselves.
22
23 o When returning a JSON object, be sure to leave a single reference
24 for the caller. (If you tried to decref before returning, you
25 could potentially drop the refcount to zero.)
26
27 o Newly created structures are the sole exception to the previous
28 rule. This is because it is useful to be able to use constructors
29 as arguments to functions as follows:
30 mowgli_json_object_add(obj, "sum", mowgli_json_create_integer(10));
31 Note that this ONLY applies to the mowgli_json_create_*
32 constructors provided by mowgli.json. The typical case of
33 assigning a new structure to a variable name then looks like this:
34 my_integer = mowgli_json_incref(mowgli_json_create_integer(0));
35
36 Since failure to pay attention to reference counts will cause memory
37 problems, *always double check your reference counting*!
38
39 --aji */
40
41 #ifndef MOWGLI_SRC_LIBMOWGLI_EXT_JSON_H_INCLUDE_GUARD
42 #define MOWGLI_SRC_LIBMOWGLI_EXT_JSON_H_INCLUDE_GUARD 1
43
44 #include "container/list.h"
45 #include "container/patricia.h"
46 #include "core/mowgli_string.h"
47 #include "core/stdinc.h"
48 #include "platform/attributes.h"
49
50 /* Types with public definitons */
51 typedef enum
52 {
53 MOWGLI_JSON_TAG_NULL,
54 MOWGLI_JSON_TAG_BOOLEAN,
55
56 /* JSON does not distinguish between integers or floating points
57 (they are both just number types), but in C we are forced to
58 make the distinction */
59 MOWGLI_JSON_TAG_INTEGER,
60 MOWGLI_JSON_TAG_FLOAT,
61 MOWGLI_JSON_TAG_STRING,
62 MOWGLI_JSON_TAG_ARRAY,
63 MOWGLI_JSON_TAG_OBJECT,
64 } mowgli_json_tag_t;
65 typedef struct _mowgli_json_t mowgli_json_t;
66
67 /* Types whose definitons are kept private */
68 typedef struct _mowgli_json_parse_t mowgli_json_parse_t;
69
70 struct _mowgli_json_t
71 {
72 mowgli_json_tag_t tag;
73 int refcount;
74
75 union
76 {
77 bool v_bool;
78 int v_int;
79 double v_float;
80 mowgli_string_t *v_string;
81 mowgli_list_t *v_array;
82 mowgli_patricia_t *v_object;
83 } v;
84 };
85
86 /* Helper macros useful for cleanly interacting with mowgli_json_t
87 structs. These are just helpers and are not intended to provide a
88 consistent interface. They are as likely to change as the mowgli_json_t
89 struct itself. */
90 #define MOWGLI_JSON_TAG(n) ((n)->tag)
91 #define MOWGLI_JSON_BOOLEAN(n) ((n)->v.v_bool)
92 #define MOWGLI_JSON_INTEGER(n) ((n)->v.v_int)
93 #define MOWGLI_JSON_FLOAT(n) ((n)->v.v_float)
94 #define MOWGLI_JSON_NUMBER(n) ((n)->tag == MOWGLI_JSON_TAG_FLOAT ? \
95 (n)->v.v_float : (n)->v.v_int)
96 #define MOWGLI_JSON_STRING(n) ((n)->v.v_string)
97 #define MOWGLI_JSON_STRING_STR(n) ((n)->v.v_string->str)
98 #define MOWGLI_JSON_STRING_LEN(n) ((n)->v.v_string->pos)
99 #define MOWGLI_JSON_ARRAY(n) ((n)->v.v_array)
100 #define MOWGLI_JSON_OBJECT(n) ((n)->v.v_object)
101
102 /* Users of the JSON parser/formatter are not required to use these
103 constants, but the parser will ALWAYS parse the symbols "null",
104 "true", and "false" into these constants */
105 extern mowgli_json_t *mowgli_json_null;
106 extern mowgli_json_t *mowgli_json_true;
107 extern mowgli_json_t *mowgli_json_false;
108
109 /* These simply return the node argument. If the node was destroyed,
110 the return value will be NULL. Note that in all other cases (invalid
111 refcount, unknown type, etc.) the node will be returned untouched */
112 extern mowgli_json_t *mowgli_json_incref(mowgli_json_t *n);
113 extern mowgli_json_t *mowgli_json_decref(mowgli_json_t *n);
114
115 extern mowgli_json_t *mowgli_json_create_integer(int v_int)
116 MOWGLI_FATTR_MALLOC;
117
118 extern mowgli_json_t *mowgli_json_create_float(double v_float)
119 MOWGLI_FATTR_MALLOC;
120
121 /* #define mowgli_json_create_number(V) _Generic((V), \
122 int: mowgli_json_create_integer, \
123 double: mowgli_json_create_float)(V) */
124 extern mowgli_json_t *mowgli_json_create_string_n(const char *str, size_t len)
125 MOWGLI_FATTR_MALLOC;
126
127 extern mowgli_json_t *mowgli_json_create_string(const char *str)
128 MOWGLI_FATTR_MALLOC;
129
130 extern mowgli_json_t *mowgli_json_create_array(void)
131 MOWGLI_FATTR_MALLOC;
132
133 extern mowgli_json_t *mowgli_json_create_object(void)
134 MOWGLI_FATTR_MALLOC;
135
136 typedef struct _mowgli_json_output_t mowgli_json_output_t;
137
138 struct _mowgli_json_output_t
139 {
140 void (*append)(mowgli_json_output_t *out, const char *str, size_t len);
141 void (*append_char)(mowgli_json_output_t *out, const char c);
142 void *priv;
143 };
144
145 extern void mowgli_json_serialize(mowgli_json_t *n, mowgli_json_output_t *out, int pretty);
146 extern void mowgli_json_serialize_to_string(mowgli_json_t *n, mowgli_string_t *str, int pretty);
147
148 /* extended parsing interface. The 'multidoc' parameter here indicates
149 whether we intend to parse multiple documents from a single data source
150 or not. If you are expecting exactly one complete JSON document,
151 indicate 'false'. */
152 extern mowgli_json_parse_t *mowgli_json_parse_create(bool multidoc)
153 MOWGLI_FATTR_MALLOC;
154
155 extern void mowgli_json_parse_destroy(mowgli_json_parse_t *parse);
156 extern void mowgli_json_parse_reset(mowgli_json_parse_t *parse, bool multidoc);
157 extern void mowgli_json_parse_data(mowgli_json_parse_t *parse, const char *data, size_t len);
158 extern char *mowgli_json_parse_error(mowgli_json_parse_t *parse);
159 extern bool mowgli_json_parse_more(mowgli_json_parse_t *parse);
160 extern mowgli_json_t *mowgli_json_parse_next(mowgli_json_parse_t *parse);
161
162 /* Simple parsing interface. These expect the given data source to
163 represent exactly one complete JSON document */
164 extern mowgli_json_t *mowgli_json_parse_file(const char *path);
165 extern mowgli_json_t *mowgli_json_parse_string(const char *data);
166
167 #endif /* MOWGLI_SRC_LIBMOWGLI_EXT_JSON_H_INCLUDE_GUARD */