source: lib/parson.h @ 782a6ee

Last change on this file since 782a6ee was 782a6ee, checked in by Wilmer van der Gaast <wilmer@…>, at 2015-02-22T16:12:13Z

Remove old JSON library, replace it with Parson. No other code changes so
this won't even compile yet.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*
2 Parson ( http://kgabis.github.com/parson/ )
3 Copyright (c) 2012 - 2014 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   
56/* Parses first JSON value in a file, returns NULL in case of error */
57JSON_Value * json_parse_file(const char *filename);
58
59/* Parses first JSON value in a file and ignores comments (/ * * / and //),
60   returns NULL in case of error */
61JSON_Value * json_parse_file_with_comments(const char *filename);
62   
63/*  Parses first JSON value in a string, returns NULL in case of error */
64JSON_Value * json_parse_string(const char *string);
65
66/*  Parses first JSON value in a string and ignores comments (/ * * / and //),
67    returns NULL in case of error */
68JSON_Value * json_parse_string_with_comments(const char *string);
69   
70/* Serialization */
71size_t      json_serialization_size(const JSON_Value *value);
72JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
73JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename);
74char *      json_serialize_to_string(const JSON_Value *value);
75void        json_free_serialized_string(char *string); /* frees string from json_serialize_to_string */
76
77/* Comparing */
78int  json_value_equals(const JSON_Value *a, const JSON_Value *b);
79   
80/* Validation
81   This is *NOT* JSON Schema. It validates json by checking if object have identically
82   named fields with matching types.
83   For example schema {"name":"", "age":0} will validate
84   {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"},
85   but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}.
86   In case of arrays, only first value in schema is checked against all values in tested array.
87   Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays,
88   null validates values of every type.
89 */
90JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value);
91   
92/*
93 * JSON Object
94 */
95JSON_Value  * json_object_get_value  (const JSON_Object *object, const char *name);
96const char  * json_object_get_string (const JSON_Object *object, const char *name);
97JSON_Object * json_object_get_object (const JSON_Object *object, const char *name);
98JSON_Array  * json_object_get_array  (const JSON_Object *object, const char *name);
99double        json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
100int           json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
101
102/* dotget functions enable addressing values with dot notation in nested objects,
103 just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
104 Because valid names in JSON can contain dots, some values may be inaccessible
105 this way. */
106JSON_Value  * json_object_dotget_value  (const JSON_Object *object, const char *name);
107const char  * json_object_dotget_string (const JSON_Object *object, const char *name);
108JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
109JSON_Array  * json_object_dotget_array  (const JSON_Object *object, const char *name);
110double        json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
111int           json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
112
113/* Functions to get available names */
114size_t        json_object_get_count(const JSON_Object *object);
115const char  * json_object_get_name (const JSON_Object *object, size_t index);
116   
117/* Creates new name-value pair or frees and replaces old value with new one. */
118JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
119JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
120JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
121JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
122JSON_Status json_object_set_null(JSON_Object *object, const char *name);
123
124/* Works like dotget functions, but creates whole hierarchy if necessary. */
125JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value);
126JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string);
127JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number);
128JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean);
129JSON_Status json_object_dotset_null(JSON_Object *object, const char *name);
130
131/* Frees and removes name-value pair */
132JSON_Status json_object_remove(JSON_Object *object, const char *name);
133
134/* Works like dotget function, but removes name-value pair only on exact match. */
135JSON_Status json_object_dotremove(JSON_Object *object, const char *key);
136
137/* Removes all name-value pairs in object */
138JSON_Status json_object_clear(JSON_Object *object);
139   
140/*
141 *JSON Array
142 */
143JSON_Value  * json_array_get_value  (const JSON_Array *array, size_t index);
144const char  * json_array_get_string (const JSON_Array *array, size_t index);
145JSON_Object * json_array_get_object (const JSON_Array *array, size_t index);
146JSON_Array  * json_array_get_array  (const JSON_Array *array, size_t index);
147double        json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */
148int           json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */
149size_t        json_array_get_count  (const JSON_Array *array);
150   
151/* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
152 * Order of values in array may change during execution.  */
153JSON_Status json_array_remove(JSON_Array *array, size_t i);
154
155/* Frees and removes from array value at given index and replaces it with given one.
156 * Does nothing and returns JSONFailure if index doesn't exist. */
157JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value);
158JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string);
159JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number);
160JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean);
161JSON_Status json_array_replace_null(JSON_Array *array, size_t i);
162
163/* Frees and removes all values from array */
164JSON_Status json_array_clear(JSON_Array *array);
165
166/* Appends new value at the end of array. */
167JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value);
168JSON_Status json_array_append_string(JSON_Array *array, const char *string);
169JSON_Status json_array_append_number(JSON_Array *array, double number);
170JSON_Status json_array_append_boolean(JSON_Array *array, int boolean);
171JSON_Status json_array_append_null(JSON_Array *array);
172   
173/*
174 *JSON Value
175 */
176JSON_Value * json_value_init_object (void);
177JSON_Value * json_value_init_array  (void);
178JSON_Value * json_value_init_string (const char *string); /* copies passed string */
179JSON_Value * json_value_init_number (double number);
180JSON_Value * json_value_init_boolean(int boolean);
181JSON_Value * json_value_init_null   (void);
182JSON_Value * json_value_deep_copy   (const JSON_Value *value);
183void         json_value_free        (JSON_Value *value);
184
185JSON_Value_Type json_value_get_type   (const JSON_Value *value);
186JSON_Object *   json_value_get_object (const JSON_Value *value);
187JSON_Array  *   json_value_get_array  (const JSON_Value *value);
188const char  *   json_value_get_string (const JSON_Value *value);
189double          json_value_get_number (const JSON_Value *value);
190int             json_value_get_boolean(const JSON_Value *value);
191
192/* Same as above, but shorter */
193JSON_Value_Type json_type   (const JSON_Value *value);
194JSON_Object *   json_object (const JSON_Value *value);
195JSON_Array  *   json_array  (const JSON_Value *value);
196const char  *   json_string (const JSON_Value *value);
197double          json_number (const JSON_Value *value);
198int             json_boolean(const JSON_Value *value);
199   
200#ifdef __cplusplus
201}
202#endif
203
204#endif
Note: See TracBrowser for help on using the repository browser.