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