source: azure_iot_hub/trunk/azure_iothub/deps/parson/parson.c@ 389

Last change on this file since 389 was 389, checked in by coas-nagasima, 5 years ago

ビルドが通るよう更新

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