1
1
/*
2
2
Parson ( http://kgabis.github.com/parson/ )
3
- Copyright (C) 2013 Krzysztof Gabis
4
-
3
+ Copyright (c) 2012 - 2016 Krzysztof Gabis
4
+
5
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
of this software and associated documentation files (the "Software"), to deal
7
7
in the Software without restriction, including without limitation the rights
8
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
copies of the Software, and to permit persons to whom the Software is
10
10
furnished to do so, subject to the following conditions:
11
-
11
+
12
12
The above copyright notice and this permission notice shall be included in
13
13
all copies or substantial portions of the Software.
14
-
14
+
15
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
27
#ifdef __cplusplus
28
28
extern "C"
29
29
{
30
- #endif
31
-
32
- #include <stddef.h> /* size_t */
30
+ #endif
31
+
32
+ #include <stddef.h> /* size_t */
33
33
34
- #define PARSON_VERSION 20131130
35
-
36
34
/* Types and enums */
37
35
typedef struct json_object_t JSON_Object ;
38
36
typedef struct json_array_t JSON_Array ;
39
37
typedef struct json_value_t JSON_Value ;
40
38
41
- typedef enum json_value_type {
42
- JSONError = 0 ,
39
+ enum json_value_type {
40
+ JSONError = -1 ,
43
41
JSONNull = 1 ,
44
42
JSONString = 2 ,
45
43
JSONNumber = 3 ,
46
44
JSONObject = 4 ,
47
45
JSONArray = 5 ,
48
46
JSONBoolean = 6
49
- } JSON_Value_Type ;
47
+ };
48
+ typedef int JSON_Value_Type ;
49
+
50
+ enum json_result_t {
51
+ JSONSuccess = 0 ,
52
+ JSONFailure = -1
53
+ };
54
+ typedef int JSON_Status ;
55
+
56
+ typedef void * (* JSON_Malloc_Function )(size_t );
57
+ typedef void (* 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 */
61
+ void json_set_allocation_functions (JSON_Malloc_Function malloc_fun , JSON_Free_Function free_fun );
50
62
51
-
52
63
/* Parses first JSON value in a file, returns NULL in case of error */
53
- JSON_Value * json_parse_file (const char * filename );
64
+ JSON_Value * json_parse_file (const char * filename );
54
65
55
66
/* Parses first JSON value in a file and ignores comments (/ * * / and //),
56
67
returns NULL in case of error */
57
- JSON_Value * json_parse_file_with_comments (const char * filename );
58
-
68
+ JSON_Value * json_parse_file_with_comments (const char * filename );
69
+
59
70
/* Parses first JSON value in a string, returns NULL in case of error */
60
- JSON_Value * json_parse_string (const char * string );
71
+ JSON_Value * json_parse_string (const char * string );
61
72
62
73
/* Parses first JSON value in a string and ignores comments (/ * * / and //),
63
74
returns NULL in case of error */
64
- JSON_Value * json_parse_string_with_comments (const char * string );
65
-
66
- /* JSON Object */
75
+ JSON_Value * json_parse_string_with_comments (const char * string );
76
+
77
+ /* Serialization */
78
+ size_t json_serialization_size (const JSON_Value * value ); /* returns 0 on fail */
79
+ JSON_Status json_serialize_to_buffer (const JSON_Value * value , char * buf , size_t buf_size_in_bytes );
80
+ JSON_Status json_serialize_to_file (const JSON_Value * value , const char * filename );
81
+ char * json_serialize_to_string (const JSON_Value * value );
82
+
83
+ /* Pretty serialization */
84
+ size_t json_serialization_size_pretty (const JSON_Value * value ); /* returns 0 on fail */
85
+ JSON_Status json_serialize_to_buffer_pretty (const JSON_Value * value , char * buf , size_t buf_size_in_bytes );
86
+ JSON_Status json_serialize_to_file_pretty (const JSON_Value * value , const char * filename );
87
+ char * json_serialize_to_string_pretty (const JSON_Value * value );
88
+
89
+ void json_free_serialized_string (char * string ); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */
90
+
91
+ /* Comparing */
92
+ int json_value_equals (const JSON_Value * a , const 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
+ */
104
+ JSON_Status json_validate (const JSON_Value * schema , const JSON_Value * value );
105
+
106
+ /*
107
+ * JSON Object
108
+ */
67
109
JSON_Value * json_object_get_value (const JSON_Object * object , const char * name );
68
110
const char * json_object_get_string (const JSON_Object * object , const char * name );
69
111
JSON_Object * json_object_get_object (const JSON_Object * object , const char * name );
70
112
JSON_Array * json_object_get_array (const JSON_Object * object , const char * name );
71
- double json_object_get_number (const JSON_Object * object , const char * name );
72
- int json_object_get_boolean (const JSON_Object * object , const char * name );
113
+ double json_object_get_number (const JSON_Object * object , const char * name ); /* returns 0 on fail */
114
+ int json_object_get_boolean (const JSON_Object * object , const char * name ); /* returns -1 on fail */
73
115
74
116
/* dotget functions enable addressing values with dot notation in nested objects,
75
117
just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
@@ -79,31 +121,100 @@ JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *
79
121
const char * json_object_dotget_string (const JSON_Object * object , const char * name );
80
122
JSON_Object * json_object_dotget_object (const JSON_Object * object , const char * name );
81
123
JSON_Array * json_object_dotget_array (const JSON_Object * object , const char * name );
82
- double json_object_dotget_number (const JSON_Object * object , const char * name );
83
- int json_object_dotget_boolean (const JSON_Object * object , const char * name );
124
+ double json_object_dotget_number (const JSON_Object * object , const char * name ); /* returns 0 on fail */
125
+ int json_object_dotget_boolean (const JSON_Object * object , const char * name ); /* returns -1 on fail */
84
126
85
127
/* Functions to get available names */
86
128
size_t json_object_get_count (const JSON_Object * object );
87
129
const char * json_object_get_name (const JSON_Object * object , size_t index );
88
-
89
- /* JSON Array */
130
+
131
+ /* Creates new name-value pair or frees and replaces old value with a new one.
132
+ * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */
133
+ JSON_Status json_object_set_value (JSON_Object * object , const char * name , JSON_Value * value );
134
+ JSON_Status json_object_set_string (JSON_Object * object , const char * name , const char * string );
135
+ JSON_Status json_object_set_number (JSON_Object * object , const char * name , double number );
136
+ JSON_Status json_object_set_boolean (JSON_Object * object , const char * name , int boolean );
137
+ JSON_Status json_object_set_null (JSON_Object * object , const char * name );
138
+
139
+ /* Works like dotget functions, but creates whole hierarchy if necessary.
140
+ * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */
141
+ JSON_Status json_object_dotset_value (JSON_Object * object , const char * name , JSON_Value * value );
142
+ JSON_Status json_object_dotset_string (JSON_Object * object , const char * name , const char * string );
143
+ JSON_Status json_object_dotset_number (JSON_Object * object , const char * name , double number );
144
+ JSON_Status json_object_dotset_boolean (JSON_Object * object , const char * name , int boolean );
145
+ JSON_Status json_object_dotset_null (JSON_Object * object , const char * name );
146
+
147
+ /* Frees and removes name-value pair */
148
+ JSON_Status json_object_remove (JSON_Object * object , const char * name );
149
+
150
+ /* Works like dotget function, but removes name-value pair only on exact match. */
151
+ JSON_Status json_object_dotremove (JSON_Object * object , const char * key );
152
+
153
+ /* Removes all name-value pairs in object */
154
+ JSON_Status json_object_clear (JSON_Object * object );
155
+
156
+ /*
157
+ *JSON Array
158
+ */
90
159
JSON_Value * json_array_get_value (const JSON_Array * array , size_t index );
91
160
const char * json_array_get_string (const JSON_Array * array , size_t index );
92
161
JSON_Object * json_array_get_object (const JSON_Array * array , size_t index );
93
162
JSON_Array * json_array_get_array (const JSON_Array * array , size_t index );
94
- double json_array_get_number (const JSON_Array * array , size_t index );
95
- int json_array_get_boolean (const JSON_Array * array , size_t index );
163
+ double json_array_get_number (const JSON_Array * array , size_t index ); /* returns 0 on fail */
164
+ int json_array_get_boolean (const JSON_Array * array , size_t index ); /* returns -1 on fail */
96
165
size_t json_array_get_count (const JSON_Array * array );
97
166
98
- /* JSON Value */
167
+ /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
168
+ * Order of values in array may change during execution. */
169
+ JSON_Status json_array_remove (JSON_Array * array , size_t i );
170
+
171
+ /* Frees and removes from array value at given index and replaces it with given one.
172
+ * Does nothing and returns JSONFailure if index doesn't exist.
173
+ * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */
174
+ JSON_Status json_array_replace_value (JSON_Array * array , size_t i , JSON_Value * value );
175
+ JSON_Status json_array_replace_string (JSON_Array * array , size_t i , const char * string );
176
+ JSON_Status json_array_replace_number (JSON_Array * array , size_t i , double number );
177
+ JSON_Status json_array_replace_boolean (JSON_Array * array , size_t i , int boolean );
178
+ JSON_Status json_array_replace_null (JSON_Array * array , size_t i );
179
+
180
+ /* Frees and removes all values from array */
181
+ JSON_Status json_array_clear (JSON_Array * array );
182
+
183
+ /* Appends new value at the end of array.
184
+ * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */
185
+ JSON_Status json_array_append_value (JSON_Array * array , JSON_Value * value );
186
+ JSON_Status json_array_append_string (JSON_Array * array , const char * string );
187
+ JSON_Status json_array_append_number (JSON_Array * array , double number );
188
+ JSON_Status json_array_append_boolean (JSON_Array * array , int boolean );
189
+ JSON_Status json_array_append_null (JSON_Array * array );
190
+
191
+ /*
192
+ *JSON Value
193
+ */
194
+ JSON_Value * json_value_init_object (void );
195
+ JSON_Value * json_value_init_array (void );
196
+ JSON_Value * json_value_init_string (const char * string ); /* copies passed string */
197
+ JSON_Value * json_value_init_number (double number );
198
+ JSON_Value * json_value_init_boolean (int boolean );
199
+ JSON_Value * json_value_init_null (void );
200
+ JSON_Value * json_value_deep_copy (const JSON_Value * value );
201
+ void json_value_free (JSON_Value * value );
202
+
99
203
JSON_Value_Type json_value_get_type (const JSON_Value * value );
100
204
JSON_Object * json_value_get_object (const JSON_Value * value );
101
205
JSON_Array * json_value_get_array (const JSON_Value * value );
102
206
const char * json_value_get_string (const JSON_Value * value );
103
207
double json_value_get_number (const JSON_Value * value );
104
208
int json_value_get_boolean (const JSON_Value * value );
105
- void json_value_free (JSON_Value * value );
106
-
209
+
210
+ /* Same as above, but shorter */
211
+ JSON_Value_Type json_type (const JSON_Value * value );
212
+ JSON_Object * json_object (const JSON_Value * value );
213
+ JSON_Array * json_array (const JSON_Value * value );
214
+ const char * json_string (const JSON_Value * value );
215
+ double json_number (const JSON_Value * value );
216
+ int json_boolean (const JSON_Value * value );
217
+
107
218
#ifdef __cplusplus
108
219
}
109
220
#endif
0 commit comments