-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_json.h
225 lines (199 loc) · 6.85 KB
/
easy_json.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#pragma once
#include <stdbool.h>
#include <stdio.h>
#define MAX_JSON_DEPTH 1000
/**
* Enum for eror codes.
*/
enum ejson_errors {
/** Everything is ok */
EJSON_OK = 0,
/** The given json is not valid json. */
EJSON_INVALID_JSON = 1,
/** You try to access a value with the wrong type. */
EJSON_WRONG_TYPE = 2,
/** Cannot find key. */
EJSON_KEY_NOT_FOUND = 3
};
/**
* Enum for json types. Number is split in double and int.
*/
enum ejson_types {
/** Int type. Part of the number json type */
EJSON_INT = 10,
/** Double type. Part of the number json type */
EJSON_DOUBLE = 11,
/** A json object */
EJSON_OBJECT = 12,
/** A json string. */
EJSON_STRING = 13,
/** A boolean value. */
EJSON_BOOLEAN = 14,
/** An json array. */
EJSON_ARRAY = 15,
/** Json null value */
EJSON_NULL = 16
};
union ejson_base;
typedef struct {
char* key;
union ejson_base* value;
} ejson_key;
typedef struct {
enum ejson_types type;
long length;
ejson_key** keys;
} ejson_object;
typedef struct {
enum ejson_types type;
long length;
union ejson_base** values;
} ejson_array;
typedef struct {
enum ejson_types type;
char* value;
} ejson_string;
typedef struct {
enum ejson_types type;
} ejson_null;
typedef struct {
enum ejson_types type;
long value;
} ejson_number;
typedef struct {
enum ejson_types type;
double value;
} ejson_real;
typedef struct {
enum ejson_types type;
int value;
} ejson_bool;
typedef union ejson_base {
enum ejson_types type;
ejson_object object;
ejson_array array;
ejson_string string;
ejson_null null;
ejson_number number;
ejson_real real;
ejson_bool boolean;
} ejson_base;
/**
* Internal json parser structure.
*/
typedef struct {
enum ejson_errors error;
char* data;
size_t pos;
const size_t len;
long counter;
const bool warnings;
FILE* log;
} ejson_state;
ejson_base* ejson_find_by_key(const ejson_object* root, const char* key, const int case_insensitive, const int childs);
/**
* Gets the value as int from given struct.
* @param ejson_base* root element
* @param int* i place for the returned int
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_int(const ejson_base* root, int* i);
/**
* Gets the value of the given key in an object as int.
* @param ejson_object* root element
* @param char* key key to search for.
* @param int case_insensitive Search for key case insentitive.
* @param int childs Search the childs, too.
* @param char** i place for the returned int
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_int_from_key(const ejson_object* root, const char* key, const int case_insensitive, const int childs, int* i);
/**
* Gets the value as int from given struct.
* @param ejson_base* root element
* @param int* i place for the returned int
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_double(const ejson_base* root, double* d);
/**
* Gets the value of the given key in an object as double.
* @param ejson_object* root element
* @param char* key key to search for.
* @param int case_insensitive Search for key case insentitive.
* @param int childs Search the childs, too.
* @param char** i place for the returned double
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_double_from_key(const ejson_object* root, const char* key, const int case_insensitive, const int childs, double* d);
/**
* Gets the value as double from the given struct. It also returns the number if the type is int.
* @param ejson_base* root element
* @param double* i place for the returned double
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_number(const ejson_base* root, double* d);
/**
* Gets the value of the given key in an object as double.
* Returns also the number if type is int.
* @param ejson_object* root element
* @param char* key key to search for.
* @param int case_insensitive Search for key case insentitive.
* @param int childs Search the childs, too.
* @param double* i place for the returned double
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_number_from_key(const ejson_object* root, const char* key, const int case_insensitive, const int childs, double* d);
/**
* Gets the value of the given struct as string.
* @param ejson_root* root element
* @param char** s place for holding the string. Must not be freed.
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_string(const ejson_base* root, char** s);
/**
* Gets the value of the given key in an object as string.
* @param ejson_object* root element
* @param char* key key to search for.
* @param int case_insensitive Search for key case insentitive.
* @param int childs Search the childs, too.
* @param char** i place for the returned string
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_string_from_key(const ejson_object* root, const char* key, const int case_insensitive, const int childs, char** i);
/**
* Gets the value as boolean from the given struct.
* @param ejson_struct* ejson json struct.
* @param bool* b place for holding the boolean value.
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_boolean(const ejson_base* root, bool* b);
/**
* Gets the value of the given key in an object as bool.
* @param ejson_object* root element
* @param char* key key to search for.
* @param int case_insensitive Search for key case insentitive.
* @param int childs Search the childs, too.
* @param char** i place for the returned bool
* @return enum ejson_errors returns EJSON_WRONG_TYPE if there is an error.
*/
enum ejson_errors ejson_get_boolean_from_key(const ejson_object* root, const char* key, const int case_insensitive, const int childs, bool* i);
/**
* Parses an json string into the given structure pointer.
* IMPORTANT: It works direct on the given string. So it must be writable.
* After the parsing you cannot parse this string again.
* @param ejson_base** element to hold the parsed data.
* @param char* string json string. MUST BE writable because this lib works directly
* on the given memory.
* @return enum ejson_errors enum with error flags for parsing. @see enum
*/
enum ejson_errors ejson_parse(char* string, const size_t len, ejson_base** root);
/**
* Same as ejson_parse but allows to write the warnings to a file or stderr.
*/
enum ejson_errors ejson_parse_warnings(char* string, const size_t len, const bool warnings, FILE* outputstream, ejson_base** root);
/**
* Cleanup the json structure.
* It doesn't cleanup any key or value strings.
* @param ejson_struct* ejson structure for cleanup.
*/
void ejson_cleanup(ejson_base* root);