GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
Loading...
Searching...
No Matches
parson.c
Go to the documentation of this file.
1/*
2 SPDX-License-Identifier: MIT
3
4 Parson 1.5.3 (https://github.com/kgabis/parson)
5 Copyright (c) 2012 - 2023 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#define PARSON_IMPL_VERSION_MAJOR 1
34#define PARSON_IMPL_VERSION_MINOR 5
35#define PARSON_IMPL_VERSION_PATCH 3
36
37#if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR) || \
38 (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR) || \
39 (PARSON_VERSION_PATCH != PARSON_IMPL_VERSION_PATCH)
40#error "parson version mismatch between parson.c and parson.h"
41#endif
42
43#include <stdarg.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <ctype.h>
48#include <math.h>
49#include <errno.h>
50
51/* Apparently sscanf is not implemented in some "standard" libraries, so don't
52 * use it, if you don't have to. */
53#ifdef sscanf
54#undef sscanf
55#define sscanf THINK_TWICE_ABOUT_USING_SSCANF
56#endif
57
58/* strcpy is unsafe */
59#ifdef strcpy
60#undef strcpy
61#endif
62#define strcpy USE_MEMCPY_INSTEAD_OF_STRCPY
63
64#define STARTING_CAPACITY 16
65#define MAX_NESTING 2048
66
67#ifndef PARSON_DEFAULT_FLOAT_FORMAT
68#define PARSON_DEFAULT_FLOAT_FORMAT \
69 "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */
70#endif
71
72#ifndef PARSON_NUM_BUF_SIZE
73#define PARSON_NUM_BUF_SIZE \
74 64 /* double printed with "%1.17g" shouldn't be longer than 25 bytes so \
75 let's be paranoid and use 64 */
76#endif
77
78#ifndef PARSON_INDENT_STR
79#define PARSON_INDENT_STR " "
80#endif
82#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
83#define SKIP_CHAR(str) ((*str)++)
84#define SKIP_WHITESPACES(str) \
85 while (isspace((unsigned char)(**str))) { \
86 SKIP_CHAR(str); \
87 }
88#define MAX(a, b) ((a) > (b) ? (a) : (b))
89
90#undef malloc
91#undef free
92
93#if defined(isnan) && defined(isinf)
94#define IS_NUMBER_INVALID(x) (isnan((x)) || isinf((x)))
95#else
96#define IS_NUMBER_INVALID(x) (((x)*0.0) != 0.0)
97#endif
99#define OBJECT_INVALID_IX ((size_t)-1)
100
101static JSON_Malloc_Function parson_malloc = malloc;
102static JSON_Free_Function parson_free = free;
103
104static int parson_escape_slashes = 1;
105
106static char *parson_float_format = NULL;
107
108static JSON_Number_Serialization_Function parson_number_serialization_function =
109 NULL;
111#define IS_CONT(b) \
112 (((unsigned char)(b)&0xC0) == 0x80) /* is utf-8 continuation byte */
114typedef int parson_bool_t;
116#define PARSON_TRUE 1
117#define PARSON_FALSE 0
118
119typedef struct json_string {
120 char *chars;
121 size_t length;
123
124/* Type definitions */
125typedef union json_value_value {
126 JSON_String string;
127 double number;
128 JSON_Object *object;
129 JSON_Array *array;
130 int boolean;
131 int null;
133
134struct json_value_t {
135 JSON_Value *parent;
136 JSON_Value_Type type;
137 JSON_Value_Value value;
138};
139
140struct json_object_t {
141 JSON_Value *wrapping_value;
142 size_t *cells;
143 unsigned long *hashes;
144 char **names;
145 JSON_Value **values;
146 size_t *cell_ixs;
147 size_t count;
148 size_t item_capacity;
149 size_t cell_capacity;
150};
151
152struct json_array_t {
153 JSON_Value *wrapping_value;
154 JSON_Value **items;
155 size_t count;
156 size_t capacity;
157};
158
159/* Various */
160static char *read_file(const char *filename);
161static void remove_comments(char *string, const char *start_token,
162 const char *end_token);
163static char *parson_strndup(const char *string, size_t n);
164static char *parson_strdup(const char *string);
165static int parson_sprintf(char *s, const char *format, ...);
166static int hex_char_to_int(char c);
167static JSON_Status parse_utf16_hex(const char *string, unsigned int *result);
168static int num_bytes_in_utf8_sequence(unsigned char c);
169static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len);
170static parson_bool_t is_valid_utf8(const char *string, size_t string_len);
171static parson_bool_t is_decimal(const char *string, size_t length);
172static unsigned long hash_string(const char *string, size_t n);
173
174/* JSON Object */
175static JSON_Object *json_object_make(JSON_Value *wrapping_value);
176static JSON_Status json_object_init(JSON_Object *object, size_t capacity);
177static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys,
178 parson_bool_t free_values);
179static JSON_Status json_object_grow_and_rehash(JSON_Object *object);
180static size_t json_object_get_cell_ix(const JSON_Object *object,
181 const char *key, size_t key_len,
182 unsigned long hash,
183 parson_bool_t *out_found);
184static JSON_Status json_object_add(JSON_Object *object, char *name,
185 JSON_Value *value);
186static JSON_Value *json_object_getn_value(const JSON_Object *object,
187 const char *name, size_t name_len);
188static JSON_Status json_object_remove_internal(JSON_Object *object,
189 const char *name,
190 parson_bool_t free_value);
191static JSON_Status json_object_dotremove_internal(JSON_Object *object,
192 const char *name,
193 parson_bool_t free_value);
194static void json_object_free(JSON_Object *object);
195
196/* JSON Array */
197static JSON_Array *json_array_make(JSON_Value *wrapping_value);
198static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value);
199static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity);
200static void json_array_free(JSON_Array *array);
201
202/* JSON Value */
203static JSON_Value *json_value_init_string_no_copy(char *string, size_t length);
204static const JSON_String *json_value_get_string_desc(const JSON_Value *value);
205
206/* Parser */
207static JSON_Status skip_quotes(const char **string);
208static JSON_Status parse_utf16(const char **unprocessed, char **processed);
209static char *process_string(const char *input, size_t input_len,
210 size_t *output_len);
211static char *get_quoted_string(const char **string, size_t *output_string_len);
212static JSON_Value *parse_object_value(const char **string, size_t nesting);
213static JSON_Value *parse_array_value(const char **string, size_t nesting);
214static JSON_Value *parse_string_value(const char **string);
215static JSON_Value *parse_boolean_value(const char **string);
216static JSON_Value *parse_number_value(const char **string);
217static JSON_Value *parse_null_value(const char **string);
218static JSON_Value *parse_value(const char **string, size_t nesting);
219
220/* Serialization */
221static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf,
222 int level, parson_bool_t is_pretty,
223 char *num_buf);
224static int json_serialize_string(const char *string, size_t len, char *buf);
225
226/* Various */
227static char *read_file(const char *filename)
228{
229 FILE *fp = fopen(filename, "r");
230 size_t size_to_read = 0;
231 size_t size_read = 0;
232 long pos;
233 char *file_contents;
234 if (!fp) {
235 return NULL;
236 }
237 fseek(fp, 0L, SEEK_END);
238 pos = ftell(fp);
239 if (pos < 0) {
240 fclose(fp);
241 return NULL;
242 }
243 size_to_read = pos;
244 rewind(fp);
245 file_contents = (char *)parson_malloc(sizeof(char) * (size_to_read + 1));
246 if (!file_contents) {
247 fclose(fp);
248 return NULL;
249 }
250 size_read = fread(file_contents, 1, size_to_read, fp);
251 if (size_read == 0 || ferror(fp)) {
252 fclose(fp);
253 parson_free(file_contents);
254 return NULL;
255 }
256 fclose(fp);
257 file_contents[size_read] = '\0';
258 return file_contents;
259}
260
261static void remove_comments(char *string, const char *start_token,
262 const char *end_token)
263{
264 parson_bool_t in_string = PARSON_FALSE, escaped = PARSON_FALSE;
265 size_t i;
266 char *ptr = NULL, current_char;
267 size_t start_token_len = strlen(start_token);
268 size_t end_token_len = strlen(end_token);
269 if (start_token_len == 0 || end_token_len == 0) {
270 return;
271 }
272 while ((current_char = *string) != '\0') {
273 if (current_char == '\\' && !escaped) {
274 escaped = PARSON_TRUE;
275 string++;
276 continue;
277 }
278 else if (current_char == '\"' && !escaped) {
279 in_string = !in_string;
280 }
281 else if (!in_string &&
282 strncmp(string, start_token, start_token_len) == 0) {
283 for (i = 0; i < start_token_len; i++) {
284 string[i] = ' ';
285 }
286 string = string + start_token_len;
287 ptr = strstr(string, end_token);
288 if (!ptr) {
289 return;
290 }
291 for (i = 0; i < (ptr - string) + end_token_len; i++) {
292 string[i] = ' ';
293 }
294 string = ptr + end_token_len - 1;
295 }
296 escaped = PARSON_FALSE;
297 string++;
298 }
299}
300
301static char *parson_strndup(const char *string, size_t n)
302{
303 /* We expect the caller has validated that 'n' fits within the input buffer.
304 */
305 char *output_string = (char *)parson_malloc(n + 1);
306 if (!output_string) {
307 return NULL;
308 }
309 output_string[n] = '\0';
310 memcpy(output_string, string, n);
311 return output_string;
312}
313
314static char *parson_strdup(const char *string)
315{
316 return parson_strndup(string, strlen(string));
317}
318
319static int parson_sprintf(char *s, const char *format, ...)
320{
321 int result;
322 va_list args;
323 va_start(args, format);
324
325#if defined(__APPLE__) && defined(__clang__)
326#pragma clang diagnostic push
327#pragma clang diagnostic ignored "-Wdeprecated-declarations"
328#endif
329 result = vsprintf(s, format, args);
330#if defined(__APPLE__) && defined(__clang__)
331#pragma clang diagnostic pop
332#endif
333
334 va_end(args);
335 return result;
336}
337
338static int hex_char_to_int(char c)
339{
340 if (c >= '0' && c <= '9') {
341 return c - '0';
342 }
343 else if (c >= 'a' && c <= 'f') {
344 return c - 'a' + 10;
345 }
346 else if (c >= 'A' && c <= 'F') {
347 return c - 'A' + 10;
348 }
349 return -1;
350}
351
352static JSON_Status parse_utf16_hex(const char *s, unsigned int *result)
353{
354 int x1, x2, x3, x4;
355 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0' || s[3] == '\0') {
356 return JSONFailure;
357 }
358 x1 = hex_char_to_int(s[0]);
359 x2 = hex_char_to_int(s[1]);
360 x3 = hex_char_to_int(s[2]);
361 x4 = hex_char_to_int(s[3]);
362 if (x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) {
363 return JSONFailure;
364 }
365 *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4);
366 return JSONSuccess;
367}
368
369static int num_bytes_in_utf8_sequence(unsigned char c)
370{
371 if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) {
372 return 0;
373 }
374 else if ((c & 0x80) == 0) { /* 0xxxxxxx */
375 return 1;
376 }
377 else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */
378 return 2;
379 }
380 else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */
381 return 3;
382 }
383 else if ((c & 0xF8) == 0xF0) { /* 11110xxx */
384 return 4;
385 }
386 return 0; /* won't happen */
387}
388
389static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len)
390{
391 unsigned int cp = 0;
392 *len = num_bytes_in_utf8_sequence(string[0]);
393
394 if (*len == 1) {
395 cp = string[0];
396 }
397 else if (*len == 2 && IS_CONT(string[1])) {
398 cp = string[0] & 0x1F;
399 cp = (cp << 6) | (string[1] & 0x3F);
400 }
401 else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) {
402 cp = ((unsigned char)string[0]) & 0xF;
403 cp = (cp << 6) | (string[1] & 0x3F);
404 cp = (cp << 6) | (string[2] & 0x3F);
405 }
406 else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) &&
407 IS_CONT(string[3])) {
408 cp = string[0] & 0x7;
409 cp = (cp << 6) | (string[1] & 0x3F);
410 cp = (cp << 6) | (string[2] & 0x3F);
411 cp = (cp << 6) | (string[3] & 0x3F);
412 }
413 else {
414 return JSONFailure;
415 }
416
417 /* overlong encodings */
418 if ((cp < 0x80 && *len > 1) || (cp < 0x800 && *len > 2) ||
419 (cp < 0x10000 && *len > 3)) {
420 return JSONFailure;
421 }
422
423 /* invalid unicode */
424 if (cp > 0x10FFFF) {
425 return JSONFailure;
426 }
427
428 /* surrogate halves */
429 if (cp >= 0xD800 && cp <= 0xDFFF) {
430 return JSONFailure;
431 }
432
433 return JSONSuccess;
434}
435
436static int is_valid_utf8(const char *string, size_t string_len)
437{
438 int len = 0;
439 const char *string_end = string + string_len;
440 while (string < string_end) {
441 if (verify_utf8_sequence((const unsigned char *)string, &len) !=
442 JSONSuccess) {
443 return PARSON_FALSE;
444 }
445 string += len;
446 }
447 return PARSON_TRUE;
448}
449
450static parson_bool_t is_decimal(const char *string, size_t length)
451{
452 if (length > 1 && string[0] == '0' && string[1] != '.') {
453 return PARSON_FALSE;
454 }
455 if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') {
456 return PARSON_FALSE;
457 }
458 while (length--) {
459 if (strchr("xX", string[length])) {
460 return PARSON_FALSE;
461 }
462 }
463 return PARSON_TRUE;
464}
465
466static unsigned long hash_string(const char *string, size_t n)
467{
468#ifdef PARSON_FORCE_HASH_COLLISIONS
469 (void)string;
470 (void)n;
471 return 0;
472#else
473 unsigned long hash = 5381;
474 unsigned char c;
475 size_t i = 0;
476 for (i = 0; i < n; i++) {
477 c = string[i];
478 if (c == '\0') {
479 break;
480 }
481 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
482 }
483 return hash;
484#endif
485}
486
487/* JSON Object */
488static JSON_Object *json_object_make(JSON_Value *wrapping_value)
489{
491 JSON_Object *new_obj = (JSON_Object *)parson_malloc(sizeof(JSON_Object));
492 if (new_obj == NULL) {
493 return NULL;
494 }
495 new_obj->wrapping_value = wrapping_value;
496 res = json_object_init(new_obj, 0);
497 if (res != JSONSuccess) {
498 parson_free(new_obj);
499 return NULL;
500 }
501 return new_obj;
502}
503
504static JSON_Status json_object_init(JSON_Object *object, size_t capacity)
505{
506 unsigned int i = 0;
507
508 object->cells = NULL;
509 object->names = NULL;
510 object->values = NULL;
511 object->cell_ixs = NULL;
512 object->hashes = NULL;
513
514 object->count = 0;
515 object->cell_capacity = capacity;
516 object->item_capacity = (unsigned int)(capacity * 7 / 10);
517
518 if (capacity == 0) {
519 return JSONSuccess;
520 }
521
522 object->cells =
523 (size_t *)parson_malloc(object->cell_capacity * sizeof(*object->cells));
524 object->names =
525 (char **)parson_malloc(object->item_capacity * sizeof(*object->names));
526 object->values = (JSON_Value **)parson_malloc(object->item_capacity *
527 sizeof(*object->values));
528 object->cell_ixs = (size_t *)parson_malloc(object->item_capacity *
529 sizeof(*object->cell_ixs));
530 object->hashes = (unsigned long *)parson_malloc(object->item_capacity *
531 sizeof(*object->hashes));
532 if (object->cells == NULL || object->names == NULL ||
533 object->values == NULL || object->cell_ixs == NULL ||
534 object->hashes == NULL) {
535 goto error;
536 }
537 for (i = 0; i < object->cell_capacity; i++) {
538 object->cells[i] = OBJECT_INVALID_IX;
539 }
540 return JSONSuccess;
541error:
542 parson_free(object->cells);
543 parson_free(object->names);
544 parson_free(object->values);
545 parson_free(object->cell_ixs);
546 parson_free(object->hashes);
547 return JSONFailure;
548}
549
550static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys,
551 parson_bool_t free_values)
552{
553 unsigned int i = 0;
554 for (i = 0; i < object->count; i++) {
555 if (free_keys) {
556 parson_free(object->names[i]);
557 }
558 if (free_values) {
559 json_value_free(object->values[i]);
560 }
561 }
562
563 object->count = 0;
564 object->item_capacity = 0;
565 object->cell_capacity = 0;
566
567 parson_free(object->cells);
568 parson_free(object->names);
569 parson_free(object->values);
570 parson_free(object->cell_ixs);
571 parson_free(object->hashes);
572
573 object->cells = NULL;
574 object->names = NULL;
575 object->values = NULL;
576 object->cell_ixs = NULL;
577 object->hashes = NULL;
578}
579
580static JSON_Status json_object_grow_and_rehash(JSON_Object *object)
581{
582 JSON_Value *wrapping_value = NULL;
583 JSON_Object new_object;
584 char *key = NULL;
585 JSON_Value *value = NULL;
586 unsigned int i = 0;
587 size_t new_capacity = MAX(object->cell_capacity * 2, STARTING_CAPACITY);
588 JSON_Status res = json_object_init(&new_object, new_capacity);
589 if (res != JSONSuccess) {
590 return JSONFailure;
591 }
592
593 wrapping_value = json_object_get_wrapping_value(object);
594 new_object.wrapping_value = wrapping_value;
595
596 for (i = 0; i < object->count; i++) {
597 key = object->names[i];
598 value = object->values[i];
599 res = json_object_add(&new_object, key, value);
600 if (res != JSONSuccess) {
601 json_object_deinit(&new_object, PARSON_FALSE, PARSON_FALSE);
602 return JSONFailure;
603 }
604 value->parent = wrapping_value;
605 }
606 json_object_deinit(object, PARSON_FALSE, PARSON_FALSE);
607 *object = new_object;
608 return JSONSuccess;
609}
610
611static size_t json_object_get_cell_ix(const JSON_Object *object,
612 const char *key, size_t key_len,
613 unsigned long hash,
614 parson_bool_t *out_found)
615{
616 size_t cell_ix = hash & (object->cell_capacity - 1);
617 size_t cell = 0;
618 size_t ix = 0;
619 unsigned int i = 0;
620 unsigned long hash_to_check = 0;
621 const char *key_to_check = NULL;
622 size_t key_to_check_len = 0;
623
624 *out_found = PARSON_FALSE;
625
626 for (i = 0; i < object->cell_capacity; i++) {
627 ix = (cell_ix + i) & (object->cell_capacity - 1);
628 cell = object->cells[ix];
629 if (cell == OBJECT_INVALID_IX) {
630 return ix;
631 }
632 hash_to_check = object->hashes[cell];
633 if (hash != hash_to_check) {
634 continue;
635 }
636 key_to_check = object->names[cell];
637 key_to_check_len = strlen(key_to_check);
638 if (key_to_check_len == key_len &&
639 strncmp(key, key_to_check, key_len) == 0) {
640 *out_found = PARSON_TRUE;
641 return ix;
642 }
643 }
644 return OBJECT_INVALID_IX;
645}
646
647static JSON_Status json_object_add(JSON_Object *object, char *name,
648 JSON_Value *value)
649{
650 unsigned long hash = 0;
652 size_t cell_ix = 0;
654
655 if (!object || !name || !value) {
656 return JSONFailure;
657 }
658
659 hash = hash_string(name, strlen(name));
660 found = PARSON_FALSE;
661 cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
662 if (found) {
663 return JSONFailure;
664 }
665
666 if (object->count >= object->item_capacity) {
667 res = json_object_grow_and_rehash(object);
668 if (res != JSONSuccess) {
669 return JSONFailure;
670 }
671 cell_ix =
672 json_object_get_cell_ix(object, name, strlen(name), hash, &found);
673 }
674
675 object->names[object->count] = name;
676 object->cells[cell_ix] = object->count;
677 object->values[object->count] = value;
678 object->cell_ixs[object->count] = cell_ix;
679 object->hashes[object->count] = hash;
680 object->count++;
681 value->parent = json_object_get_wrapping_value(object);
682
683 return JSONSuccess;
684}
685
686static JSON_Value *json_object_getn_value(const JSON_Object *object,
687 const char *name, size_t name_len)
688{
689 unsigned long hash = 0;
691 size_t cell_ix = 0;
692 size_t item_ix = 0;
693 if (!object || !name) {
694 return NULL;
695 }
696 hash = hash_string(name, name_len);
697 found = PARSON_FALSE;
698 cell_ix = json_object_get_cell_ix(object, name, name_len, hash, &found);
699 if (!found) {
700 return NULL;
701 }
702 item_ix = object->cells[cell_ix];
703 return object->values[item_ix];
704}
705
706static JSON_Status json_object_remove_internal(JSON_Object *object,
707 const char *name,
708 parson_bool_t free_value)
709{
710 unsigned long hash = 0;
712 size_t cell = 0;
713 size_t item_ix = 0;
714 size_t last_item_ix = 0;
715 size_t i = 0;
716 size_t j = 0;
717 size_t x = 0;
718 size_t k = 0;
719 JSON_Value *val = NULL;
720
721 if (object == NULL) {
722 return JSONFailure;
723 }
724
725 hash = hash_string(name, strlen(name));
726 found = PARSON_FALSE;
727 cell = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
728 if (!found) {
729 return JSONFailure;
730 }
731
732 item_ix = object->cells[cell];
733 if (free_value) {
734 val = object->values[item_ix];
735 json_value_free(val);
736 val = NULL;
737 }
738
739 parson_free(object->names[item_ix]);
740 last_item_ix = object->count - 1;
741 if (item_ix < last_item_ix) {
742 object->names[item_ix] = object->names[last_item_ix];
743 object->values[item_ix] = object->values[last_item_ix];
744 object->cell_ixs[item_ix] = object->cell_ixs[last_item_ix];
745 object->hashes[item_ix] = object->hashes[last_item_ix];
746 object->cells[object->cell_ixs[item_ix]] = item_ix;
747 }
748 object->count--;
749
750 i = cell;
751 j = i;
752 for (x = 0; x < (object->cell_capacity - 1); x++) {
753 j = (j + 1) & (object->cell_capacity - 1);
754 if (object->cells[j] == OBJECT_INVALID_IX) {
755 break;
756 }
757 k = object->hashes[object->cells[j]] & (object->cell_capacity - 1);
758 if ((j > i && (k <= i || k > j)) || (j < i && (k <= i && k > j))) {
759 object->cell_ixs[object->cells[j]] = i;
760 object->cells[i] = object->cells[j];
761 i = j;
762 }
763 }
764 object->cells[i] = OBJECT_INVALID_IX;
765 return JSONSuccess;
766}
767
768static JSON_Status json_object_dotremove_internal(JSON_Object *object,
769 const char *name,
770 parson_bool_t free_value)
771{
772 JSON_Value *temp_value = NULL;
773 JSON_Object *temp_object = NULL;
774 const char *dot_pos = strchr(name, '.');
775 if (!dot_pos) {
776 return json_object_remove_internal(object, name, free_value);
777 }
778 temp_value = json_object_getn_value(object, name, dot_pos - name);
779 if (json_value_get_type(temp_value) != JSONObject) {
780 return JSONFailure;
781 }
782 temp_object = json_value_get_object(temp_value);
783 return json_object_dotremove_internal(temp_object, dot_pos + 1, free_value);
784}
785
786static void json_object_free(JSON_Object *object)
787{
788 json_object_deinit(object, PARSON_TRUE, PARSON_TRUE);
789 parson_free(object);
790}
791
792/* JSON Array */
793static JSON_Array *json_array_make(JSON_Value *wrapping_value)
794{
795 JSON_Array *new_array = (JSON_Array *)parson_malloc(sizeof(JSON_Array));
796 if (new_array == NULL) {
797 return NULL;
798 }
799 new_array->wrapping_value = wrapping_value;
800 new_array->items = (JSON_Value **)NULL;
801 new_array->capacity = 0;
802 new_array->count = 0;
803 return new_array;
804}
805
806static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value)
807{
808 if (array->count >= array->capacity) {
809 size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
810 if (json_array_resize(array, new_capacity) != JSONSuccess) {
811 return JSONFailure;
812 }
813 }
814 value->parent = json_array_get_wrapping_value(array);
815 array->items[array->count] = value;
816 array->count++;
817 return JSONSuccess;
818}
819
820static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity)
821{
822 JSON_Value **new_items = NULL;
823 if (new_capacity == 0) {
824 return JSONFailure;
825 }
826 new_items =
827 (JSON_Value **)parson_malloc(new_capacity * sizeof(JSON_Value *));
828 if (new_items == NULL) {
829 return JSONFailure;
830 }
831 if (array->items != NULL && array->count > 0) {
832 memcpy(new_items, array->items, array->count * sizeof(JSON_Value *));
833 }
834 parson_free(array->items);
835 array->items = new_items;
836 array->capacity = new_capacity;
837 return JSONSuccess;
838}
839
840static void json_array_free(JSON_Array *array)
841{
842 size_t i;
843 for (i = 0; i < array->count; i++) {
844 json_value_free(array->items[i]);
845 }
846 parson_free(array->items);
847 parson_free(array);
848}
849
850/* JSON Value */
851static JSON_Value *json_value_init_string_no_copy(char *string, size_t length)
852{
853 JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value));
854 if (!new_value) {
855 return NULL;
856 }
857 new_value->parent = NULL;
858 new_value->type = JSONString;
859 new_value->value.string.chars = string;
860 new_value->value.string.length = length;
861 return new_value;
862}
863
864/* Parser */
865static JSON_Status skip_quotes(const char **string)
866{
867 if (**string != '\"') {
868 return JSONFailure;
869 }
870 SKIP_CHAR(string);
871 while (**string != '\"') {
872 if (**string == '\0') {
873 return JSONFailure;
874 }
875 else if (**string == '\\') {
876 SKIP_CHAR(string);
877 if (**string == '\0') {
878 return JSONFailure;
879 }
880 }
881 SKIP_CHAR(string);
882 }
883 SKIP_CHAR(string);
884 return JSONSuccess;
885}
886
887static JSON_Status parse_utf16(const char **unprocessed, char **processed)
888{
889 unsigned int cp, lead, trail;
890 char *processed_ptr = *processed;
891 const char *unprocessed_ptr = *unprocessed;
892 JSON_Status status = JSONFailure;
893 unprocessed_ptr++; /* skips u */
894 status = parse_utf16_hex(unprocessed_ptr, &cp);
895 if (status != JSONSuccess) {
896 return JSONFailure;
897 }
898 if (cp < 0x80) {
899 processed_ptr[0] = (char)cp; /* 0xxxxxxx */
900 }
901 else if (cp < 0x800) {
902 processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
903 processed_ptr[1] = ((cp)&0x3F) | 0x80; /* 10xxxxxx */
904 processed_ptr += 1;
905 }
906 else if (cp < 0xD800 || cp > 0xDFFF) {
907 processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
908 processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
909 processed_ptr[2] = ((cp)&0x3F) | 0x80; /* 10xxxxxx */
910 processed_ptr += 2;
911 }
912 else if (cp >= 0xD800 &&
913 cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
914 lead = cp;
915 unprocessed_ptr += 4; /* should always be within the buffer, otherwise
916 previous sscanf would fail */
917 if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u') {
918 return JSONFailure;
919 }
920 status = parse_utf16_hex(unprocessed_ptr, &trail);
921 if (status != JSONSuccess || trail < 0xDC00 ||
922 trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
923 return JSONFailure;
924 }
925 cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) +
926 0x010000;
927 processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
928 processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
929 processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
930 processed_ptr[3] = (((cp)&0x3F) | 0x80); /* 10xxxxxx */
931 processed_ptr += 3;
932 }
933 else { /* trail surrogate before lead surrogate */
934 return JSONFailure;
935 }
936 unprocessed_ptr += 3;
937 *processed = processed_ptr;
938 *unprocessed = unprocessed_ptr;
939 return JSONSuccess;
940}
941
942/* Copies and processes passed string up to supplied length.
943Example: "\u006Corem ipsum" -> lorem ipsum */
944static char *process_string(const char *input, size_t input_len,
945 size_t *output_len)
946{
947 const char *input_ptr = input;
948 size_t initial_size = (input_len + 1) * sizeof(char);
949 size_t final_size = 0;
950 char *output = NULL, *output_ptr = NULL, *resized_output = NULL;
951 output = (char *)parson_malloc(initial_size);
952 if (output == NULL) {
953 goto error;
954 }
955 output_ptr = output;
956 while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < input_len) {
957 if (*input_ptr == '\\') {
958 input_ptr++;
959 switch (*input_ptr) {
960 case '\"':
961 *output_ptr = '\"';
962 break;
963 case '\\':
964 *output_ptr = '\\';
965 break;
966 case '/':
967 *output_ptr = '/';
968 break;
969 case 'b':
970 *output_ptr = '\b';
971 break;
972 case 'f':
973 *output_ptr = '\f';
974 break;
975 case 'n':
976 *output_ptr = '\n';
977 break;
978 case 'r':
979 *output_ptr = '\r';
980 break;
981 case 't':
982 *output_ptr = '\t';
983 break;
984 case 'u':
985 if (parse_utf16(&input_ptr, &output_ptr) != JSONSuccess) {
986 goto error;
987 }
988 break;
989 default:
990 goto error;
991 }
992 }
993 else if ((unsigned char)*input_ptr < 0x20) {
994 goto error; /* 0x00-0x19 are invalid characters for json string
995 (http://www.ietf.org/rfc/rfc4627.txt) */
996 }
997 else {
998 *output_ptr = *input_ptr;
999 }
1000 output_ptr++;
1001 input_ptr++;
1002 }
1003 *output_ptr = '\0';
1004 /* resize to new length */
1005 final_size = (size_t)(output_ptr - output) + 1;
1006 /* todo: don't resize if final_size == initial_size */
1007 resized_output = (char *)parson_malloc(final_size);
1008 if (resized_output == NULL) {
1009 goto error;
1010 }
1011 memcpy(resized_output, output, final_size);
1012 *output_len = final_size - 1;
1013 parson_free(output);
1014 return resized_output;
1015error:
1016 parson_free(output);
1017 return NULL;
1018}
1019
1020/* Return processed contents of a string between quotes and
1021 skips passed argument to a matching quote. */
1022static char *get_quoted_string(const char **string, size_t *output_string_len)
1023{
1024 const char *string_start = *string;
1025 size_t input_string_len = 0;
1026 JSON_Status status = skip_quotes(string);
1027 if (status != JSONSuccess) {
1028 return NULL;
1029 }
1030 input_string_len = *string - string_start - 2; /* length without quotes */
1031 return process_string(string_start + 1, input_string_len,
1032 output_string_len);
1033}
1034
1035static JSON_Value *parse_value(const char **string, size_t nesting)
1036{
1037 if (nesting > MAX_NESTING) {
1038 return NULL;
1039 }
1040 SKIP_WHITESPACES(string);
1041 switch (**string) {
1042 case '{':
1043 return parse_object_value(string, nesting + 1);
1044 case '[':
1045 return parse_array_value(string, nesting + 1);
1046 case '\"':
1047 return parse_string_value(string);
1048 case 'f':
1049 case 't':
1050 return parse_boolean_value(string);
1051 case '-':
1052 case '0':
1053 case '1':
1054 case '2':
1055 case '3':
1056 case '4':
1057 case '5':
1058 case '6':
1059 case '7':
1060 case '8':
1061 case '9':
1062 return parse_number_value(string);
1063 case 'n':
1064 return parse_null_value(string);
1065 default:
1066 return NULL;
1067 }
1068}
1069
1070static JSON_Value *parse_object_value(const char **string, size_t nesting)
1071{
1072 JSON_Status status = JSONFailure;
1073 JSON_Value *output_value = NULL, *new_value = NULL;
1074 JSON_Object *output_object = NULL;
1075 char *new_key = NULL;
1076
1077 output_value = json_value_init_object();
1078 if (output_value == NULL) {
1079 return NULL;
1080 }
1081 if (**string != '{') {
1082 json_value_free(output_value);
1083 return NULL;
1084 }
1085 output_object = json_value_get_object(output_value);
1086 SKIP_CHAR(string);
1087 SKIP_WHITESPACES(string);
1088 if (**string == '}') { /* empty object */
1089 SKIP_CHAR(string);
1090 return output_value;
1091 }
1092 while (**string != '\0') {
1093 size_t key_len = 0;
1094 new_key = get_quoted_string(string, &key_len);
1095 /* We do not support key names with embedded \0 chars */
1096 if (!new_key) {
1097 json_value_free(output_value);
1098 return NULL;
1099 }
1100 if (key_len != strlen(new_key)) {
1101 parson_free(new_key);
1102 json_value_free(output_value);
1103 return NULL;
1104 }
1105 SKIP_WHITESPACES(string);
1106 if (**string != ':') {
1107 parson_free(new_key);
1108 json_value_free(output_value);
1109 return NULL;
1110 }
1111 SKIP_CHAR(string);
1112 new_value = parse_value(string, nesting);
1113 if (new_value == NULL) {
1114 parson_free(new_key);
1115 json_value_free(output_value);
1116 return NULL;
1117 }
1118 status = json_object_add(output_object, new_key, new_value);
1119 if (status != JSONSuccess) {
1120 parson_free(new_key);
1121 json_value_free(new_value);
1122 json_value_free(output_value);
1123 return NULL;
1124 }
1125 SKIP_WHITESPACES(string);
1126 if (**string != ',') {
1127 break;
1128 }
1129 SKIP_CHAR(string);
1130 SKIP_WHITESPACES(string);
1131 if (**string == '}') {
1132 break;
1133 }
1134 }
1135 SKIP_WHITESPACES(string);
1136 if (**string != '}') {
1137 json_value_free(output_value);
1138 return NULL;
1139 }
1140 SKIP_CHAR(string);
1141 return output_value;
1142}
1143
1144static JSON_Value *parse_array_value(const char **string, size_t nesting)
1145{
1146 JSON_Value *output_value = NULL, *new_array_value = NULL;
1147 JSON_Array *output_array = NULL;
1148 output_value = json_value_init_array();
1149 if (output_value == NULL) {
1150 return NULL;
1151 }
1152 if (**string != '[') {
1153 json_value_free(output_value);
1154 return NULL;
1155 }
1156 output_array = json_value_get_array(output_value);
1157 SKIP_CHAR(string);
1158 SKIP_WHITESPACES(string);
1159 if (**string == ']') { /* empty array */
1160 SKIP_CHAR(string);
1161 return output_value;
1162 }
1163 while (**string != '\0') {
1164 new_array_value = parse_value(string, nesting);
1165 if (new_array_value == NULL) {
1166 json_value_free(output_value);
1167 return NULL;
1168 }
1169 if (json_array_add(output_array, new_array_value) != JSONSuccess) {
1170 json_value_free(new_array_value);
1171 json_value_free(output_value);
1172 return NULL;
1173 }
1174 SKIP_WHITESPACES(string);
1175 if (**string != ',') {
1176 break;
1177 }
1178 SKIP_CHAR(string);
1179 SKIP_WHITESPACES(string);
1180 if (**string == ']') {
1181 break;
1182 }
1183 }
1184 SKIP_WHITESPACES(string);
1185 if (**string != ']' || /* Trim array after parsing is over */
1186 json_array_resize(output_array, json_array_get_count(output_array)) !=
1187 JSONSuccess) {
1188 json_value_free(output_value);
1189 return NULL;
1190 }
1191 SKIP_CHAR(string);
1192 return output_value;
1193}
1194
1195static JSON_Value *parse_string_value(const char **string)
1196{
1197 JSON_Value *value = NULL;
1198 size_t new_string_len = 0;
1199 char *new_string = get_quoted_string(string, &new_string_len);
1200 if (new_string == NULL) {
1201 return NULL;
1202 }
1203 value = json_value_init_string_no_copy(new_string, new_string_len);
1204 if (value == NULL) {
1205 parson_free(new_string);
1206 return NULL;
1207 }
1208 return value;
1209}
1210
1211static JSON_Value *parse_boolean_value(const char **string)
1212{
1213 size_t true_token_size = SIZEOF_TOKEN("true");
1214 size_t false_token_size = SIZEOF_TOKEN("false");
1215 if (strncmp("true", *string, true_token_size) == 0) {
1216 *string += true_token_size;
1217 return json_value_init_boolean(1);
1218 }
1219 else if (strncmp("false", *string, false_token_size) == 0) {
1220 *string += false_token_size;
1221 return json_value_init_boolean(0);
1222 }
1223 return NULL;
1224}
1225
1226static JSON_Value *parse_number_value(const char **string)
1227{
1228 char *end;
1229 double number = 0;
1230 errno = 0;
1231 number = strtod(*string, &end);
1232 if (errno == ERANGE && (number <= -HUGE_VAL || number >= HUGE_VAL)) {
1233 return NULL;
1234 }
1235 if ((errno && errno != ERANGE) || !is_decimal(*string, end - *string)) {
1236 return NULL;
1237 }
1238 *string = end;
1239 return json_value_init_number(number);
1240}
1241
1242static JSON_Value *parse_null_value(const char **string)
1243{
1244 size_t token_size = SIZEOF_TOKEN("null");
1245 if (strncmp("null", *string, token_size) == 0) {
1246 *string += token_size;
1247 return json_value_init_null();
1248 }
1249 return NULL;
1250}
1251
1252/* Serialization */
1253
1254/* APPEND_STRING() is only called on string literals.
1255 It's a bit hacky because it makes plenty of assumptions about the external
1256 state and should eventually be tidied up into a function (same goes for
1257 APPEND_INDENT)
1259#define APPEND_STRING(str) \
1260 do { \
1261 written = SIZEOF_TOKEN((str)); \
1262 if (buf != NULL) { \
1263 memcpy(buf, (str), written); \
1264 buf[written] = '\0'; \
1265 buf += written; \
1266 } \
1267 written_total += written; \
1268 } while (0)
1270#define APPEND_INDENT(level) \
1271 do { \
1272 int level_i = 0; \
1273 for (level_i = 0; level_i < (level); level_i++) { \
1274 APPEND_STRING(PARSON_INDENT_STR); \
1275 } \
1276 } while (0)
1277
1278static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf,
1279 int level, parson_bool_t is_pretty,
1280 char *num_buf)
1281{
1282 const char *key = NULL, *string = NULL;
1283 JSON_Value *temp_value = NULL;
1284 JSON_Array *array = NULL;
1285 JSON_Object *object = NULL;
1286 size_t i = 0, count = 0;
1287 double num = 0.0;
1288 int written = -1, written_total = 0;
1289 size_t len = 0;
1290
1291 switch (json_value_get_type(value)) {
1292 case JSONArray:
1293 array = json_value_get_array(value);
1294 count = json_array_get_count(array);
1295 APPEND_STRING("[");
1296 if (count > 0 && is_pretty) {
1297 APPEND_STRING("\n");
1298 }
1299 for (i = 0; i < count; i++) {
1300 if (is_pretty) {
1301 APPEND_INDENT(level + 1);
1302 }
1303 temp_value = json_array_get_value(array, i);
1304 written = json_serialize_to_buffer_r(temp_value, buf, level + 1,
1305 is_pretty, num_buf);
1306 if (written < 0) {
1307 return -1;
1308 }
1309 if (buf != NULL) {
1310 buf += written;
1311 }
1312 written_total += written;
1313 if (i < (count - 1)) {
1314 APPEND_STRING(",");
1315 }
1316 if (is_pretty) {
1317 APPEND_STRING("\n");
1318 }
1319 }
1320 if (count > 0 && is_pretty) {
1321 APPEND_INDENT(level);
1322 }
1323 APPEND_STRING("]");
1324 return written_total;
1325 case JSONObject:
1326 object = json_value_get_object(value);
1327 count = json_object_get_count(object);
1328 APPEND_STRING("{");
1329 if (count > 0 && is_pretty) {
1330 APPEND_STRING("\n");
1331 }
1332 for (i = 0; i < count; i++) {
1333 key = json_object_get_name(object, i);
1334 if (key == NULL) {
1335 return -1;
1336 }
1337 if (is_pretty) {
1338 APPEND_INDENT(level + 1);
1339 }
1340 /* We do not support key names with embedded \0 chars */
1341 written = json_serialize_string(key, strlen(key), buf);
1342 if (written < 0) {
1343 return -1;
1344 }
1345 if (buf != NULL) {
1346 buf += written;
1347 }
1348 written_total += written;
1349 APPEND_STRING(":");
1350 if (is_pretty) {
1351 APPEND_STRING(" ");
1352 }
1353 temp_value = json_object_get_value_at(object, i);
1354 written = json_serialize_to_buffer_r(temp_value, buf, level + 1,
1355 is_pretty, num_buf);
1356 if (written < 0) {
1357 return -1;
1358 }
1359 if (buf != NULL) {
1360 buf += written;
1361 }
1362 written_total += written;
1363 if (i < (count - 1)) {
1364 APPEND_STRING(",");
1365 }
1366 if (is_pretty) {
1367 APPEND_STRING("\n");
1368 }
1369 }
1370 if (count > 0 && is_pretty) {
1371 APPEND_INDENT(level);
1372 }
1373 APPEND_STRING("}");
1374 return written_total;
1375 case JSONString:
1376 string = json_value_get_string(value);
1377 if (string == NULL) {
1378 return -1;
1379 }
1380 len = json_value_get_string_len(value);
1381 written = json_serialize_string(string, len, buf);
1382 if (written < 0) {
1383 return -1;
1384 }
1385 if (buf != NULL) {
1386 buf += written;
1387 }
1388 written_total += written;
1389 return written_total;
1390 case JSONBoolean:
1391 if (json_value_get_boolean(value)) {
1392 APPEND_STRING("true");
1393 }
1394 else {
1395 APPEND_STRING("false");
1396 }
1397 return written_total;
1398 case JSONNumber:
1399 num = json_value_get_number(value);
1400 if (buf != NULL) {
1401 num_buf = buf;
1402 }
1403 if (parson_number_serialization_function) {
1404 written = parson_number_serialization_function(num, num_buf);
1405 }
1406 else {
1407 const char *float_format = parson_float_format
1408 ? parson_float_format
1410 written = parson_sprintf(num_buf, float_format, num);
1411 }
1412 if (written < 0) {
1413 return -1;
1414 }
1415 if (buf != NULL) {
1416 buf += written;
1417 }
1418 written_total += written;
1419 return written_total;
1420 case JSONNull:
1421 APPEND_STRING("null");
1422 return written_total;
1423 case JSONError:
1424 return -1;
1425 default:
1426 return -1;
1427 }
1428}
1429
1430static int json_serialize_string(const char *string, size_t len, char *buf)
1431{
1432 size_t i = 0;
1433 char c = '\0';
1434 int written = -1, written_total = 0;
1435 APPEND_STRING("\"");
1436 for (i = 0; i < len; i++) {
1437 c = string[i];
1438 switch (c) {
1439 case '\"':
1440 APPEND_STRING("\\\"");
1441 break;
1442 case '\\':
1443 APPEND_STRING("\\\\");
1444 break;
1445 case '\b':
1446 APPEND_STRING("\\b");
1447 break;
1448 case '\f':
1449 APPEND_STRING("\\f");
1450 break;
1451 case '\n':
1452 APPEND_STRING("\\n");
1453 break;
1454 case '\r':
1455 APPEND_STRING("\\r");
1456 break;
1457 case '\t':
1458 APPEND_STRING("\\t");
1459 break;
1460 case '\x00':
1461 APPEND_STRING("\\u0000");
1462 break;
1463 case '\x01':
1464 APPEND_STRING("\\u0001");
1465 break;
1466 case '\x02':
1467 APPEND_STRING("\\u0002");
1468 break;
1469 case '\x03':
1470 APPEND_STRING("\\u0003");
1471 break;
1472 case '\x04':
1473 APPEND_STRING("\\u0004");
1474 break;
1475 case '\x05':
1476 APPEND_STRING("\\u0005");
1477 break;
1478 case '\x06':
1479 APPEND_STRING("\\u0006");
1480 break;
1481 case '\x07':
1482 APPEND_STRING("\\u0007");
1483 break;
1484 /* '\x08' duplicate: '\b' */
1485 /* '\x09' duplicate: '\t' */
1486 /* '\x0a' duplicate: '\n' */
1487 case '\x0b':
1488 APPEND_STRING("\\u000b");
1489 break;
1490 /* '\x0c' duplicate: '\f' */
1491 /* '\x0d' duplicate: '\r' */
1492 case '\x0e':
1493 APPEND_STRING("\\u000e");
1494 break;
1495 case '\x0f':
1496 APPEND_STRING("\\u000f");
1497 break;
1498 case '\x10':
1499 APPEND_STRING("\\u0010");
1500 break;
1501 case '\x11':
1502 APPEND_STRING("\\u0011");
1503 break;
1504 case '\x12':
1505 APPEND_STRING("\\u0012");
1506 break;
1507 case '\x13':
1508 APPEND_STRING("\\u0013");
1509 break;
1510 case '\x14':
1511 APPEND_STRING("\\u0014");
1512 break;
1513 case '\x15':
1514 APPEND_STRING("\\u0015");
1515 break;
1516 case '\x16':
1517 APPEND_STRING("\\u0016");
1518 break;
1519 case '\x17':
1520 APPEND_STRING("\\u0017");
1521 break;
1522 case '\x18':
1523 APPEND_STRING("\\u0018");
1524 break;
1525 case '\x19':
1526 APPEND_STRING("\\u0019");
1527 break;
1528 case '\x1a':
1529 APPEND_STRING("\\u001a");
1530 break;
1531 case '\x1b':
1532 APPEND_STRING("\\u001b");
1533 break;
1534 case '\x1c':
1535 APPEND_STRING("\\u001c");
1536 break;
1537 case '\x1d':
1538 APPEND_STRING("\\u001d");
1539 break;
1540 case '\x1e':
1541 APPEND_STRING("\\u001e");
1542 break;
1543 case '\x1f':
1544 APPEND_STRING("\\u001f");
1545 break;
1546 case '/':
1547 if (parson_escape_slashes) {
1548 APPEND_STRING("\\/"); /* to make json embeddable in xml\/html */
1549 }
1550 else {
1551 APPEND_STRING("/");
1552 }
1553 break;
1554 default:
1555 if (buf != NULL) {
1556 buf[0] = c;
1557 buf += 1;
1558 }
1559 written_total += 1;
1560 break;
1561 }
1562 }
1563 APPEND_STRING("\"");
1564 return written_total;
1565}
1566
1567#undef APPEND_STRING
1568#undef APPEND_INDENT
1569
1570/* Parser API */
1571JSON_Value *json_parse_file(const char *filename)
1572{
1573 char *file_contents = read_file(filename);
1574 JSON_Value *output_value = NULL;
1575 if (file_contents == NULL) {
1576 return NULL;
1577 }
1578 output_value = json_parse_string(file_contents);
1579 parson_free(file_contents);
1580 return output_value;
1581}
1583JSON_Value *json_parse_file_with_comments(const char *filename)
1584{
1585 char *file_contents = read_file(filename);
1586 JSON_Value *output_value = NULL;
1587 if (file_contents == NULL) {
1588 return NULL;
1589 }
1590 output_value = json_parse_string_with_comments(file_contents);
1591 parson_free(file_contents);
1592 return output_value;
1593}
1595JSON_Value *json_parse_string(const char *string)
1596{
1597 if (string == NULL) {
1598 return NULL;
1599 }
1600 if (string[0] == '\xEF' && string[1] == '\xBB' && string[2] == '\xBF') {
1601 string = string + 3; /* Support for UTF-8 BOM */
1602 }
1603 return parse_value((const char **)&string, 0);
1604}
1607{
1608 JSON_Value *result = NULL;
1609 char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL;
1610 string_mutable_copy = parson_strdup(string);
1611 if (string_mutable_copy == NULL) {
1612 return NULL;
1613 }
1614 remove_comments(string_mutable_copy, "/*", "*/");
1615 remove_comments(string_mutable_copy, "//", "\n");
1616 string_mutable_copy_ptr = string_mutable_copy;
1617 result = parse_value((const char **)&string_mutable_copy_ptr, 0);
1618 parson_free(string_mutable_copy);
1619 return result;
1620}
1621
1622/* JSON Object API */
1624JSON_Value *json_object_get_value(const JSON_Object *object, const char *name)
1625{
1626 if (object == NULL || name == NULL) {
1627 return NULL;
1628 }
1629 return json_object_getn_value(object, name, strlen(name));
1630}
1632const char *json_object_get_string(const JSON_Object *object, const char *name)
1633{
1635}
1637size_t json_object_get_string_len(const JSON_Object *object, const char *name)
1638{
1640}
1642double json_object_get_number(const JSON_Object *object, const char *name)
1643{
1645}
1647JSON_Object *json_object_get_object(const JSON_Object *object, const char *name)
1648{
1650}
1652JSON_Array *json_object_get_array(const JSON_Object *object, const char *name)
1653{
1655}
1657int json_object_get_boolean(const JSON_Object *object, const char *name)
1658{
1660}
1663 const char *name)
1664{
1665 const char *dot_position = strchr(name, '.');
1666 if (!dot_position) {
1667 return json_object_get_value(object, name);
1668 }
1669 object = json_value_get_object(
1670 json_object_getn_value(object, name, dot_position - name));
1671 return json_object_dotget_value(object, dot_position + 1);
1672}
1674const char *json_object_dotget_string(const JSON_Object *object,
1675 const char *name)
1676{
1678}
1680size_t json_object_dotget_string_len(const JSON_Object *object,
1681 const char *name)
1682{
1684}
1686double json_object_dotget_number(const JSON_Object *object, const char *name)
1687{
1689}
1692 const char *name)
1693{
1695}
1698 const char *name)
1699{
1701}
1703int json_object_dotget_boolean(const JSON_Object *object, const char *name)
1704{
1706}
1708size_t json_object_get_count(const JSON_Object *object)
1709{
1710 return object ? object->count : 0;
1711}
1713const char *json_object_get_name(const JSON_Object *object, size_t index)
1714{
1715 if (object == NULL || index >= json_object_get_count(object)) {
1716 return NULL;
1717 }
1718 return object->names[index];
1719}
1721JSON_Value *json_object_get_value_at(const JSON_Object *object, size_t index)
1722{
1723 if (object == NULL || index >= json_object_get_count(object)) {
1724 return NULL;
1725 }
1726 return object->values[index];
1727}
1730{
1731 if (!object) {
1732 return NULL;
1733 }
1734 return object->wrapping_value;
1735}
1737int json_object_has_value(const JSON_Object *object, const char *name)
1738{
1739 return json_object_get_value(object, name) != NULL;
1740}
1742int json_object_has_value_of_type(const JSON_Object *object, const char *name,
1743 JSON_Value_Type type)
1744{
1745 JSON_Value *val = json_object_get_value(object, name);
1746 return val != NULL && json_value_get_type(val) == type;
1747}
1749int json_object_dothas_value(const JSON_Object *object, const char *name)
1750{
1751 return json_object_dotget_value(object, name) != NULL;
1752}
1755 const char *name, JSON_Value_Type type)
1756{
1758 return val != NULL && json_value_get_type(val) == type;
1759}
1760
1761/* JSON Array API */
1762JSON_Value *json_array_get_value(const JSON_Array *array, size_t index)
1763{
1764 if (array == NULL || index >= json_array_get_count(array)) {
1765 return NULL;
1766 }
1767 return array->items[index];
1768}
1770const char *json_array_get_string(const JSON_Array *array, size_t index)
1771{
1772 return json_value_get_string(json_array_get_value(array, index));
1773}
1775size_t json_array_get_string_len(const JSON_Array *array, size_t index)
1776{
1777 return json_value_get_string_len(json_array_get_value(array, index));
1778}
1780double json_array_get_number(const JSON_Array *array, size_t index)
1781{
1782 return json_value_get_number(json_array_get_value(array, index));
1783}
1785JSON_Object *json_array_get_object(const JSON_Array *array, size_t index)
1786{
1787 return json_value_get_object(json_array_get_value(array, index));
1788}
1790JSON_Array *json_array_get_array(const JSON_Array *array, size_t index)
1791{
1792 return json_value_get_array(json_array_get_value(array, index));
1793}
1795int json_array_get_boolean(const JSON_Array *array, size_t index)
1796{
1797 return json_value_get_boolean(json_array_get_value(array, index));
1798}
1800size_t json_array_get_count(const JSON_Array *array)
1801{
1802 return array ? array->count : 0;
1803}
1806{
1807 if (!array) {
1808 return NULL;
1809 }
1810 return array->wrapping_value;
1811}
1812
1813/* JSON Value API */
1815{
1816 return value ? value->type : JSONError;
1817}
1820{
1821 return json_value_get_type(value) == JSONObject ? value->value.object
1822 : NULL;
1823}
1826{
1827 return json_value_get_type(value) == JSONArray ? value->value.array : NULL;
1828}
1829
1830static const JSON_String *json_value_get_string_desc(const JSON_Value *value)
1831{
1832 return json_value_get_type(value) == JSONString ? &value->value.string
1833 : NULL;
1834}
1836const char *json_value_get_string(const JSON_Value *value)
1837{
1838 const JSON_String *str = json_value_get_string_desc(value);
1839 return str ? str->chars : NULL;
1840}
1842size_t json_value_get_string_len(const JSON_Value *value)
1843{
1844 const JSON_String *str = json_value_get_string_desc(value);
1845 return str ? str->length : 0;
1846}
1848double json_value_get_number(const JSON_Value *value)
1849{
1850 return json_value_get_type(value) == JSONNumber ? value->value.number : 0;
1851}
1853int json_value_get_boolean(const JSON_Value *value)
1854{
1855 return json_value_get_type(value) == JSONBoolean ? value->value.boolean
1856 : -1;
1857}
1860{
1861 return value ? value->parent : NULL;
1862}
1864void json_value_free(JSON_Value *value)
1865{
1866 switch (json_value_get_type(value)) {
1867 case JSONObject:
1868 json_object_free(value->value.object);
1869 break;
1870 case JSONString:
1871 parson_free(value->value.string.chars);
1872 break;
1873 case JSONArray:
1874 json_array_free(value->value.array);
1875 break;
1876 default:
1877 break;
1878 }
1879 parson_free(value);
1880}
1883{
1884 JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value));
1885 if (!new_value) {
1886 return NULL;
1887 }
1888 new_value->parent = NULL;
1889 new_value->type = JSONObject;
1890 new_value->value.object = json_object_make(new_value);
1891 if (!new_value->value.object) {
1892 parson_free(new_value);
1893 return NULL;
1894 }
1895 return new_value;
1896}
1899{
1900 JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value));
1901 if (!new_value) {
1902 return NULL;
1903 }
1904 new_value->parent = NULL;
1905 new_value->type = JSONArray;
1906 new_value->value.array = json_array_make(new_value);
1907 if (!new_value->value.array) {
1908 parson_free(new_value);
1909 return NULL;
1910 }
1911 return new_value;
1912}
1914JSON_Value *json_value_init_string(const char *string)
1915{
1916 if (string == NULL) {
1917 return NULL;
1918 }
1919 return json_value_init_string_with_len(string, strlen(string));
1920}
1922JSON_Value *json_value_init_string_with_len(const char *string, size_t length)
1923{
1924 char *copy = NULL;
1925 JSON_Value *value;
1926 if (string == NULL) {
1927 return NULL;
1928 }
1929 if (!is_valid_utf8(string, length)) {
1930 return NULL;
1931 }
1932 copy = parson_strndup(string, length);
1933 if (copy == NULL) {
1934 return NULL;
1935 }
1936 value = json_value_init_string_no_copy(copy, length);
1937 if (value == NULL) {
1938 parson_free(copy);
1939 }
1940 return value;
1941}
1943JSON_Value *json_value_init_number(double number)
1944{
1945 JSON_Value *new_value = NULL;
1946 if (IS_NUMBER_INVALID(number)) {
1947 return NULL;
1948 }
1949 new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value));
1950 if (new_value == NULL) {
1951 return NULL;
1952 }
1953 new_value->parent = NULL;
1954 new_value->type = JSONNumber;
1955 new_value->value.number = number;
1956 return new_value;
1957}
1960{
1961 JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value));
1962 if (!new_value) {
1963 return NULL;
1964 }
1965 new_value->parent = NULL;
1966 new_value->type = JSONBoolean;
1967 new_value->value.boolean = boolean ? 1 : 0;
1968 return new_value;
1969}
1972{
1973 JSON_Value *new_value = (JSON_Value *)parson_malloc(sizeof(JSON_Value));
1974 if (!new_value) {
1975 return NULL;
1976 }
1977 new_value->parent = NULL;
1978 new_value->type = JSONNull;
1979 return new_value;
1980}
1983{
1984 size_t i = 0;
1985 JSON_Value *return_value = NULL, *temp_value_copy = NULL,
1986 *temp_value = NULL;
1987 const JSON_String *temp_string = NULL;
1988 const char *temp_key = NULL;
1989 char *temp_string_copy = NULL;
1990 JSON_Array *temp_array = NULL, *temp_array_copy = NULL;
1991 JSON_Object *temp_object = NULL, *temp_object_copy = NULL;
1993 char *key_copy = NULL;
1994
1995 switch (json_value_get_type(value)) {
1996 case JSONArray:
1997 temp_array = json_value_get_array(value);
1998 return_value = json_value_init_array();
1999 if (return_value == NULL) {
2000 return NULL;
2001 }
2002 temp_array_copy = json_value_get_array(return_value);
2003 for (i = 0; i < json_array_get_count(temp_array); i++) {
2004 temp_value = json_array_get_value(temp_array, i);
2005 temp_value_copy = json_value_deep_copy(temp_value);
2006 if (temp_value_copy == NULL) {
2007 json_value_free(return_value);
2008 return NULL;
2009 }
2010 if (json_array_add(temp_array_copy, temp_value_copy) !=
2011 JSONSuccess) {
2012 json_value_free(return_value);
2013 json_value_free(temp_value_copy);
2014 return NULL;
2015 }
2016 }
2017 return return_value;
2018 case JSONObject:
2019 temp_object = json_value_get_object(value);
2020 return_value = json_value_init_object();
2021 if (!return_value) {
2022 return NULL;
2023 }
2024 temp_object_copy = json_value_get_object(return_value);
2025 for (i = 0; i < json_object_get_count(temp_object); i++) {
2026 temp_key = json_object_get_name(temp_object, i);
2027 temp_value = json_object_get_value(temp_object, temp_key);
2028 temp_value_copy = json_value_deep_copy(temp_value);
2029 if (!temp_value_copy) {
2030 json_value_free(return_value);
2031 return NULL;
2032 }
2033 key_copy = parson_strdup(temp_key);
2034 if (!key_copy) {
2035 json_value_free(temp_value_copy);
2036 json_value_free(return_value);
2037 return NULL;
2038 }
2039 res = json_object_add(temp_object_copy, key_copy, temp_value_copy);
2040 if (res != JSONSuccess) {
2041 parson_free(key_copy);
2042 json_value_free(temp_value_copy);
2043 json_value_free(return_value);
2044 return NULL;
2045 }
2046 }
2047 return return_value;
2048 case JSONBoolean:
2050 case JSONNumber:
2052 case JSONString:
2053 temp_string = json_value_get_string_desc(value);
2054 if (temp_string == NULL) {
2055 return NULL;
2056 }
2057 temp_string_copy =
2058 parson_strndup(temp_string->chars, temp_string->length);
2059 if (temp_string_copy == NULL) {
2060 return NULL;
2061 }
2062 return_value = json_value_init_string_no_copy(temp_string_copy,
2063 temp_string->length);
2064 if (return_value == NULL) {
2065 parson_free(temp_string_copy);
2066 }
2067 return return_value;
2068 case JSONNull:
2069 return json_value_init_null();
2070 case JSONError:
2071 return NULL;
2072 default:
2073 return NULL;
2074 }
2075}
2077size_t json_serialization_size(const JSON_Value *value)
2078{
2079 char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack
2080 is a bad idea, so let's do it only
2081 once */
2082 int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_FALSE, num_buf);
2083 return res < 0 ? 0 : (size_t)(res) + 1;
2084}
2086JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf,
2087 size_t buf_size_in_bytes)
2088{
2089 int written = -1;
2090 size_t needed_size_in_bytes = json_serialization_size(value);
2091 if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
2092 return JSONFailure;
2093 }
2094 written = json_serialize_to_buffer_r(value, buf, 0, PARSON_FALSE, NULL);
2095 if (written < 0) {
2096 return JSONFailure;
2097 }
2098 return JSONSuccess;
2099}
2102 const char *filename)
2103{
2104 JSON_Status return_code = JSONSuccess;
2105 FILE *fp = NULL;
2106 char *serialized_string = json_serialize_to_string(value);
2107 if (serialized_string == NULL) {
2108 return JSONFailure;
2109 }
2110 fp = fopen(filename, "w");
2111 if (fp == NULL) {
2112 json_free_serialized_string(serialized_string);
2113 return JSONFailure;
2114 }
2115 if (fputs(serialized_string, fp) == EOF) {
2116 return_code = JSONFailure;
2117 }
2118 if (fclose(fp) == EOF) {
2119 return_code = JSONFailure;
2120 }
2121 json_free_serialized_string(serialized_string);
2122 return return_code;
2123}
2125char *json_serialize_to_string(const JSON_Value *value)
2126{
2127 JSON_Status serialization_result = JSONFailure;
2128 size_t buf_size_bytes = json_serialization_size(value);
2129 char *buf = NULL;
2130 if (buf_size_bytes == 0) {
2131 return NULL;
2132 }
2133 buf = (char *)parson_malloc(buf_size_bytes);
2134 if (buf == NULL) {
2135 return NULL;
2136 }
2137 serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes);
2138 if (serialization_result != JSONSuccess) {
2140 return NULL;
2141 }
2142 return buf;
2143}
2145size_t json_serialization_size_pretty(const JSON_Value *value)
2146{
2147 char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack
2148 is a bad idea, so let's do it only
2149 once */
2150 int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_TRUE, num_buf);
2151 return res < 0 ? 0 : (size_t)(res) + 1;
2152}
2155 size_t buf_size_in_bytes)
2156{
2157 int written = -1;
2158 size_t needed_size_in_bytes = json_serialization_size_pretty(value);
2159 if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
2160 return JSONFailure;
2161 }
2162 written = json_serialize_to_buffer_r(value, buf, 0, PARSON_TRUE, NULL);
2163 if (written < 0) {
2164 return JSONFailure;
2165 }
2166 return JSONSuccess;
2167}
2170 const char *filename)
2171{
2172 JSON_Status return_code = JSONSuccess;
2173 FILE *fp = NULL;
2174 char *serialized_string = json_serialize_to_string_pretty(value);
2175 if (serialized_string == NULL) {
2176 return JSONFailure;
2177 }
2178 fp = fopen(filename, "w");
2179 if (fp == NULL) {
2180 json_free_serialized_string(serialized_string);
2181 return JSONFailure;
2182 }
2183 if (fputs(serialized_string, fp) == EOF) {
2184 return_code = JSONFailure;
2185 }
2186 if (fclose(fp) == EOF) {
2187 return_code = JSONFailure;
2188 }
2189 json_free_serialized_string(serialized_string);
2190 return return_code;
2191}
2194{
2195 JSON_Status serialization_result = JSONFailure;
2196 size_t buf_size_bytes = json_serialization_size_pretty(value);
2197 char *buf = NULL;
2198 if (buf_size_bytes == 0) {
2199 return NULL;
2200 }
2201 buf = (char *)parson_malloc(buf_size_bytes);
2202 if (buf == NULL) {
2203 return NULL;
2204 }
2205 serialization_result =
2206 json_serialize_to_buffer_pretty(value, buf, buf_size_bytes);
2207 if (serialization_result != JSONSuccess) {
2209 return NULL;
2210 }
2211 return buf;
2212}
2214void json_free_serialized_string(char *string)
2215{
2216 parson_free(string);
2217}
2219JSON_Status json_array_remove(JSON_Array *array, size_t ix)
2220{
2221 size_t to_move_bytes = 0;
2222 if (array == NULL || ix >= json_array_get_count(array)) {
2223 return JSONFailure;
2224 }
2226 to_move_bytes =
2227 (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value *);
2228 memmove(array->items + ix, array->items + ix + 1, to_move_bytes);
2229 array->count -= 1;
2230 return JSONSuccess;
2231}
2234 JSON_Value *value)
2235{
2236 if (array == NULL || value == NULL || value->parent != NULL ||
2237 ix >= json_array_get_count(array)) {
2238 return JSONFailure;
2239 }
2241 value->parent = json_array_get_wrapping_value(array);
2242 array->items[ix] = value;
2243 return JSONSuccess;
2244}
2247 const char *string)
2248{
2249 JSON_Value *value = json_value_init_string(string);
2250 if (value == NULL) {
2251 return JSONFailure;
2252 }
2253 if (json_array_replace_value(array, i, value) != JSONSuccess) {
2254 json_value_free(value);
2255 return JSONFailure;
2256 }
2257 return JSONSuccess;
2258}
2261 const char *string, size_t len)
2262{
2263 JSON_Value *value = json_value_init_string_with_len(string, len);
2264 if (value == NULL) {
2265 return JSONFailure;
2266 }
2267 if (json_array_replace_value(array, i, value) != JSONSuccess) {
2268 json_value_free(value);
2269 return JSONFailure;
2270 }
2271 return JSONSuccess;
2272}
2275 double number)
2276{
2277 JSON_Value *value = json_value_init_number(number);
2278 if (value == NULL) {
2279 return JSONFailure;
2280 }
2281 if (json_array_replace_value(array, i, value) != JSONSuccess) {
2282 json_value_free(value);
2283 return JSONFailure;
2284 }
2285 return JSONSuccess;
2286}
2288JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean)
2289{
2290 JSON_Value *value = json_value_init_boolean(boolean);
2291 if (value == NULL) {
2292 return JSONFailure;
2293 }
2294 if (json_array_replace_value(array, i, value) != JSONSuccess) {
2295 json_value_free(value);
2296 return JSONFailure;
2297 }
2298 return JSONSuccess;
2299}
2302{
2304 if (value == NULL) {
2305 return JSONFailure;
2306 }
2307 if (json_array_replace_value(array, i, value) != JSONSuccess) {
2308 json_value_free(value);
2309 return JSONFailure;
2310 }
2311 return JSONSuccess;
2312}
2315{
2316 size_t i = 0;
2317 if (array == NULL) {
2318 return JSONFailure;
2319 }
2320 for (i = 0; i < json_array_get_count(array); i++) {
2322 }
2323 array->count = 0;
2324 return JSONSuccess;
2325}
2328{
2329 if (array == NULL || value == NULL || value->parent != NULL) {
2330 return JSONFailure;
2331 }
2332 return json_array_add(array, value);
2333}
2335JSON_Status json_array_append_string(JSON_Array *array, const char *string)
2336{
2337 JSON_Value *value = json_value_init_string(string);
2338 if (value == NULL) {
2339 return JSONFailure;
2340 }
2341 if (json_array_append_value(array, value) != JSONSuccess) {
2342 json_value_free(value);
2343 return JSONFailure;
2344 }
2345 return JSONSuccess;
2346}
2349 const char *string, size_t len)
2350{
2351 JSON_Value *value = json_value_init_string_with_len(string, len);
2352 if (value == NULL) {
2353 return JSONFailure;
2354 }
2355 if (json_array_append_value(array, value) != JSONSuccess) {
2356 json_value_free(value);
2357 return JSONFailure;
2358 }
2359 return JSONSuccess;
2360}
2362JSON_Status json_array_append_number(JSON_Array *array, double number)
2363{
2364 JSON_Value *value = json_value_init_number(number);
2365 if (value == NULL) {
2366 return JSONFailure;
2367 }
2368 if (json_array_append_value(array, value) != JSONSuccess) {
2369 json_value_free(value);
2370 return JSONFailure;
2371 }
2372 return JSONSuccess;
2373}
2376{
2377 JSON_Value *value = json_value_init_boolean(boolean);
2378 if (value == NULL) {
2379 return JSONFailure;
2380 }
2381 if (json_array_append_value(array, value) != JSONSuccess) {
2382 json_value_free(value);
2383 return JSONFailure;
2384 }
2385 return JSONSuccess;
2386}
2389{
2391 if (value == NULL) {
2392 return JSONFailure;
2393 }
2394 if (json_array_append_value(array, value) != JSONSuccess) {
2395 json_value_free(value);
2396 return JSONFailure;
2397 }
2398 return JSONSuccess;
2399}
2402 JSON_Value *value)
2403{
2404 unsigned long hash = 0;
2406 size_t cell_ix = 0;
2407 size_t item_ix = 0;
2408 JSON_Value *old_value = NULL;
2409 char *key_copy = NULL;
2410
2411 if (!object || !name || !value || value->parent) {
2412 return JSONFailure;
2413 }
2414 hash = hash_string(name, strlen(name));
2415 found = PARSON_FALSE;
2416 cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
2417 if (found) {
2418 item_ix = object->cells[cell_ix];
2419 old_value = object->values[item_ix];
2420 json_value_free(old_value);
2421 object->values[item_ix] = value;
2422 value->parent = json_object_get_wrapping_value(object);
2423 return JSONSuccess;
2424 }
2425 if (object->count >= object->item_capacity) {
2426 JSON_Status res = json_object_grow_and_rehash(object);
2427 if (res != JSONSuccess) {
2428 return JSONFailure;
2429 }
2430 cell_ix =
2431 json_object_get_cell_ix(object, name, strlen(name), hash, &found);
2432 }
2433 key_copy = parson_strdup(name);
2434 if (!key_copy) {
2435 return JSONFailure;
2436 }
2437 object->names[object->count] = key_copy;
2438 object->cells[cell_ix] = object->count;
2439 object->values[object->count] = value;
2440 object->cell_ixs[object->count] = cell_ix;
2441 object->hashes[object->count] = hash;
2442 object->count++;
2443 value->parent = json_object_get_wrapping_value(object);
2444 return JSONSuccess;
2445}
2448 const char *string)
2449{
2450 JSON_Value *value = json_value_init_string(string);
2451 JSON_Status status = json_object_set_value(object, name, value);
2452 if (status != JSONSuccess) {
2453 json_value_free(value);
2454 }
2455 return status;
2456}
2459 const char *name,
2460 const char *string, size_t len)
2461{
2462 JSON_Value *value = json_value_init_string_with_len(string, len);
2463 JSON_Status status = json_object_set_value(object, name, value);
2464 if (status != JSONSuccess) {
2465 json_value_free(value);
2466 }
2467 return status;
2468}
2471 double number)
2472{
2473 JSON_Value *value = json_value_init_number(number);
2474 JSON_Status status = json_object_set_value(object, name, value);
2475 if (status != JSONSuccess) {
2476 json_value_free(value);
2477 }
2478 return status;
2479}
2482 int boolean)
2483{
2484 JSON_Value *value = json_value_init_boolean(boolean);
2485 JSON_Status status = json_object_set_value(object, name, value);
2486 if (status != JSONSuccess) {
2487 json_value_free(value);
2488 }
2489 return status;
2490}
2493{
2495 JSON_Status status = json_object_set_value(object, name, value);
2496 if (status != JSONSuccess) {
2497 json_value_free(value);
2498 }
2499 return status;
2500}
2503 JSON_Value *value)
2504{
2505 const char *dot_pos = NULL;
2506 JSON_Value *temp_value = NULL, *new_value = NULL;
2507 JSON_Object *temp_object = NULL, *new_object = NULL;
2508 JSON_Status status = JSONFailure;
2509 size_t name_len = 0;
2510 char *name_copy = NULL;
2511
2512 if (object == NULL || name == NULL || value == NULL) {
2513 return JSONFailure;
2514 }
2515 dot_pos = strchr(name, '.');
2516 if (dot_pos == NULL) {
2517 return json_object_set_value(object, name, value);
2518 }
2519 name_len = dot_pos - name;
2520 temp_value = json_object_getn_value(object, name, name_len);
2521 if (temp_value) {
2522 /* Don't overwrite existing non-object (unlike json_object_set_value,
2523 * but it shouldn't be changed at this point) */
2524 if (json_value_get_type(temp_value) != JSONObject) {
2525 return JSONFailure;
2526 }
2527 temp_object = json_value_get_object(temp_value);
2528 return json_object_dotset_value(temp_object, dot_pos + 1, value);
2529 }
2530 new_value = json_value_init_object();
2531 if (new_value == NULL) {
2532 return JSONFailure;
2533 }
2534 new_object = json_value_get_object(new_value);
2535 status = json_object_dotset_value(new_object, dot_pos + 1, value);
2536 if (status != JSONSuccess) {
2537 json_value_free(new_value);
2538 return JSONFailure;
2539 }
2540 name_copy = parson_strndup(name, name_len);
2541 if (!name_copy) {
2542 json_object_dotremove_internal(new_object, dot_pos + 1, 0);
2543 json_value_free(new_value);
2544 return JSONFailure;
2545 }
2546 status = json_object_add(object, name_copy, new_value);
2547 if (status != JSONSuccess) {
2548 parson_free(name_copy);
2549 json_object_dotremove_internal(new_object, dot_pos + 1, 0);
2550 json_value_free(new_value);
2551 return JSONFailure;
2552 }
2553 return JSONSuccess;
2554}
2557 const char *string)
2558{
2559 JSON_Value *value = json_value_init_string(string);
2560 if (value == NULL) {
2561 return JSONFailure;
2562 }
2563 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2564 json_value_free(value);
2565 return JSONFailure;
2566 }
2567 return JSONSuccess;
2568}
2571 const char *name,
2572 const char *string, size_t len)
2573{
2574 JSON_Value *value = json_value_init_string_with_len(string, len);
2575 if (value == NULL) {
2576 return JSONFailure;
2577 }
2578 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2579 json_value_free(value);
2580 return JSONFailure;
2581 }
2582 return JSONSuccess;
2583}
2586 double number)
2587{
2588 JSON_Value *value = json_value_init_number(number);
2589 if (value == NULL) {
2590 return JSONFailure;
2591 }
2592 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2593 json_value_free(value);
2594 return JSONFailure;
2595 }
2596 return JSONSuccess;
2597}
2600 int boolean)
2601{
2602 JSON_Value *value = json_value_init_boolean(boolean);
2603 if (value == NULL) {
2604 return JSONFailure;
2605 }
2606 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2607 json_value_free(value);
2608 return JSONFailure;
2609 }
2610 return JSONSuccess;
2611}
2614{
2616 if (value == NULL) {
2617 return JSONFailure;
2618 }
2619 if (json_object_dotset_value(object, name, value) != JSONSuccess) {
2620 json_value_free(value);
2621 return JSONFailure;
2622 }
2623 return JSONSuccess;
2624}
2626JSON_Status json_object_remove(JSON_Object *object, const char *name)
2627{
2628 return json_object_remove_internal(object, name, PARSON_TRUE);
2629}
2632{
2633 return json_object_dotremove_internal(object, name, PARSON_TRUE);
2634}
2637{
2638 size_t i = 0;
2639 if (object == NULL) {
2640 return JSONFailure;
2641 }
2642 for (i = 0; i < json_object_get_count(object); i++) {
2643 parson_free(object->names[i]);
2644 object->names[i] = NULL;
2645
2646 json_value_free(object->values[i]);
2647 object->values[i] = NULL;
2648 }
2649 object->count = 0;
2650 for (i = 0; i < object->cell_capacity; i++) {
2651 object->cells[i] = OBJECT_INVALID_IX;
2652 }
2653 return JSONSuccess;
2654}
2656JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value)
2657{
2658 JSON_Value *temp_schema_value = NULL, *temp_value = NULL;
2659 JSON_Array *schema_array = NULL, *value_array = NULL;
2660 JSON_Object *schema_object = NULL, *value_object = NULL;
2661 JSON_Value_Type schema_type = JSONError, value_type = JSONError;
2662 const char *key = NULL;
2663 size_t i = 0, count = 0;
2664 if (schema == NULL || value == NULL) {
2665 return JSONFailure;
2666 }
2667 schema_type = json_value_get_type(schema);
2668 value_type = json_value_get_type(value);
2669 if (schema_type != value_type &&
2670 schema_type != JSONNull) { /* null represents all values */
2671 return JSONFailure;
2672 }
2673 switch (schema_type) {
2674 case JSONArray:
2675 schema_array = json_value_get_array(schema);
2676 value_array = json_value_get_array(value);
2677 count = json_array_get_count(schema_array);
2678 if (count == 0) {
2679 return JSONSuccess; /* Empty array allows all types */
2680 }
2681 /* Get first value from array, rest is ignored */
2682 temp_schema_value = json_array_get_value(schema_array, 0);
2683 for (i = 0; i < json_array_get_count(value_array); i++) {
2684 temp_value = json_array_get_value(value_array, i);
2685 if (json_validate(temp_schema_value, temp_value) != JSONSuccess) {
2686 return JSONFailure;
2687 }
2688 }
2689 return JSONSuccess;
2690 case JSONObject:
2691 schema_object = json_value_get_object(schema);
2692 value_object = json_value_get_object(value);
2693 count = json_object_get_count(schema_object);
2694 if (count == 0) {
2695 return JSONSuccess; /* Empty object allows all objects */
2696 }
2697 else if (json_object_get_count(value_object) < count) {
2698 return JSONFailure; /* Tested object mustn't have less name-value
2699 pairs than schema */
2700 }
2701 for (i = 0; i < count; i++) {
2702 key = json_object_get_name(schema_object, i);
2703 temp_schema_value = json_object_get_value(schema_object, key);
2704 temp_value = json_object_get_value(value_object, key);
2705 if (temp_value == NULL) {
2706 return JSONFailure;
2707 }
2708 if (json_validate(temp_schema_value, temp_value) != JSONSuccess) {
2709 return JSONFailure;
2710 }
2711 }
2712 return JSONSuccess;
2713 case JSONString:
2714 case JSONNumber:
2715 case JSONBoolean:
2716 case JSONNull:
2717 return JSONSuccess; /* equality already tested before switch */
2718 case JSONError:
2719 default:
2720 return JSONFailure;
2721 }
2722}
2724int json_value_equals(const JSON_Value *a, const JSON_Value *b)
2725{
2726 JSON_Object *a_object = NULL, *b_object = NULL;
2727 JSON_Array *a_array = NULL, *b_array = NULL;
2728 const JSON_String *a_string = NULL, *b_string = NULL;
2729 const char *key = NULL;
2730 size_t a_count = 0, b_count = 0, i = 0;
2731 JSON_Value_Type a_type, b_type;
2732 a_type = json_value_get_type(a);
2733 b_type = json_value_get_type(b);
2734 if (a_type != b_type) {
2735 return PARSON_FALSE;
2736 }
2737 switch (a_type) {
2738 case JSONArray:
2739 a_array = json_value_get_array(a);
2740 b_array = json_value_get_array(b);
2741 a_count = json_array_get_count(a_array);
2742 b_count = json_array_get_count(b_array);
2743 if (a_count != b_count) {
2744 return PARSON_FALSE;
2745 }
2746 for (i = 0; i < a_count; i++) {
2747 if (!json_value_equals(json_array_get_value(a_array, i),
2748 json_array_get_value(b_array, i))) {
2749 return PARSON_FALSE;
2750 }
2751 }
2752 return PARSON_TRUE;
2753 case JSONObject:
2754 a_object = json_value_get_object(a);
2755 b_object = json_value_get_object(b);
2756 a_count = json_object_get_count(a_object);
2757 b_count = json_object_get_count(b_object);
2758 if (a_count != b_count) {
2759 return PARSON_FALSE;
2760 }
2761 for (i = 0; i < a_count; i++) {
2762 key = json_object_get_name(a_object, i);
2763 if (!json_value_equals(json_object_get_value(a_object, key),
2764 json_object_get_value(b_object, key))) {
2765 return PARSON_FALSE;
2766 }
2767 }
2768 return PARSON_TRUE;
2769 case JSONString:
2770 a_string = json_value_get_string_desc(a);
2771 b_string = json_value_get_string_desc(b);
2772 if (a_string == NULL || b_string == NULL) {
2773 return PARSON_FALSE; /* shouldn't happen */
2774 }
2775 return a_string->length == b_string->length &&
2776 memcmp(a_string->chars, b_string->chars, a_string->length) == 0;
2777 case JSONBoolean:
2779 case JSONNumber:
2780 return fabs(json_value_get_number(a) - json_value_get_number(b)) <
2781 0.000001; /* EPSILON */
2782 case JSONError:
2783 return PARSON_TRUE;
2784 case JSONNull:
2785 return PARSON_TRUE;
2786 default:
2787 return PARSON_TRUE;
2788 }
2789}
2792{
2793 return json_value_get_type(value);
2794}
2796JSON_Object *json_object(const JSON_Value *value)
2797{
2798 return json_value_get_object(value);
2799}
2801JSON_Array *json_array(const JSON_Value *value)
2802{
2803 return json_value_get_array(value);
2804}
2806const char *json_string(const JSON_Value *value)
2807{
2808 return json_value_get_string(value);
2809}
2811size_t json_string_len(const JSON_Value *value)
2812{
2813 return json_value_get_string_len(value);
2814}
2816double json_number(const JSON_Value *value)
2817{
2818 return json_value_get_number(value);
2819}
2821int json_boolean(const JSON_Value *value)
2822{
2823 return json_value_get_boolean(value);
2824}
2827 JSON_Free_Function free_fun)
2828{
2829 parson_malloc = malloc_fun;
2830 parson_free = free_fun;
2831}
2833void json_set_escape_slashes(int escape_slashes)
2834{
2835 parson_escape_slashes = escape_slashes;
2836}
2838void json_set_float_serialization_format(const char *format)
2839{
2840 if (parson_float_format) {
2841 parson_free(parson_float_format);
2842 parson_float_format = NULL;
2843 }
2844 if (!format) {
2845 parson_float_format = NULL;
2846 return;
2847 }
2848 parson_float_format = parson_strdup(format);
2849}
2853{
2854 parson_number_serialization_function = func;
2855}
#define NULL
Definition ccmath.h:32
double b
#define HUGE_VAL
Values needed for Ray-Convex Polyhedron Intersection Test below originally by Eric Haines,...
Definition gs_query.c:29
int count
const char * name
Definition named_colr.c:6
JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string)
Definition parson.c:2555
size_t json_object_get_string_len(const JSON_Object *object, const char *name)
Definition parson.c:1636
JSON_Status json_array_replace_null(JSON_Array *array, size_t i)
Definition parson.c:2300
const char * json_value_get_string(const JSON_Value *value)
Definition parson.c:1835
const char * json_string(const JSON_Value *value)
Definition parson.c:2805
int json_object_get_boolean(const JSON_Object *object, const char *name)
Definition parson.c:1656
size_t json_value_get_string_len(const JSON_Value *value)
Definition parson.c:1841
size_t json_string_len(const JSON_Value *value)
Definition parson.c:2810
JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean)
Definition parson.c:2598
#define PARSON_FALSE
Definition parson.c:116
#define SKIP_CHAR(str)
Definition parson.c:82
JSON_Status json_array_append_null(JSON_Array *array)
Definition parson.c:2387
JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean)
Definition parson.c:2287
JSON_Status json_object_remove(JSON_Object *object, const char *name)
Definition parson.c:2625
JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes)
Definition parson.c:2153
#define IS_NUMBER_INVALID(x)
Definition parson.c:95
size_t json_serialization_size_pretty(const JSON_Value *value)
Definition parson.c:2144
JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value)
Definition parson.c:2232
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index)
Definition parson.c:1761
const char * json_array_get_string(const JSON_Array *array, size_t index)
Definition parson.c:1769
JSON_Status json_array_append_boolean(JSON_Array *array, int boolean)
Definition parson.c:2374
JSON_Value_Type json_type(const JSON_Value *value)
Definition parson.c:2790
int json_object_dotget_boolean(const JSON_Object *object, const char *name)
Definition parson.c:1702
const char * json_object_get_name(const JSON_Object *object, size_t index)
Definition parson.c:1712
JSON_Status json_array_replace_string_with_len(JSON_Array *array, size_t i, const char *string, size_t len)
Definition parson.c:2259
JSON_Array * json_value_get_array(const JSON_Value *value)
Definition parson.c:1824
const char * json_object_dotget_string(const JSON_Object *object, const char *name)
Definition parson.c:1673
JSON_Status json_object_dotset_null(JSON_Object *object, const char *name)
Definition parson.c:2612
JSON_Status json_object_set_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len)
Definition parson.c:2457
struct json_string JSON_String
#define SKIP_WHITESPACES(str)
Definition parson.c:83
JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number)
Definition parson.c:2469
int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type)
Definition parson.c:1741
JSON_Object * json_object_get_object(const JSON_Object *object, const char *name)
Definition parson.c:1646
void json_set_float_serialization_format(const char *format)
Definition parson.c:2837
size_t json_array_get_count(const JSON_Array *array)
Definition parson.c:1799
double json_array_get_number(const JSON_Array *array, size_t index)
Definition parson.c:1779
JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number)
Definition parson.c:2273
JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name)
Definition parson.c:1661
void json_set_number_serialization_function(JSON_Number_Serialization_Function func)
Definition parson.c:2850
JSON_Status json_array_clear(JSON_Array *array)
Definition parson.c:2313
JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string)
Definition parson.c:2446
JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean)
Definition parson.c:2480
void json_free_serialized_string(char *string)
Definition parson.c:2213
JSON_Status json_array_remove(JSON_Array *array, size_t ix)
Definition parson.c:2218
int json_value_equals(const JSON_Value *a, const JSON_Value *b)
Definition parson.c:2723
union json_value_value JSON_Value_Value
JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value)
Definition parson.c:2655
JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename)
Definition parson.c:2100
JSON_Status json_array_append_string_with_len(JSON_Array *array, const char *string, size_t len)
Definition parson.c:2347
#define PARSON_DEFAULT_FLOAT_FORMAT
Definition parson.c:68
JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name)
Definition parson.c:1696
int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type)
Definition parson.c:1753
void json_set_escape_slashes(int escape_slashes)
Definition parson.c:2832
JSON_Status json_object_dotremove(JSON_Object *object, const char *name)
Definition parson.c:2630
JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value)
Definition parson.c:2501
JSON_Value * json_value_init_array(void)
Definition parson.c:1897
size_t json_object_get_count(const JSON_Object *object)
Definition parson.c:1707
#define STARTING_CAPACITY
Definition parson.c:64
JSON_Value * json_object_get_wrapping_value(const JSON_Object *object)
Definition parson.c:1728
JSON_Object * json_array_get_object(const JSON_Array *array, size_t index)
Definition parson.c:1784
int json_boolean(const JSON_Value *value)
Definition parson.c:2820
double json_number(const JSON_Value *value)
Definition parson.c:2815
JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name)
Definition parson.c:1690
JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index)
Definition parson.c:1720
JSON_Value * json_parse_string(const char *string)
Definition parson.c:1594
JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value)
Definition parson.c:2326
int json_object_dothas_value(const JSON_Object *object, const char *name)
Definition parson.c:1748
#define APPEND_INDENT(level)
Definition parson.c:1269
JSON_Value * json_value_init_number(double number)
Definition parson.c:1942
#define PARSON_NUM_BUF_SIZE
Definition parson.c:73
JSON_Array * json_array(const JSON_Value *value)
Definition parson.c:2800
JSON_Array * json_object_get_array(const JSON_Object *object, const char *name)
Definition parson.c:1651
int json_object_has_value(const JSON_Object *object, const char *name)
Definition parson.c:1736
int json_value_get_boolean(const JSON_Value *value)
Definition parson.c:1852
JSON_Value * json_value_deep_copy(const JSON_Value *value)
Definition parson.c:1981
JSON_Value * json_parse_string_with_comments(const char *string)
Definition parson.c:1605
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name)
Definition parson.c:1623
#define MAX_NESTING
Definition parson.c:65
JSON_Status json_object_clear(JSON_Object *object)
Definition parson.c:2635
const char * json_object_get_string(const JSON_Object *object, const char *name)
Definition parson.c:1631
int json_array_get_boolean(const JSON_Array *array, size_t index)
Definition parson.c:1794
char * json_serialize_to_string(const JSON_Value *value)
Definition parson.c:2124
JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value)
Definition parson.c:2400
JSON_Status json_object_dotset_string_with_len(JSON_Object *object, const char *name, const char *string, size_t len)
Definition parson.c:2569
JSON_Value * json_value_init_null(void)
Definition parson.c:1970
#define IS_CONT(b)
Definition parson.c:110
JSON_Value * json_array_get_wrapping_value(const JSON_Array *array)
Definition parson.c:1804
size_t json_array_get_string_len(const JSON_Array *array, size_t index)
Definition parson.c:1774
void json_value_free(JSON_Value *value)
Definition parson.c:1863
JSON_Status json_object_set_null(JSON_Object *object, const char *name)
Definition parson.c:2491
JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename)
Definition parson.c:2168
JSON_Value * json_parse_file(const char *filename)
Definition parson.c:1570
size_t json_serialization_size(const JSON_Value *value)
Definition parson.c:2076
#define PARSON_TRUE
Definition parson.c:115
#define APPEND_STRING(str)
Definition parson.c:1258
JSON_Status json_array_append_string(JSON_Array *array, const char *string)
Definition parson.c:2334
JSON_Array * json_array_get_array(const JSON_Array *array, size_t index)
Definition parson.c:1789
JSON_Value * json_value_init_string_with_len(const char *string, size_t length)
Definition parson.c:1921
size_t json_object_dotget_string_len(const JSON_Object *object, const char *name)
Definition parson.c:1679
JSON_Value * json_value_init_boolean(int boolean)
Definition parson.c:1958
double json_value_get_number(const JSON_Value *value)
Definition parson.c:1847
double json_object_get_number(const JSON_Object *object, const char *name)
Definition parson.c:1641
double json_object_dotget_number(const JSON_Object *object, const char *name)
Definition parson.c:1685
JSON_Value_Type json_value_get_type(const JSON_Value *value)
Definition parson.c:1813
JSON_Object * json_value_get_object(const JSON_Value *value)
Definition parson.c:1818
JSON_Object * json_object(const JSON_Value *value)
Definition parson.c:2795
JSON_Value * json_value_init_object(void)
Definition parson.c:1881
#define SIZEOF_TOKEN(a)
Definition parson.c:81
char * json_serialize_to_string_pretty(const JSON_Value *value)
Definition parson.c:2192
JSON_Value * json_value_init_string(const char *string)
Definition parson.c:1913
int parson_bool_t
Definition parson.c:113
JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number)
Definition parson.c:2584
#define OBJECT_INVALID_IX
Definition parson.c:98
void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun)
Definition parson.c:2825
JSON_Value * json_parse_file_with_comments(const char *filename)
Definition parson.c:1582
JSON_Status json_array_append_number(JSON_Array *array, double number)
Definition parson.c:2361
JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char *string)
Definition parson.c:2245
JSON_Value * json_value_get_parent(const JSON_Value *value)
Definition parson.c:1858
JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes)
Definition parson.c:2085
#define MAX(a, b)
Definition parson.c:87
@ JSONError
Definition parson.h:50
@ JSONObject
Definition parson.h:54
@ JSONNull
Definition parson.h:51
@ JSONNumber
Definition parson.h:53
@ JSONBoolean
Definition parson.h:56
@ JSONString
Definition parson.h:52
@ JSONArray
Definition parson.h:55
@ JSONSuccess
Definition parson.h:60
@ JSONFailure
Definition parson.h:60
int JSON_Value_Type
Definition parson.h:58
int(* JSON_Number_Serialization_Function)(double num, char *buf)
Definition parson.h:71
struct json_array_t JSON_Array
Definition parson.h:46
int JSON_Status
Definition parson.h:61
struct json_value_t JSON_Value
Definition parson.h:47
void(* JSON_Free_Function)(void *)
Definition parson.h:64
struct json_object_t JSON_Object
Definition parson.h:45
void *(* JSON_Malloc_Function)(size_t)
Definition parson.h:63
void output(const char *fmt,...)
#define x