NCBI C++ ToolKit
parson.h
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2 Parson ( http://kgabis.github.com/parson/ )
3 Copyright (c) 2012 - 2016 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 x_json_object_t x_JSON_Object;
36  typedef struct x_json_array_t x_JSON_Array;
37  typedef struct x_json_value_t x_JSON_Value;
38 
40  JSONError = -1,
41  JSONNull = 1,
45  JSONArray = 5,
46  JSONBoolean = 6
47  };
48  typedef int x_JSON_Value_Type;
49 
52  JSONFailure = -1
53  };
54  typedef int x_JSON_Status;
55 
56  typedef void * (*x_JSON_Malloc_Function)(size_t);
57  typedef void(*x_JSON_Free_Function)(void *);
58 
59  /* Call only once, before calling any other function from parson API. If not called, malloc and free
60  from stdlib will be used for all allocations */
62 
63  /* Parses first JSON value in a file, returns NULL in case of error */
64  x_JSON_Value * x_json_parse_file(const char *filename);
65 
66  /* Parses first JSON value in a file and ignores comments (/ * * / and //),
67  returns NULL in case of error */
68  x_JSON_Value * x_json_parse_file_with_comments(const char *filename);
69 
70  /* Parses first JSON value in a string, returns NULL in case of error */
71  x_JSON_Value * x_json_parse_string(const char *string);
72 
73  /* Parses first JSON value in a string and ignores comments (/ * * / and //),
74  returns NULL in case of error */
75  x_JSON_Value * x_json_parse_string_with_comments(const char *string);
76 
77  /* Serialization */
78  size_t x_json_serialization_size(const x_JSON_Value *value); /* returns 0 on fail */
79  x_JSON_Status x_json_serialize_to_buffer(const x_JSON_Value *value, char *buf, size_t buf_size_in_bytes);
80  x_JSON_Status x_json_serialize_to_file(const x_JSON_Value *value, const char *filename);
82 
83  /* Pretty serialization */
84  size_t x_json_serialization_size_pretty(const x_JSON_Value *value); /* returns 0 on fail */
85  x_JSON_Status x_json_serialize_to_buffer_pretty(const x_JSON_Value *value, char *buf, size_t buf_size_in_bytes);
88 
89  void x_json_free_serialized_string(char *string); /* frees string from x_json_serialize_to_string and x_json_serialize_to_string_pretty */
90 
91  /* Comparing */
92  int x_json_value_equals(const x_JSON_Value *a, const x_JSON_Value *b);
93 
94  /* Validation
95  This is *NOT* JSON Schema. It validates json by checking if object have identically
96  named fields with matching types.
97  For example schema {"name":"", "age":0} will validate
98  {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"},
99  but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}.
100  In case of arrays, only first value in schema is checked against all values in tested array.
101  Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays,
102  null validates values of every type.
103  */
105 
106  /*
107  * JSON Object
108  */
109  x_JSON_Value * x_json_object_get_value(const x_JSON_Object *object, const char *name);
110  const char * x_json_object_get_string(const x_JSON_Object *object, const char *name);
111  x_JSON_Object * x_json_object_get_object(const x_JSON_Object *object, const char *name);
112  x_JSON_Array * x_json_object_get_array(const x_JSON_Object *object, const char *name);
113  double x_json_object_get_number(const x_JSON_Object *object, const char *name); /* returns 0 on fail */
114  int x_json_object_get_boolean(const x_JSON_Object *object, const char *name); /* returns -1 on fail */
115 
116  /* dotget functions enable addressing values with dot notation in nested objects,
117  just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
118  Because valid names in JSON can contain dots, some values may be inaccessible
119  this way. */
120  x_JSON_Value * x_json_object_dotget_value(const x_JSON_Object *object, const char *name);
121  const char * x_json_object_dotget_string(const x_JSON_Object *object, const char *name);
122  x_JSON_Object * x_json_object_dotget_object(const x_JSON_Object *object, const char *name);
123  x_JSON_Array * x_json_object_dotget_array(const x_JSON_Object *object, const char *name);
124  double x_json_object_dotget_number(const x_JSON_Object *object, const char *name); /* returns 0 on fail */
125  int x_json_object_dotget_boolean(const x_JSON_Object *object, const char *name); /* returns -1 on fail */
126 
127  /* Functions to get available names */
128  size_t x_json_object_get_count(const x_JSON_Object *object);
129  const char * x_json_object_get_name(const x_JSON_Object *object, size_t index);
130  x_JSON_Value * x_json_object_get_value_at(const x_JSON_Object *object, size_t index);
131 
132  /* Functions to check if object has a value with a specific name. Returned value is 1 if object has
133  * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */
134  int x_json_object_has_value(const x_JSON_Object *object, const char *name);
135  int x_json_object_has_value_of_type(const x_JSON_Object *object, const char *name, x_JSON_Value_Type type);
136 
137  int x_json_object_dothas_value(const x_JSON_Object *object, const char *name);
138  int x_json_object_dothas_value_of_type(const x_JSON_Object *object, const char *name, x_JSON_Value_Type type);
139 
140  /* Creates new name-value pair or frees and replaces old value with a new one.
141  * x_json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */
143  x_JSON_Status x_json_object_set_string(x_JSON_Object *object, const char *name, const char *string);
144  x_JSON_Status x_json_object_set_number(x_JSON_Object *object, const char *name, double number);
145  x_JSON_Status x_json_object_set_boolean(x_JSON_Object *object, const char *name, int boolean);
146  x_JSON_Status x_json_object_set_null(x_JSON_Object *object, const char *name);
147 
148  /* Works like dotget functions, but creates whole hierarchy if necessary.
149  * x_json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */
151  x_JSON_Status x_json_object_dotset_string(x_JSON_Object *object, const char *name, const char *string);
152  x_JSON_Status x_json_object_dotset_number(x_JSON_Object *object, const char *name, double number);
153  x_JSON_Status x_json_object_dotset_boolean(x_JSON_Object *object, const char *name, int boolean);
154  x_JSON_Status x_json_object_dotset_null(x_JSON_Object *object, const char *name);
155 
156  /* Frees and removes name-value pair */
157  x_JSON_Status x_json_object_remove(x_JSON_Object *object, const char *name);
158 
159  /* Works like dotget function, but removes name-value pair only on exact match. */
161 
162  /* Removes all name-value pairs in object */
164 
165  /*
166  *JSON Array
167  */
168  x_JSON_Value * x_json_array_get_value(const x_JSON_Array *array, size_t index);
169  const char * x_json_array_get_string(const x_JSON_Array *array, size_t index);
171  x_JSON_Array * x_json_array_get_array(const x_JSON_Array *array, size_t index);
172  double x_json_array_get_number(const x_JSON_Array *array, size_t index); /* returns 0 on fail */
173  int x_json_array_get_boolean(const x_JSON_Array *array, size_t index); /* returns -1 on fail */
175 
176  /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
177  * Order of values in array may change during execution. */
179 
180  /* Frees and removes from array value at given index and replaces it with given one.
181  * Does nothing and returns JSONFailure if index doesn't exist.
182  * x_json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */
184  x_JSON_Status x_json_array_replace_string(x_JSON_Array *array, size_t i, const char* string);
188 
189  /* Frees and removes all values from array */
191 
192  /* Appends new value at the end of array.
193  * x_json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */
199 
200  /*
201  *JSON Value
202  */
205  x_JSON_Value * x_json_value_init_string(const char *string); /* copies passed string */
211 
215  const char * x_json_value_get_string(const x_JSON_Value *value);
218 
219  /* Same as above, but shorter */
223  const char * x_json_string(const x_JSON_Value *value);
224  double x_json_number(const x_JSON_Value *value);
225  int x_json_boolean(const x_JSON_Value *value);
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 
231 #endif
static const char * schema
Definition: stats.c:20
char * buf
int i
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
unsigned int a
Definition: ncbi_localip.c:102
x_JSON_Array * x_json_array(const x_JSON_Value *value)
Definition: parson.c:1765
x_JSON_Value * x_json_value_init_boolean(int boolean)
Definition: parson.c:1140
double x_json_object_get_number(const x_JSON_Object *object, const char *name)
Definition: parson.c:936
x_JSON_Status x_json_object_set_value(x_JSON_Object *object, const char *name, x_JSON_Value *value)
Definition: parson.c:1481
x_JSON_Array * x_json_array_get_array(const x_JSON_Array *array, size_t index)
Definition: parson.c:1033
x_JSON_Object * x_json_object_dotget_object(const x_JSON_Object *object, const char *name)
Definition: parson.c:968
x_JSON_Status x_json_object_dotset_value(x_JSON_Object *object, const char *name, x_JSON_Value *value)
Definition: parson.c:1516
void *(* x_JSON_Malloc_Function)(size_t)
Definition: parson.h:56
const char * x_json_string(const x_JSON_Value *value)
Definition: parson.c:1769
const char * x_json_object_get_string(const x_JSON_Object *object, const char *name)
Definition: parson.c:932
void x_json_free_serialized_string(char *string)
Definition: parson.c:1344
int x_json_object_get_boolean(const x_JSON_Object *object, const char *name)
Definition: parson.c:948
x_JSON_Value * x_json_array_get_value(const x_JSON_Array *array, size_t index)
Definition: parson.c:1015
int x_json_value_equals(const x_JSON_Value *a, const x_JSON_Value *b)
Definition: parson.c:1696
x_JSON_Object * x_json_value_get_object(const x_JSON_Value *value)
Definition: parson.c:1050
x_JSON_Value * x_json_object_get_value_at(const x_JSON_Object *object, size_t index)
Definition: parson.c:990
x_JSON_Value * x_json_parse_file_with_comments(const char *filename)
Definition: parson.c:894
x_JSON_Array * x_json_value_get_array(const x_JSON_Value *value)
Definition: parson.c:1054
void x_json_value_free(x_JSON_Value *value)
Definition: parson.c:1070
double x_json_value_get_number(const x_JSON_Value *value)
Definition: parson.c:1062
int x_json_object_has_value(const x_JSON_Object *object, const char *name)
Definition: parson.c:996
x_JSON_Array * x_json_object_dotget_array(const x_JSON_Object *object, const char *name)
Definition: parson.c:972
x_JSON_Status x_json_serialize_to_file_pretty(const x_JSON_Value *value, const char *filename)
Definition: parson.c:1304
int x_json_value_get_boolean(const x_JSON_Value *value)
Definition: parson.c:1066
const char * x_json_object_get_name(const x_JSON_Object *object, size_t index)
Definition: parson.c:984
char * x_json_serialize_to_string(const x_JSON_Value *value)
Definition: parson.c:1269
const char * x_json_value_get_string(const x_JSON_Value *value)
Definition: parson.c:1058
x_JSON_Status x_json_object_set_number(x_JSON_Object *object, const char *name, double number)
Definition: parson.c:1504
x_JSON_Status x_json_array_remove(x_JSON_Array *array, size_t i)
Definition: parson.c:1348
x_JSON_Value * x_json_value_init_object(void)
Definition: parson.c:1087
x_JSON_Value * x_json_value_deep_copy(const x_JSON_Value *value)
Definition: parson.c:1157
size_t x_json_serialization_size_pretty(const x_JSON_Value *value)
Definition: parson.c:1287
double x_json_number(const x_JSON_Value *value)
Definition: parson.c:1773
x_JSON_Status x_json_serialize_to_buffer_pretty(const x_JSON_Value *value, char *buf, size_t buf_size_in_bytes)
Definition: parson.c:1293
x_JSON_Status x_json_array_clear(x_JSON_Array *array)
Definition: parson.c:1420
x_JSON_Status x_json_array_replace_boolean(x_JSON_Array *array, size_t i, int boolean)
Definition: parson.c:1398
x_JSON_Status x_json_validate(const x_JSON_Value *schema, const x_JSON_Value *value)
Definition: parson.c:1642
int x_JSON_Value_Type
Definition: parson.h:48
int x_JSON_Status
Definition: parson.h:54
void x_json_set_allocation_functions(x_JSON_Malloc_Function malloc_fun, x_JSON_Free_Function free_fun)
Definition: parson.c:1781
int x_json_boolean(const x_JSON_Value *value)
Definition: parson.c:1777
char * x_json_serialize_to_string_pretty(const x_JSON_Value *value)
Definition: parson.c:1326
x_JSON_Object * x_json_object(const x_JSON_Value *value)
Definition: parson.c:1761
x_JSON_Status x_json_array_append_value(x_JSON_Array *array, x_JSON_Value *value)
Definition: parson.c:1431
x_JSON_Value * x_json_object_dotget_value(const x_JSON_Object *object, const char *name)
Definition: parson.c:952
double x_json_object_dotget_number(const x_JSON_Object *object, const char *name)
Definition: parson.c:964
x_JSON_Status x_json_array_append_null(x_JSON_Array *array)
Definition: parson.c:1470
x_JSON_Status x_json_object_dotset_number(x_JSON_Object *object, const char *name, double number)
Definition: parson.c:1558
x_JSON_Object * x_json_array_get_object(const x_JSON_Array *array, size_t index)
Definition: parson.c:1029
x_JSON_Value * x_json_value_init_string(const char *string)
Definition: parson.c:1113
const char * x_json_array_get_string(const x_JSON_Array *array, size_t index)
Definition: parson.c:1021
x_JSON_Status x_json_array_replace_value(x_JSON_Array *array, size_t i, x_JSON_Value *value)
Definition: parson.c:1367
x_JSON_Status x_json_array_replace_number(x_JSON_Array *array, size_t i, double number)
Definition: parson.c:1387
x_JSON_Value * x_json_parse_string_with_comments(const char *string)
Definition: parson.c:910
x_JSON_Status x_json_object_dotset_string(x_JSON_Object *object, const char *name, const char *string)
Definition: parson.c:1547
x_JSON_Value * x_json_parse_string(const char *string)
Definition: parson.c:904
x_JSON_Value * x_json_value_init_array(void)
Definition: parson.c:1100
int x_json_object_dotget_boolean(const x_JSON_Object *object, const char *name)
Definition: parson.c:976
x_JSON_Status x_json_object_set_string(x_JSON_Object *object, const char *name, const char *string)
Definition: parson.c:1500
x_JSON_Value * x_json_value_init_null(void)
Definition: parson.c:1149
void(* x_JSON_Free_Function)(void *)
Definition: parson.h:57
x_JSON_Array * x_json_object_get_array(const x_JSON_Object *object, const char *name)
Definition: parson.c:944
x_JSON_Status x_json_array_replace_null(x_JSON_Array *array, size_t i)
Definition: parson.c:1409
size_t x_json_array_get_count(const x_JSON_Array *array)
Definition: parson.c:1041
x_json_result_t
Definition: parson.h:50
@ JSONSuccess
Definition: parson.h:51
@ JSONFailure
Definition: parson.h:52
x_JSON_Status x_json_object_dotset_boolean(x_JSON_Object *object, const char *name, int boolean)
Definition: parson.c:1569
x_JSON_Status x_json_object_set_boolean(x_JSON_Object *object, const char *name, int boolean)
Definition: parson.c:1508
x_JSON_Status x_json_object_clear(x_JSON_Object *object)
Definition: parson.c:1629
int x_json_object_dothas_value(const x_JSON_Object *object, const char *name)
Definition: parson.c:1005
size_t x_json_serialization_size(const x_JSON_Value *value)
Definition: parson.c:1229
x_JSON_Status x_json_array_append_number(x_JSON_Array *array, double number)
Definition: parson.c:1448
x_JSON_Status x_json_serialize_to_file(const x_JSON_Value *value, const char *filename)
Definition: parson.c:1247
x_JSON_Value_Type x_json_value_get_type(const x_JSON_Value *value)
Definition: parson.c:1046
x_JSON_Status x_json_array_append_boolean(x_JSON_Array *array, int boolean)
Definition: parson.c:1459
int x_json_object_dothas_value_of_type(const x_JSON_Object *object, const char *name, x_JSON_Value_Type type)
Definition: parson.c:1009
x_JSON_Value_Type x_json_type(const x_JSON_Value *value)
Definition: parson.c:1757
x_JSON_Status x_json_array_append_string(x_JSON_Array *array, const char *string)
Definition: parson.c:1437
x_JSON_Status x_json_object_dotremove(x_JSON_Object *object, const char *key)
Definition: parson.c:1611
x_JSON_Status x_json_array_replace_string(x_JSON_Array *array, size_t i, const char *string)
Definition: parson.c:1376
size_t x_json_object_get_count(const x_JSON_Object *object)
Definition: parson.c:980
double x_json_array_get_number(const x_JSON_Array *array, size_t index)
Definition: parson.c:1025
x_JSON_Value * x_json_parse_file(const char *filename)
Definition: parson.c:884
x_JSON_Status x_json_object_set_null(x_JSON_Object *object, const char *name)
Definition: parson.c:1512
int x_json_object_has_value_of_type(const x_JSON_Object *object, const char *name, x_JSON_Value_Type type)
Definition: parson.c:1000
x_JSON_Status x_json_object_dotset_null(x_JSON_Object *object, const char *name)
Definition: parson.c:1580
x_JSON_Status x_json_object_remove(x_JSON_Object *object, const char *name)
Definition: parson.c:1591
x_JSON_Value * x_json_object_get_value(const x_JSON_Object *object, const char *name)
Definition: parson.c:926
x_JSON_Status x_json_serialize_to_buffer(const x_JSON_Value *value, char *buf, size_t buf_size_in_bytes)
Definition: parson.c:1235
x_json_value_type
Definition: parson.h:39
@ JSONError
Definition: parson.h:40
@ JSONObject
Definition: parson.h:44
@ JSONNull
Definition: parson.h:41
@ JSONNumber
Definition: parson.h:43
@ JSONBoolean
Definition: parson.h:46
@ JSONString
Definition: parson.h:42
@ JSONArray
Definition: parson.h:45
const char * x_json_object_dotget_string(const x_JSON_Object *object, const char *name)
Definition: parson.c:960
int x_json_array_get_boolean(const x_JSON_Array *array, size_t index)
Definition: parson.c:1037
x_JSON_Object * x_json_object_get_object(const x_JSON_Object *object, const char *name)
Definition: parson.c:940
x_JSON_Value * x_json_value_init_number(double number)
Definition: parson.c:1131
static BOOL number
Definition: pcregrep.c:193
Definition: type.c:6
Modified on Wed May 29 18:36:12 2024 by modify_doxy.py rev. 669887