1 | /* |
---|
2 | Parson ( http://kgabis.github.com/parson/ ) |
---|
3 | Copyright (c) 2012 - 2014 Krzysztof Gabis |
---|
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 parson_parson_h |
---|
25 | #define parson_parson_h |
---|
26 | |
---|
27 | #ifdef __cplusplus |
---|
28 | extern "C" |
---|
29 | { |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <stddef.h> /* size_t */ |
---|
33 | |
---|
34 | /* Types and enums */ |
---|
35 | typedef struct json_object_t JSON_Object; |
---|
36 | typedef struct json_array_t JSON_Array; |
---|
37 | typedef struct json_value_t JSON_Value; |
---|
38 | |
---|
39 | typedef long long jint; |
---|
40 | |
---|
41 | enum json_value_type { |
---|
42 | JSONError = -1, |
---|
43 | JSONNull = 1, |
---|
44 | JSONString = 2, |
---|
45 | JSONInteger = 7, |
---|
46 | JSONNumber = 3, |
---|
47 | JSONObject = 4, |
---|
48 | JSONArray = 5, |
---|
49 | JSONBoolean = 6 |
---|
50 | }; |
---|
51 | typedef int JSON_Value_Type; |
---|
52 | |
---|
53 | enum json_result_t { |
---|
54 | JSONSuccess = 0, |
---|
55 | JSONFailure = -1 |
---|
56 | }; |
---|
57 | typedef int JSON_Status; |
---|
58 | |
---|
59 | /* Parses first JSON value in a file, returns NULL in case of error */ |
---|
60 | JSON_Value * json_parse_file(const char *filename); |
---|
61 | |
---|
62 | /* Parses first JSON value in a file and ignores comments (/ * * / and //), |
---|
63 | returns NULL in case of error */ |
---|
64 | JSON_Value * json_parse_file_with_comments(const char *filename); |
---|
65 | |
---|
66 | /* Like json_parse_string, but *end is set to wherever it stopped parsing. */ |
---|
67 | JSON_Value * json_parse_first(const char *string, const char **end); |
---|
68 | |
---|
69 | /* Parses first JSON value in a string, returns NULL in case of error */ |
---|
70 | JSON_Value * json_parse_string(const char *string); |
---|
71 | |
---|
72 | /* Parses first JSON value in a string and ignores comments (/ * * / and //), |
---|
73 | returns NULL in case of error */ |
---|
74 | JSON_Value * json_parse_string_with_comments(const char *string); |
---|
75 | |
---|
76 | /* Serialization */ |
---|
77 | size_t json_serialization_size(const JSON_Value *value); |
---|
78 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); |
---|
79 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); |
---|
80 | char * json_serialize_to_string(const JSON_Value *value); |
---|
81 | void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string */ |
---|
82 | |
---|
83 | /* Comparing */ |
---|
84 | int json_value_equals(const JSON_Value *a, const JSON_Value *b); |
---|
85 | |
---|
86 | /* Validation |
---|
87 | This is *NOT* JSON Schema. It validates json by checking if object have identically |
---|
88 | named fields with matching types. |
---|
89 | For example schema {"name":"", "age":0} will validate |
---|
90 | {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, |
---|
91 | but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. |
---|
92 | In case of arrays, only first value in schema is checked against all values in tested array. |
---|
93 | Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, |
---|
94 | null validates values of every type. |
---|
95 | */ |
---|
96 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); |
---|
97 | |
---|
98 | /* |
---|
99 | * JSON Object |
---|
100 | */ |
---|
101 | JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); |
---|
102 | const char * json_object_get_string (const JSON_Object *object, const char *name); |
---|
103 | JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); |
---|
104 | JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); |
---|
105 | jint json_object_get_integer(const JSON_Object *object, const char *name); /* returns 0 on fail */ |
---|
106 | double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ |
---|
107 | int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ |
---|
108 | |
---|
109 | /* dotget functions enable addressing values with dot notation in nested objects, |
---|
110 | just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). |
---|
111 | Because valid names in JSON can contain dots, some values may be inaccessible |
---|
112 | this way. */ |
---|
113 | JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); |
---|
114 | const char * json_object_dotget_string (const JSON_Object *object, const char *name); |
---|
115 | JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); |
---|
116 | JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); |
---|
117 | jint json_object_dotget_integer(const JSON_Object *object, const char *name); /* returns 0 on fail */ |
---|
118 | double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ |
---|
119 | int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ |
---|
120 | |
---|
121 | /* Functions to get available names */ |
---|
122 | size_t json_object_get_count(const JSON_Object *object); |
---|
123 | const char * json_object_get_name (const JSON_Object *object, size_t index); |
---|
124 | int json_object_get_tuple(const JSON_Object *object, size_t index, |
---|
125 | const char **key, const JSON_Value **value); /* 0 == fail */ |
---|
126 | |
---|
127 | /* Creates new name-value pair or frees and replaces old value with new one. */ |
---|
128 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); |
---|
129 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); |
---|
130 | JSON_Status json_object_set_integer(JSON_Object *object, const char *name, jint number); |
---|
131 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); |
---|
132 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); |
---|
133 | JSON_Status json_object_set_null(JSON_Object *object, const char *name); |
---|
134 | |
---|
135 | /* Works like dotget functions, but creates whole hierarchy if necessary. */ |
---|
136 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); |
---|
137 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); |
---|
138 | JSON_Status json_object_dotset_integer(JSON_Object *object, const char *name, jint number); |
---|
139 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); |
---|
140 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); |
---|
141 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); |
---|
142 | |
---|
143 | /* Frees and removes name-value pair */ |
---|
144 | JSON_Status json_object_remove(JSON_Object *object, const char *name); |
---|
145 | |
---|
146 | /* Works like dotget function, but removes name-value pair only on exact match. */ |
---|
147 | JSON_Status json_object_dotremove(JSON_Object *object, const char *key); |
---|
148 | |
---|
149 | /* Removes all name-value pairs in object */ |
---|
150 | JSON_Status json_object_clear(JSON_Object *object); |
---|
151 | |
---|
152 | /* |
---|
153 | *JSON Array |
---|
154 | */ |
---|
155 | JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); |
---|
156 | const char * json_array_get_string (const JSON_Array *array, size_t index); |
---|
157 | JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); |
---|
158 | JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); |
---|
159 | jint json_array_get_integer(const JSON_Array *array, size_t index); /* returns 0 on fail */ |
---|
160 | double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ |
---|
161 | int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ |
---|
162 | size_t json_array_get_count (const JSON_Array *array); |
---|
163 | |
---|
164 | /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. |
---|
165 | * Order of values in array may change during execution. */ |
---|
166 | JSON_Status json_array_remove(JSON_Array *array, size_t i); |
---|
167 | |
---|
168 | /* Frees and removes from array value at given index and replaces it with given one. |
---|
169 | * Does nothing and returns JSONFailure if index doesn't exist. */ |
---|
170 | JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); |
---|
171 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); |
---|
172 | JSON_Status json_array_replace_integer(JSON_Array *array, size_t i, jint number); |
---|
173 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); |
---|
174 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); |
---|
175 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i); |
---|
176 | |
---|
177 | /* Frees and removes all values from array */ |
---|
178 | JSON_Status json_array_clear(JSON_Array *array); |
---|
179 | |
---|
180 | /* Appends new value at the end of array. */ |
---|
181 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); |
---|
182 | JSON_Status json_array_append_string(JSON_Array *array, const char *string); |
---|
183 | JSON_Status json_array_append_integer(JSON_Array *array, jint number); |
---|
184 | JSON_Status json_array_append_number(JSON_Array *array, double number); |
---|
185 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); |
---|
186 | JSON_Status json_array_append_null(JSON_Array *array); |
---|
187 | |
---|
188 | /* |
---|
189 | *JSON Value |
---|
190 | */ |
---|
191 | JSON_Value * json_value_init_object (void); |
---|
192 | JSON_Value * json_value_init_array (void); |
---|
193 | JSON_Value * json_value_init_string (const char *string); /* copies passed string */ |
---|
194 | JSON_Value * json_value_init_integer(jint number); |
---|
195 | JSON_Value * json_value_init_number (double number); |
---|
196 | JSON_Value * json_value_init_boolean(int boolean); |
---|
197 | JSON_Value * json_value_init_null (void); |
---|
198 | JSON_Value * json_value_deep_copy (const JSON_Value *value); |
---|
199 | void json_value_free (JSON_Value *value); |
---|
200 | |
---|
201 | JSON_Value_Type json_value_get_type (const JSON_Value *value); |
---|
202 | JSON_Object * json_value_get_object (const JSON_Value *value); |
---|
203 | JSON_Array * json_value_get_array (const JSON_Value *value); |
---|
204 | const char * json_value_get_string (const JSON_Value *value); |
---|
205 | jint json_value_get_integer(const JSON_Value *value); |
---|
206 | double json_value_get_number (const JSON_Value *value); |
---|
207 | int json_value_get_boolean(const JSON_Value *value); |
---|
208 | |
---|
209 | /* Same as above, but shorter */ |
---|
210 | JSON_Value_Type json_type (const JSON_Value *value); |
---|
211 | JSON_Object * json_object (const JSON_Value *value); |
---|
212 | JSON_Array * json_array (const JSON_Value *value); |
---|
213 | const char * json_string (const JSON_Value *value); |
---|
214 | jint json_integer(const JSON_Value *value); |
---|
215 | double json_number (const JSON_Value *value); |
---|
216 | int json_boolean(const JSON_Value *value); |
---|
217 | |
---|
218 | #ifdef __cplusplus |
---|
219 | } |
---|
220 | #endif |
---|
221 | |
---|
222 | #endif |
---|