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