1 | /*
|
---|
2 | SPDX-License-Identifier: MIT
|
---|
3 |
|
---|
4 | Parson ( http://kgabis.github.com/parson/ )
|
---|
5 | Copyright (c) 2012 - 2019 Krzysztof Gabis
|
---|
6 |
|
---|
7 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
8 | of this software and associated documentation files (the "Software"), to deal
|
---|
9 | in the Software without restriction, including without limitation the rights
|
---|
10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
11 | copies of the Software, and to permit persons to whom the Software is
|
---|
12 | furnished to do so, subject to the following conditions:
|
---|
13 |
|
---|
14 | The above copyright notice and this permission notice shall be included in
|
---|
15 | all copies or substantial portions of the Software.
|
---|
16 |
|
---|
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
23 | THE SOFTWARE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef parson_parson_h
|
---|
27 | #define parson_parson_h
|
---|
28 |
|
---|
29 | #ifdef __cplusplus
|
---|
30 | extern "C"
|
---|
31 | {
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <stddef.h> /* size_t */
|
---|
35 |
|
---|
36 | /* Types and enums */
|
---|
37 | typedef struct json_object_t JSON_Object;
|
---|
38 | typedef struct json_array_t JSON_Array;
|
---|
39 | typedef struct json_value_t JSON_Value;
|
---|
40 |
|
---|
41 | enum json_value_type {
|
---|
42 | JSONError = -1,
|
---|
43 | JSONNull = 1,
|
---|
44 | JSONString = 2,
|
---|
45 | JSONNumber = 3,
|
---|
46 | JSONObject = 4,
|
---|
47 | JSONArray = 5,
|
---|
48 | JSONBoolean = 6
|
---|
49 | };
|
---|
50 | typedef int JSON_Value_Type;
|
---|
51 |
|
---|
52 | enum json_result_t {
|
---|
53 | JSONSuccess = 0,
|
---|
54 | JSONFailure = -1
|
---|
55 | };
|
---|
56 | typedef int JSON_Status;
|
---|
57 |
|
---|
58 | typedef void * (*JSON_Malloc_Function)(size_t);
|
---|
59 | typedef void (*JSON_Free_Function)(void *);
|
---|
60 |
|
---|
61 | /* Call only once, before calling any other function from parson API. If not called, malloc and free
|
---|
62 | from stdlib will be used for all allocations */
|
---|
63 | void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun);
|
---|
64 |
|
---|
65 | /* Sets if slashes should be escaped or not when serializing JSON. By default slashes are escaped.
|
---|
66 | This function sets a global setting and is not thread safe. */
|
---|
67 | void json_set_escape_slashes(int escape_slashes);
|
---|
68 |
|
---|
69 | /* Parses first JSON value in a file, returns NULL in case of error */
|
---|
70 | JSON_Value * json_parse_file(const char *filename);
|
---|
71 |
|
---|
72 | /* Parses first JSON value in a file and ignores comments (/ * * / and //),
|
---|
73 | returns NULL in case of error */
|
---|
74 | JSON_Value * json_parse_file_with_comments(const char *filename);
|
---|
75 |
|
---|
76 | /* Parses first JSON value in a string, returns NULL in case of error */
|
---|
77 | JSON_Value * json_parse_string(const char *string);
|
---|
78 |
|
---|
79 | /* Parses first JSON value in a string and ignores comments (/ * * / and //),
|
---|
80 | returns NULL in case of error */
|
---|
81 | JSON_Value * json_parse_string_with_comments(const char *string);
|
---|
82 |
|
---|
83 | /* Serialization */
|
---|
84 | size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */
|
---|
85 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
|
---|
86 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename);
|
---|
87 | char * json_serialize_to_string(const JSON_Value *value);
|
---|
88 |
|
---|
89 | /* Pretty serialization */
|
---|
90 | size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */
|
---|
91 | JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
|
---|
92 | JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename);
|
---|
93 | char * json_serialize_to_string_pretty(const JSON_Value *value);
|
---|
94 |
|
---|
95 | void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */
|
---|
96 |
|
---|
97 | /* Comparing */
|
---|
98 | int json_value_equals(const JSON_Value *a, const JSON_Value *b);
|
---|
99 |
|
---|
100 | /* Validation
|
---|
101 | This is *NOT* JSON Schema. It validates json by checking if object have identically
|
---|
102 | named fields with matching types.
|
---|
103 | For example schema {"name":"", "age":0} will validate
|
---|
104 | {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"},
|
---|
105 | but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}.
|
---|
106 | In case of arrays, only first value in schema is checked against all values in tested array.
|
---|
107 | Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays,
|
---|
108 | null validates values of every type.
|
---|
109 | */
|
---|
110 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value);
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * JSON Object
|
---|
114 | */
|
---|
115 | JSON_Value * json_object_get_value (const JSON_Object *object, const char *name);
|
---|
116 | const char * json_object_get_string (const JSON_Object *object, const char *name);
|
---|
117 | JSON_Object * json_object_get_object (const JSON_Object *object, const char *name);
|
---|
118 | JSON_Array * json_object_get_array (const JSON_Object *object, const char *name);
|
---|
119 | double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
|
---|
120 | int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
|
---|
121 |
|
---|
122 | /* dotget functions enable addressing values with dot notation in nested objects,
|
---|
123 | just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
|
---|
124 | Because valid names in JSON can contain dots, some values may be inaccessible
|
---|
125 | this way. */
|
---|
126 | JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name);
|
---|
127 | const char * json_object_dotget_string (const JSON_Object *object, const char *name);
|
---|
128 | JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
|
---|
129 | JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name);
|
---|
130 | double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
|
---|
131 | int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
|
---|
132 |
|
---|
133 | /* Functions to get available names */
|
---|
134 | size_t json_object_get_count (const JSON_Object *object);
|
---|
135 | const char * json_object_get_name (const JSON_Object *object, size_t index);
|
---|
136 | JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index);
|
---|
137 | JSON_Value * json_object_get_wrapping_value(const JSON_Object *object);
|
---|
138 |
|
---|
139 | /* Functions to check if object has a value with a specific name. Returned value is 1 if object has
|
---|
140 | * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */
|
---|
141 | int json_object_has_value (const JSON_Object *object, const char *name);
|
---|
142 | int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
|
---|
143 |
|
---|
144 | int json_object_dothas_value (const JSON_Object *object, const char *name);
|
---|
145 | int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
|
---|
146 |
|
---|
147 | /* Creates new name-value pair or frees and replaces old value with a new one.
|
---|
148 | * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */
|
---|
149 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
|
---|
150 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
|
---|
151 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
|
---|
152 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
|
---|
153 | JSON_Status json_object_set_null(JSON_Object *object, const char *name);
|
---|
154 |
|
---|
155 | /* Works like dotget functions, but creates whole hierarchy if necessary.
|
---|
156 | * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */
|
---|
157 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value);
|
---|
158 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string);
|
---|
159 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number);
|
---|
160 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean);
|
---|
161 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name);
|
---|
162 |
|
---|
163 | /* Frees and removes name-value pair */
|
---|
164 | JSON_Status json_object_remove(JSON_Object *object, const char *name);
|
---|
165 |
|
---|
166 | /* Works like dotget function, but removes name-value pair only on exact match. */
|
---|
167 | JSON_Status json_object_dotremove(JSON_Object *object, const char *key);
|
---|
168 |
|
---|
169 | /* Removes all name-value pairs in object */
|
---|
170 | JSON_Status json_object_clear(JSON_Object *object);
|
---|
171 |
|
---|
172 | /*
|
---|
173 | *JSON Array
|
---|
174 | */
|
---|
175 | JSON_Value * json_array_get_value (const JSON_Array *array, size_t index);
|
---|
176 | const char * json_array_get_string (const JSON_Array *array, size_t index);
|
---|
177 | JSON_Object * json_array_get_object (const JSON_Array *array, size_t index);
|
---|
178 | JSON_Array * json_array_get_array (const JSON_Array *array, size_t index);
|
---|
179 | double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */
|
---|
180 | int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */
|
---|
181 | size_t json_array_get_count (const JSON_Array *array);
|
---|
182 | JSON_Value * json_array_get_wrapping_value(const JSON_Array *array);
|
---|
183 |
|
---|
184 | /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
|
---|
185 | * Order of values in array may change during execution. */
|
---|
186 | JSON_Status json_array_remove(JSON_Array *array, size_t i);
|
---|
187 |
|
---|
188 | /* Frees and removes from array value at given index and replaces it with given one.
|
---|
189 | * Does nothing and returns JSONFailure if index doesn't exist.
|
---|
190 | * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */
|
---|
191 | JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value);
|
---|
192 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string);
|
---|
193 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number);
|
---|
194 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean);
|
---|
195 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i);
|
---|
196 |
|
---|
197 | /* Frees and removes all values from array */
|
---|
198 | JSON_Status json_array_clear(JSON_Array *array);
|
---|
199 |
|
---|
200 | /* Appends new value at the end of array.
|
---|
201 | * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */
|
---|
202 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value);
|
---|
203 | JSON_Status json_array_append_string(JSON_Array *array, const char *string);
|
---|
204 | JSON_Status json_array_append_number(JSON_Array *array, double number);
|
---|
205 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean);
|
---|
206 | JSON_Status json_array_append_null(JSON_Array *array);
|
---|
207 |
|
---|
208 | /*
|
---|
209 | *JSON Value
|
---|
210 | */
|
---|
211 | JSON_Value * json_value_init_object (void);
|
---|
212 | JSON_Value * json_value_init_array (void);
|
---|
213 | JSON_Value * json_value_init_string (const char *string); /* copies passed string */
|
---|
214 | JSON_Value * json_value_init_number (double number);
|
---|
215 | JSON_Value * json_value_init_boolean(int boolean);
|
---|
216 | JSON_Value * json_value_init_null (void);
|
---|
217 | JSON_Value * json_value_deep_copy (const JSON_Value *value);
|
---|
218 | void json_value_free (JSON_Value *value);
|
---|
219 |
|
---|
220 | JSON_Value_Type json_value_get_type (const JSON_Value *value);
|
---|
221 | JSON_Object * json_value_get_object (const JSON_Value *value);
|
---|
222 | JSON_Array * json_value_get_array (const JSON_Value *value);
|
---|
223 | const char * json_value_get_string (const JSON_Value *value);
|
---|
224 | double json_value_get_number (const JSON_Value *value);
|
---|
225 | int json_value_get_boolean(const JSON_Value *value);
|
---|
226 | JSON_Value * json_value_get_parent (const JSON_Value *value);
|
---|
227 |
|
---|
228 | /* Same as above, but shorter */
|
---|
229 | JSON_Value_Type json_type (const JSON_Value *value);
|
---|
230 | JSON_Object * json_object (const JSON_Value *value);
|
---|
231 | JSON_Array * json_array (const JSON_Value *value);
|
---|
232 | const char * json_string (const JSON_Value *value);
|
---|
233 | double json_number (const JSON_Value *value);
|
---|
234 | int json_boolean(const JSON_Value *value);
|
---|
235 |
|
---|
236 | #ifdef __cplusplus
|
---|
237 | }
|
---|
238 | #endif
|
---|
239 |
|
---|
240 | #endif
|
---|