source: azure_iot_hub_f767zi/trunk/azure_iot_sdk/deps/parson/parson.h

Last change on this file was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 11.6 KB
Line 
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
30extern "C"
31{
32#endif
33
34#include <stddef.h> /* size_t */
35
36/* Types and enums */
37typedef struct json_object_t JSON_Object;
38typedef struct json_array_t JSON_Array;
39typedef struct json_value_t JSON_Value;
40
41enum 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};
50typedef int JSON_Value_Type;
51
52enum json_result_t {
53 JSONSuccess = 0,
54 JSONFailure = -1
55};
56typedef int JSON_Status;
57
58typedef void * (*JSON_Malloc_Function)(size_t);
59typedef 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 */
63void 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. */
67void json_set_escape_slashes(int escape_slashes);
68
69/* Parses first JSON value in a file, returns NULL in case of error */
70JSON_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 */
74JSON_Value * json_parse_file_with_comments(const char *filename);
75
76/* Parses first JSON value in a string, returns NULL in case of error */
77JSON_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 */
81JSON_Value * json_parse_string_with_comments(const char *string);
82
83/* Serialization */
84size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */
85JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
86JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename);
87char * json_serialize_to_string(const JSON_Value *value);
88
89/* Pretty serialization */
90size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */
91JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
92JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename);
93char * json_serialize_to_string_pretty(const JSON_Value *value);
94
95void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */
96
97/* Comparing */
98int 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 */
110JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value);
111
112/*
113 * JSON Object
114 */
115JSON_Value * json_object_get_value (const JSON_Object *object, const char *name);
116const char * json_object_get_string (const JSON_Object *object, const char *name);
117JSON_Object * json_object_get_object (const JSON_Object *object, const char *name);
118JSON_Array * json_object_get_array (const JSON_Object *object, const char *name);
119double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
120int 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. */
126JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name);
127const char * json_object_dotget_string (const JSON_Object *object, const char *name);
128JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
129JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name);
130double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
131int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
132
133/* Functions to get available names */
134size_t json_object_get_count (const JSON_Object *object);
135const char * json_object_get_name (const JSON_Object *object, size_t index);
136JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index);
137JSON_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. */
141int json_object_has_value (const JSON_Object *object, const char *name);
142int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type);
143
144int json_object_dothas_value (const JSON_Object *object, const char *name);
145int 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. */
149JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
150JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
151JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
152JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
153JSON_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. */
157JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value);
158JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string);
159JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number);
160JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean);
161JSON_Status json_object_dotset_null(JSON_Object *object, const char *name);
162
163/* Frees and removes name-value pair */
164JSON_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. */
167JSON_Status json_object_dotremove(JSON_Object *object, const char *key);
168
169/* Removes all name-value pairs in object */
170JSON_Status json_object_clear(JSON_Object *object);
171
172/*
173 *JSON Array
174 */
175JSON_Value * json_array_get_value (const JSON_Array *array, size_t index);
176const char * json_array_get_string (const JSON_Array *array, size_t index);
177JSON_Object * json_array_get_object (const JSON_Array *array, size_t index);
178JSON_Array * json_array_get_array (const JSON_Array *array, size_t index);
179double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */
180int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */
181size_t json_array_get_count (const JSON_Array *array);
182JSON_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. */
186JSON_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. */
191JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value);
192JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string);
193JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number);
194JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean);
195JSON_Status json_array_replace_null(JSON_Array *array, size_t i);
196
197/* Frees and removes all values from array */
198JSON_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. */
202JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value);
203JSON_Status json_array_append_string(JSON_Array *array, const char *string);
204JSON_Status json_array_append_number(JSON_Array *array, double number);
205JSON_Status json_array_append_boolean(JSON_Array *array, int boolean);
206JSON_Status json_array_append_null(JSON_Array *array);
207
208/*
209 *JSON Value
210 */
211JSON_Value * json_value_init_object (void);
212JSON_Value * json_value_init_array (void);
213JSON_Value * json_value_init_string (const char *string); /* copies passed string */
214JSON_Value * json_value_init_number (double number);
215JSON_Value * json_value_init_boolean(int boolean);
216JSON_Value * json_value_init_null (void);
217JSON_Value * json_value_deep_copy (const JSON_Value *value);
218void json_value_free (JSON_Value *value);
219
220JSON_Value_Type json_value_get_type (const JSON_Value *value);
221JSON_Object * json_value_get_object (const JSON_Value *value);
222JSON_Array * json_value_get_array (const JSON_Value *value);
223const char * json_value_get_string (const JSON_Value *value);
224double json_value_get_number (const JSON_Value *value);
225int json_value_get_boolean(const JSON_Value *value);
226JSON_Value * json_value_get_parent (const JSON_Value *value);
227
228/* Same as above, but shorter */
229JSON_Value_Type json_type (const JSON_Value *value);
230JSON_Object * json_object (const JSON_Value *value);
231JSON_Array * json_array (const JSON_Value *value);
232const char * json_string (const JSON_Value *value);
233double json_number (const JSON_Value *value);
234int json_boolean(const JSON_Value *value);
235
236#ifdef __cplusplus
237}
238#endif
239
240#endif
Note: See TracBrowser for help on using the repository browser.