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 | #ifdef _MSC_VER
|
---|
26 | #ifndef _CRT_SECURE_NO_WARNINGS
|
---|
27 | #define _CRT_SECURE_NO_WARNINGS
|
---|
28 | #endif /* _CRT_SECURE_NO_WARNINGS */
|
---|
29 | #endif /* _MSC_VER */
|
---|
30 |
|
---|
31 | #include "parson.h"
|
---|
32 |
|
---|
33 | #include <stdio.h>
|
---|
34 | #include <fcntl.h>
|
---|
35 | #include <stdlib.h>
|
---|
36 | #include <string.h>
|
---|
37 | #include <ctype.h>
|
---|
38 | #include <math.h>
|
---|
39 | #include <errno.h>
|
---|
40 |
|
---|
41 | /* Apparently sscanf is not implemented in some "standard" libraries, so don't use it, if you
|
---|
42 | * don't have to. */
|
---|
43 | #define sscanf THINK_TWICE_ABOUT_USING_SSCANF
|
---|
44 |
|
---|
45 | #define STARTING_CAPACITY 16
|
---|
46 | #define MAX_NESTING 2048
|
---|
47 |
|
---|
48 | #define FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */
|
---|
49 | #define NUM_BUF_SIZE 64 /* double printed with "%1.17g" shouldn't be longer than 25 bytes so let's be paranoid and use 64 */
|
---|
50 |
|
---|
51 | #define SIZEOF_TOKEN(a) (sizeof(a) - 1)
|
---|
52 | #define SKIP_CHAR(str) ((*str)++)
|
---|
53 | #define SKIP_WHITESPACES(str) while (isspace((unsigned char)(**str))) { SKIP_CHAR(str); }
|
---|
54 | #define MAX(a, b) ((a) > (b) ? (a) : (b))
|
---|
55 |
|
---|
56 | #undef malloc
|
---|
57 | #undef free
|
---|
58 |
|
---|
59 | #if defined(isnan) && defined(isinf)
|
---|
60 | #define IS_NUMBER_INVALID(x) (isnan((x)) || isinf((x)))
|
---|
61 | #else
|
---|
62 | #define IS_NUMBER_INVALID(x) (((x) * 0.0) != 0.0)
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | static JSON_Malloc_Function parson_malloc = malloc;
|
---|
66 | static JSON_Free_Function parson_free = free;
|
---|
67 |
|
---|
68 | static int parson_escape_slashes = 1;
|
---|
69 |
|
---|
70 | #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */
|
---|
71 |
|
---|
72 | /* Type definitions */
|
---|
73 | typedef union json_value_value {
|
---|
74 | char *string;
|
---|
75 | double number;
|
---|
76 | JSON_Object *object;
|
---|
77 | JSON_Array *array;
|
---|
78 | int boolean;
|
---|
79 | int null;
|
---|
80 | } JSON_Value_Value;
|
---|
81 |
|
---|
82 | struct json_value_t {
|
---|
83 | JSON_Value *parent;
|
---|
84 | JSON_Value_Type type;
|
---|
85 | JSON_Value_Value value;
|
---|
86 | };
|
---|
87 |
|
---|
88 | struct json_object_t {
|
---|
89 | JSON_Value *wrapping_value;
|
---|
90 | char **names;
|
---|
91 | JSON_Value **values;
|
---|
92 | size_t count;
|
---|
93 | size_t capacity;
|
---|
94 | };
|
---|
95 |
|
---|
96 | struct json_array_t {
|
---|
97 | JSON_Value *wrapping_value;
|
---|
98 | JSON_Value **items;
|
---|
99 | size_t count;
|
---|
100 | size_t capacity;
|
---|
101 | };
|
---|
102 |
|
---|
103 | /* Various */
|
---|
104 | static char * read_file(const char *filename);
|
---|
105 | static void remove_comments(char *string, const char *start_token, const char *end_token);
|
---|
106 | static char * parson_strndup(const char *string, size_t n);
|
---|
107 | static char * parson_strdup(const char *string);
|
---|
108 | static int hex_char_to_int(char c);
|
---|
109 | static int parse_utf16_hex(const char *string, unsigned int *result);
|
---|
110 | static int num_bytes_in_utf8_sequence(unsigned char c);
|
---|
111 | static int verify_utf8_sequence(const unsigned char *string, int *len);
|
---|
112 | static int is_valid_utf8(const char *string, size_t string_len);
|
---|
113 | static int is_decimal(const char *string, size_t length);
|
---|
114 |
|
---|
115 | /* JSON Object */
|
---|
116 | static JSON_Object * json_object_init(JSON_Value *wrapping_value);
|
---|
117 | static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value);
|
---|
118 | static JSON_Status json_object_addn(JSON_Object *object, const char *name, size_t name_len, JSON_Value *value);
|
---|
119 | static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity);
|
---|
120 | static JSON_Value * json_object_getn_value(const JSON_Object *object, const char *name, size_t name_len);
|
---|
121 | static JSON_Status json_object_remove_internal(JSON_Object *object, const char *name, int free_value);
|
---|
122 | static JSON_Status json_object_dotremove_internal(JSON_Object *object, const char *name, int free_value);
|
---|
123 | static void json_object_free(JSON_Object *object);
|
---|
124 |
|
---|
125 | /* JSON Array */
|
---|
126 | static JSON_Array * json_array_init(JSON_Value *wrapping_value);
|
---|
127 | static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value);
|
---|
128 | static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity);
|
---|
129 | static void json_array_free(JSON_Array *array);
|
---|
130 |
|
---|
131 | /* JSON Value */
|
---|
132 | static JSON_Value * json_value_init_string_no_copy(char *string);
|
---|
133 |
|
---|
134 | /* Parser */
|
---|
135 | static JSON_Status skip_quotes(const char **string);
|
---|
136 | static int parse_utf16(const char **unprocessed, char **processed);
|
---|
137 | static char * process_string(const char *input, size_t len);
|
---|
138 | static char * get_quoted_string(const char **string);
|
---|
139 | static JSON_Value * parse_object_value(const char **string, size_t nesting);
|
---|
140 | static JSON_Value * parse_array_value(const char **string, size_t nesting);
|
---|
141 | static JSON_Value * parse_string_value(const char **string);
|
---|
142 | static JSON_Value * parse_boolean_value(const char **string);
|
---|
143 | static JSON_Value * parse_number_value(const char **string);
|
---|
144 | static JSON_Value * parse_null_value(const char **string);
|
---|
145 | static JSON_Value * parse_value(const char **string, size_t nesting);
|
---|
146 |
|
---|
147 | /* Serialization */
|
---|
148 | static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf);
|
---|
149 | static int json_serialize_string(const char *string, char *buf);
|
---|
150 | static int append_indent(char *buf, int level);
|
---|
151 | static int append_string(char *buf, const char *string);
|
---|
152 |
|
---|
153 | /* Various */
|
---|
154 | static char * parson_strndup(const char *string, size_t n) {
|
---|
155 | char *output_string = (char*)parson_malloc(n + 1);
|
---|
156 | if (!output_string) {
|
---|
157 | return NULL;
|
---|
158 | }
|
---|
159 | output_string[n] = '\0';
|
---|
160 | strncpy(output_string, string, n);
|
---|
161 | return output_string;
|
---|
162 | }
|
---|
163 |
|
---|
164 | static char * parson_strdup(const char *string) {
|
---|
165 | return parson_strndup(string, strlen(string));
|
---|
166 | }
|
---|
167 |
|
---|
168 | static int hex_char_to_int(char c) {
|
---|
169 | if (c >= '0' && c <= '9') {
|
---|
170 | return c - '0';
|
---|
171 | } else if (c >= 'a' && c <= 'f') {
|
---|
172 | return c - 'a' + 10;
|
---|
173 | } else if (c >= 'A' && c <= 'F') {
|
---|
174 | return c - 'A' + 10;
|
---|
175 | }
|
---|
176 | return -1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | static int parse_utf16_hex(const char *s, unsigned int *result) {
|
---|
180 | int x1, x2, x3, x4;
|
---|
181 | if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0' || s[3] == '\0') {
|
---|
182 | return 0;
|
---|
183 | }
|
---|
184 | x1 = hex_char_to_int(s[0]);
|
---|
185 | x2 = hex_char_to_int(s[1]);
|
---|
186 | x3 = hex_char_to_int(s[2]);
|
---|
187 | x4 = hex_char_to_int(s[3]);
|
---|
188 | if (x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) {
|
---|
189 | return 0;
|
---|
190 | }
|
---|
191 | *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4);
|
---|
192 | return 1;
|
---|
193 | }
|
---|
194 |
|
---|
195 | static int num_bytes_in_utf8_sequence(unsigned char c) {
|
---|
196 | if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) {
|
---|
197 | return 0;
|
---|
198 | } else if ((c & 0x80) == 0) { /* 0xxxxxxx */
|
---|
199 | return 1;
|
---|
200 | } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */
|
---|
201 | return 2;
|
---|
202 | } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */
|
---|
203 | return 3;
|
---|
204 | } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */
|
---|
205 | return 4;
|
---|
206 | }
|
---|
207 | return 0; /* won't happen */
|
---|
208 | }
|
---|
209 |
|
---|
210 | static int verify_utf8_sequence(const unsigned char *string, int *len) {
|
---|
211 | unsigned int cp = 0;
|
---|
212 | *len = num_bytes_in_utf8_sequence(string[0]);
|
---|
213 |
|
---|
214 | if (*len == 1) {
|
---|
215 | cp = string[0];
|
---|
216 | } else if (*len == 2 && IS_CONT(string[1])) {
|
---|
217 | cp = string[0] & 0x1F;
|
---|
218 | cp = (cp << 6) | (string[1] & 0x3F);
|
---|
219 | } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) {
|
---|
220 | cp = ((unsigned char)string[0]) & 0xF;
|
---|
221 | cp = (cp << 6) | (string[1] & 0x3F);
|
---|
222 | cp = (cp << 6) | (string[2] & 0x3F);
|
---|
223 | } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) {
|
---|
224 | cp = string[0] & 0x7;
|
---|
225 | cp = (cp << 6) | (string[1] & 0x3F);
|
---|
226 | cp = (cp << 6) | (string[2] & 0x3F);
|
---|
227 | cp = (cp << 6) | (string[3] & 0x3F);
|
---|
228 | } else {
|
---|
229 | return 0;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* overlong encodings */
|
---|
233 | if ((cp < 0x80 && *len > 1) ||
|
---|
234 | (cp < 0x800 && *len > 2) ||
|
---|
235 | (cp < 0x10000 && *len > 3)) {
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* invalid unicode */
|
---|
240 | if (cp > 0x10FFFF) {
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /* surrogate halves */
|
---|
245 | if (cp >= 0xD800 && cp <= 0xDFFF) {
|
---|
246 | return 0;
|
---|
247 | }
|
---|
248 |
|
---|
249 | return 1;
|
---|
250 | }
|
---|
251 |
|
---|
252 | static int is_valid_utf8(const char *string, size_t string_len) {
|
---|
253 | int len = 0;
|
---|
254 | const char *string_end = string + string_len;
|
---|
255 | while (string < string_end) {
|
---|
256 | if (!verify_utf8_sequence((const unsigned char*)string, &len)) {
|
---|
257 | return 0;
|
---|
258 | }
|
---|
259 | string += len;
|
---|
260 | }
|
---|
261 | return 1;
|
---|
262 | }
|
---|
263 |
|
---|
264 | static int is_decimal(const char *string, size_t length) {
|
---|
265 | if (length > 1 && string[0] == '0' && string[1] != '.') {
|
---|
266 | return 0;
|
---|
267 | }
|
---|
268 | if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') {
|
---|
269 | return 0;
|
---|
270 | }
|
---|
271 | while (length--) {
|
---|
272 | if (strchr("xX", string[length])) {
|
---|
273 | return 0;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | return 1;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static char * read_file(const char * filename) {
|
---|
280 | FILE *fp = fopen(filename, "r");
|
---|
281 | size_t size_to_read = 0;
|
---|
282 | size_t size_read = 0;
|
---|
283 | long pos;
|
---|
284 | char *file_contents;
|
---|
285 | if (!fp) {
|
---|
286 | return NULL;
|
---|
287 | }
|
---|
288 | fseek(fp, 0L, SEEK_END);
|
---|
289 | pos = ftell(fp);
|
---|
290 | if (pos < 0) {
|
---|
291 | fclose(fp);
|
---|
292 | return NULL;
|
---|
293 | }
|
---|
294 | size_to_read = pos;
|
---|
295 | rewind(fp);
|
---|
296 | file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1));
|
---|
297 | if (!file_contents) {
|
---|
298 | fclose(fp);
|
---|
299 | return NULL;
|
---|
300 | }
|
---|
301 | size_read = fread(file_contents, 1, size_to_read, fp);
|
---|
302 | if (size_read == 0 || ferror(fp)) {
|
---|
303 | fclose(fp);
|
---|
304 | parson_free(file_contents);
|
---|
305 | return NULL;
|
---|
306 | }
|
---|
307 | fclose(fp);
|
---|
308 | file_contents[size_read] = '\0';
|
---|
309 | return file_contents;
|
---|
310 | }
|
---|
311 |
|
---|
312 | static void remove_comments(char *string, const char *start_token, const char *end_token) {
|
---|
313 | int in_string = 0, escaped = 0;
|
---|
314 | size_t i;
|
---|
315 | char *ptr = NULL, current_char;
|
---|
316 | size_t start_token_len = strlen(start_token);
|
---|
317 | size_t end_token_len = strlen(end_token);
|
---|
318 | if (start_token_len == 0 || end_token_len == 0) {
|
---|
319 | return;
|
---|
320 | }
|
---|
321 | while ((current_char = *string) != '\0') {
|
---|
322 | if (current_char == '\\' && !escaped) {
|
---|
323 | escaped = 1;
|
---|
324 | string++;
|
---|
325 | continue;
|
---|
326 | } else if (current_char == '\"' && !escaped) {
|
---|
327 | in_string = !in_string;
|
---|
328 | } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) {
|
---|
329 | for(i = 0; i < start_token_len; i++) {
|
---|
330 | string[i] = ' ';
|
---|
331 | }
|
---|
332 | string = string + start_token_len;
|
---|
333 | ptr = strstr(string, end_token);
|
---|
334 | if (!ptr) {
|
---|
335 | return;
|
---|
336 | }
|
---|
337 | for (i = 0; i < (ptr - string) + end_token_len; i++) {
|
---|
338 | string[i] = ' ';
|
---|
339 | }
|
---|
340 | string = ptr + end_token_len - 1;
|
---|
341 | }
|
---|
342 | escaped = 0;
|
---|
343 | string++;
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* JSON Object */
|
---|
348 | static JSON_Object * json_object_init(JSON_Value *wrapping_value) {
|
---|
349 | JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object));
|
---|
350 | if (new_obj == NULL) {
|
---|
351 | return NULL;
|
---|
352 | }
|
---|
353 | new_obj->wrapping_value = wrapping_value;
|
---|
354 | new_obj->names = (char**)NULL;
|
---|
355 | new_obj->values = (JSON_Value**)NULL;
|
---|
356 | new_obj->capacity = 0;
|
---|
357 | new_obj->count = 0;
|
---|
358 | return new_obj;
|
---|
359 | }
|
---|
360 |
|
---|
361 | static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) {
|
---|
362 | if (name == NULL) {
|
---|
363 | return JSONFailure;
|
---|
364 | }
|
---|
365 | return json_object_addn(object, name, strlen(name), value);
|
---|
366 | }
|
---|
367 |
|
---|
368 | static JSON_Status json_object_addn(JSON_Object *object, const char *name, size_t name_len, JSON_Value *value) {
|
---|
369 | size_t index = 0;
|
---|
370 | if (object == NULL || name == NULL || value == NULL) {
|
---|
371 | return JSONFailure;
|
---|
372 | }
|
---|
373 | if (json_object_getn_value(object, name, name_len) != NULL) {
|
---|
374 | return JSONFailure;
|
---|
375 | }
|
---|
376 | if (object->count >= object->capacity) {
|
---|
377 | size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
|
---|
378 | if (json_object_resize(object, new_capacity) == JSONFailure) {
|
---|
379 | return JSONFailure;
|
---|
380 | }
|
---|
381 | }
|
---|
382 | index = object->count;
|
---|
383 | object->names[index] = parson_strndup(name, name_len);
|
---|
384 | if (object->names[index] == NULL) {
|
---|
385 | return JSONFailure;
|
---|
386 | }
|
---|
387 | value->parent = json_object_get_wrapping_value(object);
|
---|
388 | object->values[index] = value;
|
---|
389 | object->count++;
|
---|
390 | return JSONSuccess;
|
---|
391 | }
|
---|
392 |
|
---|
393 | static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) {
|
---|
394 | char **temp_names = NULL;
|
---|
395 | JSON_Value **temp_values = NULL;
|
---|
396 |
|
---|
397 | if ((object->names == NULL && object->values != NULL) ||
|
---|
398 | (object->names != NULL && object->values == NULL) ||
|
---|
399 | new_capacity == 0) {
|
---|
400 | return JSONFailure; /* Shouldn't happen */
|
---|
401 | }
|
---|
402 | temp_names = (char**)parson_malloc(new_capacity * sizeof(char*));
|
---|
403 | if (temp_names == NULL) {
|
---|
404 | return JSONFailure;
|
---|
405 | }
|
---|
406 | temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*));
|
---|
407 | if (temp_values == NULL) {
|
---|
408 | parson_free(temp_names);
|
---|
409 | return JSONFailure;
|
---|
410 | }
|
---|
411 | if (object->names != NULL && object->values != NULL && object->count > 0) {
|
---|
412 | memcpy(temp_names, object->names, object->count * sizeof(char*));
|
---|
413 | memcpy(temp_values, object->values, object->count * sizeof(JSON_Value*));
|
---|
414 | }
|
---|
415 | parson_free(object->names);
|
---|
416 | parson_free(object->values);
|
---|
417 | object->names = temp_names;
|
---|
418 | object->values = temp_values;
|
---|
419 | object->capacity = new_capacity;
|
---|
420 | return JSONSuccess;
|
---|
421 | }
|
---|
422 |
|
---|
423 | static JSON_Value * json_object_getn_value(const JSON_Object *object, const char *name, size_t name_len) {
|
---|
424 | size_t i, name_length;
|
---|
425 | for (i = 0; i < json_object_get_count(object); i++) {
|
---|
426 | name_length = strlen(object->names[i]);
|
---|
427 | if (name_length != name_len) {
|
---|
428 | continue;
|
---|
429 | }
|
---|
430 | if (strncmp(object->names[i], name, name_len) == 0) {
|
---|
431 | return object->values[i];
|
---|
432 | }
|
---|
433 | }
|
---|
434 | return NULL;
|
---|
435 | }
|
---|
436 |
|
---|
437 | static JSON_Status json_object_remove_internal(JSON_Object *object, const char *name, int free_value) {
|
---|
438 | size_t i = 0, last_item_index = 0;
|
---|
439 | if (object == NULL || json_object_get_value(object, name) == NULL) {
|
---|
440 | return JSONFailure;
|
---|
441 | }
|
---|
442 | last_item_index = json_object_get_count(object) - 1;
|
---|
443 | for (i = 0; i < json_object_get_count(object); i++) {
|
---|
444 | if (strcmp(object->names[i], name) == 0) {
|
---|
445 | parson_free(object->names[i]);
|
---|
446 | if (free_value) {
|
---|
447 | json_value_free(object->values[i]);
|
---|
448 | }
|
---|
449 | if (i != last_item_index) { /* Replace key value pair with one from the end */
|
---|
450 | object->names[i] = object->names[last_item_index];
|
---|
451 | object->values[i] = object->values[last_item_index];
|
---|
452 | }
|
---|
453 | object->count -= 1;
|
---|
454 | return JSONSuccess;
|
---|
455 | }
|
---|
456 | }
|
---|
457 | return JSONFailure; /* No execution path should end here */
|
---|
458 | }
|
---|
459 |
|
---|
460 | static JSON_Status json_object_dotremove_internal(JSON_Object *object, const char *name, int free_value) {
|
---|
461 | JSON_Value *temp_value = NULL;
|
---|
462 | JSON_Object *temp_object = NULL;
|
---|
463 | const char *dot_pos = strchr(name, '.');
|
---|
464 | if (dot_pos == NULL) {
|
---|
465 | return json_object_remove_internal(object, name, free_value);
|
---|
466 | }
|
---|
467 | temp_value = json_object_getn_value(object, name, dot_pos - name);
|
---|
468 | if (json_value_get_type(temp_value) != JSONObject) {
|
---|
469 | return JSONFailure;
|
---|
470 | }
|
---|
471 | temp_object = json_value_get_object(temp_value);
|
---|
472 | return json_object_dotremove_internal(temp_object, dot_pos + 1, free_value);
|
---|
473 | }
|
---|
474 |
|
---|
475 | static void json_object_free(JSON_Object *object) {
|
---|
476 | size_t i;
|
---|
477 | for (i = 0; i < object->count; i++) {
|
---|
478 | parson_free(object->names[i]);
|
---|
479 | json_value_free(object->values[i]);
|
---|
480 | }
|
---|
481 | parson_free(object->names);
|
---|
482 | parson_free(object->values);
|
---|
483 | parson_free(object);
|
---|
484 | }
|
---|
485 |
|
---|
486 | /* JSON Array */
|
---|
487 | static JSON_Array * json_array_init(JSON_Value *wrapping_value) {
|
---|
488 | JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array));
|
---|
489 | if (new_array == NULL) {
|
---|
490 | return NULL;
|
---|
491 | }
|
---|
492 | new_array->wrapping_value = wrapping_value;
|
---|
493 | new_array->items = (JSON_Value**)NULL;
|
---|
494 | new_array->capacity = 0;
|
---|
495 | new_array->count = 0;
|
---|
496 | return new_array;
|
---|
497 | }
|
---|
498 |
|
---|
499 | static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
|
---|
500 | if (array->count >= array->capacity) {
|
---|
501 | size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
|
---|
502 | if (json_array_resize(array, new_capacity) == JSONFailure) {
|
---|
503 | return JSONFailure;
|
---|
504 | }
|
---|
505 | }
|
---|
506 | value->parent = json_array_get_wrapping_value(array);
|
---|
507 | array->items[array->count] = value;
|
---|
508 | array->count++;
|
---|
509 | return JSONSuccess;
|
---|
510 | }
|
---|
511 |
|
---|
512 | static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) {
|
---|
513 | JSON_Value **new_items = NULL;
|
---|
514 | if (new_capacity == 0) {
|
---|
515 | return JSONFailure;
|
---|
516 | }
|
---|
517 | new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*));
|
---|
518 | if (new_items == NULL) {
|
---|
519 | return JSONFailure;
|
---|
520 | }
|
---|
521 | if (array->items != NULL && array->count > 0) {
|
---|
522 | memcpy(new_items, array->items, array->count * sizeof(JSON_Value*));
|
---|
523 | }
|
---|
524 | parson_free(array->items);
|
---|
525 | array->items = new_items;
|
---|
526 | array->capacity = new_capacity;
|
---|
527 | return JSONSuccess;
|
---|
528 | }
|
---|
529 |
|
---|
530 | static void json_array_free(JSON_Array *array) {
|
---|
531 | size_t i;
|
---|
532 | for (i = 0; i < array->count; i++) {
|
---|
533 | json_value_free(array->items[i]);
|
---|
534 | }
|
---|
535 | parson_free(array->items);
|
---|
536 | parson_free(array);
|
---|
537 | }
|
---|
538 |
|
---|
539 | /* JSON Value */
|
---|
540 | static JSON_Value * json_value_init_string_no_copy(char *string) {
|
---|
541 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
---|
542 | if (!new_value) {
|
---|
543 | return NULL;
|
---|
544 | }
|
---|
545 | new_value->parent = NULL;
|
---|
546 | new_value->type = JSONString;
|
---|
547 | new_value->value.string = string;
|
---|
548 | return new_value;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* Parser */
|
---|
552 | static JSON_Status skip_quotes(const char **string) {
|
---|
553 | if (**string != '\"') {
|
---|
554 | return JSONFailure;
|
---|
555 | }
|
---|
556 | SKIP_CHAR(string);
|
---|
557 | while (**string != '\"') {
|
---|
558 | if (**string == '\0') {
|
---|
559 | return JSONFailure;
|
---|
560 | } else if (**string == '\\') {
|
---|
561 | SKIP_CHAR(string);
|
---|
562 | if (**string == '\0') {
|
---|
563 | return JSONFailure;
|
---|
564 | }
|
---|
565 | }
|
---|
566 | SKIP_CHAR(string);
|
---|
567 | }
|
---|
568 | SKIP_CHAR(string);
|
---|
569 | return JSONSuccess;
|
---|
570 | }
|
---|
571 |
|
---|
572 | static int parse_utf16(const char **unprocessed, char **processed) {
|
---|
573 | unsigned int cp, lead, trail;
|
---|
574 | int parse_succeeded = 0;
|
---|
575 | char *processed_ptr = *processed;
|
---|
576 | const char *unprocessed_ptr = *unprocessed;
|
---|
577 | unprocessed_ptr++; /* skips u */
|
---|
578 | parse_succeeded = parse_utf16_hex(unprocessed_ptr, &cp);
|
---|
579 | if (!parse_succeeded) {
|
---|
580 | return JSONFailure;
|
---|
581 | }
|
---|
582 | if (cp < 0x80) {
|
---|
583 | processed_ptr[0] = (char)cp; /* 0xxxxxxx */
|
---|
584 | } else if (cp < 0x800) {
|
---|
585 | processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
|
---|
586 | processed_ptr[1] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
|
---|
587 | processed_ptr += 1;
|
---|
588 | } else if (cp < 0xD800 || cp > 0xDFFF) {
|
---|
589 | processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
|
---|
590 | processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
|
---|
591 | processed_ptr[2] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
|
---|
592 | processed_ptr += 2;
|
---|
593 | } else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
|
---|
594 | lead = cp;
|
---|
595 | unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
|
---|
596 | if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u') {
|
---|
597 | return JSONFailure;
|
---|
598 | }
|
---|
599 | parse_succeeded = parse_utf16_hex(unprocessed_ptr, &trail);
|
---|
600 | if (!parse_succeeded || trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
|
---|
601 | return JSONFailure;
|
---|
602 | }
|
---|
603 | cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) + 0x010000;
|
---|
604 | processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
|
---|
605 | processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
|
---|
606 | processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
|
---|
607 | processed_ptr[3] = (((cp) & 0x3F) | 0x80); /* 10xxxxxx */
|
---|
608 | processed_ptr += 3;
|
---|
609 | } else { /* trail surrogate before lead surrogate */
|
---|
610 | return JSONFailure;
|
---|
611 | }
|
---|
612 | unprocessed_ptr += 3;
|
---|
613 | *processed = processed_ptr;
|
---|
614 | *unprocessed = unprocessed_ptr;
|
---|
615 | return JSONSuccess;
|
---|
616 | }
|
---|
617 |
|
---|
618 |
|
---|
619 | /* Copies and processes passed string up to supplied length.
|
---|
620 | Example: "\u006Corem ipsum" -> lorem ipsum */
|
---|
621 | static char* process_string(const char *input, size_t len) {
|
---|
622 | const char *input_ptr = input;
|
---|
623 | size_t initial_size = (len + 1) * sizeof(char);
|
---|
624 | size_t final_size = 0;
|
---|
625 | char *output = NULL, *output_ptr = NULL, *resized_output = NULL;
|
---|
626 | output = (char*)parson_malloc(initial_size);
|
---|
627 | if (output == NULL) {
|
---|
628 | goto error;
|
---|
629 | }
|
---|
630 | output_ptr = output;
|
---|
631 | while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) {
|
---|
632 | if (*input_ptr == '\\') {
|
---|
633 | input_ptr++;
|
---|
634 | switch (*input_ptr) {
|
---|
635 | case '\"': *output_ptr = '\"'; break;
|
---|
636 | case '\\': *output_ptr = '\\'; break;
|
---|
637 | case '/': *output_ptr = '/'; break;
|
---|
638 | case 'b': *output_ptr = '\b'; break;
|
---|
639 | case 'f': *output_ptr = '\f'; break;
|
---|
640 | case 'n': *output_ptr = '\n'; break;
|
---|
641 | case 'r': *output_ptr = '\r'; break;
|
---|
642 | case 't': *output_ptr = '\t'; break;
|
---|
643 | case 'u':
|
---|
644 | if (parse_utf16(&input_ptr, &output_ptr) == JSONFailure) {
|
---|
645 | goto error;
|
---|
646 | }
|
---|
647 | break;
|
---|
648 | default:
|
---|
649 | goto error;
|
---|
650 | }
|
---|
651 | } else if ((unsigned char)*input_ptr < 0x20) {
|
---|
652 | goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */
|
---|
653 | } else {
|
---|
654 | *output_ptr = *input_ptr;
|
---|
655 | }
|
---|
656 | output_ptr++;
|
---|
657 | input_ptr++;
|
---|
658 | }
|
---|
659 | *output_ptr = '\0';
|
---|
660 | /* resize to new length */
|
---|
661 | final_size = (size_t)(output_ptr-output) + 1;
|
---|
662 | /* todo: don't resize if final_size == initial_size */
|
---|
663 | resized_output = (char*)parson_malloc(final_size);
|
---|
664 | if (resized_output == NULL) {
|
---|
665 | goto error;
|
---|
666 | }
|
---|
667 | memcpy(resized_output, output, final_size);
|
---|
668 | parson_free(output);
|
---|
669 | return resized_output;
|
---|
670 | error:
|
---|
671 | parson_free(output);
|
---|
672 | return NULL;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /* Return processed contents of a string between quotes and
|
---|
676 | skips passed argument to a matching quote. */
|
---|
677 | static char * get_quoted_string(const char **string) {
|
---|
678 | const char *string_start = *string;
|
---|
679 | size_t string_len = 0;
|
---|
680 | JSON_Status status = skip_quotes(string);
|
---|
681 | if (status != JSONSuccess) {
|
---|
682 | return NULL;
|
---|
683 | }
|
---|
684 | string_len = *string - string_start - 2; /* length without quotes */
|
---|
685 | return process_string(string_start + 1, string_len);
|
---|
686 | }
|
---|
687 |
|
---|
688 | static JSON_Value * parse_value(const char **string, size_t nesting) {
|
---|
689 | if (nesting > MAX_NESTING) {
|
---|
690 | return NULL;
|
---|
691 | }
|
---|
692 | SKIP_WHITESPACES(string);
|
---|
693 | switch (**string) {
|
---|
694 | case '{':
|
---|
695 | return parse_object_value(string, nesting + 1);
|
---|
696 | case '[':
|
---|
697 | return parse_array_value(string, nesting + 1);
|
---|
698 | case '\"':
|
---|
699 | return parse_string_value(string);
|
---|
700 | case 'f': case 't':
|
---|
701 | return parse_boolean_value(string);
|
---|
702 | case '-':
|
---|
703 | case '0': case '1': case '2': case '3': case '4':
|
---|
704 | case '5': case '6': case '7': case '8': case '9':
|
---|
705 | return parse_number_value(string);
|
---|
706 | case 'n':
|
---|
707 | return parse_null_value(string);
|
---|
708 | default:
|
---|
709 | return NULL;
|
---|
710 | }
|
---|
711 | }
|
---|
712 |
|
---|
713 | static JSON_Value * parse_object_value(const char **string, size_t nesting) {
|
---|
714 | JSON_Value *output_value = NULL, *new_value = NULL;
|
---|
715 | JSON_Object *output_object = NULL;
|
---|
716 | char *new_key = NULL;
|
---|
717 | output_value = json_value_init_object();
|
---|
718 | if (output_value == NULL) {
|
---|
719 | return NULL;
|
---|
720 | }
|
---|
721 | if (**string != '{') {
|
---|
722 | json_value_free(output_value);
|
---|
723 | return NULL;
|
---|
724 | }
|
---|
725 | output_object = json_value_get_object(output_value);
|
---|
726 | SKIP_CHAR(string);
|
---|
727 | SKIP_WHITESPACES(string);
|
---|
728 | if (**string == '}') { /* empty object */
|
---|
729 | SKIP_CHAR(string);
|
---|
730 | return output_value;
|
---|
731 | }
|
---|
732 | while (**string != '\0') {
|
---|
733 | new_key = get_quoted_string(string);
|
---|
734 | if (new_key == NULL) {
|
---|
735 | json_value_free(output_value);
|
---|
736 | return NULL;
|
---|
737 | }
|
---|
738 | SKIP_WHITESPACES(string);
|
---|
739 | if (**string != ':') {
|
---|
740 | parson_free(new_key);
|
---|
741 | json_value_free(output_value);
|
---|
742 | return NULL;
|
---|
743 | }
|
---|
744 | SKIP_CHAR(string);
|
---|
745 | new_value = parse_value(string, nesting);
|
---|
746 | if (new_value == NULL) {
|
---|
747 | parson_free(new_key);
|
---|
748 | json_value_free(output_value);
|
---|
749 | return NULL;
|
---|
750 | }
|
---|
751 | if (json_object_add(output_object, new_key, new_value) == JSONFailure) {
|
---|
752 | parson_free(new_key);
|
---|
753 | json_value_free(new_value);
|
---|
754 | json_value_free(output_value);
|
---|
755 | return NULL;
|
---|
756 | }
|
---|
757 | parson_free(new_key);
|
---|
758 | SKIP_WHITESPACES(string);
|
---|
759 | if (**string != ',') {
|
---|
760 | break;
|
---|
761 | }
|
---|
762 | SKIP_CHAR(string);
|
---|
763 | SKIP_WHITESPACES(string);
|
---|
764 | }
|
---|
765 | SKIP_WHITESPACES(string);
|
---|
766 | if (**string != '}' || /* Trim object after parsing is over */
|
---|
767 | json_object_resize(output_object, json_object_get_count(output_object)) == JSONFailure) {
|
---|
768 | json_value_free(output_value);
|
---|
769 | return NULL;
|
---|
770 | }
|
---|
771 | SKIP_CHAR(string);
|
---|
772 | return output_value;
|
---|
773 | }
|
---|
774 |
|
---|
775 | static JSON_Value * parse_array_value(const char **string, size_t nesting) {
|
---|
776 | JSON_Value *output_value = NULL, *new_array_value = NULL;
|
---|
777 | JSON_Array *output_array = NULL;
|
---|
778 | output_value = json_value_init_array();
|
---|
779 | if (output_value == NULL) {
|
---|
780 | return NULL;
|
---|
781 | }
|
---|
782 | if (**string != '[') {
|
---|
783 | json_value_free(output_value);
|
---|
784 | return NULL;
|
---|
785 | }
|
---|
786 | output_array = json_value_get_array(output_value);
|
---|
787 | SKIP_CHAR(string);
|
---|
788 | SKIP_WHITESPACES(string);
|
---|
789 | if (**string == ']') { /* empty array */
|
---|
790 | SKIP_CHAR(string);
|
---|
791 | return output_value;
|
---|
792 | }
|
---|
793 | while (**string != '\0') {
|
---|
794 | new_array_value = parse_value(string, nesting);
|
---|
795 | if (new_array_value == NULL) {
|
---|
796 | json_value_free(output_value);
|
---|
797 | return NULL;
|
---|
798 | }
|
---|
799 | if (json_array_add(output_array, new_array_value) == JSONFailure) {
|
---|
800 | json_value_free(new_array_value);
|
---|
801 | json_value_free(output_value);
|
---|
802 | return NULL;
|
---|
803 | }
|
---|
804 | SKIP_WHITESPACES(string);
|
---|
805 | if (**string != ',') {
|
---|
806 | break;
|
---|
807 | }
|
---|
808 | SKIP_CHAR(string);
|
---|
809 | SKIP_WHITESPACES(string);
|
---|
810 | }
|
---|
811 | SKIP_WHITESPACES(string);
|
---|
812 | if (**string != ']' || /* Trim array after parsing is over */
|
---|
813 | json_array_resize(output_array, json_array_get_count(output_array)) == JSONFailure) {
|
---|
814 | json_value_free(output_value);
|
---|
815 | return NULL;
|
---|
816 | }
|
---|
817 | SKIP_CHAR(string);
|
---|
818 | return output_value;
|
---|
819 | }
|
---|
820 |
|
---|
821 | static JSON_Value * parse_string_value(const char **string) {
|
---|
822 | JSON_Value *value = NULL;
|
---|
823 | char *new_string = get_quoted_string(string);
|
---|
824 | if (new_string == NULL) {
|
---|
825 | return NULL;
|
---|
826 | }
|
---|
827 | value = json_value_init_string_no_copy(new_string);
|
---|
828 | if (value == NULL) {
|
---|
829 | parson_free(new_string);
|
---|
830 | return NULL;
|
---|
831 | }
|
---|
832 | return value;
|
---|
833 | }
|
---|
834 |
|
---|
835 | static JSON_Value * parse_boolean_value(const char **string) {
|
---|
836 | size_t true_token_size = SIZEOF_TOKEN("true");
|
---|
837 | size_t false_token_size = SIZEOF_TOKEN("false");
|
---|
838 | if (strncmp("true", *string, true_token_size) == 0) {
|
---|
839 | *string += true_token_size;
|
---|
840 | return json_value_init_boolean(1);
|
---|
841 | } else if (strncmp("false", *string, false_token_size) == 0) {
|
---|
842 | *string += false_token_size;
|
---|
843 | return json_value_init_boolean(0);
|
---|
844 | }
|
---|
845 | return NULL;
|
---|
846 | }
|
---|
847 |
|
---|
848 | static JSON_Value * parse_number_value(const char **string) {
|
---|
849 | char *end;
|
---|
850 | double number = 0;
|
---|
851 | errno = 0;
|
---|
852 | number = strtod(*string, &end);
|
---|
853 | if (errno || !is_decimal(*string, end - *string)) {
|
---|
854 | return NULL;
|
---|
855 | }
|
---|
856 | *string = end;
|
---|
857 | return json_value_init_number(number);
|
---|
858 | }
|
---|
859 |
|
---|
860 | static JSON_Value * parse_null_value(const char **string) {
|
---|
861 | size_t token_size = SIZEOF_TOKEN("null");
|
---|
862 | if (strncmp("null", *string, token_size) == 0) {
|
---|
863 | *string += token_size;
|
---|
864 | return json_value_init_null();
|
---|
865 | }
|
---|
866 | return NULL;
|
---|
867 | }
|
---|
868 |
|
---|
869 | /* Serialization */
|
---|
870 | #define APPEND_STRING(str) do { written = append_string(buf, (str));\
|
---|
871 | if (written < 0) { return -1; }\
|
---|
872 | if (buf != NULL) { buf += written; }\
|
---|
873 | written_total += written; } while(0)
|
---|
874 |
|
---|
875 | #define APPEND_INDENT(level) do { written = append_indent(buf, (level));\
|
---|
876 | if (written < 0) { return -1; }\
|
---|
877 | if (buf != NULL) { buf += written; }\
|
---|
878 | written_total += written; } while(0)
|
---|
879 |
|
---|
880 | static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf)
|
---|
881 | {
|
---|
882 | const char *key = NULL, *string = NULL;
|
---|
883 | JSON_Value *temp_value = NULL;
|
---|
884 | JSON_Array *array = NULL;
|
---|
885 | JSON_Object *object = NULL;
|
---|
886 | size_t i = 0, count = 0;
|
---|
887 | double num = 0.0;
|
---|
888 | int written = -1, written_total = 0;
|
---|
889 |
|
---|
890 | switch (json_value_get_type(value)) {
|
---|
891 | case JSONArray:
|
---|
892 | array = json_value_get_array(value);
|
---|
893 | count = json_array_get_count(array);
|
---|
894 | APPEND_STRING("[");
|
---|
895 | if (count > 0 && is_pretty) {
|
---|
896 | APPEND_STRING("\n");
|
---|
897 | }
|
---|
898 | for (i = 0; i < count; i++) {
|
---|
899 | if (is_pretty) {
|
---|
900 | APPEND_INDENT(level+1);
|
---|
901 | }
|
---|
902 | temp_value = json_array_get_value(array, i);
|
---|
903 | written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
|
---|
904 | if (written < 0) {
|
---|
905 | return -1;
|
---|
906 | }
|
---|
907 | if (buf != NULL) {
|
---|
908 | buf += written;
|
---|
909 | }
|
---|
910 | written_total += written;
|
---|
911 | if (i < (count - 1)) {
|
---|
912 | APPEND_STRING(",");
|
---|
913 | }
|
---|
914 | if (is_pretty) {
|
---|
915 | APPEND_STRING("\n");
|
---|
916 | }
|
---|
917 | }
|
---|
918 | if (count > 0 && is_pretty) {
|
---|
919 | APPEND_INDENT(level);
|
---|
920 | }
|
---|
921 | APPEND_STRING("]");
|
---|
922 | return written_total;
|
---|
923 | case JSONObject:
|
---|
924 | object = json_value_get_object(value);
|
---|
925 | count = json_object_get_count(object);
|
---|
926 | APPEND_STRING("{");
|
---|
927 | if (count > 0 && is_pretty) {
|
---|
928 | APPEND_STRING("\n");
|
---|
929 | }
|
---|
930 | for (i = 0; i < count; i++) {
|
---|
931 | key = json_object_get_name(object, i);
|
---|
932 | if (key == NULL) {
|
---|
933 | return -1;
|
---|
934 | }
|
---|
935 | if (is_pretty) {
|
---|
936 | APPEND_INDENT(level+1);
|
---|
937 | }
|
---|
938 | written = json_serialize_string(key, buf);
|
---|
939 | if (written < 0) {
|
---|
940 | return -1;
|
---|
941 | }
|
---|
942 | if (buf != NULL) {
|
---|
943 | buf += written;
|
---|
944 | }
|
---|
945 | written_total += written;
|
---|
946 | APPEND_STRING(":");
|
---|
947 | if (is_pretty) {
|
---|
948 | APPEND_STRING(" ");
|
---|
949 | }
|
---|
950 | temp_value = json_object_get_value(object, key);
|
---|
951 | written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
|
---|
952 | if (written < 0) {
|
---|
953 | return -1;
|
---|
954 | }
|
---|
955 | if (buf != NULL) {
|
---|
956 | buf += written;
|
---|
957 | }
|
---|
958 | written_total += written;
|
---|
959 | if (i < (count - 1)) {
|
---|
960 | APPEND_STRING(",");
|
---|
961 | }
|
---|
962 | if (is_pretty) {
|
---|
963 | APPEND_STRING("\n");
|
---|
964 | }
|
---|
965 | }
|
---|
966 | if (count > 0 && is_pretty) {
|
---|
967 | APPEND_INDENT(level);
|
---|
968 | }
|
---|
969 | APPEND_STRING("}");
|
---|
970 | return written_total;
|
---|
971 | case JSONString:
|
---|
972 | string = json_value_get_string(value);
|
---|
973 | if (string == NULL) {
|
---|
974 | return -1;
|
---|
975 | }
|
---|
976 | written = json_serialize_string(string, buf);
|
---|
977 | if (written < 0) {
|
---|
978 | return -1;
|
---|
979 | }
|
---|
980 | if (buf != NULL) {
|
---|
981 | buf += written;
|
---|
982 | }
|
---|
983 | written_total += written;
|
---|
984 | return written_total;
|
---|
985 | case JSONBoolean:
|
---|
986 | if (json_value_get_boolean(value)) {
|
---|
987 | APPEND_STRING("true");
|
---|
988 | } else {
|
---|
989 | APPEND_STRING("false");
|
---|
990 | }
|
---|
991 | return written_total;
|
---|
992 | case JSONNumber:
|
---|
993 | num = json_value_get_number(value);
|
---|
994 | if (buf != NULL) {
|
---|
995 | num_buf = buf;
|
---|
996 | }
|
---|
997 | written = sprintf(num_buf, FLOAT_FORMAT, num);
|
---|
998 | if (written < 0) {
|
---|
999 | return -1;
|
---|
1000 | }
|
---|
1001 | if (buf != NULL) {
|
---|
1002 | buf += written;
|
---|
1003 | }
|
---|
1004 | written_total += written;
|
---|
1005 | return written_total;
|
---|
1006 | case JSONNull:
|
---|
1007 | APPEND_STRING("null");
|
---|
1008 | return written_total;
|
---|
1009 | case JSONError:
|
---|
1010 | return -1;
|
---|
1011 | default:
|
---|
1012 | return -1;
|
---|
1013 | }
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | static int json_serialize_string(const char *string, char *buf) {
|
---|
1017 | size_t i = 0, len = strlen(string);
|
---|
1018 | char c = '\0';
|
---|
1019 | int written = -1, written_total = 0;
|
---|
1020 | APPEND_STRING("\"");
|
---|
1021 | for (i = 0; i < len; i++) {
|
---|
1022 | c = string[i];
|
---|
1023 | switch (c) {
|
---|
1024 | case '\"': APPEND_STRING("\\\""); break;
|
---|
1025 | case '\\': APPEND_STRING("\\\\"); break;
|
---|
1026 | case '\b': APPEND_STRING("\\b"); break;
|
---|
1027 | case '\f': APPEND_STRING("\\f"); break;
|
---|
1028 | case '\n': APPEND_STRING("\\n"); break;
|
---|
1029 | case '\r': APPEND_STRING("\\r"); break;
|
---|
1030 | case '\t': APPEND_STRING("\\t"); break;
|
---|
1031 | case '\x00': APPEND_STRING("\\u0000"); break;
|
---|
1032 | case '\x01': APPEND_STRING("\\u0001"); break;
|
---|
1033 | case '\x02': APPEND_STRING("\\u0002"); break;
|
---|
1034 | case '\x03': APPEND_STRING("\\u0003"); break;
|
---|
1035 | case '\x04': APPEND_STRING("\\u0004"); break;
|
---|
1036 | case '\x05': APPEND_STRING("\\u0005"); break;
|
---|
1037 | case '\x06': APPEND_STRING("\\u0006"); break;
|
---|
1038 | case '\x07': APPEND_STRING("\\u0007"); break;
|
---|
1039 | /* '\x08' duplicate: '\b' */
|
---|
1040 | /* '\x09' duplicate: '\t' */
|
---|
1041 | /* '\x0a' duplicate: '\n' */
|
---|
1042 | case '\x0b': APPEND_STRING("\\u000b"); break;
|
---|
1043 | /* '\x0c' duplicate: '\f' */
|
---|
1044 | /* '\x0d' duplicate: '\r' */
|
---|
1045 | case '\x0e': APPEND_STRING("\\u000e"); break;
|
---|
1046 | case '\x0f': APPEND_STRING("\\u000f"); break;
|
---|
1047 | case '\x10': APPEND_STRING("\\u0010"); break;
|
---|
1048 | case '\x11': APPEND_STRING("\\u0011"); break;
|
---|
1049 | case '\x12': APPEND_STRING("\\u0012"); break;
|
---|
1050 | case '\x13': APPEND_STRING("\\u0013"); break;
|
---|
1051 | case '\x14': APPEND_STRING("\\u0014"); break;
|
---|
1052 | case '\x15': APPEND_STRING("\\u0015"); break;
|
---|
1053 | case '\x16': APPEND_STRING("\\u0016"); break;
|
---|
1054 | case '\x17': APPEND_STRING("\\u0017"); break;
|
---|
1055 | case '\x18': APPEND_STRING("\\u0018"); break;
|
---|
1056 | case '\x19': APPEND_STRING("\\u0019"); break;
|
---|
1057 | case '\x1a': APPEND_STRING("\\u001a"); break;
|
---|
1058 | case '\x1b': APPEND_STRING("\\u001b"); break;
|
---|
1059 | case '\x1c': APPEND_STRING("\\u001c"); break;
|
---|
1060 | case '\x1d': APPEND_STRING("\\u001d"); break;
|
---|
1061 | case '\x1e': APPEND_STRING("\\u001e"); break;
|
---|
1062 | case '\x1f': APPEND_STRING("\\u001f"); break;
|
---|
1063 | case '/':
|
---|
1064 | if (parson_escape_slashes) {
|
---|
1065 | APPEND_STRING("\\/"); /* to make json embeddable in xml\/html */
|
---|
1066 | } else {
|
---|
1067 | APPEND_STRING("/");
|
---|
1068 | }
|
---|
1069 | break;
|
---|
1070 | default:
|
---|
1071 | if (buf != NULL) {
|
---|
1072 | buf[0] = c;
|
---|
1073 | buf += 1;
|
---|
1074 | }
|
---|
1075 | written_total += 1;
|
---|
1076 | break;
|
---|
1077 | }
|
---|
1078 | }
|
---|
1079 | APPEND_STRING("\"");
|
---|
1080 | return written_total;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | static int append_indent(char *buf, int level) {
|
---|
1084 | int i;
|
---|
1085 | int written = -1, written_total = 0;
|
---|
1086 | for (i = 0; i < level; i++) {
|
---|
1087 | APPEND_STRING(" ");
|
---|
1088 | }
|
---|
1089 | return written_total;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | static int append_string(char *buf, const char *string) {
|
---|
1093 | if (buf == NULL) {
|
---|
1094 | return (int)strlen(string);
|
---|
1095 | }
|
---|
1096 | return sprintf(buf, "%s", string);
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | #undef APPEND_STRING
|
---|
1100 | #undef APPEND_INDENT
|
---|
1101 |
|
---|
1102 | /* Parser API */
|
---|
1103 | JSON_Value * json_parse_file(const char *filename) {
|
---|
1104 | char *file_contents = read_file(filename);
|
---|
1105 | JSON_Value *output_value = NULL;
|
---|
1106 | if (file_contents == NULL) {
|
---|
1107 | return NULL;
|
---|
1108 | }
|
---|
1109 | output_value = json_parse_string(file_contents);
|
---|
1110 | parson_free(file_contents);
|
---|
1111 | return output_value;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | JSON_Value * json_parse_file_with_comments(const char *filename) {
|
---|
1115 | char *file_contents = read_file(filename);
|
---|
1116 | JSON_Value *output_value = NULL;
|
---|
1117 | if (file_contents == NULL) {
|
---|
1118 | return NULL;
|
---|
1119 | }
|
---|
1120 | output_value = json_parse_string_with_comments(file_contents);
|
---|
1121 | parson_free(file_contents);
|
---|
1122 | return output_value;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | JSON_Value * json_parse_string(const char *string) {
|
---|
1126 | if (string == NULL) {
|
---|
1127 | return NULL;
|
---|
1128 | }
|
---|
1129 | if (string[0] == '\xEF' && string[1] == '\xBB' && string[2] == '\xBF') {
|
---|
1130 | string = string + 3; /* Support for UTF-8 BOM */
|
---|
1131 | }
|
---|
1132 | return parse_value((const char**)&string, 0);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | JSON_Value * json_parse_string_with_comments(const char *string) {
|
---|
1136 | JSON_Value *result = NULL;
|
---|
1137 | char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL;
|
---|
1138 | string_mutable_copy = parson_strdup(string);
|
---|
1139 | if (string_mutable_copy == NULL) {
|
---|
1140 | return NULL;
|
---|
1141 | }
|
---|
1142 | remove_comments(string_mutable_copy, "/*", "*/");
|
---|
1143 | remove_comments(string_mutable_copy, "//", "\n");
|
---|
1144 | string_mutable_copy_ptr = string_mutable_copy;
|
---|
1145 | result = parse_value((const char**)&string_mutable_copy_ptr, 0);
|
---|
1146 | parson_free(string_mutable_copy);
|
---|
1147 | return result;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /* JSON Object API */
|
---|
1151 |
|
---|
1152 | JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) {
|
---|
1153 | if (object == NULL || name == NULL) {
|
---|
1154 | return NULL;
|
---|
1155 | }
|
---|
1156 | return json_object_getn_value(object, name, strlen(name));
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | const char * json_object_get_string(const JSON_Object *object, const char *name) {
|
---|
1160 | return json_value_get_string(json_object_get_value(object, name));
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | double json_object_get_number(const JSON_Object *object, const char *name) {
|
---|
1164 | return json_value_get_number(json_object_get_value(object, name));
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) {
|
---|
1168 | return json_value_get_object(json_object_get_value(object, name));
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) {
|
---|
1172 | return json_value_get_array(json_object_get_value(object, name));
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | int json_object_get_boolean(const JSON_Object *object, const char *name) {
|
---|
1176 | return json_value_get_boolean(json_object_get_value(object, name));
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) {
|
---|
1180 | const char *dot_position = strchr(name, '.');
|
---|
1181 | if (!dot_position) {
|
---|
1182 | return json_object_get_value(object, name);
|
---|
1183 | }
|
---|
1184 | object = json_value_get_object(json_object_getn_value(object, name, dot_position - name));
|
---|
1185 | return json_object_dotget_value(object, dot_position + 1);
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | const char * json_object_dotget_string(const JSON_Object *object, const char *name) {
|
---|
1189 | return json_value_get_string(json_object_dotget_value(object, name));
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | double json_object_dotget_number(const JSON_Object *object, const char *name) {
|
---|
1193 | return json_value_get_number(json_object_dotget_value(object, name));
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) {
|
---|
1197 | return json_value_get_object(json_object_dotget_value(object, name));
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) {
|
---|
1201 | return json_value_get_array(json_object_dotget_value(object, name));
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | int json_object_dotget_boolean(const JSON_Object *object, const char *name) {
|
---|
1205 | return json_value_get_boolean(json_object_dotget_value(object, name));
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | size_t json_object_get_count(const JSON_Object *object) {
|
---|
1209 | return object ? object->count : 0;
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | const char * json_object_get_name(const JSON_Object *object, size_t index) {
|
---|
1213 | if (object == NULL || index >= json_object_get_count(object)) {
|
---|
1214 | return NULL;
|
---|
1215 | }
|
---|
1216 | return object->names[index];
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) {
|
---|
1220 | if (object == NULL || index >= json_object_get_count(object)) {
|
---|
1221 | return NULL;
|
---|
1222 | }
|
---|
1223 | return object->values[index];
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | JSON_Value *json_object_get_wrapping_value(const JSON_Object *object) {
|
---|
1227 | return object->wrapping_value;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | int json_object_has_value (const JSON_Object *object, const char *name) {
|
---|
1231 | return json_object_get_value(object, name) != NULL;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) {
|
---|
1235 | JSON_Value *val = json_object_get_value(object, name);
|
---|
1236 | return val != NULL && json_value_get_type(val) == type;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | int json_object_dothas_value (const JSON_Object *object, const char *name) {
|
---|
1240 | return json_object_dotget_value(object, name) != NULL;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) {
|
---|
1244 | JSON_Value *val = json_object_dotget_value(object, name);
|
---|
1245 | return val != NULL && json_value_get_type(val) == type;
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | /* JSON Array API */
|
---|
1249 | JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) {
|
---|
1250 | if (array == NULL || index >= json_array_get_count(array)) {
|
---|
1251 | return NULL;
|
---|
1252 | }
|
---|
1253 | return array->items[index];
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | const char * json_array_get_string(const JSON_Array *array, size_t index) {
|
---|
1257 | return json_value_get_string(json_array_get_value(array, index));
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | double json_array_get_number(const JSON_Array *array, size_t index) {
|
---|
1261 | return json_value_get_number(json_array_get_value(array, index));
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) {
|
---|
1265 | return json_value_get_object(json_array_get_value(array, index));
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) {
|
---|
1269 | return json_value_get_array(json_array_get_value(array, index));
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | int json_array_get_boolean(const JSON_Array *array, size_t index) {
|
---|
1273 | return json_value_get_boolean(json_array_get_value(array, index));
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | size_t json_array_get_count(const JSON_Array *array) {
|
---|
1277 | return array ? array->count : 0;
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | JSON_Value * json_array_get_wrapping_value(const JSON_Array *array) {
|
---|
1281 | return array->wrapping_value;
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | /* JSON Value API */
|
---|
1285 | JSON_Value_Type json_value_get_type(const JSON_Value *value) {
|
---|
1286 | return value ? value->type : JSONError;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | JSON_Object * json_value_get_object(const JSON_Value *value) {
|
---|
1290 | return json_value_get_type(value) == JSONObject ? value->value.object : NULL;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | JSON_Array * json_value_get_array(const JSON_Value *value) {
|
---|
1294 | return json_value_get_type(value) == JSONArray ? value->value.array : NULL;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | const char * json_value_get_string(const JSON_Value *value) {
|
---|
1298 | return json_value_get_type(value) == JSONString ? value->value.string : NULL;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | double json_value_get_number(const JSON_Value *value) {
|
---|
1302 | return json_value_get_type(value) == JSONNumber ? value->value.number : 0;
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | int json_value_get_boolean(const JSON_Value *value) {
|
---|
1306 | return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1;
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | JSON_Value * json_value_get_parent (const JSON_Value *value) {
|
---|
1310 | return value ? value->parent : NULL;
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | void json_value_free(JSON_Value *value) {
|
---|
1314 | switch (json_value_get_type(value)) {
|
---|
1315 | case JSONObject:
|
---|
1316 | json_object_free(value->value.object);
|
---|
1317 | break;
|
---|
1318 | case JSONString:
|
---|
1319 | parson_free(value->value.string);
|
---|
1320 | break;
|
---|
1321 | case JSONArray:
|
---|
1322 | json_array_free(value->value.array);
|
---|
1323 | break;
|
---|
1324 | default:
|
---|
1325 | break;
|
---|
1326 | }
|
---|
1327 | parson_free(value);
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | JSON_Value * json_value_init_object(void) {
|
---|
1331 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
---|
1332 | if (!new_value) {
|
---|
1333 | return NULL;
|
---|
1334 | }
|
---|
1335 | new_value->parent = NULL;
|
---|
1336 | new_value->type = JSONObject;
|
---|
1337 | new_value->value.object = json_object_init(new_value);
|
---|
1338 | if (!new_value->value.object) {
|
---|
1339 | parson_free(new_value);
|
---|
1340 | return NULL;
|
---|
1341 | }
|
---|
1342 | return new_value;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | JSON_Value * json_value_init_array(void) {
|
---|
1346 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
---|
1347 | if (!new_value) {
|
---|
1348 | return NULL;
|
---|
1349 | }
|
---|
1350 | new_value->parent = NULL;
|
---|
1351 | new_value->type = JSONArray;
|
---|
1352 | new_value->value.array = json_array_init(new_value);
|
---|
1353 | if (!new_value->value.array) {
|
---|
1354 | parson_free(new_value);
|
---|
1355 | return NULL;
|
---|
1356 | }
|
---|
1357 | return new_value;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | JSON_Value * json_value_init_string(const char *string) {
|
---|
1361 | char *copy = NULL;
|
---|
1362 | JSON_Value *value;
|
---|
1363 | size_t string_len = 0;
|
---|
1364 | if (string == NULL) {
|
---|
1365 | return NULL;
|
---|
1366 | }
|
---|
1367 | string_len = strlen(string);
|
---|
1368 | if (!is_valid_utf8(string, string_len)) {
|
---|
1369 | return NULL;
|
---|
1370 | }
|
---|
1371 | copy = parson_strndup(string, string_len);
|
---|
1372 | if (copy == NULL) {
|
---|
1373 | return NULL;
|
---|
1374 | }
|
---|
1375 | value = json_value_init_string_no_copy(copy);
|
---|
1376 | if (value == NULL) {
|
---|
1377 | parson_free(copy);
|
---|
1378 | }
|
---|
1379 | return value;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | JSON_Value * json_value_init_number(double number) {
|
---|
1383 | JSON_Value *new_value = NULL;
|
---|
1384 | if (IS_NUMBER_INVALID(number)) {
|
---|
1385 | return NULL;
|
---|
1386 | }
|
---|
1387 | new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
---|
1388 | if (new_value == NULL) {
|
---|
1389 | return NULL;
|
---|
1390 | }
|
---|
1391 | new_value->parent = NULL;
|
---|
1392 | new_value->type = JSONNumber;
|
---|
1393 | new_value->value.number = number;
|
---|
1394 | return new_value;
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 | JSON_Value * json_value_init_boolean(int boolean) {
|
---|
1398 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
---|
1399 | if (!new_value) {
|
---|
1400 | return NULL;
|
---|
1401 | }
|
---|
1402 | new_value->parent = NULL;
|
---|
1403 | new_value->type = JSONBoolean;
|
---|
1404 | new_value->value.boolean = boolean ? 1 : 0;
|
---|
1405 | return new_value;
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | JSON_Value * json_value_init_null(void) {
|
---|
1409 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
---|
1410 | if (!new_value) {
|
---|
1411 | return NULL;
|
---|
1412 | }
|
---|
1413 | new_value->parent = NULL;
|
---|
1414 | new_value->type = JSONNull;
|
---|
1415 | return new_value;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | JSON_Value * json_value_deep_copy(const JSON_Value *value) {
|
---|
1419 | size_t i = 0;
|
---|
1420 | JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL;
|
---|
1421 | const char *temp_string = NULL, *temp_key = NULL;
|
---|
1422 | char *temp_string_copy = NULL;
|
---|
1423 | JSON_Array *temp_array = NULL, *temp_array_copy = NULL;
|
---|
1424 | JSON_Object *temp_object = NULL, *temp_object_copy = NULL;
|
---|
1425 |
|
---|
1426 | switch (json_value_get_type(value)) {
|
---|
1427 | case JSONArray:
|
---|
1428 | temp_array = json_value_get_array(value);
|
---|
1429 | return_value = json_value_init_array();
|
---|
1430 | if (return_value == NULL) {
|
---|
1431 | return NULL;
|
---|
1432 | }
|
---|
1433 | temp_array_copy = json_value_get_array(return_value);
|
---|
1434 | for (i = 0; i < json_array_get_count(temp_array); i++) {
|
---|
1435 | temp_value = json_array_get_value(temp_array, i);
|
---|
1436 | temp_value_copy = json_value_deep_copy(temp_value);
|
---|
1437 | if (temp_value_copy == NULL) {
|
---|
1438 | json_value_free(return_value);
|
---|
1439 | return NULL;
|
---|
1440 | }
|
---|
1441 | if (json_array_add(temp_array_copy, temp_value_copy) == JSONFailure) {
|
---|
1442 | json_value_free(return_value);
|
---|
1443 | json_value_free(temp_value_copy);
|
---|
1444 | return NULL;
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 | return return_value;
|
---|
1448 | case JSONObject:
|
---|
1449 | temp_object = json_value_get_object(value);
|
---|
1450 | return_value = json_value_init_object();
|
---|
1451 | if (return_value == NULL) {
|
---|
1452 | return NULL;
|
---|
1453 | }
|
---|
1454 | temp_object_copy = json_value_get_object(return_value);
|
---|
1455 | for (i = 0; i < json_object_get_count(temp_object); i++) {
|
---|
1456 | temp_key = json_object_get_name(temp_object, i);
|
---|
1457 | temp_value = json_object_get_value(temp_object, temp_key);
|
---|
1458 | temp_value_copy = json_value_deep_copy(temp_value);
|
---|
1459 | if (temp_value_copy == NULL) {
|
---|
1460 | json_value_free(return_value);
|
---|
1461 | return NULL;
|
---|
1462 | }
|
---|
1463 | if (json_object_add(temp_object_copy, temp_key, temp_value_copy) == JSONFailure) {
|
---|
1464 | json_value_free(return_value);
|
---|
1465 | json_value_free(temp_value_copy);
|
---|
1466 | return NULL;
|
---|
1467 | }
|
---|
1468 | }
|
---|
1469 | return return_value;
|
---|
1470 | case JSONBoolean:
|
---|
1471 | return json_value_init_boolean(json_value_get_boolean(value));
|
---|
1472 | case JSONNumber:
|
---|
1473 | return json_value_init_number(json_value_get_number(value));
|
---|
1474 | case JSONString:
|
---|
1475 | temp_string = json_value_get_string(value);
|
---|
1476 | if (temp_string == NULL) {
|
---|
1477 | return NULL;
|
---|
1478 | }
|
---|
1479 | temp_string_copy = parson_strdup(temp_string);
|
---|
1480 | if (temp_string_copy == NULL) {
|
---|
1481 | return NULL;
|
---|
1482 | }
|
---|
1483 | return_value = json_value_init_string_no_copy(temp_string_copy);
|
---|
1484 | if (return_value == NULL) {
|
---|
1485 | parson_free(temp_string_copy);
|
---|
1486 | }
|
---|
1487 | return return_value;
|
---|
1488 | case JSONNull:
|
---|
1489 | return json_value_init_null();
|
---|
1490 | case JSONError:
|
---|
1491 | return NULL;
|
---|
1492 | default:
|
---|
1493 | return NULL;
|
---|
1494 | }
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | size_t json_serialization_size(const JSON_Value *value) {
|
---|
1498 | char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
|
---|
1499 | int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf);
|
---|
1500 | return res < 0 ? 0 : (size_t)(res + 1);
|
---|
1501 | }
|
---|
1502 |
|
---|
1503 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
|
---|
1504 | int written = -1;
|
---|
1505 | size_t needed_size_in_bytes = json_serialization_size(value);
|
---|
1506 | if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
|
---|
1507 | return JSONFailure;
|
---|
1508 | }
|
---|
1509 | written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL);
|
---|
1510 | if (written < 0) {
|
---|
1511 | return JSONFailure;
|
---|
1512 | }
|
---|
1513 | return JSONSuccess;
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) {
|
---|
1517 | JSON_Status return_code = JSONSuccess;
|
---|
1518 | FILE *fp = NULL;
|
---|
1519 | char *serialized_string = json_serialize_to_string(value);
|
---|
1520 | if (serialized_string == NULL) {
|
---|
1521 | return JSONFailure;
|
---|
1522 | }
|
---|
1523 | fp = fopen(filename, "w");
|
---|
1524 | if (fp == NULL) {
|
---|
1525 | json_free_serialized_string(serialized_string);
|
---|
1526 | return JSONFailure;
|
---|
1527 | }
|
---|
1528 | if (fputs(serialized_string, fp) == EOF) {
|
---|
1529 | return_code = JSONFailure;
|
---|
1530 | }
|
---|
1531 | if (fclose(fp) == EOF) {
|
---|
1532 | return_code = JSONFailure;
|
---|
1533 | }
|
---|
1534 | json_free_serialized_string(serialized_string);
|
---|
1535 | return return_code;
|
---|
1536 | }
|
---|
1537 |
|
---|
1538 | char * json_serialize_to_string(const JSON_Value *value) {
|
---|
1539 | JSON_Status serialization_result = JSONFailure;
|
---|
1540 | size_t buf_size_bytes = json_serialization_size(value);
|
---|
1541 | char *buf = NULL;
|
---|
1542 | if (buf_size_bytes == 0) {
|
---|
1543 | return NULL;
|
---|
1544 | }
|
---|
1545 | buf = (char*)parson_malloc(buf_size_bytes);
|
---|
1546 | if (buf == NULL) {
|
---|
1547 | return NULL;
|
---|
1548 | }
|
---|
1549 | serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes);
|
---|
1550 | if (serialization_result == JSONFailure) {
|
---|
1551 | json_free_serialized_string(buf);
|
---|
1552 | return NULL;
|
---|
1553 | }
|
---|
1554 | return buf;
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | size_t json_serialization_size_pretty(const JSON_Value *value) {
|
---|
1558 | char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
|
---|
1559 | int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf);
|
---|
1560 | return res < 0 ? 0 : (size_t)(res + 1);
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
|
---|
1564 | int written = -1;
|
---|
1565 | size_t needed_size_in_bytes = json_serialization_size_pretty(value);
|
---|
1566 | if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
|
---|
1567 | return JSONFailure;
|
---|
1568 | }
|
---|
1569 | written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL);
|
---|
1570 | if (written < 0) {
|
---|
1571 | return JSONFailure;
|
---|
1572 | }
|
---|
1573 | return JSONSuccess;
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) {
|
---|
1577 | JSON_Status return_code = JSONSuccess;
|
---|
1578 | FILE *fp = NULL;
|
---|
1579 | char *serialized_string = json_serialize_to_string_pretty(value);
|
---|
1580 | if (serialized_string == NULL) {
|
---|
1581 | return JSONFailure;
|
---|
1582 | }
|
---|
1583 | fp = fopen(filename, "w");
|
---|
1584 | if (fp == NULL) {
|
---|
1585 | json_free_serialized_string(serialized_string);
|
---|
1586 | return JSONFailure;
|
---|
1587 | }
|
---|
1588 | if (fputs(serialized_string, fp) == EOF) {
|
---|
1589 | return_code = JSONFailure;
|
---|
1590 | }
|
---|
1591 | if (fclose(fp) == EOF) {
|
---|
1592 | return_code = JSONFailure;
|
---|
1593 | }
|
---|
1594 | json_free_serialized_string(serialized_string);
|
---|
1595 | return return_code;
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | char * json_serialize_to_string_pretty(const JSON_Value *value) {
|
---|
1599 | JSON_Status serialization_result = JSONFailure;
|
---|
1600 | size_t buf_size_bytes = json_serialization_size_pretty(value);
|
---|
1601 | char *buf = NULL;
|
---|
1602 | if (buf_size_bytes == 0) {
|
---|
1603 | return NULL;
|
---|
1604 | }
|
---|
1605 | buf = (char*)parson_malloc(buf_size_bytes);
|
---|
1606 | if (buf == NULL) {
|
---|
1607 | return NULL;
|
---|
1608 | }
|
---|
1609 | serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes);
|
---|
1610 | if (serialization_result == JSONFailure) {
|
---|
1611 | json_free_serialized_string(buf);
|
---|
1612 | return NULL;
|
---|
1613 | }
|
---|
1614 | return buf;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | void json_free_serialized_string(char *string) {
|
---|
1618 | parson_free(string);
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | JSON_Status json_array_remove(JSON_Array *array, size_t ix) {
|
---|
1622 | size_t to_move_bytes = 0;
|
---|
1623 | if (array == NULL || ix >= json_array_get_count(array)) {
|
---|
1624 | return JSONFailure;
|
---|
1625 | }
|
---|
1626 | json_value_free(json_array_get_value(array, ix));
|
---|
1627 | to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value*);
|
---|
1628 | memmove(array->items + ix, array->items + ix + 1, to_move_bytes);
|
---|
1629 | array->count -= 1;
|
---|
1630 | return JSONSuccess;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value) {
|
---|
1634 | if (array == NULL || value == NULL || value->parent != NULL || ix >= json_array_get_count(array)) {
|
---|
1635 | return JSONFailure;
|
---|
1636 | }
|
---|
1637 | json_value_free(json_array_get_value(array, ix));
|
---|
1638 | value->parent = json_array_get_wrapping_value(array);
|
---|
1639 | array->items[ix] = value;
|
---|
1640 | return JSONSuccess;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) {
|
---|
1644 | JSON_Value *value = json_value_init_string(string);
|
---|
1645 | if (value == NULL) {
|
---|
1646 | return JSONFailure;
|
---|
1647 | }
|
---|
1648 | if (json_array_replace_value(array, i, value) == JSONFailure) {
|
---|
1649 | json_value_free(value);
|
---|
1650 | return JSONFailure;
|
---|
1651 | }
|
---|
1652 | return JSONSuccess;
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) {
|
---|
1656 | JSON_Value *value = json_value_init_number(number);
|
---|
1657 | if (value == NULL) {
|
---|
1658 | return JSONFailure;
|
---|
1659 | }
|
---|
1660 | if (json_array_replace_value(array, i, value) == JSONFailure) {
|
---|
1661 | json_value_free(value);
|
---|
1662 | return JSONFailure;
|
---|
1663 | }
|
---|
1664 | return JSONSuccess;
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) {
|
---|
1668 | JSON_Value *value = json_value_init_boolean(boolean);
|
---|
1669 | if (value == NULL) {
|
---|
1670 | return JSONFailure;
|
---|
1671 | }
|
---|
1672 | if (json_array_replace_value(array, i, value) == JSONFailure) {
|
---|
1673 | json_value_free(value);
|
---|
1674 | return JSONFailure;
|
---|
1675 | }
|
---|
1676 | return JSONSuccess;
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i) {
|
---|
1680 | JSON_Value *value = json_value_init_null();
|
---|
1681 | if (value == NULL) {
|
---|
1682 | return JSONFailure;
|
---|
1683 | }
|
---|
1684 | if (json_array_replace_value(array, i, value) == JSONFailure) {
|
---|
1685 | json_value_free(value);
|
---|
1686 | return JSONFailure;
|
---|
1687 | }
|
---|
1688 | return JSONSuccess;
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 | JSON_Status json_array_clear(JSON_Array *array) {
|
---|
1692 | size_t i = 0;
|
---|
1693 | if (array == NULL) {
|
---|
1694 | return JSONFailure;
|
---|
1695 | }
|
---|
1696 | for (i = 0; i < json_array_get_count(array); i++) {
|
---|
1697 | json_value_free(json_array_get_value(array, i));
|
---|
1698 | }
|
---|
1699 | array->count = 0;
|
---|
1700 | return JSONSuccess;
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) {
|
---|
1704 | if (array == NULL || value == NULL || value->parent != NULL) {
|
---|
1705 | return JSONFailure;
|
---|
1706 | }
|
---|
1707 | return json_array_add(array, value);
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | JSON_Status json_array_append_string(JSON_Array *array, const char *string) {
|
---|
1711 | JSON_Value *value = json_value_init_string(string);
|
---|
1712 | if (value == NULL) {
|
---|
1713 | return JSONFailure;
|
---|
1714 | }
|
---|
1715 | if (json_array_append_value(array, value) == JSONFailure) {
|
---|
1716 | json_value_free(value);
|
---|
1717 | return JSONFailure;
|
---|
1718 | }
|
---|
1719 | return JSONSuccess;
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 | JSON_Status json_array_append_number(JSON_Array *array, double number) {
|
---|
1723 | JSON_Value *value = json_value_init_number(number);
|
---|
1724 | if (value == NULL) {
|
---|
1725 | return JSONFailure;
|
---|
1726 | }
|
---|
1727 | if (json_array_append_value(array, value) == JSONFailure) {
|
---|
1728 | json_value_free(value);
|
---|
1729 | return JSONFailure;
|
---|
1730 | }
|
---|
1731 | return JSONSuccess;
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) {
|
---|
1735 | JSON_Value *value = json_value_init_boolean(boolean);
|
---|
1736 | if (value == NULL) {
|
---|
1737 | return JSONFailure;
|
---|
1738 | }
|
---|
1739 | if (json_array_append_value(array, value) == JSONFailure) {
|
---|
1740 | json_value_free(value);
|
---|
1741 | return JSONFailure;
|
---|
1742 | }
|
---|
1743 | return JSONSuccess;
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | JSON_Status json_array_append_null(JSON_Array *array) {
|
---|
1747 | JSON_Value *value = json_value_init_null();
|
---|
1748 | if (value == NULL) {
|
---|
1749 | return JSONFailure;
|
---|
1750 | }
|
---|
1751 | if (json_array_append_value(array, value) == JSONFailure) {
|
---|
1752 | json_value_free(value);
|
---|
1753 | return JSONFailure;
|
---|
1754 | }
|
---|
1755 | return JSONSuccess;
|
---|
1756 | }
|
---|
1757 |
|
---|
1758 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) {
|
---|
1759 | size_t i = 0;
|
---|
1760 | JSON_Value *old_value;
|
---|
1761 | if (object == NULL || name == NULL || value == NULL || value->parent != NULL) {
|
---|
1762 | return JSONFailure;
|
---|
1763 | }
|
---|
1764 | old_value = json_object_get_value(object, name);
|
---|
1765 | if (old_value != NULL) { /* free and overwrite old value */
|
---|
1766 | json_value_free(old_value);
|
---|
1767 | for (i = 0; i < json_object_get_count(object); i++) {
|
---|
1768 | if (strcmp(object->names[i], name) == 0) {
|
---|
1769 | value->parent = json_object_get_wrapping_value(object);
|
---|
1770 | object->values[i] = value;
|
---|
1771 | return JSONSuccess;
|
---|
1772 | }
|
---|
1773 | }
|
---|
1774 | }
|
---|
1775 | /* add new key value pair */
|
---|
1776 | return json_object_add(object, name, value);
|
---|
1777 | }
|
---|
1778 |
|
---|
1779 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) {
|
---|
1780 | return json_object_set_value(object, name, json_value_init_string(string));
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number) {
|
---|
1784 | return json_object_set_value(object, name, json_value_init_number(number));
|
---|
1785 | }
|
---|
1786 |
|
---|
1787 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) {
|
---|
1788 | return json_object_set_value(object, name, json_value_init_boolean(boolean));
|
---|
1789 | }
|
---|
1790 |
|
---|
1791 | JSON_Status json_object_set_null(JSON_Object *object, const char *name) {
|
---|
1792 | return json_object_set_value(object, name, json_value_init_null());
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) {
|
---|
1796 | const char *dot_pos = NULL;
|
---|
1797 | JSON_Value *temp_value = NULL, *new_value = NULL;
|
---|
1798 | JSON_Object *temp_object = NULL, *new_object = NULL;
|
---|
1799 | JSON_Status status = JSONFailure;
|
---|
1800 | size_t name_len = 0;
|
---|
1801 | if (object == NULL || name == NULL || value == NULL) {
|
---|
1802 | return JSONFailure;
|
---|
1803 | }
|
---|
1804 | dot_pos = strchr(name, '.');
|
---|
1805 | if (dot_pos == NULL) {
|
---|
1806 | return json_object_set_value(object, name, value);
|
---|
1807 | }
|
---|
1808 | name_len = dot_pos - name;
|
---|
1809 | temp_value = json_object_getn_value(object, name, name_len);
|
---|
1810 | if (temp_value) {
|
---|
1811 | /* Don't overwrite existing non-object (unlike json_object_set_value, but it shouldn't be changed at this point) */
|
---|
1812 | if (json_value_get_type(temp_value) != JSONObject) {
|
---|
1813 | return JSONFailure;
|
---|
1814 | }
|
---|
1815 | temp_object = json_value_get_object(temp_value);
|
---|
1816 | return json_object_dotset_value(temp_object, dot_pos + 1, value);
|
---|
1817 | }
|
---|
1818 | new_value = json_value_init_object();
|
---|
1819 | if (new_value == NULL) {
|
---|
1820 | return JSONFailure;
|
---|
1821 | }
|
---|
1822 | new_object = json_value_get_object(new_value);
|
---|
1823 | status = json_object_dotset_value(new_object, dot_pos + 1, value);
|
---|
1824 | if (status != JSONSuccess) {
|
---|
1825 | json_value_free(new_value);
|
---|
1826 | return JSONFailure;
|
---|
1827 | }
|
---|
1828 | status = json_object_addn(object, name, name_len, new_value);
|
---|
1829 | if (status != JSONSuccess) {
|
---|
1830 | json_object_dotremove_internal(new_object, dot_pos + 1, 0);
|
---|
1831 | json_value_free(new_value);
|
---|
1832 | return JSONFailure;
|
---|
1833 | }
|
---|
1834 | return JSONSuccess;
|
---|
1835 | }
|
---|
1836 |
|
---|
1837 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) {
|
---|
1838 | JSON_Value *value = json_value_init_string(string);
|
---|
1839 | if (value == NULL) {
|
---|
1840 | return JSONFailure;
|
---|
1841 | }
|
---|
1842 | if (json_object_dotset_value(object, name, value) == JSONFailure) {
|
---|
1843 | json_value_free(value);
|
---|
1844 | return JSONFailure;
|
---|
1845 | }
|
---|
1846 | return JSONSuccess;
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) {
|
---|
1850 | JSON_Value *value = json_value_init_number(number);
|
---|
1851 | if (value == NULL) {
|
---|
1852 | return JSONFailure;
|
---|
1853 | }
|
---|
1854 | if (json_object_dotset_value(object, name, value) == JSONFailure) {
|
---|
1855 | json_value_free(value);
|
---|
1856 | return JSONFailure;
|
---|
1857 | }
|
---|
1858 | return JSONSuccess;
|
---|
1859 | }
|
---|
1860 |
|
---|
1861 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) {
|
---|
1862 | JSON_Value *value = json_value_init_boolean(boolean);
|
---|
1863 | if (value == NULL) {
|
---|
1864 | return JSONFailure;
|
---|
1865 | }
|
---|
1866 | if (json_object_dotset_value(object, name, value) == JSONFailure) {
|
---|
1867 | json_value_free(value);
|
---|
1868 | return JSONFailure;
|
---|
1869 | }
|
---|
1870 | return JSONSuccess;
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) {
|
---|
1874 | JSON_Value *value = json_value_init_null();
|
---|
1875 | if (value == NULL) {
|
---|
1876 | return JSONFailure;
|
---|
1877 | }
|
---|
1878 | if (json_object_dotset_value(object, name, value) == JSONFailure) {
|
---|
1879 | json_value_free(value);
|
---|
1880 | return JSONFailure;
|
---|
1881 | }
|
---|
1882 | return JSONSuccess;
|
---|
1883 | }
|
---|
1884 |
|
---|
1885 | JSON_Status json_object_remove(JSON_Object *object, const char *name) {
|
---|
1886 | return json_object_remove_internal(object, name, 1);
|
---|
1887 | }
|
---|
1888 |
|
---|
1889 | JSON_Status json_object_dotremove(JSON_Object *object, const char *name) {
|
---|
1890 | return json_object_dotremove_internal(object, name, 1);
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 | JSON_Status json_object_clear(JSON_Object *object) {
|
---|
1894 | size_t i = 0;
|
---|
1895 | if (object == NULL) {
|
---|
1896 | return JSONFailure;
|
---|
1897 | }
|
---|
1898 | for (i = 0; i < json_object_get_count(object); i++) {
|
---|
1899 | parson_free(object->names[i]);
|
---|
1900 | json_value_free(object->values[i]);
|
---|
1901 | }
|
---|
1902 | object->count = 0;
|
---|
1903 | return JSONSuccess;
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) {
|
---|
1907 | JSON_Value *temp_schema_value = NULL, *temp_value = NULL;
|
---|
1908 | JSON_Array *schema_array = NULL, *value_array = NULL;
|
---|
1909 | JSON_Object *schema_object = NULL, *value_object = NULL;
|
---|
1910 | JSON_Value_Type schema_type = JSONError, value_type = JSONError;
|
---|
1911 | const char *key = NULL;
|
---|
1912 | size_t i = 0, count = 0;
|
---|
1913 | if (schema == NULL || value == NULL) {
|
---|
1914 | return JSONFailure;
|
---|
1915 | }
|
---|
1916 | schema_type = json_value_get_type(schema);
|
---|
1917 | value_type = json_value_get_type(value);
|
---|
1918 | if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */
|
---|
1919 | return JSONFailure;
|
---|
1920 | }
|
---|
1921 | switch (schema_type) {
|
---|
1922 | case JSONArray:
|
---|
1923 | schema_array = json_value_get_array(schema);
|
---|
1924 | value_array = json_value_get_array(value);
|
---|
1925 | count = json_array_get_count(schema_array);
|
---|
1926 | if (count == 0) {
|
---|
1927 | return JSONSuccess; /* Empty array allows all types */
|
---|
1928 | }
|
---|
1929 | /* Get first value from array, rest is ignored */
|
---|
1930 | temp_schema_value = json_array_get_value(schema_array, 0);
|
---|
1931 | for (i = 0; i < json_array_get_count(value_array); i++) {
|
---|
1932 | temp_value = json_array_get_value(value_array, i);
|
---|
1933 | if (json_validate(temp_schema_value, temp_value) == JSONFailure) {
|
---|
1934 | return JSONFailure;
|
---|
1935 | }
|
---|
1936 | }
|
---|
1937 | return JSONSuccess;
|
---|
1938 | case JSONObject:
|
---|
1939 | schema_object = json_value_get_object(schema);
|
---|
1940 | value_object = json_value_get_object(value);
|
---|
1941 | count = json_object_get_count(schema_object);
|
---|
1942 | if (count == 0) {
|
---|
1943 | return JSONSuccess; /* Empty object allows all objects */
|
---|
1944 | } else if (json_object_get_count(value_object) < count) {
|
---|
1945 | return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */
|
---|
1946 | }
|
---|
1947 | for (i = 0; i < count; i++) {
|
---|
1948 | key = json_object_get_name(schema_object, i);
|
---|
1949 | temp_schema_value = json_object_get_value(schema_object, key);
|
---|
1950 | temp_value = json_object_get_value(value_object, key);
|
---|
1951 | if (temp_value == NULL) {
|
---|
1952 | return JSONFailure;
|
---|
1953 | }
|
---|
1954 | if (json_validate(temp_schema_value, temp_value) == JSONFailure) {
|
---|
1955 | return JSONFailure;
|
---|
1956 | }
|
---|
1957 | }
|
---|
1958 | return JSONSuccess;
|
---|
1959 | case JSONString: case JSONNumber: case JSONBoolean: case JSONNull:
|
---|
1960 | return JSONSuccess; /* equality already tested before switch */
|
---|
1961 | case JSONError: default:
|
---|
1962 | return JSONFailure;
|
---|
1963 | }
|
---|
1964 | }
|
---|
1965 |
|
---|
1966 | int json_value_equals(const JSON_Value *a, const JSON_Value *b) {
|
---|
1967 | JSON_Object *a_object = NULL, *b_object = NULL;
|
---|
1968 | JSON_Array *a_array = NULL, *b_array = NULL;
|
---|
1969 | const char *a_string = NULL, *b_string = NULL;
|
---|
1970 | const char *key = NULL;
|
---|
1971 | size_t a_count = 0, b_count = 0, i = 0;
|
---|
1972 | JSON_Value_Type a_type, b_type;
|
---|
1973 | a_type = json_value_get_type(a);
|
---|
1974 | b_type = json_value_get_type(b);
|
---|
1975 | if (a_type != b_type) {
|
---|
1976 | return 0;
|
---|
1977 | }
|
---|
1978 | switch (a_type) {
|
---|
1979 | case JSONArray:
|
---|
1980 | a_array = json_value_get_array(a);
|
---|
1981 | b_array = json_value_get_array(b);
|
---|
1982 | a_count = json_array_get_count(a_array);
|
---|
1983 | b_count = json_array_get_count(b_array);
|
---|
1984 | if (a_count != b_count) {
|
---|
1985 | return 0;
|
---|
1986 | }
|
---|
1987 | for (i = 0; i < a_count; i++) {
|
---|
1988 | if (!json_value_equals(json_array_get_value(a_array, i),
|
---|
1989 | json_array_get_value(b_array, i))) {
|
---|
1990 | return 0;
|
---|
1991 | }
|
---|
1992 | }
|
---|
1993 | return 1;
|
---|
1994 | case JSONObject:
|
---|
1995 | a_object = json_value_get_object(a);
|
---|
1996 | b_object = json_value_get_object(b);
|
---|
1997 | a_count = json_object_get_count(a_object);
|
---|
1998 | b_count = json_object_get_count(b_object);
|
---|
1999 | if (a_count != b_count) {
|
---|
2000 | return 0;
|
---|
2001 | }
|
---|
2002 | for (i = 0; i < a_count; i++) {
|
---|
2003 | key = json_object_get_name(a_object, i);
|
---|
2004 | if (!json_value_equals(json_object_get_value(a_object, key),
|
---|
2005 | json_object_get_value(b_object, key))) {
|
---|
2006 | return 0;
|
---|
2007 | }
|
---|
2008 | }
|
---|
2009 | return 1;
|
---|
2010 | case JSONString:
|
---|
2011 | a_string = json_value_get_string(a);
|
---|
2012 | b_string = json_value_get_string(b);
|
---|
2013 | if (a_string == NULL || b_string == NULL) {
|
---|
2014 | return 0; /* shouldn't happen */
|
---|
2015 | }
|
---|
2016 | return strcmp(a_string, b_string) == 0;
|
---|
2017 | case JSONBoolean:
|
---|
2018 | return json_value_get_boolean(a) == json_value_get_boolean(b);
|
---|
2019 | case JSONNumber:
|
---|
2020 | return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */
|
---|
2021 | case JSONError:
|
---|
2022 | return 1;
|
---|
2023 | case JSONNull:
|
---|
2024 | return 1;
|
---|
2025 | default:
|
---|
2026 | return 1;
|
---|
2027 | }
|
---|
2028 | }
|
---|
2029 |
|
---|
2030 | JSON_Value_Type json_type(const JSON_Value *value) {
|
---|
2031 | return json_value_get_type(value);
|
---|
2032 | }
|
---|
2033 |
|
---|
2034 | JSON_Object * json_object (const JSON_Value *value) {
|
---|
2035 | return json_value_get_object(value);
|
---|
2036 | }
|
---|
2037 |
|
---|
2038 | JSON_Array * json_array (const JSON_Value *value) {
|
---|
2039 | return json_value_get_array(value);
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 | const char * json_string (const JSON_Value *value) {
|
---|
2043 | return json_value_get_string(value);
|
---|
2044 | }
|
---|
2045 |
|
---|
2046 | double json_number (const JSON_Value *value) {
|
---|
2047 | return json_value_get_number(value);
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | int json_boolean(const JSON_Value *value) {
|
---|
2051 | return json_value_get_boolean(value);
|
---|
2052 | }
|
---|
2053 |
|
---|
2054 | void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun) {
|
---|
2055 | parson_malloc = malloc_fun;
|
---|
2056 | parson_free = free_fun;
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | void json_set_escape_slashes(int escape_slashes) {
|
---|
2060 | parson_escape_slashes = escape_slashes;
|
---|
2061 | }
|
---|