-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsmn_parser.h
291 lines (254 loc) · 8.11 KB
/
jsmn_parser.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* MIT License
*
* Copyright (c) 2021 Max Lichtenegger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef JSMN_PARSER_H
#define JSMN_PARSER_H
#include "jsmn.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/*****************************************************************************/
/* Function prototypes */
/*****************************************************************************/
/**
* @brief Jumpt to the next JSON element.
*
* Objects and arrays do have sizes, but the length of each element is
* unknown. In JSMN, all the tokens are in an array, so this function just
* helps finding the next JSON element.
*
* @param tok Pointer to the current token. Tokens
* must be ordered in an array.
* @return const jsmntok_t* Pointer to the token of the next
* element. If no next element is found
* the given token is returned.
*/
static const jsmntok_t* tok_next_elem(const jsmntok_t* tok);
/**
* @brief Check if the given token is a name.
*
* JSMN does not specify names, however a JSMN_STRING with size one is a
* JSON name compliant to rfc8259.
*
* @param tok Pointer to the token to check.
* @return true if token points to a name.
* @return false if token points to something else.
*/
static bool tok_is_name(const jsmntok_t* tok);
/**
* @brief Check if the given token is a leaf.
*
* Leaf as in does not contain any nested objects or arrays.
*
* @param tok Pointer to the token to check.
* @return true if oken points to a leaf.
* @return false if oken points to something else.
*/
static bool tok_is_leaf(const jsmntok_t* tok);
/**
* @brief Compare the value pointed to by the token with another string.
*
* Since tokens just point to substrings of a JSON string, comparison of
* strings does not include zero terminator, i.e. length checks.
*
* @param tok Pointer to token to check.
* @param js JSON string tokens point to.
* @param value String value to compare to.
* @return true if strings are identical.
* @return false if strings mismatch.
*/
static bool tok_string_compare(const jsmntok_t* tok, const char* js, const char* value);
/**
* @brief Get the value of a name/value pair.
*
* @param tok Pointer to the token whose value we
* want to get.
* @return const jsmntok_t* Pointer to the value, returns the given
* token if it is a value.
*/
JSMN_API const jsmntok_t* tok_get_value(const jsmntok_t* tok);
/**
* @brief Get the token at the given index.
*
* Works on objects and arrays and on names of these.
*
* @param tok Pointer to the object or array
* (or their name).
* @param index Index within the given object/array.
* @return const jsmntok_t* Pointer to the token at the given
* index. If index is out of range, the
* given token is returned.
*/
JSMN_API const jsmntok_t* tok_get_index(const jsmntok_t* tok, int index);
/**
* @brief Get the token with the given name.
*
* Only works on objects (and their names), since they can contain name/value
* pairs.
*
* @param tok Pointer to the object (or its name).
* @param js JSON string tokens point to.
* @param name The name to get.
* @return const jsmntok_t* Pointer to the token with the given
* name, returns the given token if the
* name could not be found.
*/
JSMN_API const jsmntok_t* tok_get_name(const jsmntok_t* tok, const char* js, const char* name);
/**
* @brief Get the token with the value for the given name, if it is a leaf.
*
* Convenience function combining tok_get_name and tok_get_value. Also checks
* if returned value is a leaf.
*
* @param tok Pointer to the object (or its name).
* @param js JSON string tokens point to.
* @param name The name whose value to get.
* @return const jsmntok_t* Pointer to the token of the value with
* the given name, returns the given token
* if the name could not be found or the
* value is not a leaf.
*/
JSMN_API const jsmntok_t* tok_get_leaf(const jsmntok_t* tok, const char* js, const char* name);
#ifndef JSMN_HEADER
/*****************************************************************************/
/* Functions */
/*****************************************************************************/
static const jsmntok_t* tok_next_elem(const jsmntok_t* tok)
{
const jsmntok_t* start_tok = tok;
// Add ourselves
const jsmntok_t* end_tok = ++tok;
int layers = start_tok->size;
// TODO: Make sure we have a way out of the recursion
for (int i = 0; i < layers; i++)
{
end_tok = tok_next_elem(end_tok);
}
return end_tok;
}
static bool tok_is_name(const jsmntok_t* tok)
{
return (tok->type == JSMN_STRING && tok->size == 1);
}
static bool tok_is_leaf(const jsmntok_t* tok)
{
return (tok->size == 0);
}
static bool tok_string_compare(const jsmntok_t* tok, const char* js, const char* value)
{
bool same = false;
const char* start = js + tok->start;
const char* end = js + tok->end;
while (start != end)
{
if (*start != *value)
{
return same;
}
start++;
value++;
}
same = true;
return same;
}
JSMN_API const jsmntok_t* tok_get_value(const jsmntok_t* tok)
{
const jsmntok_t* value = tok;
if (tok_is_name(tok))
{
++value;
}
return value;
}
JSMN_API const jsmntok_t* tok_get_index(const jsmntok_t* tok, int index)
{
const jsmntok_t* tok_at_index = tok;
// Only applicable to objects and arrays (or names of those)
if (tok_get_value(tok)->type == JSMN_OBJECT || tok_get_value(tok)->type == JSMN_ARRAY)
{
if (tok_get_value(tok)->size > index)
{
// Workaround for names
if (tok_is_name(tok))
{
++tok_at_index;
}
// Start at the next token
++tok_at_index;
for (int curr_index = 0; curr_index < index; curr_index++)
{
tok_at_index = tok_next_elem(tok_at_index);
}
}
}
return tok_at_index;
}
JSMN_API const jsmntok_t* tok_get_name(const jsmntok_t* tok, const char* js, const char* name)
{
const jsmntok_t* tok_with_name = tok;
// Only applicable to objects (and keys of objects)
if (tok_get_value(tok)->type == JSMN_OBJECT)
{
// Workaround for keys
if (tok_is_name(tok))
{
++tok_with_name;
}
// Start at the next token
++tok_with_name;
for (int curr_index = 0; curr_index < tok_get_value(tok)->size; curr_index++)
{
// Sanity check
if (!tok_is_name(tok_with_name))
{
return tok;
}
if (tok_string_compare(tok_with_name, js, name))
{
return tok_with_name;
}
tok_with_name = tok_next_elem(tok_with_name);
}
}
return tok;
}
JSMN_API const jsmntok_t* tok_get_leaf(const jsmntok_t* tok, const char* js, const char* name)
{
const jsmntok_t* tok_with_name = tok_get_name(tok, js, name);
// Check if key found
if (tok_with_name != tok)
{
const jsmntok_t* tok_with_value = tok_get_value(tok_with_name);
if (tok_is_leaf(tok_with_value))
{
return tok_with_value;
}
}
return tok;
}
#endif /* JSMN_HEADER */
#ifdef __cplusplus
}
#endif
#endif /* JSMN_PARSER_H */