source: azure_iot_hub/trunk/azure_iohub/deps/parson/parson.h@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-chdr
File size: 11.3 KB
Line 
1/*
2 Parson ( http://kgabis.github.com/parson/ )
3 Copyright (c) 2012 - 2017 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
28extern "C"
29{
30#endif
31
32#include <stddef.h> /* size_t */
33
34/* Types and enums */
35typedef struct json_object_t JSON_Object;
36typedef struct json_array_t JSON_Array;
37typedef struct json_value_t JSON_Value;
38
39enum json_value_type {
40 JSONError = -1,
41 JSONNull = 1,
42 JSONString = 2,
43 JSONNumber = 3,
44 JSONObject = 4,
45 JSONArray = 5,
46 JSONBoolean = 6
47};
48typedef int JSON_Value_Type;
49
50enum json_result_t {
51 JSONSuccess = 0,
52 JSONFailure = -1
53};
54typedef int JSON_Status;
55
56typedef void * (*JSON_Malloc_Function)(size_t);
57typedef 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 */
61void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun);
62
63/* Parses first JSON value in a file, returns NULL in case of error */
64JSON_Value * 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 */
68JSON_Value * json_parse_file_with_comments(const char *filename);
69
70/* Parses first JSON value in a string, returns NULL in case of error */
71JSON_Value * 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 */
75JSON_Value * json_parse_string_with_comments(const char *string);
76
77/* Serialization */
78size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */
79JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
80JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename);
81char * json_serialize_to_string(const JSON_Value *value);
82
83/* Pretty serialization */
84size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */
85JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
86JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename);
87char * json_serialize_to_string_pretty(const JSON_Value *value);
88
89void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */
90
91/* Comparing */
92int 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 */
104JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value);
105
106/*
107 * JSON Object
108 */
109JSON_Value * json_object_get_value (const JSON_Object *object, const char *name);
110const char * json_object_get_string (const JSON_Object *object, const char *name);
111JSON_Object * json_object_get_object (const JSON_Object *object, const char *name);
112JSON_Array * json_object_get_array (const JSON_Object *object, const char *name);
113double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
114int json_object_get_boolean(const 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. */
120JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name);
121const char * json_object_dotget_string (const JSON_Object *object, const char *name);
122JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
123JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name);
124double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
125int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
126
127/* Functions to get available names */
128size_t json_object_get_count (const JSON_Object *object);
129const char * json_object_get_name (const JSON_Object *object, size_t index);
130JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index);
131JSON_Value * json_object_get_wrapping_value(const JSON_Object *object);
132
133/* Functions to check if object has a value with a specific name. Returned value is 1 if object has
134 * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */
135int json_object_has_value (const JSON_Object *object, const char *name);
136int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
137
138int json_object_dothas_value (const JSON_Object *object, const char *name);
139int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
140
141/* Creates new name-value pair or frees and replaces old value with a new one.
142 * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */
143JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
144JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
145JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
146JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
147JSON_Status json_object_set_null(JSON_Object *object, const char *name);
148
149/* Works like dotget functions, but creates whole hierarchy if necessary.
150 * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */
151JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value);
152JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string);
153JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number);
154JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean);
155JSON_Status json_object_dotset_null(JSON_Object *object, const char *name);
156
157/* Frees and removes name-value pair */
158JSON_Status json_object_remove(JSON_Object *object, const char *name);
159
160/* Works like dotget function, but removes name-value pair only on exact match. */
161JSON_Status json_object_dotremove(JSON_Object *object, const char *key);
162
163/* Removes all name-value pairs in object */
164JSON_Status json_object_clear(JSON_Object *object);
165
166/*
167 *JSON Array
168 */
169JSON_Value * json_array_get_value (const JSON_Array *array, size_t index);
170const char * json_array_get_string (const JSON_Array *array, size_t index);
171JSON_Object * json_array_get_object (const JSON_Array *array, size_t index);
172JSON_Array * json_array_get_array (const JSON_Array *array, size_t index);
173double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */
174int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */
175size_t json_array_get_count (const JSON_Array *array);
176JSON_Value * json_array_get_wrapping_value(const JSON_Array *array);
177
178/* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
179 * Order of values in array may change during execution. */
180JSON_Status json_array_remove(JSON_Array *array, size_t i);
181
182/* Frees and removes from array value at given index and replaces it with given one.
183 * Does nothing and returns JSONFailure if index doesn't exist.
184 * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */
185JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value);
186JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string);
187JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number);
188JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean);
189JSON_Status json_array_replace_null(JSON_Array *array, size_t i);
190
191/* Frees and removes all values from array */
192JSON_Status json_array_clear(JSON_Array *array);
193
194/* Appends new value at the end of array.
195 * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */
196JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value);
197JSON_Status json_array_append_string(JSON_Array *array, const char *string);
198JSON_Status json_array_append_number(JSON_Array *array, double number);
199JSON_Status json_array_append_boolean(JSON_Array *array, int boolean);
200JSON_Status json_array_append_null(JSON_Array *array);
201
202/*
203 *JSON Value
204 */
205JSON_Value * json_value_init_object (void);
206JSON_Value * json_value_init_array (void);
207JSON_Value * json_value_init_string (const char *string); /* copies passed string */
208JSON_Value * json_value_init_number (double number);
209JSON_Value * json_value_init_boolean(int boolean);
210JSON_Value * json_value_init_null (void);
211JSON_Value * json_value_deep_copy (const JSON_Value *value);
212void json_value_free (JSON_Value *value);
213
214JSON_Value_Type json_value_get_type (const JSON_Value *value);
215JSON_Object * json_value_get_object (const JSON_Value *value);
216JSON_Array * json_value_get_array (const JSON_Value *value);
217const char * json_value_get_string (const JSON_Value *value);
218double json_value_get_number (const JSON_Value *value);
219int json_value_get_boolean(const JSON_Value *value);
220JSON_Value * json_value_get_parent (const JSON_Value *value);
221
222/* Same as above, but shorter */
223JSON_Value_Type json_type (const JSON_Value *value);
224JSON_Object * json_object (const JSON_Value *value);
225JSON_Array * json_array (const JSON_Value *value);
226const char * json_string (const JSON_Value *value);
227double json_number (const JSON_Value *value);
228int json_boolean(const JSON_Value *value);
229
230#ifdef __cplusplus
231}
232#endif
233
234#endif
Note: See TracBrowser for help on using the repository browser.