GRASS 8 Programmer's Manual  8.5.0dev(2025)-a92a3a018a
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
85 
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
102 
103 #define OBJECT_INVALID_IX ((size_t) - 1)
104 
105 static JSON_Malloc_Function parson_malloc = malloc;
106 static JSON_Free_Function parson_free = free;
107 
108 static int parson_escape_slashes = 1;
109 
110 static char *parson_float_format = NULL;
111 
112 static JSON_Number_Serialization_Function parson_number_serialization_function =
113  NULL;
114 
115 #define IS_CONT(b) \
116  (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */
117 
118 typedef int parson_bool_t;
119 
120 #define PARSON_TRUE 1
121 #define PARSON_FALSE 0
122 
123 typedef struct json_string {
124  char *chars;
125  size_t length;
126 } JSON_String;
127 
128 /* Type definitions */
129 typedef 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 
138 struct json_value_t {
139  JSON_Value *parent;
140  JSON_Value_Type type;
141  JSON_Value_Value value;
142 };
143 
144 struct 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 
156 struct json_array_t {
157  JSON_Value *wrapping_value;
158  JSON_Value **items;
159  size_t count;
160  size_t capacity;
161 };
162 
163 /* Various */
164 static char *read_file(const char *filename);
165 static void remove_comments(char *string, const char *start_token,
166  const char *end_token);
167 static char *parson_strndup(const char *string, size_t n);
168 static char *parson_strdup(const char *string);
169 static int parson_sprintf(char *s, const char *format, ...);
170 static int hex_char_to_int(char c);
171 static JSON_Status parse_utf16_hex(const char *string, unsigned int *result);
172 static int num_bytes_in_utf8_sequence(unsigned char c);
173 static JSON_Status verify_utf8_sequence(const unsigned char *string, int *len);
174 static parson_bool_t is_valid_utf8(const char *string, size_t string_len);
175 static parson_bool_t is_decimal(const char *string, size_t length);
176 static unsigned long hash_string(const char *string, size_t n);
177 
178 /* JSON Object */
179 static JSON_Object *json_object_make(JSON_Value *wrapping_value);
180 static JSON_Status json_object_init(JSON_Object *object, size_t capacity);
181 static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys,
182  parson_bool_t free_values);
183 static JSON_Status json_object_grow_and_rehash(JSON_Object *object);
184 static size_t json_object_get_cell_ix(const JSON_Object *object,
185  const char *key, size_t key_len,
186  unsigned long hash,
187  parson_bool_t *out_found);
188 static JSON_Status json_object_add(JSON_Object *object, char *name,
189  JSON_Value *value);
190 static JSON_Value *json_object_getn_value(const JSON_Object *object,
191  const char *name, size_t name_len);
192 static JSON_Status json_object_remove_internal(JSON_Object *object,
193  const char *name,
194  parson_bool_t free_value);
195 static JSON_Status json_object_dotremove_internal(JSON_Object *object,
196  const char *name,
197  parson_bool_t free_value);
198 static void json_object_free(JSON_Object *object);
199 
200 /* JSON Array */
201 static JSON_Array *json_array_make(JSON_Value *wrapping_value);
202 static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value);
203 static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity);
204 static void json_array_free(JSON_Array *array);
205 
206 /* JSON Value */
207 static JSON_Value *json_value_init_string_no_copy(char *string, size_t length);
208 static const JSON_String *json_value_get_string_desc(const JSON_Value *value);
209 
210 /* Parser */
211 static JSON_Status skip_quotes(const char **string);
212 static JSON_Status parse_utf16(const char **unprocessed, char **processed);
213 static char *process_string(const char *input, size_t input_len,
214  size_t *output_len);
215 static char *get_quoted_string(const char **string, size_t *output_string_len);
216 static JSON_Value *parse_object_value(const char **string, size_t nesting);
217 static JSON_Value *parse_array_value(const char **string, size_t nesting);
218 static JSON_Value *parse_string_value(const char **string);
219 static JSON_Value *parse_boolean_value(const char **string);
220 static JSON_Value *parse_number_value(const char **string);
221 static JSON_Value *parse_null_value(const char **string);
222 static JSON_Value *parse_value(const char **string, size_t nesting);
223 
224 /* Serialization */
225 static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf,
226  int level, parson_bool_t is_pretty,
227  char *num_buf);
228 static int json_serialize_string(const char *string, size_t len, char *buf);
229 
230 /* Various */
231 static 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  }
254  size_read = fread(file_contents, 1, size_to_read, fp);
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 
265 static void remove_comments(char *string, const char *start_token,
266  const char *end_token)
267 {
268  parson_bool_t in_string = PARSON_FALSE, escaped = PARSON_FALSE;
269  size_t i;
270  char *ptr = NULL, current_char;
271  size_t start_token_len = strlen(start_token);
272  size_t end_token_len = strlen(end_token);
273  if (start_token_len == 0 || end_token_len == 0) {
274  return;
275  }
276  while ((current_char = *string) != '\0') {
277  if (current_char == '\\' && !escaped) {
278  escaped = PARSON_TRUE;
279  string++;
280  continue;
281  }
282  else if (current_char == '\"' && !escaped) {
283  in_string = !in_string;
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  }
300  escaped = PARSON_FALSE;
301  string++;
302  }
303 }
304 
305 static 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 
318 static char *parson_strdup(const char *string)
319 {
320  return parson_strndup(string, strlen(string));
321 }
322 
323 static 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 
342 static 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 
356 static 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 
373 static 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 
393 static 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 */
422  if ((cp < 0x80 && *len > 1) || (cp < 0x800 && *len > 2) ||
423  (cp < 0x10000 && *len > 3)) {
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 
440 static 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 
454 static 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 
470 static 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 */
492 static JSON_Object *json_object_make(JSON_Value *wrapping_value)
493 {
494  JSON_Status res = JSONFailure;
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 
508 static 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;
545 error:
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 
554 static void json_object_deinit(JSON_Object *object, parson_bool_t free_keys,
555  parson_bool_t free_values)
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 
584 static JSON_Status json_object_grow_and_rehash(JSON_Object *object)
585 {
586  JSON_Value *wrapping_value = NULL;
587  JSON_Object new_object;
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 
615 static size_t json_object_get_cell_ix(const JSON_Object *object,
616  const char *key, size_t key_len,
617  unsigned long hash,
618  parson_bool_t *out_found)
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 
628  *out_found = PARSON_FALSE;
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];
641  key_to_check_len = strlen(key_to_check);
642  if (key_to_check_len == key_len &&
643  strncmp(key, key_to_check, key_len) == 0) {
644  *out_found = PARSON_TRUE;
645  return ix;
646  }
647  }
648  return OBJECT_INVALID_IX;
649 }
650 
651 static JSON_Status json_object_add(JSON_Object *object, char *name,
652  JSON_Value *value)
653 {
654  unsigned long hash = 0;
655  parson_bool_t found = PARSON_FALSE;
656  size_t cell_ix = 0;
657  JSON_Status res = JSONFailure;
658 
659  if (!object || !name || !value) {
660  return JSONFailure;
661  }
662 
663  hash = hash_string(name, strlen(name));
664  found = PARSON_FALSE;
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 
690 static JSON_Value *json_object_getn_value(const JSON_Object *object,
691  const char *name, size_t name_len)
692 {
693  unsigned long hash = 0;
694  parson_bool_t found = PARSON_FALSE;
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);
701  found = PARSON_FALSE;
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 
710 static JSON_Status json_object_remove_internal(JSON_Object *object,
711  const char *name,
712  parson_bool_t free_value)
713 {
714  unsigned long hash = 0;
715  parson_bool_t found = PARSON_FALSE;
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));
730  found = PARSON_FALSE;
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 
772 static JSON_Status json_object_dotremove_internal(JSON_Object *object,
773  const char *name,
774  parson_bool_t free_value)
775 {
776  JSON_Value *temp_value = NULL;
777  JSON_Object *temp_object = NULL;
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);
783  if (json_value_get_type(temp_value) != JSONObject) {
784  return JSONFailure;
785  }
786  temp_object = json_value_get_object(temp_value);
787  return json_object_dotremove_internal(temp_object, dot_pos + 1, free_value);
788 }
789 
790 static 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 */
797 static 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 
810 static 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 
824 static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity)
825 {
826  JSON_Value **new_items = NULL;
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 
844 static 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 */
855 static 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 */
869 static 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 
891 static 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;
941  *processed = processed_ptr;
942  *unprocessed = unprocessed_ptr;
943  return JSONSuccess;
944 }
945 
946 /* Copies and processes passed string up to supplied length.
947 Example: "\u006Corem ipsum" -> lorem ipsum */
948 static 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;
954  char *output = NULL, *output_ptr = NULL, *resized_output = NULL;
955  output = (char *)parson_malloc(initial_size);
956  if (output == NULL) {
957  goto error;
958  }
959  output_ptr = output;
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 {
1002  *output_ptr = *input_ptr;
1003  }
1004  output_ptr++;
1005  input_ptr++;
1006  }
1007  *output_ptr = '\0';
1008  /* resize to new length */
1009  final_size = (size_t)(output_ptr - output) + 1;
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  }
1015  memcpy(resized_output, output, final_size);
1016  *output_len = final_size - 1;
1017  parson_free(output);
1018  return resized_output;
1019 error:
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. */
1026 static 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,
1036  output_string_len);
1037 }
1038 
1039 static 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 
1074 static JSON_Value *parse_object_value(const char **string, size_t nesting)
1075 {
1076  JSON_Status status = JSONFailure;
1077  JSON_Value *output_value = NULL, *new_value = NULL;
1078  JSON_Object *output_object = NULL;
1079  char *new_key = NULL;
1080 
1081  output_value = json_value_init_object();
1082  if (output_value == NULL) {
1083  return NULL;
1084  }
1085  if (**string != '{') {
1086  json_value_free(output_value);
1087  return NULL;
1088  }
1089  output_object = json_value_get_object(output_value);
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) {
1101  json_value_free(output_value);
1102  return NULL;
1103  }
1104  if (key_len != strlen(new_key)) {
1105  parson_free(new_key);
1106  json_value_free(output_value);
1107  return NULL;
1108  }
1109  SKIP_WHITESPACES(string);
1110  if (**string != ':') {
1111  parson_free(new_key);
1112  json_value_free(output_value);
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);
1119  json_value_free(output_value);
1120  return NULL;
1121  }
1122  status = json_object_add(output_object, new_key, new_value);
1123  if (status != JSONSuccess) {
1124  parson_free(new_key);
1125  json_value_free(new_value);
1126  json_value_free(output_value);
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 != '}') {
1141  json_value_free(output_value);
1142  return NULL;
1143  }
1144  SKIP_CHAR(string);
1145  return output_value;
1146 }
1147 
1148 static JSON_Value *parse_array_value(const char **string, size_t nesting)
1149 {
1150  JSON_Value *output_value = NULL, *new_array_value = NULL;
1151  JSON_Array *output_array = NULL;
1152  output_value = json_value_init_array();
1153  if (output_value == NULL) {
1154  return NULL;
1155  }
1156  if (**string != '[') {
1157  json_value_free(output_value);
1158  return NULL;
1159  }
1160  output_array = json_value_get_array(output_value);
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) {
1170  json_value_free(output_value);
1171  return NULL;
1172  }
1173  if (json_array_add(output_array, new_array_value) != JSONSuccess) {
1174  json_value_free(new_array_value);
1175  json_value_free(output_value);
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) {
1192  json_value_free(output_value);
1193  return NULL;
1194  }
1195  SKIP_CHAR(string);
1196  return output_value;
1197 }
1198 
1199 static 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 
1215 static 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 
1230 static JSON_Value *parse_number_value(const char **string)
1231 {
1232  char *end;
1233  double number = 0;
1234  errno = 0;
1235  number = strtod(*string, &end);
1236  if (errno == ERANGE && (number <= -HUGE_VAL || number >= HUGE_VAL)) {
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 
1246 static 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)
1262  */
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 
1282 static 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;
1287  JSON_Value *temp_value = 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,
1309  is_pretty, num_buf);
1310  if (written < 0) {
1311  return -1;
1312  }
1313  if (buf != NULL) {
1314  buf += written;
1315  }
1316  written_total += written;
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  }
1352  written_total += written;
1353  APPEND_STRING(":");
1354  if (is_pretty) {
1355  APPEND_STRING(" ");
1356  }
1357  temp_value = json_object_get_value_at(object, i);
1358  written = json_serialize_to_buffer_r(temp_value, buf, level + 1,
1359  is_pretty, num_buf);
1360  if (written < 0) {
1361  return -1;
1362  }
1363  if (buf != NULL) {
1364  buf += written;
1365  }
1366  written_total += written;
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  }
1392  written_total += written;
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  }
1422  written_total += written;
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 
1434 static 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 */
1575 JSON_Value *json_parse_file(const char *filename)
1576 {
1577  char *file_contents = read_file(filename);
1578  JSON_Value *output_value = NULL;
1579  if (file_contents == NULL) {
1580  return NULL;
1581  }
1582  output_value = json_parse_string(file_contents);
1583  parson_free(file_contents);
1584  return output_value;
1585 }
1587 JSON_Value *json_parse_file_with_comments(const char *filename)
1588 {
1589  char *file_contents = read_file(filename);
1590  JSON_Value *output_value = NULL;
1591  if (file_contents == NULL) {
1592  return NULL;
1593  }
1594  output_value = json_parse_string_with_comments(file_contents);
1595  parson_free(file_contents);
1596  return output_value;
1597 }
1599 JSON_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 }
1610 JSON_Value *json_parse_string_with_comments(const char *string)
1611 {
1612  JSON_Value *result = NULL;
1613  char *string_mutable_copy = NULL, *string_mutable_copy_ptr = 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");
1620  string_mutable_copy_ptr = string_mutable_copy;
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 */
1628 JSON_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 }
1636 const char *json_object_get_string(const JSON_Object *object, const char *name)
1637 {
1639 }
1641 size_t json_object_get_string_len(const JSON_Object *object, const char *name)
1642 {
1644 }
1646 double json_object_get_number(const JSON_Object *object, const char *name)
1647 {
1649 }
1651 JSON_Object *json_object_get_object(const JSON_Object *object, const char *name)
1652 {
1654 }
1656 JSON_Array *json_object_get_array(const JSON_Object *object, const char *name)
1657 {
1659 }
1661 int 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 }
1678 const char *json_object_dotget_string(const JSON_Object *object,
1679  const char *name)
1680 {
1682 }
1684 size_t json_object_dotget_string_len(const JSON_Object *object,
1685  const char *name)
1686 {
1688 }
1690 double 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 }
1707 int json_object_dotget_boolean(const JSON_Object *object, const char *name)
1708 {
1710 }
1712 size_t json_object_get_count(const JSON_Object *object)
1713 {
1714  return object ? object->count : 0;
1715 }
1717 const 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 }
1725 JSON_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 }
1741 int json_object_has_value(const JSON_Object *object, const char *name)
1742 {
1743  return json_object_get_value(object, name) != NULL;
1744 }
1746 int 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 }
1753 int 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 {
1761  JSON_Value *val = json_object_dotget_value(object, name);
1762  return val != NULL && json_value_get_type(val) == type;
1763 }
1764 
1765 /* JSON Array API */
1766 JSON_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 }
1774 const 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 }
1779 size_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 }
1784 double 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 }
1789 JSON_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 }
1794 JSON_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 }
1799 int 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 }
1804 size_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 
1834 static 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 }
1840 const 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 }
1846 size_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 }
1852 double json_value_get_number(const JSON_Value *value)
1853 {
1854  return json_value_get_type(value) == JSONNumber ? value->value.number : 0;
1855 }
1857 int 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 }
1868 void 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 }
1918 JSON_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 }
1926 JSON_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 }
1947 JSON_Value *json_value_init_number(double number)
1948 {
1949  JSON_Value *new_value = NULL;
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 }
1963 JSON_Value *json_value_init_boolean(int boolean)
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;
1989  JSON_Value *return_value = NULL, *temp_value_copy = NULL,
1990  *temp_value = NULL;
1991  const JSON_String *temp_string = NULL;
1992  const char *temp_key = NULL;
1993  char *temp_string_copy = NULL;
1994  JSON_Array *temp_array = NULL, *temp_array_copy = NULL;
1995  JSON_Object *temp_object = NULL, *temp_object_copy = NULL;
1996  JSON_Status res = JSONFailure;
1997  char *key_copy = NULL;
1998 
1999  switch (json_value_get_type(value)) {
2000  case JSONArray:
2001  temp_array = json_value_get_array(value);
2002  return_value = json_value_init_array();
2003  if (return_value == NULL) {
2004  return NULL;
2005  }
2006  temp_array_copy = json_value_get_array(return_value);
2007  for (i = 0; i < json_array_get_count(temp_array); i++) {
2008  temp_value = json_array_get_value(temp_array, i);
2009  temp_value_copy = json_value_deep_copy(temp_value);
2010  if (temp_value_copy == NULL) {
2011  json_value_free(return_value);
2012  return NULL;
2013  }
2014  if (json_array_add(temp_array_copy, temp_value_copy) !=
2015  JSONSuccess) {
2016  json_value_free(return_value);
2017  json_value_free(temp_value_copy);
2018  return NULL;
2019  }
2020  }
2021  return return_value;
2022  case JSONObject:
2023  temp_object = json_value_get_object(value);
2024  return_value = json_value_init_object();
2025  if (!return_value) {
2026  return NULL;
2027  }
2028  temp_object_copy = json_value_get_object(return_value);
2029  for (i = 0; i < json_object_get_count(temp_object); i++) {
2030  temp_key = json_object_get_name(temp_object, i);
2031  temp_value = json_object_get_value(temp_object, temp_key);
2032  temp_value_copy = json_value_deep_copy(temp_value);
2033  if (!temp_value_copy) {
2034  json_value_free(return_value);
2035  return NULL;
2036  }
2037  key_copy = parson_strdup(temp_key);
2038  if (!key_copy) {
2039  json_value_free(temp_value_copy);
2040  json_value_free(return_value);
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);
2046  json_value_free(temp_value_copy);
2047  json_value_free(return_value);
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  }
2061  temp_string_copy =
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 }
2081 size_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 }
2090 JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf,
2091  size_t buf_size_in_bytes)
2092 {
2093  int written = -1;
2094  size_t needed_size_in_bytes = json_serialization_size(value);
2095  if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
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 {
2108  JSON_Status return_code = JSONSuccess;
2109  FILE *fp = NULL;
2110  char *serialized_string = json_serialize_to_string(value);
2111  if (serialized_string == NULL) {
2112  return JSONFailure;
2113  }
2114  fp = fopen(filename, "w");
2115  if (fp == NULL) {
2116  json_free_serialized_string(serialized_string);
2117  return JSONFailure;
2118  }
2119  if (fputs(serialized_string, fp) == EOF) {
2120  return_code = JSONFailure;
2121  }
2122  if (fclose(fp) == EOF) {
2123  return_code = JSONFailure;
2124  }
2125  json_free_serialized_string(serialized_string);
2126  return return_code;
2127 }
2129 char *json_serialize_to_string(const JSON_Value *value)
2130 {
2131  JSON_Status serialization_result = JSONFailure;
2132  size_t buf_size_bytes = json_serialization_size(value);
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  }
2141  serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes);
2142  if (serialization_result != JSONSuccess) {
2144  return NULL;
2145  }
2146  return buf;
2147 }
2149 size_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;
2162  size_t needed_size_in_bytes = json_serialization_size_pretty(value);
2163  if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
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 {
2176  JSON_Status return_code = JSONSuccess;
2177  FILE *fp = NULL;
2178  char *serialized_string = json_serialize_to_string_pretty(value);
2179  if (serialized_string == NULL) {
2180  return JSONFailure;
2181  }
2182  fp = fopen(filename, "w");
2183  if (fp == NULL) {
2184  json_free_serialized_string(serialized_string);
2185  return JSONFailure;
2186  }
2187  if (fputs(serialized_string, fp) == EOF) {
2188  return_code = JSONFailure;
2189  }
2190  if (fclose(fp) == EOF) {
2191  return_code = JSONFailure;
2192  }
2193  json_free_serialized_string(serialized_string);
2194  return return_code;
2195 }
2197 char *json_serialize_to_string_pretty(const JSON_Value *value)
2198 {
2199  JSON_Status serialization_result = JSONFailure;
2200  size_t buf_size_bytes = json_serialization_size_pretty(value);
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  }
2209  serialization_result =
2210  json_serialize_to_buffer_pretty(value, buf, buf_size_bytes);
2211  if (serialization_result != JSONSuccess) {
2213  return NULL;
2214  }
2215  return buf;
2216 }
2218 void json_free_serialized_string(char *string)
2219 {
2220  parson_free(string);
2221 }
2223 JSON_Status json_array_remove(JSON_Array *array, size_t ix)
2224 {
2225  size_t to_move_bytes = 0;
2226  if (array == NULL || ix >= json_array_get_count(array)) {
2227  return JSONFailure;
2228  }
2230  to_move_bytes =
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 }
2292 JSON_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 {
2307  JSON_Value *value = json_value_init_null();
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 }
2339 JSON_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 }
2366 JSON_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 {
2394  JSON_Value *value = json_value_init_null();
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 }
2405 JSON_Status json_object_set_value(JSON_Object *object, const char *name,
2406  JSON_Value *value)
2407 {
2408  unsigned long hash = 0;
2409  parson_bool_t found = PARSON_FALSE;
2410  size_t cell_ix = 0;
2411  size_t item_ix = 0;
2412  JSON_Value *old_value = NULL;
2413  char *key_copy = NULL;
2414 
2415  if (!object || !name || !value || value->parent) {
2416  return JSONFailure;
2417  }
2418  hash = hash_string(name, strlen(name));
2419  found = PARSON_FALSE;
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];
2424  json_value_free(old_value);
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 }
2451 JSON_Status json_object_set_string(JSON_Object *object, const char *name,
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 }
2474 JSON_Status json_object_set_number(JSON_Object *object, const char *name,
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 }
2496 JSON_Status json_object_set_null(JSON_Object *object, const char *name)
2497 {
2498  JSON_Value *value = json_value_init_null();
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;
2510  JSON_Value *temp_value = NULL, *new_value = NULL;
2511  JSON_Object *temp_object = NULL, *new_object = 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) */
2528  if (json_value_get_type(temp_value) != JSONObject) {
2529  return JSONFailure;
2530  }
2531  temp_object = json_value_get_object(temp_value);
2532  return json_object_dotset_value(temp_object, dot_pos + 1, value);
2533  }
2534  new_value = json_value_init_object();
2535  if (new_value == NULL) {
2536  return JSONFailure;
2537  }
2538  new_object = json_value_get_object(new_value);
2539  status = json_object_dotset_value(new_object, dot_pos + 1, value);
2540  if (status != JSONSuccess) {
2541  json_value_free(new_value);
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);
2547  json_value_free(new_value);
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);
2554  json_value_free(new_value);
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 {
2619  JSON_Value *value = json_value_init_null();
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 }
2630 JSON_Status json_object_remove(JSON_Object *object, const char *name)
2631 {
2632  return json_object_remove_internal(object, name, PARSON_TRUE);
2633 }
2635 JSON_Status json_object_dotremove(JSON_Object *object, const char *name)
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 }
2660 JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value)
2661 {
2662  JSON_Value *temp_schema_value = NULL, *temp_value = NULL;
2663  JSON_Array *schema_array = NULL, *value_array = NULL;
2664  JSON_Object *schema_object = NULL, *value_object = NULL;
2665  JSON_Value_Type schema_type = JSONError, value_type = JSONError;
2666  const char *key = NULL;
2667  size_t i = 0, count = 0;
2668  if (schema == NULL || value == NULL) {
2669  return JSONFailure;
2670  }
2671  schema_type = json_value_get_type(schema);
2672  value_type = json_value_get_type(value);
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:
2679  schema_array = json_value_get_array(schema);
2680  value_array = json_value_get_array(value);
2681  count = json_array_get_count(schema_array);
2682  if (count == 0) {
2683  return JSONSuccess; /* Empty array allows all types */
2684  }
2685  /* Get first value from array, rest is ignored */
2686  temp_schema_value = json_array_get_value(schema_array, 0);
2687  for (i = 0; i < json_array_get_count(value_array); i++) {
2688  temp_value = json_array_get_value(value_array, i);
2689  if (json_validate(temp_schema_value, temp_value) != JSONSuccess) {
2690  return JSONFailure;
2691  }
2692  }
2693  return JSONSuccess;
2694  case JSONObject:
2695  schema_object = json_value_get_object(schema);
2696  value_object = json_value_get_object(value);
2697  count = json_object_get_count(schema_object);
2698  if (count == 0) {
2699  return JSONSuccess; /* Empty object allows all objects */
2700  }
2701  else if (json_object_get_count(value_object) < count) {
2702  return JSONFailure; /* Tested object mustn't have less name-value
2703  pairs than schema */
2704  }
2705  for (i = 0; i < count; i++) {
2706  key = json_object_get_name(schema_object, i);
2707  temp_schema_value = json_object_get_value(schema_object, key);
2708  temp_value = json_object_get_value(value_object, key);
2709  if (temp_value == NULL) {
2710  return JSONFailure;
2711  }
2712  if (json_validate(temp_schema_value, temp_value) != JSONSuccess) {
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 }
2728 int json_value_equals(const JSON_Value *a, const JSON_Value *b)
2729 {
2730  JSON_Object *a_object = NULL, *b_object = NULL;
2731  JSON_Array *a_array = NULL, *b_array = NULL;
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;
2735  JSON_Value_Type a_type, b_type;
2736  a_type = json_value_get_type(a);
2737  b_type = json_value_get_type(b);
2738  if (a_type != b_type) {
2739  return PARSON_FALSE;
2740  }
2741  switch (a_type) {
2742  case JSONArray:
2743  a_array = json_value_get_array(a);
2744  b_array = json_value_get_array(b);
2745  a_count = json_array_get_count(a_array);
2746  b_count = json_array_get_count(b_array);
2747  if (a_count != b_count) {
2748  return PARSON_FALSE;
2749  }
2750  for (i = 0; i < a_count; i++) {
2751  if (!json_value_equals(json_array_get_value(a_array, i),
2752  json_array_get_value(b_array, i))) {
2753  return PARSON_FALSE;
2754  }
2755  }
2756  return PARSON_TRUE;
2757  case JSONObject:
2758  a_object = json_value_get_object(a);
2759  b_object = json_value_get_object(b);
2760  a_count = json_object_get_count(a_object);
2761  b_count = json_object_get_count(b_object);
2762  if (a_count != b_count) {
2763  return PARSON_FALSE;
2764  }
2765  for (i = 0; i < a_count; i++) {
2766  key = json_object_get_name(a_object, i);
2767  if (!json_value_equals(json_object_get_value(a_object, key),
2768  json_object_get_value(b_object, key))) {
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:
2784  return fabs(json_value_get_number(a) - json_value_get_number(b)) <
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 }
2795 JSON_Value_Type json_type(const JSON_Value *value)
2796 {
2797  return json_value_get_type(value);
2798 }
2800 JSON_Object *json_object(const JSON_Value *value)
2801 {
2802  return json_value_get_object(value);
2803 }
2805 JSON_Array *json_array(const JSON_Value *value)
2806 {
2807  return json_value_get_array(value);
2808 }
2810 const char *json_string(const JSON_Value *value)
2811 {
2812  return json_value_get_string(value);
2813 }
2815 size_t json_string_len(const JSON_Value *value)
2816 {
2817  return json_value_get_string_len(value);
2818 }
2820 double json_number(const JSON_Value *value)
2821 {
2822  return json_value_get_number(value);
2823 }
2825 int json_boolean(const JSON_Value *value)
2826 {
2827  return json_value_get_boolean(value);
2828 }
2831  JSON_Free_Function free_fun)
2832 {
2833  parson_malloc = malloc_fun;
2834  parson_free = free_fun;
2835 }
2837 void json_set_escape_slashes(int escape_slashes)
2838 {
2839  parson_escape_slashes = escape_slashes;
2840 }
2842 void 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_Value * json_parse_file_with_comments(const char *filename)
Definition: parson.c:1586
JSON_Status json_array_replace_null(JSON_Array *array, size_t i)
Definition: parson.c:2304
JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name)
Definition: parson.c:1694
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_Value * json_value_init_array(void)
Definition: parson.c:1901
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index)
Definition: parson.c:1765
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_Value * json_value_deep_copy(const JSON_Value *value)
Definition: parson.c:1985
JSON_Status json_object_remove(JSON_Object *object, const char *name)
Definition: parson.c:2629
JSON_Object * json_value_get_object(const JSON_Value *value)
Definition: parson.c:1822
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
JSON_Object * json_array_get_object(const JSON_Array *array, size_t index)
Definition: parson.c:1788
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_value_init_string(const char *string)
Definition: parson.c:1917
JSON_Value * json_value_init_string_with_len(const char *string, size_t length)
Definition: parson.c:1925
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
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name)
Definition: parson.c:1627
JSON_Status json_array_replace_string_with_len(JSON_Array *array, size_t i, const char *string, size_t len)
Definition: parson.c:2263
const char * json_value_get_string(const JSON_Value *value)
Definition: parson.c:1839
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
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
const char * json_array_get_string(const JSON_Array *array, size_t index)
Definition: parson.c:1773
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_Array * json_value_get_array(const JSON_Value *value)
Definition: parson.c:1828
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_Object * json_object_get_object(const JSON_Object *object, const char *name)
Definition: parson.c:1650
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_Value * json_object_get_wrapping_value(const JSON_Object *object)
Definition: parson.c:1732
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_Value * json_object_dotget_value(const JSON_Object *object, const char *name)
Definition: parson.c:1665
char * json_serialize_to_string_pretty(const JSON_Value *value)
Definition: parson.c:2196
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
size_t json_object_get_count(const JSON_Object *object)
Definition: parson.c:1711
#define STARTING_CAPACITY
Definition: parson.c:68
JSON_Value * json_parse_string(const char *string)
Definition: parson.c:1598
int json_boolean(const JSON_Value *value)
Definition: parson.c:2824
JSON_Value * json_value_get_parent(const JSON_Value *value)
Definition: parson.c:1862
double json_number(const JSON_Value *value)
Definition: parson.c:2819
JSON_Value * json_parse_string_with_comments(const char *string)
Definition: parson.c:1609
JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index)
Definition: parson.c:1724
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
#define PARSON_NUM_BUF_SIZE
Definition: parson.c:77
const char * json_string(const JSON_Value *value)
Definition: parson.c:2809
JSON_Object * json_object(const JSON_Value *value)
Definition: parson.c:2799
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_init_object(void)
Definition: parson.c:1885
#define MAX_NESTING
Definition: parson.c:69
JSON_Status json_object_clear(JSON_Object *object)
Definition: parson.c:2639
JSON_Array * json_array_get_array(const JSON_Array *array, size_t index)
Definition: parson.c:1793
JSON_Value * json_value_init_null(void)
Definition: parson.c:1974
int json_array_get_boolean(const JSON_Array *array, size_t index)
Definition: parson.c:1798
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
#define IS_CONT(b)
Definition: parson.c:114
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
size_t json_serialization_size(const JSON_Value *value)
Definition: parson.c:2080
JSON_Array * json_object_get_array(const JSON_Object *object, const char *name)
Definition: parson.c:1655
#define PARSON_TRUE
Definition: parson.c:119
const char * json_object_dotget_string(const JSON_Object *object, const char *name)
Definition: parson.c:1677
#define APPEND_STRING(str)
Definition: parson.c:1262
JSON_Status json_array_append_string(JSON_Array *array, const char *string)
Definition: parson.c:2338
const char * json_object_get_string(const JSON_Object *object, const char *name)
Definition: parson.c:1635
size_t json_object_dotget_string_len(const JSON_Object *object, const char *name)
Definition: parson.c:1683
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 * json_parse_file(const char *filename)
Definition: parson.c:1574
JSON_Value_Type json_value_get_type(const JSON_Value *value)
Definition: parson.c:1817
JSON_Array * json_array(const JSON_Value *value)
Definition: parson.c:2804
#define SIZEOF_TOKEN(a)
Definition: parson.c:85
JSON_Value * json_value_init_boolean(int boolean)
Definition: parson.c:1962
char * json_serialize_to_string(const JSON_Value *value)
Definition: parson.c:2128
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
const char * json_object_get_name(const JSON_Object *object, size_t index)
Definition: parson.c:1716
void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun)
Definition: parson.c:2829
JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name)
Definition: parson.c:1700
JSON_Value * json_array_get_wrapping_value(const JSON_Array *array)
Definition: parson.c:1808
JSON_Value * json_value_init_number(double number)
Definition: parson.c:1946
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_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
void *(* JSON_Malloc_Function)(size_t)
Definition: parson.h:63
@ 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 output(const char *fmt,...)
double b
Definition: r_raster.c:39
void * malloc(unsigned)
void free(void *)
#define x