1 /*
2  SPDX-License-Identifier: MIT
3 
4  Parson 1.0.2~vifm ( 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 /* Sets if strings should be checked for being UTF-8 encoded.  By default they are checked.
70  This function sets a global setting and is not thread safe. */
71 void json_set_check_strings(int check_strings);
72 
73 /* Parses first JSON value in a file, returns NULL in case of error */
74 JSON_Value * json_parse_file(const char *filename);
75 
76 /* Parses first JSON value in a file and ignores comments (/ * * / and //),
77    returns NULL in case of error */
78 JSON_Value * json_parse_file_with_comments(const char *filename);
79 
80 /*  Parses first JSON value in a string, returns NULL in case of error */
81 JSON_Value * json_parse_string(const char *string);
82 
83 /*  Parses first JSON value in a string and ignores comments (/ * * / and //),
84     returns NULL in case of error */
85 JSON_Value * json_parse_string_with_comments(const char *string);
86 
87 /* Serialization */
88 size_t      json_serialization_size(const JSON_Value *value); /* returns 0 on fail */
89 JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
90 JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename);
91 char *      json_serialize_to_string(const JSON_Value *value);
92 
93 /* Pretty serialization */
94 size_t      json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */
95 JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
96 JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename);
97 char *      json_serialize_to_string_pretty(const JSON_Value *value);
98 
99 void        json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */
100 
101 /* Comparing */
102 int  json_value_equals(const JSON_Value *a, const JSON_Value *b);
103 
104 /* Validation
105    This is *NOT* JSON Schema. It validates json by checking if object have identically
106    named fields with matching types.
107    For example schema {"name":"", "age":0} will validate
108    {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"},
109    but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}.
110    In case of arrays, only first value in schema is checked against all values in tested array.
111    Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays,
112    null validates values of every type.
113  */
114 JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value);
115 
116 /*
117  * JSON Object
118  */
119 JSON_Value  * json_object_get_value  (const JSON_Object *object, const char *name);
120 const char  * json_object_get_string (const JSON_Object *object, const char *name);
121 JSON_Object * json_object_get_object (const JSON_Object *object, const char *name);
122 JSON_Array  * json_object_get_array  (const JSON_Object *object, const char *name);
123 double        json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
124 int           json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
125 
126 /* dotget functions enable addressing values with dot notation in nested objects,
127  just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
128  Because valid names in JSON can contain dots, some values may be inaccessible
129  this way. */
130 JSON_Value  * json_object_dotget_value  (const JSON_Object *object, const char *name);
131 const char  * json_object_dotget_string (const JSON_Object *object, const char *name);
132 JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
133 JSON_Array  * json_object_dotget_array  (const JSON_Object *object, const char *name);
134 double        json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
135 int           json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
136 
137 /* Functions to get available names */
138 size_t        json_object_get_count   (const JSON_Object *object);
139 const char  * json_object_get_name    (const JSON_Object *object, size_t index);
140 JSON_Value  * json_object_get_value_at(const JSON_Object *object, size_t index);
141 JSON_Value  * json_object_get_wrapping_value(const JSON_Object *object);
142 
143 /* Functions to check if object has a value with a specific name. Returned value is 1 if object has
144  * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */
145 int json_object_has_value        (const JSON_Object *object, const char *name);
146 int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
147 
148 int json_object_dothas_value        (const JSON_Object *object, const char *name);
149 int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
150 
151 /* Creates new name-value pair or frees and replaces old value with a new one.
152  * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */
153 JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
154 JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
155 JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
156 JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
157 JSON_Status json_object_set_null(JSON_Object *object, const char *name);
158 
159 /* Works like dotget functions, but creates whole hierarchy if necessary.
160  * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */
161 JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value);
162 JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string);
163 JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number);
164 JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean);
165 JSON_Status json_object_dotset_null(JSON_Object *object, const char *name);
166 
167 /* Frees and removes name-value pair */
168 JSON_Status json_object_remove(JSON_Object *object, const char *name);
169 
170 /* Works like dotget function, but removes name-value pair only on exact match. */
171 JSON_Status json_object_dotremove(JSON_Object *object, const char *key);
172 
173 /* Removes all name-value pairs in object */
174 JSON_Status json_object_clear(JSON_Object *object);
175 
176 /*
177  *JSON Array
178  */
179 JSON_Value  * json_array_get_value  (const JSON_Array *array, size_t index);
180 const char  * json_array_get_string (const JSON_Array *array, size_t index);
181 JSON_Object * json_array_get_object (const JSON_Array *array, size_t index);
182 JSON_Array  * json_array_get_array  (const JSON_Array *array, size_t index);
183 double        json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */
184 int           json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */
185 size_t        json_array_get_count  (const JSON_Array *array);
186 JSON_Value  * json_array_get_wrapping_value(const JSON_Array *array);
187 
188 /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
189  * Order of values in array may change during execution.  */
190 JSON_Status json_array_remove(JSON_Array *array, size_t i);
191 
192 /* Frees and removes from array value at given index and replaces it with given one.
193  * Does nothing and returns JSONFailure if index doesn't exist.
194  * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */
195 JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value);
196 JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string);
197 JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number);
198 JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean);
199 JSON_Status json_array_replace_null(JSON_Array *array, size_t i);
200 
201 /* Frees and removes all values from array */
202 JSON_Status json_array_clear(JSON_Array *array);
203 
204 /* Appends new value at the end of array.
205  * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */
206 JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value);
207 JSON_Status json_array_append_string(JSON_Array *array, const char *string);
208 JSON_Status json_array_append_number(JSON_Array *array, double number);
209 JSON_Status json_array_append_boolean(JSON_Array *array, int boolean);
210 JSON_Status json_array_append_null(JSON_Array *array);
211 
212 /*
213  *JSON Value
214  */
215 JSON_Value * json_value_init_object (void);
216 JSON_Value * json_value_init_array  (void);
217 JSON_Value * json_value_init_string (const char *string); /* copies passed string */
218 JSON_Value * json_value_init_number (double number);
219 JSON_Value * json_value_init_boolean(int boolean);
220 JSON_Value * json_value_init_null   (void);
221 JSON_Value * json_value_deep_copy   (const JSON_Value *value);
222 void         json_value_free        (JSON_Value *value);
223 
224 JSON_Value_Type json_value_get_type   (const JSON_Value *value);
225 JSON_Object *   json_value_get_object (const JSON_Value *value);
226 JSON_Array  *   json_value_get_array  (const JSON_Value *value);
227 const char  *   json_value_get_string (const JSON_Value *value);
228 double          json_value_get_number (const JSON_Value *value);
229 int             json_value_get_boolean(const JSON_Value *value);
230 JSON_Value  *   json_value_get_parent (const JSON_Value *value);
231 
232 /* Same as above, but shorter */
233 JSON_Value_Type json_type   (const JSON_Value *value);
234 JSON_Object *   json_object (const JSON_Value *value);
235 JSON_Array  *   json_array  (const JSON_Value *value);
236 const char  *   json_string (const JSON_Value *value);
237 double          json_number (const JSON_Value *value);
238 int             json_boolean(const JSON_Value *value);
239 
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif
245