Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip
parson.c
00001 /* 00002 Parson ( http://kgabis.github.com/parson/ ) 00003 Copyright (c) 2012 - 2016 Krzysztof Gabis 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 #ifdef _MSC_VER 00024 #ifndef _CRT_SECURE_NO_WARNINGS 00025 #define _CRT_SECURE_NO_WARNINGS 00026 #endif /* _CRT_SECURE_NO_WARNINGS */ 00027 #endif /* _MSC_VER */ 00028 00029 #include "parson.h" 00030 00031 #include <stdio.h> 00032 #include <stdlib.h> 00033 #include <string.h> 00034 #include <ctype.h> 00035 #include <math.h> 00036 00037 #define STARTING_CAPACITY 15 00038 #define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */ 00039 #define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */ 00040 #define MAX_NESTING 19 00041 #define DOUBLE_SERIALIZATION_FORMAT "%f" 00042 00043 #define SIZEOF_TOKEN(a) (sizeof(a) - 1) 00044 #define SKIP_CHAR(str) ((*str)++) 00045 #define SKIP_WHITESPACES(str) while (isspace(**str)) { SKIP_CHAR(str); } 00046 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 00047 00048 #undef malloc 00049 #undef free 00050 00051 static JSON_Malloc_Function parson_malloc = malloc; 00052 static JSON_Free_Function parson_free = free; 00053 00054 #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ 00055 00056 /* Type definitions */ 00057 typedef union json_value_value { 00058 char *string; 00059 double number; 00060 JSON_Object *object; 00061 JSON_Array *array; 00062 int boolean; 00063 int null; 00064 } JSON_Value_Value; 00065 00066 struct json_value_t { 00067 JSON_Value_Type type; 00068 JSON_Value_Value value; 00069 }; 00070 00071 struct json_object_t { 00072 char **names; 00073 JSON_Value **values; 00074 size_t count; 00075 size_t capacity; 00076 }; 00077 00078 struct json_array_t { 00079 JSON_Value **items; 00080 size_t count; 00081 size_t capacity; 00082 }; 00083 00084 /* Various */ 00085 static char * read_file(const char *filename); 00086 static void remove_comments(char *string, const char *start_token, const char *end_token); 00087 static char * parson_strndup(const char *string, size_t n); 00088 static char * parson_strdup(const char *string); 00089 static int is_utf16_hex(const unsigned char *string); 00090 static int num_bytes_in_utf8_sequence(unsigned char c); 00091 static int verify_utf8_sequence(const unsigned char *string, int *len); 00092 static int is_valid_utf8(const char *string, size_t string_len); 00093 static int is_decimal(const char *string, size_t length); 00094 00095 /* JSON Object */ 00096 static JSON_Object * json_object_init(void); 00097 static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value); 00098 static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity); 00099 static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n); 00100 static void json_object_free(JSON_Object *object); 00101 00102 /* JSON Array */ 00103 static JSON_Array * json_array_init(void); 00104 static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value); 00105 static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity); 00106 static void json_array_free(JSON_Array *array); 00107 00108 /* JSON Value */ 00109 static JSON_Value * json_value_init_string_no_copy(char *string); 00110 00111 /* Parser */ 00112 static JSON_Status skip_quotes(const char **string); 00113 static int parse_utf_16(const char **unprocessed, char **processed); 00114 static char * process_string(const char *input, size_t len); 00115 static char * get_quoted_string(const char **string); 00116 static JSON_Value * parse_object_value(const char **string, size_t nesting); 00117 static JSON_Value * parse_array_value(const char **string, size_t nesting); 00118 static JSON_Value * parse_string_value(const char **string); 00119 static JSON_Value * parse_boolean_value(const char **string); 00120 static JSON_Value * parse_number_value(const char **string); 00121 static JSON_Value * parse_null_value(const char **string); 00122 static JSON_Value * parse_value(const char **string, size_t nesting); 00123 00124 /* Serialization */ 00125 static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf); 00126 static int json_serialize_string(const char *string, char *buf); 00127 static int append_indent(char *buf, int level); 00128 static int append_string(char *buf, const char *string); 00129 00130 /* Various */ 00131 static char * parson_strndup(const char *string, size_t n) { 00132 char *output_string = (char*)parson_malloc(n + 1); 00133 if (!output_string) { 00134 return NULL; 00135 } 00136 output_string[n] = '\0'; 00137 strncpy(output_string, string, n); 00138 return output_string; 00139 } 00140 00141 static char * parson_strdup(const char *string) { 00142 return parson_strndup(string, strlen(string)); 00143 } 00144 00145 static int is_utf16_hex(const unsigned char *s) { 00146 return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]); 00147 } 00148 00149 static int num_bytes_in_utf8_sequence(unsigned char c) { 00150 if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { 00151 return 0; 00152 } else if ((c & 0x80) == 0) { /* 0xxxxxxx */ 00153 return 1; 00154 } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ 00155 return 2; 00156 } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ 00157 return 3; 00158 } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ 00159 return 4; 00160 } 00161 return 0; /* won't happen */ 00162 } 00163 00164 static int verify_utf8_sequence(const unsigned char *string, int *len) { 00165 unsigned int cp = 0; 00166 *len = num_bytes_in_utf8_sequence(string[0]); 00167 00168 if (*len == 1) { 00169 cp = string[0]; 00170 } else if (*len == 2 && IS_CONT(string[1])) { 00171 cp = string[0] & 0x1F; 00172 cp = (cp << 6) | (string[1] & 0x3F); 00173 } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { 00174 cp = ((unsigned char)string[0]) & 0xF; 00175 cp = (cp << 6) | (string[1] & 0x3F); 00176 cp = (cp << 6) | (string[2] & 0x3F); 00177 } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) { 00178 cp = string[0] & 0x7; 00179 cp = (cp << 6) | (string[1] & 0x3F); 00180 cp = (cp << 6) | (string[2] & 0x3F); 00181 cp = (cp << 6) | (string[3] & 0x3F); 00182 } else { 00183 return 0; 00184 } 00185 00186 /* overlong encodings */ 00187 if ((cp < 0x80 && *len > 1) || 00188 (cp < 0x800 && *len > 2) || 00189 (cp < 0x10000 && *len > 3)) { 00190 return 0; 00191 } 00192 00193 /* invalid unicode */ 00194 if (cp > 0x10FFFF) { 00195 return 0; 00196 } 00197 00198 /* surrogate halves */ 00199 if (cp >= 0xD800 && cp <= 0xDFFF) { 00200 return 0; 00201 } 00202 00203 return 1; 00204 } 00205 00206 static int is_valid_utf8(const char *string, size_t string_len) { 00207 int len = 0; 00208 const char *string_end = string + string_len; 00209 while (string < string_end) { 00210 if (!verify_utf8_sequence((const unsigned char*)string, &len)) { 00211 return 0; 00212 } 00213 string += len; 00214 } 00215 return 1; 00216 } 00217 00218 static int is_decimal(const char *string, size_t length) { 00219 if (length > 1 && string[0] == '0' && string[1] != '.') { 00220 return 0; 00221 } 00222 if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') { 00223 return 0; 00224 } 00225 while (length--) 00226 if (strchr("xX", string[length])) { 00227 return 0; 00228 } 00229 return 1; 00230 } 00231 00232 static char * read_file(const char * filename) { 00233 FILE *fp = fopen(filename, "r"); 00234 size_t file_size; 00235 long pos; 00236 char *file_contents; 00237 if (!fp) { 00238 return NULL; 00239 } 00240 fseek(fp, 0L, SEEK_END); 00241 pos = ftell(fp); 00242 if (pos < 0) { 00243 fclose(fp); 00244 return NULL; 00245 } 00246 file_size = pos; 00247 rewind(fp); 00248 file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1)); 00249 if (!file_contents) { 00250 fclose(fp); 00251 return NULL; 00252 } 00253 if (fread(file_contents, file_size, 1, fp) < 1) { 00254 if (ferror(fp)) { 00255 fclose(fp); 00256 parson_free(file_contents); 00257 return NULL; 00258 } 00259 } 00260 fclose(fp); 00261 file_contents[file_size] = '\0'; 00262 return file_contents; 00263 } 00264 00265 static void remove_comments(char *string, const char *start_token, const char *end_token) { 00266 int in_string = 0, escaped = 0; 00267 size_t i; 00268 char *ptr = NULL, current_char; 00269 size_t start_token_len = strlen(start_token); 00270 size_t end_token_len = strlen(end_token); 00271 if (start_token_len == 0 || end_token_len == 0) { 00272 return; 00273 } 00274 while ((current_char = *string) != '\0') { 00275 if (current_char == '\\' && !escaped) { 00276 escaped = 1; 00277 string++; 00278 continue; 00279 } else if (current_char == '\"' && !escaped) { 00280 in_string = !in_string; 00281 } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { 00282 for(i = 0; i < start_token_len; i++) { 00283 string[i] = ' '; 00284 } 00285 string = string + start_token_len; 00286 ptr = strstr(string, end_token); 00287 if (!ptr) { 00288 return; 00289 } 00290 for (i = 0; i < (ptr - string) + end_token_len; i++) { 00291 string[i] = ' '; 00292 } 00293 string = ptr + end_token_len - 1; 00294 } 00295 escaped = 0; 00296 string++; 00297 } 00298 } 00299 00300 /* JSON Object */ 00301 static JSON_Object * json_object_init(void) { 00302 JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); 00303 if (!new_obj) { 00304 return NULL; 00305 } 00306 new_obj->names = (char**)NULL; 00307 new_obj->values = (JSON_Value**)NULL; 00308 new_obj->capacity = 0; 00309 new_obj->count = 0; 00310 return new_obj; 00311 } 00312 00313 static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { 00314 size_t index = 0; 00315 if (object == NULL || name == NULL || value == NULL) { 00316 return JSONFailure; 00317 } 00318 if (json_object_get_value(object, name) != NULL) { 00319 return JSONFailure; 00320 } 00321 if (object->count >= object->capacity) { 00322 size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); 00323 if (new_capacity > OBJECT_MAX_CAPACITY) { 00324 return JSONFailure; 00325 } 00326 if (json_object_resize(object, new_capacity) == JSONFailure) { 00327 return JSONFailure; 00328 } 00329 } 00330 index = object->count; 00331 object->names[index] = parson_strdup(name); 00332 if (object->names[index] == NULL) { 00333 return JSONFailure; 00334 } 00335 object->values[index] = value; 00336 object->count++; 00337 return JSONSuccess; 00338 } 00339 00340 static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) { 00341 char **temp_names = NULL; 00342 JSON_Value **temp_values = NULL; 00343 00344 if ((object->names == NULL && object->values != NULL) || 00345 (object->names != NULL && object->values == NULL) || 00346 new_capacity == 0) { 00347 return JSONFailure; /* Shouldn't happen */ 00348 } 00349 00350 temp_names = (char**)parson_malloc(new_capacity * sizeof(char*)); 00351 if (temp_names == NULL) { 00352 return JSONFailure; 00353 } 00354 00355 temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); 00356 if (temp_values == NULL) { 00357 parson_free(temp_names); 00358 return JSONFailure; 00359 } 00360 00361 if (object->names != NULL && object->values != NULL && object->count > 0) { 00362 memcpy(temp_names, object->names, object->count * sizeof(char*)); 00363 memcpy(temp_values, object->values, object->count * sizeof(JSON_Value*)); 00364 } 00365 parson_free(object->names); 00366 parson_free(object->values); 00367 object->names = temp_names; 00368 object->values = temp_values; 00369 object->capacity = new_capacity; 00370 return JSONSuccess; 00371 } 00372 00373 static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) { 00374 size_t i, name_length; 00375 for (i = 0; i < json_object_get_count(object); i++) { 00376 name_length = strlen(object->names[i]); 00377 if (name_length != n) { 00378 continue; 00379 } 00380 if (strncmp(object->names[i], name, n) == 0) { 00381 return object->values[i]; 00382 } 00383 } 00384 return NULL; 00385 } 00386 00387 static void json_object_free(JSON_Object *object) { 00388 while(object->count--) { 00389 parson_free(object->names[object->count]); 00390 json_value_free(object->values[object->count]); 00391 } 00392 parson_free(object->names); 00393 parson_free(object->values); 00394 parson_free(object); 00395 } 00396 00397 /* JSON Array */ 00398 static JSON_Array * json_array_init(void) { 00399 JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array)); 00400 if (!new_array) { 00401 return NULL; 00402 } 00403 new_array->items = (JSON_Value**)NULL; 00404 new_array->capacity = 0; 00405 new_array->count = 0; 00406 return new_array; 00407 } 00408 00409 static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) { 00410 if (array->count >= array->capacity) { 00411 size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); 00412 if (new_capacity > ARRAY_MAX_CAPACITY) { 00413 return JSONFailure; 00414 } 00415 if (json_array_resize(array, new_capacity) == JSONFailure) { 00416 return JSONFailure; 00417 } 00418 } 00419 array->items[array->count] = value; 00420 array->count++; 00421 return JSONSuccess; 00422 } 00423 00424 static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) { 00425 JSON_Value **new_items = NULL; 00426 if (new_capacity == 0) { 00427 return JSONFailure; 00428 } 00429 new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); 00430 if (new_items == NULL) { 00431 return JSONFailure; 00432 } 00433 if (array->items != NULL && array->count > 0) { 00434 memcpy(new_items, array->items, array->count * sizeof(JSON_Value*)); 00435 } 00436 parson_free(array->items); 00437 array->items = new_items; 00438 array->capacity = new_capacity; 00439 return JSONSuccess; 00440 } 00441 00442 static void json_array_free(JSON_Array *array) { 00443 while (array->count--) 00444 json_value_free(array->items[array->count]); 00445 parson_free(array->items); 00446 parson_free(array); 00447 } 00448 00449 /* JSON Value */ 00450 static JSON_Value * json_value_init_string_no_copy(char *string) { 00451 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 00452 if (!new_value) { 00453 return NULL; 00454 } 00455 new_value->type = JSONString; 00456 new_value->value.string = string; 00457 return new_value; 00458 } 00459 00460 /* Parser */ 00461 static JSON_Status skip_quotes(const char **string) { 00462 if (**string != '\"') { 00463 return JSONFailure; 00464 } 00465 SKIP_CHAR(string); 00466 while (**string != '\"') { 00467 if (**string == '\0') { 00468 return JSONFailure; 00469 } else if (**string == '\\') { 00470 SKIP_CHAR(string); 00471 if (**string == '\0') { 00472 return JSONFailure; 00473 } 00474 } 00475 SKIP_CHAR(string); 00476 } 00477 SKIP_CHAR(string); 00478 return JSONSuccess; 00479 } 00480 00481 static int parse_utf_16(const char **unprocessed, char **processed) { 00482 unsigned int cp, lead, trail; 00483 char *processed_ptr = *processed; 00484 const char *unprocessed_ptr = *unprocessed; 00485 unprocessed_ptr++; /* skips u */ 00486 if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) { 00487 return JSONFailure; 00488 } 00489 if (cp < 0x80) { 00490 *processed_ptr = cp; /* 0xxxxxxx */ 00491 } else if (cp < 0x800) { 00492 *processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */ 00493 *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */ 00494 } else if (cp < 0xD800 || cp > 0xDFFF) { 00495 *processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */ 00496 *processed_ptr++ = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */ 00497 *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */ 00498 } else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */ 00499 lead = cp; 00500 unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */ 00501 if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */ 00502 !is_utf16_hex((const unsigned char*)unprocessed_ptr) || 00503 sscanf(unprocessed_ptr, "%4x", &trail) == EOF || 00504 trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ 00505 return JSONFailure; 00506 } 00507 cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000; 00508 *processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */ 00509 *processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */ 00510 *processed_ptr++ = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */ 00511 *processed_ptr = (((cp ) & 0x3F) | 0x80); /* 10xxxxxx */ 00512 } else { /* trail surrogate before lead surrogate */ 00513 return JSONFailure; 00514 } 00515 unprocessed_ptr += 3; 00516 *processed = processed_ptr; 00517 *unprocessed = unprocessed_ptr; 00518 return JSONSuccess; 00519 } 00520 00521 00522 /* Copies and processes passed string up to supplied length. 00523 Example: "\u006Corem ipsum" -> lorem ipsum */ 00524 static char* process_string(const char *input, size_t len) { 00525 const char *input_ptr = input; 00526 size_t initial_size = (len + 1) * sizeof(char); 00527 size_t final_size = 0; 00528 char *output = (char*)parson_malloc(initial_size); 00529 char *output_ptr = output; 00530 char *resized_output = NULL; 00531 while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) { 00532 if (*input_ptr == '\\') { 00533 input_ptr++; 00534 switch (*input_ptr) { 00535 case '\"': *output_ptr = '\"'; break; 00536 case '\\': *output_ptr = '\\'; break; 00537 case '/': *output_ptr = '/'; break; 00538 case 'b': *output_ptr = '\b'; break; 00539 case 'f': *output_ptr = '\f'; break; 00540 case 'n': *output_ptr = '\n'; break; 00541 case 'r': *output_ptr = '\r'; break; 00542 case 't': *output_ptr = '\t'; break; 00543 case 'u': 00544 if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) { 00545 goto error; 00546 } 00547 break; 00548 default: 00549 goto error; 00550 } 00551 } else if ((unsigned char)*input_ptr < 0x20) { 00552 goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */ 00553 } else { 00554 *output_ptr = *input_ptr; 00555 } 00556 output_ptr++; 00557 input_ptr++; 00558 } 00559 *output_ptr = '\0'; 00560 /* resize to new length */ 00561 final_size = (size_t)(output_ptr-output) + 1; 00562 /* todo: don't resize if final_size == initial_size */ 00563 resized_output = (char*)parson_malloc(final_size); 00564 if (resized_output == NULL) { 00565 goto error; 00566 } 00567 memcpy(resized_output, output, final_size); 00568 parson_free(output); 00569 return resized_output; 00570 error: 00571 parson_free(output); 00572 return NULL; 00573 } 00574 00575 /* Return processed contents of a string between quotes and 00576 skips passed argument to a matching quote. */ 00577 static char * get_quoted_string(const char **string) { 00578 const char *string_start = *string; 00579 size_t string_len = 0; 00580 JSON_Status status = skip_quotes(string); 00581 if (status != JSONSuccess) { 00582 return NULL; 00583 } 00584 string_len = *string - string_start - 2; /* length without quotes */ 00585 return process_string(string_start + 1, string_len); 00586 } 00587 00588 static JSON_Value * parse_value(const char **string, size_t nesting) { 00589 if (nesting > MAX_NESTING) { 00590 return NULL; 00591 } 00592 SKIP_WHITESPACES(string); 00593 switch (**string) { 00594 case '{': 00595 return parse_object_value(string, nesting + 1); 00596 case '[': 00597 return parse_array_value(string, nesting + 1); 00598 case '\"': 00599 return parse_string_value(string); 00600 case 'f': case 't': 00601 return parse_boolean_value(string); 00602 case '-': 00603 case '0': case '1': case '2': case '3': case '4': 00604 case '5': case '6': case '7': case '8': case '9': 00605 return parse_number_value(string); 00606 case 'n': 00607 return parse_null_value(string); 00608 default: 00609 return NULL; 00610 } 00611 } 00612 00613 static JSON_Value * parse_object_value(const char **string, size_t nesting) { 00614 JSON_Value *output_value = json_value_init_object(), *new_value = NULL; 00615 JSON_Object *output_object = json_value_get_object(output_value); 00616 char *new_key = NULL; 00617 if (output_value == NULL || **string != '{') { 00618 return NULL; 00619 } 00620 SKIP_CHAR(string); 00621 SKIP_WHITESPACES(string); 00622 if (**string == '}') { /* empty object */ 00623 SKIP_CHAR(string); 00624 return output_value; 00625 } 00626 while (**string != '\0') { 00627 new_key = get_quoted_string(string); 00628 SKIP_WHITESPACES(string); 00629 if (new_key == NULL || **string != ':') { 00630 json_value_free(output_value); 00631 return NULL; 00632 } 00633 SKIP_CHAR(string); 00634 new_value = parse_value(string, nesting); 00635 if (new_value == NULL) { 00636 parson_free(new_key); 00637 json_value_free(output_value); 00638 return NULL; 00639 } 00640 if(json_object_add(output_object, new_key, new_value) == JSONFailure) { 00641 parson_free(new_key); 00642 parson_free(new_value); 00643 json_value_free(output_value); 00644 return NULL; 00645 } 00646 parson_free(new_key); 00647 SKIP_WHITESPACES(string); 00648 if (**string != ',') { 00649 break; 00650 } 00651 SKIP_CHAR(string); 00652 SKIP_WHITESPACES(string); 00653 } 00654 SKIP_WHITESPACES(string); 00655 if (**string != '}' || /* Trim object after parsing is over */ 00656 json_object_resize(output_object, json_object_get_count(output_object)) == JSONFailure) { 00657 json_value_free(output_value); 00658 return NULL; 00659 } 00660 SKIP_CHAR(string); 00661 return output_value; 00662 } 00663 00664 static JSON_Value * parse_array_value(const char **string, size_t nesting) { 00665 JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL; 00666 JSON_Array *output_array = json_value_get_array(output_value); 00667 if (!output_value || **string != '[') { 00668 return NULL; 00669 } 00670 SKIP_CHAR(string); 00671 SKIP_WHITESPACES(string); 00672 if (**string == ']') { /* empty array */ 00673 SKIP_CHAR(string); 00674 return output_value; 00675 } 00676 while (**string != '\0') { 00677 new_array_value = parse_value(string, nesting); 00678 if (!new_array_value) { 00679 json_value_free(output_value); 00680 return NULL; 00681 } 00682 if (json_array_add(output_array, new_array_value) == JSONFailure) { 00683 parson_free(new_array_value); 00684 json_value_free(output_value); 00685 return NULL; 00686 } 00687 SKIP_WHITESPACES(string); 00688 if (**string != ',') { 00689 break; 00690 } 00691 SKIP_CHAR(string); 00692 SKIP_WHITESPACES(string); 00693 } 00694 SKIP_WHITESPACES(string); 00695 if (**string != ']' || /* Trim array after parsing is over */ 00696 json_array_resize(output_array, json_array_get_count(output_array)) == JSONFailure) { 00697 json_value_free(output_value); 00698 return NULL; 00699 } 00700 SKIP_CHAR(string); 00701 return output_value; 00702 } 00703 00704 static JSON_Value * parse_string_value(const char **string) { 00705 JSON_Value *value = NULL; 00706 char *new_string = get_quoted_string(string); 00707 if (new_string == NULL) { 00708 return NULL; 00709 } 00710 value = json_value_init_string_no_copy(new_string); 00711 if (value == NULL) { 00712 parson_free(new_string); 00713 return NULL; 00714 } 00715 return value; 00716 } 00717 00718 static JSON_Value * parse_boolean_value(const char **string) { 00719 size_t true_token_size = SIZEOF_TOKEN("true"); 00720 size_t false_token_size = SIZEOF_TOKEN("false"); 00721 if (strncmp("true", *string, true_token_size) == 0) { 00722 *string += true_token_size; 00723 return json_value_init_boolean(1); 00724 } else if (strncmp("false", *string, false_token_size) == 0) { 00725 *string += false_token_size; 00726 return json_value_init_boolean(0); 00727 } 00728 return NULL; 00729 } 00730 00731 static JSON_Value * parse_number_value(const char **string) { 00732 char *end; 00733 double number = strtod(*string, &end); 00734 JSON_Value *output_value; 00735 if (is_decimal(*string, end - *string)) { 00736 *string = end; 00737 output_value = json_value_init_number(number); 00738 } else { 00739 output_value = NULL; 00740 } 00741 return output_value; 00742 } 00743 00744 static JSON_Value * parse_null_value(const char **string) { 00745 size_t token_size = SIZEOF_TOKEN("null"); 00746 if (strncmp("null", *string, token_size) == 0) { 00747 *string += token_size; 00748 return json_value_init_null(); 00749 } 00750 return NULL; 00751 } 00752 00753 /* Serialization */ 00754 #define APPEND_STRING(str) do { written = append_string(buf, (str));\ 00755 if (written < 0) { return -1; }\ 00756 if (buf != NULL) { buf += written; }\ 00757 written_total += written; } while(0) 00758 00759 #define APPEND_INDENT(level) do { written = append_indent(buf, (level));\ 00760 if (written < 0) { return -1; }\ 00761 if (buf != NULL) { buf += written; }\ 00762 written_total += written; } while(0) 00763 00764 static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf) 00765 { 00766 const char *key = NULL, *string = NULL; 00767 JSON_Value *temp_value = NULL; 00768 JSON_Array *array = NULL; 00769 JSON_Object *object = NULL; 00770 size_t i = 0, count = 0; 00771 double num = 0.0; 00772 int written = -1, written_total = 0; 00773 00774 switch (json_value_get_type(value)) { 00775 case JSONArray: 00776 array = json_value_get_array(value); 00777 count = json_array_get_count(array); 00778 APPEND_STRING("["); 00779 if (count > 0 && is_pretty) { 00780 APPEND_STRING("\n"); 00781 } 00782 for (i = 0; i < count; i++) { 00783 if (is_pretty) { 00784 APPEND_INDENT(level+1); 00785 } 00786 temp_value = json_array_get_value(array, i); 00787 written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); 00788 if (written < 0) { 00789 return -1; 00790 } 00791 if (buf != NULL) { 00792 buf += written; 00793 } 00794 written_total += written; 00795 if (i < (count - 1)) { 00796 APPEND_STRING(","); 00797 } 00798 if (is_pretty) { 00799 APPEND_STRING("\n"); 00800 } 00801 } 00802 if (count > 0 && is_pretty) { 00803 APPEND_INDENT(level); 00804 } 00805 APPEND_STRING("]"); 00806 return written_total; 00807 case JSONObject: 00808 object = json_value_get_object(value); 00809 count = json_object_get_count(object); 00810 APPEND_STRING("{"); 00811 if (count > 0 && is_pretty) { 00812 APPEND_STRING("\n"); 00813 } 00814 for (i = 0; i < count; i++) { 00815 key = json_object_get_name(object, i); 00816 if (is_pretty) { 00817 APPEND_INDENT(level+1); 00818 } 00819 written = json_serialize_string(key, buf); 00820 if (written < 0) { 00821 return -1; 00822 } 00823 if (buf != NULL) { 00824 buf += written; 00825 } 00826 written_total += written; 00827 APPEND_STRING(":"); 00828 if (is_pretty) { 00829 APPEND_STRING(" "); 00830 } 00831 temp_value = json_object_get_value(object, key); 00832 written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); 00833 if (written < 0) { 00834 return -1; 00835 } 00836 if (buf != NULL) { 00837 buf += written; 00838 } 00839 written_total += written; 00840 if (i < (count - 1)) { 00841 APPEND_STRING(","); 00842 } 00843 if (is_pretty) { 00844 APPEND_STRING("\n"); 00845 } 00846 } 00847 if (count > 0 && is_pretty) { 00848 APPEND_INDENT(level); 00849 } 00850 APPEND_STRING("}"); 00851 return written_total; 00852 case JSONString: 00853 string = json_value_get_string(value); 00854 written = json_serialize_string(string, buf); 00855 if (written < 0) { 00856 return -1; 00857 } 00858 if (buf != NULL) { 00859 buf += written; 00860 } 00861 written_total += written; 00862 return written_total; 00863 case JSONBoolean: 00864 if (json_value_get_boolean(value)) { 00865 APPEND_STRING("true"); 00866 } else { 00867 APPEND_STRING("false"); 00868 } 00869 return written_total; 00870 case JSONNumber: 00871 num = json_value_get_number(value); 00872 if (buf != NULL) { 00873 num_buf = buf; 00874 } 00875 if (num == ((double)(int)num)) { /* check if num is integer */ 00876 written = sprintf(num_buf, "%d", (int)num); 00877 } else { 00878 written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num); 00879 } 00880 if (written < 0) { 00881 return -1; 00882 } 00883 if (buf != NULL) { 00884 buf += written; 00885 } 00886 written_total += written; 00887 return written_total; 00888 case JSONNull: 00889 APPEND_STRING("null"); 00890 return written_total; 00891 case JSONError: 00892 return -1; 00893 default: 00894 return -1; 00895 } 00896 } 00897 00898 static int json_serialize_string(const char *string, char *buf) { 00899 size_t i = 0, len = strlen(string); 00900 char c = '\0'; 00901 int written = -1, written_total = 0; 00902 APPEND_STRING("\""); 00903 for (i = 0; i < len; i++) { 00904 c = string[i]; 00905 switch (c) { 00906 case '\"': APPEND_STRING("\\\""); break; 00907 case '\\': APPEND_STRING("\\\\"); break; 00908 case '/': APPEND_STRING("\\/"); break; /* to make json embeddable in xml\/html */ 00909 case '\b': APPEND_STRING("\\b"); break; 00910 case '\f': APPEND_STRING("\\f"); break; 00911 case '\n': APPEND_STRING("\\n"); break; 00912 case '\r': APPEND_STRING("\\r"); break; 00913 case '\t': APPEND_STRING("\\t"); break; 00914 default: 00915 if (buf != NULL) { 00916 buf[0] = c; 00917 buf += 1; 00918 } 00919 written_total += 1; 00920 break; 00921 } 00922 } 00923 APPEND_STRING("\""); 00924 return written_total; 00925 } 00926 00927 static int append_indent(char *buf, int level) { 00928 int i; 00929 int written = -1, written_total = 0; 00930 for (i = 0; i < level; i++) { 00931 APPEND_STRING(" "); 00932 } 00933 return written_total; 00934 } 00935 00936 static int append_string(char *buf, const char *string) { 00937 if (buf == NULL) { 00938 return (int)strlen(string); 00939 } 00940 return sprintf(buf, "%s", string); 00941 } 00942 00943 #undef APPEND_STRING 00944 #undef APPEND_INDENT 00945 00946 /* Parser API */ 00947 JSON_Value * json_parse_file(const char *filename) { 00948 char *file_contents = read_file(filename); 00949 JSON_Value *output_value = NULL; 00950 if (file_contents == NULL) { 00951 return NULL; 00952 } 00953 output_value = json_parse_string(file_contents); 00954 parson_free(file_contents); 00955 return output_value; 00956 } 00957 00958 JSON_Value * json_parse_file_with_comments(const char *filename) { 00959 char *file_contents = read_file(filename); 00960 JSON_Value *output_value = NULL; 00961 if (file_contents == NULL) { 00962 return NULL; 00963 } 00964 output_value = json_parse_string_with_comments(file_contents); 00965 parson_free(file_contents); 00966 return output_value; 00967 } 00968 00969 JSON_Value * json_parse_string(const char *string) { 00970 if (string == NULL) { 00971 return NULL; 00972 } 00973 return parse_value((const char**)&string, 0); 00974 } 00975 00976 JSON_Value * json_parse_string_with_comments(const char *string) { 00977 JSON_Value *result = NULL; 00978 char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; 00979 string_mutable_copy = parson_strdup(string); 00980 if (string_mutable_copy == NULL) { 00981 return NULL; 00982 } 00983 remove_comments(string_mutable_copy, "/*", "*/"); 00984 remove_comments(string_mutable_copy, "//", "\n"); 00985 string_mutable_copy_ptr = string_mutable_copy; 00986 result = parse_value((const char**)&string_mutable_copy_ptr, 0); 00987 parson_free(string_mutable_copy); 00988 return result; 00989 } 00990 00991 /* JSON Object API */ 00992 00993 JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { 00994 if (object == NULL || name == NULL) { 00995 return NULL; 00996 } 00997 return json_object_nget_value(object, name, strlen(name)); 00998 } 00999 01000 const char * json_object_get_string(const JSON_Object *object, const char *name) { 01001 return json_value_get_string(json_object_get_value(object, name)); 01002 } 01003 01004 double json_object_get_number(const JSON_Object *object, const char *name) { 01005 return json_value_get_number(json_object_get_value(object, name)); 01006 } 01007 01008 JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) { 01009 return json_value_get_object(json_object_get_value(object, name)); 01010 } 01011 01012 JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) { 01013 return json_value_get_array(json_object_get_value(object, name)); 01014 } 01015 01016 int json_object_get_boolean(const JSON_Object *object, const char *name) { 01017 return json_value_get_boolean(json_object_get_value(object, name)); 01018 } 01019 01020 JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) { 01021 const char *dot_position = strchr(name, '.'); 01022 if (!dot_position) { 01023 return json_object_get_value(object, name); 01024 } 01025 object = json_value_get_object(json_object_nget_value(object, name, dot_position - name)); 01026 return json_object_dotget_value(object, dot_position + 1); 01027 } 01028 01029 const char * json_object_dotget_string(const JSON_Object *object, const char *name) { 01030 return json_value_get_string(json_object_dotget_value(object, name)); 01031 } 01032 01033 double json_object_dotget_number(const JSON_Object *object, const char *name) { 01034 return json_value_get_number(json_object_dotget_value(object, name)); 01035 } 01036 01037 JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) { 01038 return json_value_get_object(json_object_dotget_value(object, name)); 01039 } 01040 01041 JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) { 01042 return json_value_get_array(json_object_dotget_value(object, name)); 01043 } 01044 01045 int json_object_dotget_boolean(const JSON_Object *object, const char *name) { 01046 return json_value_get_boolean(json_object_dotget_value(object, name)); 01047 } 01048 01049 size_t json_object_get_count(const JSON_Object *object) { 01050 return object ? object->count : 0; 01051 } 01052 01053 const char * json_object_get_name(const JSON_Object *object, size_t index) { 01054 if (object == NULL || index >= json_object_get_count(object)) { 01055 return NULL; 01056 } 01057 return object->names[index]; 01058 } 01059 01060 JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) { 01061 if (object == NULL || index >= json_object_get_count(object)) { 01062 return NULL; 01063 } 01064 return object->values[index]; 01065 } 01066 01067 int json_object_has_value (const JSON_Object *object, const char *name) { 01068 return json_object_get_value(object, name) != NULL; 01069 } 01070 01071 int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { 01072 JSON_Value *val = json_object_get_value(object, name); 01073 return val != NULL && json_value_get_type(val) == type; 01074 } 01075 01076 int json_object_dothas_value (const JSON_Object *object, const char *name) { 01077 return json_object_dotget_value(object, name) != NULL; 01078 } 01079 01080 int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { 01081 JSON_Value *val = json_object_dotget_value(object, name); 01082 return val != NULL && json_value_get_type(val) == type; 01083 } 01084 01085 /* JSON Array API */ 01086 JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) { 01087 if (array == NULL || index >= json_array_get_count(array)) { 01088 return NULL; 01089 } 01090 return array->items[index]; 01091 } 01092 01093 const char * json_array_get_string(const JSON_Array *array, size_t index) { 01094 return json_value_get_string(json_array_get_value(array, index)); 01095 } 01096 01097 double json_array_get_number(const JSON_Array *array, size_t index) { 01098 return json_value_get_number(json_array_get_value(array, index)); 01099 } 01100 01101 JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) { 01102 return json_value_get_object(json_array_get_value(array, index)); 01103 } 01104 01105 JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) { 01106 return json_value_get_array(json_array_get_value(array, index)); 01107 } 01108 01109 int json_array_get_boolean(const JSON_Array *array, size_t index) { 01110 return json_value_get_boolean(json_array_get_value(array, index)); 01111 } 01112 01113 size_t json_array_get_count(const JSON_Array *array) { 01114 return array ? array->count : 0; 01115 } 01116 01117 /* JSON Value API */ 01118 JSON_Value_Type json_value_get_type(const JSON_Value *value) { 01119 return value ? value->type : JSONError; 01120 } 01121 01122 JSON_Object * json_value_get_object(const JSON_Value *value) { 01123 return json_value_get_type(value) == JSONObject ? value->value.object : NULL; 01124 } 01125 01126 JSON_Array * json_value_get_array(const JSON_Value *value) { 01127 return json_value_get_type(value) == JSONArray ? value->value.array : NULL; 01128 } 01129 01130 const char * json_value_get_string(const JSON_Value *value) { 01131 return json_value_get_type(value) == JSONString ? value->value.string : NULL; 01132 } 01133 01134 double json_value_get_number(const JSON_Value *value) { 01135 return json_value_get_type(value) == JSONNumber ? value->value.number : 0; 01136 } 01137 01138 int json_value_get_boolean(const JSON_Value *value) { 01139 return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1; 01140 } 01141 01142 void json_value_free(JSON_Value *value) { 01143 switch (json_value_get_type(value)) { 01144 case JSONObject: 01145 json_object_free(value->value.object); 01146 break; 01147 case JSONString: 01148 if (value->value.string) { 01149 parson_free(value->value.string); 01150 } 01151 break; 01152 case JSONArray: 01153 json_array_free(value->value.array); 01154 break; 01155 default: 01156 break; 01157 } 01158 parson_free(value); 01159 } 01160 01161 JSON_Value * json_value_init_object(void) { 01162 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 01163 if (!new_value) { 01164 return NULL; 01165 } 01166 new_value->type = JSONObject; 01167 new_value->value.object = json_object_init(); 01168 if (!new_value->value.object) { 01169 parson_free(new_value); 01170 return NULL; 01171 } 01172 return new_value; 01173 } 01174 01175 JSON_Value * json_value_init_array(void) { 01176 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 01177 if (!new_value) { 01178 return NULL; 01179 } 01180 new_value->type = JSONArray; 01181 new_value->value.array = json_array_init(); 01182 if (!new_value->value.array) { 01183 parson_free(new_value); 01184 return NULL; 01185 } 01186 return new_value; 01187 } 01188 01189 JSON_Value * json_value_init_string(const char *string) { 01190 char *copy = NULL; 01191 JSON_Value *value; 01192 size_t string_len = 0; 01193 if (string == NULL) { 01194 return NULL; 01195 } 01196 string_len = strlen(string); 01197 if (!is_valid_utf8(string, string_len)) { 01198 return NULL; 01199 } 01200 copy = parson_strndup(string, string_len); 01201 if (copy == NULL) { 01202 return NULL; 01203 } 01204 value = json_value_init_string_no_copy(copy); 01205 if (value == NULL) { 01206 parson_free(copy); 01207 } 01208 return value; 01209 } 01210 01211 JSON_Value * json_value_init_number(double number) { 01212 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 01213 if (!new_value) { 01214 return NULL; 01215 } 01216 new_value->type = JSONNumber; 01217 new_value->value.number = number; 01218 return new_value; 01219 } 01220 01221 JSON_Value * json_value_init_boolean(int boolean) { 01222 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 01223 if (!new_value) { 01224 return NULL; 01225 } 01226 new_value->type = JSONBoolean; 01227 new_value->value.boolean = boolean ? 1 : 0; 01228 return new_value; 01229 } 01230 01231 JSON_Value * json_value_init_null(void) { 01232 JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 01233 if (!new_value) { 01234 return NULL; 01235 } 01236 new_value->type = JSONNull; 01237 return new_value; 01238 } 01239 01240 JSON_Value * json_value_deep_copy(const JSON_Value *value) { 01241 size_t i = 0; 01242 JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL; 01243 const char *temp_string = NULL, *temp_key = NULL; 01244 char *temp_string_copy = NULL; 01245 JSON_Array *temp_array = NULL, *temp_array_copy = NULL; 01246 JSON_Object *temp_object = NULL, *temp_object_copy = NULL; 01247 01248 switch (json_value_get_type(value)) { 01249 case JSONArray: 01250 temp_array = json_value_get_array(value); 01251 return_value = json_value_init_array(); 01252 if (return_value == NULL) { 01253 return NULL; 01254 } 01255 temp_array_copy = json_value_get_array(return_value); 01256 for (i = 0; i < json_array_get_count(temp_array); i++) { 01257 temp_value = json_array_get_value(temp_array, i); 01258 temp_value_copy = json_value_deep_copy(temp_value); 01259 if (temp_value_copy == NULL) { 01260 json_value_free(return_value); 01261 return NULL; 01262 } 01263 if (json_array_add(temp_array_copy, temp_value_copy) == JSONFailure) { 01264 json_value_free(return_value); 01265 json_value_free(temp_value_copy); 01266 return NULL; 01267 } 01268 } 01269 return return_value; 01270 case JSONObject: 01271 temp_object = json_value_get_object(value); 01272 return_value = json_value_init_object(); 01273 if (return_value == NULL) { 01274 return NULL; 01275 } 01276 temp_object_copy = json_value_get_object(return_value); 01277 for (i = 0; i < json_object_get_count(temp_object); i++) { 01278 temp_key = json_object_get_name(temp_object, i); 01279 temp_value = json_object_get_value(temp_object, temp_key); 01280 temp_value_copy = json_value_deep_copy(temp_value); 01281 if (temp_value_copy == NULL) { 01282 json_value_free(return_value); 01283 return NULL; 01284 } 01285 if (json_object_add(temp_object_copy, temp_key, temp_value_copy) == JSONFailure) { 01286 json_value_free(return_value); 01287 json_value_free(temp_value_copy); 01288 return NULL; 01289 } 01290 } 01291 return return_value; 01292 case JSONBoolean: 01293 return json_value_init_boolean(json_value_get_boolean(value)); 01294 case JSONNumber: 01295 return json_value_init_number(json_value_get_number(value)); 01296 case JSONString: 01297 temp_string = json_value_get_string(value); 01298 temp_string_copy = parson_strdup(temp_string); 01299 if (temp_string_copy == NULL) { 01300 return NULL; 01301 } 01302 return_value = json_value_init_string_no_copy(temp_string_copy); 01303 if (return_value == NULL) { 01304 parson_free(temp_string_copy); 01305 } 01306 return return_value; 01307 case JSONNull: 01308 return json_value_init_null(); 01309 case JSONError: 01310 return NULL; 01311 default: 01312 return NULL; 01313 } 01314 } 01315 01316 size_t json_serialization_size(const JSON_Value *value) { 01317 char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ 01318 int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf); 01319 return res < 0 ? 0 : (size_t)(res + 1); 01320 } 01321 01322 JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { 01323 int written = -1; 01324 size_t needed_size_in_bytes = json_serialization_size(value); 01325 if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { 01326 return JSONFailure; 01327 } 01328 written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL); 01329 if (written < 0) { 01330 return JSONFailure; 01331 } 01332 return JSONSuccess; 01333 } 01334 01335 JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) { 01336 JSON_Status return_code = JSONSuccess; 01337 FILE *fp = NULL; 01338 char *serialized_string = json_serialize_to_string(value); 01339 if (serialized_string == NULL) { 01340 return JSONFailure; 01341 } 01342 fp = fopen (filename, "w"); 01343 if (fp == NULL) { 01344 json_free_serialized_string(serialized_string); 01345 return JSONFailure; 01346 } 01347 if (fputs(serialized_string, fp) == EOF) { 01348 return_code = JSONFailure; 01349 } 01350 if (fclose(fp) == EOF) { 01351 return_code = JSONFailure; 01352 } 01353 json_free_serialized_string(serialized_string); 01354 return return_code; 01355 } 01356 01357 char * json_serialize_to_string(const JSON_Value *value) { 01358 JSON_Status serialization_result = JSONFailure; 01359 size_t buf_size_bytes = json_serialization_size(value); 01360 char *buf = NULL; 01361 if (buf_size_bytes == 0) { 01362 return NULL; 01363 } 01364 buf = (char*)parson_malloc(buf_size_bytes); 01365 if (buf == NULL) { 01366 return NULL; 01367 } 01368 serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); 01369 if (serialization_result == JSONFailure) { 01370 json_free_serialized_string(buf); 01371 return NULL; 01372 } 01373 return buf; 01374 } 01375 01376 size_t json_serialization_size_pretty(const JSON_Value *value) { 01377 char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ 01378 int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf); 01379 return res < 0 ? 0 : (size_t)(res + 1); 01380 } 01381 01382 JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { 01383 int written = -1; 01384 size_t needed_size_in_bytes = json_serialization_size_pretty(value); 01385 if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { 01386 return JSONFailure; 01387 } 01388 written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL); 01389 if (written < 0) { 01390 return JSONFailure; 01391 } 01392 return JSONSuccess; 01393 } 01394 01395 JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) { 01396 JSON_Status return_code = JSONSuccess; 01397 FILE *fp = NULL; 01398 char *serialized_string = json_serialize_to_string_pretty(value); 01399 if (serialized_string == NULL) { 01400 return JSONFailure; 01401 } 01402 fp = fopen (filename, "w"); 01403 if (fp == NULL) { 01404 json_free_serialized_string(serialized_string); 01405 return JSONFailure; 01406 } 01407 if (fputs(serialized_string, fp) == EOF) { 01408 return_code = JSONFailure; 01409 } 01410 if (fclose(fp) == EOF) { 01411 return_code = JSONFailure; 01412 } 01413 json_free_serialized_string(serialized_string); 01414 return return_code; 01415 } 01416 01417 char * json_serialize_to_string_pretty(const JSON_Value *value) { 01418 JSON_Status serialization_result = JSONFailure; 01419 size_t buf_size_bytes = json_serialization_size_pretty(value); 01420 char *buf = NULL; 01421 if (buf_size_bytes == 0) { 01422 return NULL; 01423 } 01424 buf = (char*)parson_malloc(buf_size_bytes); 01425 if (buf == NULL) { 01426 return NULL; 01427 } 01428 serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); 01429 if (serialization_result == JSONFailure) { 01430 json_free_serialized_string(buf); 01431 return NULL; 01432 } 01433 return buf; 01434 } 01435 01436 void json_free_serialized_string(char *string) { 01437 parson_free(string); 01438 } 01439 01440 JSON_Status json_array_remove(JSON_Array *array, size_t ix) { 01441 JSON_Value *temp_value = NULL; 01442 size_t last_element_ix = 0; 01443 if (array == NULL || ix >= json_array_get_count(array)) { 01444 return JSONFailure; 01445 } 01446 last_element_ix = json_array_get_count(array) - 1; 01447 json_value_free(json_array_get_value(array, ix)); 01448 if (ix != last_element_ix) { /* Replace value with one from the end of array */ 01449 temp_value = json_array_get_value(array, last_element_ix); 01450 if (temp_value == NULL) { 01451 return JSONFailure; 01452 } 01453 array->items[ix] = temp_value; 01454 } 01455 array->count -= 1; 01456 return JSONSuccess; 01457 } 01458 01459 JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value) { 01460 if (array == NULL || value == NULL || ix >= json_array_get_count(array)) { 01461 return JSONFailure; 01462 } 01463 json_value_free(json_array_get_value(array, ix)); 01464 array->items[ix] = value; 01465 return JSONSuccess; 01466 } 01467 01468 JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) { 01469 JSON_Value *value = json_value_init_string(string); 01470 if (value == NULL) { 01471 return JSONFailure; 01472 } 01473 if (json_array_replace_value(array, i, value) == JSONFailure) { 01474 json_value_free(value); 01475 return JSONFailure; 01476 } 01477 return JSONSuccess; 01478 } 01479 01480 JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { 01481 JSON_Value *value = json_value_init_number(number); 01482 if (value == NULL) { 01483 return JSONFailure; 01484 } 01485 if (json_array_replace_value(array, i, value) == JSONFailure) { 01486 json_value_free(value); 01487 return JSONFailure; 01488 } 01489 return JSONSuccess; 01490 } 01491 01492 JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) { 01493 JSON_Value *value = json_value_init_boolean(boolean); 01494 if (value == NULL) { 01495 return JSONFailure; 01496 } 01497 if (json_array_replace_value(array, i, value) == JSONFailure) { 01498 json_value_free(value); 01499 return JSONFailure; 01500 } 01501 return JSONSuccess; 01502 } 01503 01504 JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { 01505 JSON_Value *value = json_value_init_null(); 01506 if (value == NULL) { 01507 return JSONFailure; 01508 } 01509 if (json_array_replace_value(array, i, value) == JSONFailure) { 01510 json_value_free(value); 01511 return JSONFailure; 01512 } 01513 return JSONSuccess; 01514 } 01515 01516 JSON_Status json_array_clear(JSON_Array *array) { 01517 size_t i = 0; 01518 if (array == NULL) { 01519 return JSONFailure; 01520 } 01521 for (i = 0; i < json_array_get_count(array); i++) { 01522 json_value_free(json_array_get_value(array, i)); 01523 } 01524 array->count = 0; 01525 return JSONSuccess; 01526 } 01527 01528 JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) { 01529 if (array == NULL || value == NULL) { 01530 return JSONFailure; 01531 } 01532 return json_array_add(array, value); 01533 } 01534 01535 JSON_Status json_array_append_string(JSON_Array *array, const char *string) { 01536 JSON_Value *value = json_value_init_string(string); 01537 if (value == NULL) { 01538 return JSONFailure; 01539 } 01540 if (json_array_append_value(array, value) == JSONFailure) { 01541 json_value_free(value); 01542 return JSONFailure; 01543 } 01544 return JSONSuccess; 01545 } 01546 01547 JSON_Status json_array_append_number(JSON_Array *array, double number) { 01548 JSON_Value *value = json_value_init_number(number); 01549 if (value == NULL) { 01550 return JSONFailure; 01551 } 01552 if (json_array_append_value(array, value) == JSONFailure) { 01553 json_value_free(value); 01554 return JSONFailure; 01555 } 01556 return JSONSuccess; 01557 } 01558 01559 JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { 01560 JSON_Value *value = json_value_init_boolean(boolean); 01561 if (value == NULL) { 01562 return JSONFailure; 01563 } 01564 if (json_array_append_value(array, value) == JSONFailure) { 01565 json_value_free(value); 01566 return JSONFailure; 01567 } 01568 return JSONSuccess; 01569 } 01570 01571 JSON_Status json_array_append_null(JSON_Array *array) { 01572 JSON_Value *value = json_value_init_null(); 01573 if (value == NULL) { 01574 return JSONFailure; 01575 } 01576 if (json_array_append_value(array, value) == JSONFailure) { 01577 json_value_free(value); 01578 return JSONFailure; 01579 } 01580 return JSONSuccess; 01581 } 01582 01583 JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) { 01584 size_t i = 0; 01585 JSON_Value *old_value; 01586 if (object == NULL || name == NULL || value == NULL) { 01587 return JSONFailure; 01588 } 01589 old_value = json_object_get_value(object, name); 01590 if (old_value != NULL) { /* free and overwrite old value */ 01591 json_value_free(old_value); 01592 for (i = 0; i < json_object_get_count(object); i++) { 01593 if (strcmp(object->names[i], name) == 0) { 01594 object->values[i] = value; 01595 return JSONSuccess; 01596 } 01597 } 01598 } 01599 /* add new key value pair */ 01600 return json_object_add(object, name, value); 01601 } 01602 01603 JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) { 01604 return json_object_set_value(object, name, json_value_init_string(string)); 01605 } 01606 01607 JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number) { 01608 return json_object_set_value(object, name, json_value_init_number(number)); 01609 } 01610 01611 JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) { 01612 return json_object_set_value(object, name, json_value_init_boolean(boolean)); 01613 } 01614 01615 JSON_Status json_object_set_null(JSON_Object *object, const char *name) { 01616 return json_object_set_value(object, name, json_value_init_null()); 01617 } 01618 01619 JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) { 01620 const char *dot_pos = NULL; 01621 char *current_name = NULL; 01622 JSON_Object *temp_obj = NULL; 01623 JSON_Value *new_value = NULL; 01624 if (value == NULL || name == NULL || value == NULL) { 01625 return JSONFailure; 01626 } 01627 dot_pos = strchr(name, '.'); 01628 if (dot_pos == NULL) { 01629 return json_object_set_value(object, name, value); 01630 } else { 01631 current_name = parson_strndup(name, dot_pos - name); 01632 temp_obj = json_object_get_object(object, current_name); 01633 if (temp_obj == NULL) { 01634 new_value = json_value_init_object(); 01635 if (new_value == NULL) { 01636 parson_free(current_name); 01637 return JSONFailure; 01638 } 01639 if (json_object_add(object, current_name, new_value) == JSONFailure) { 01640 json_value_free(new_value); 01641 parson_free(current_name); 01642 return JSONFailure; 01643 } 01644 temp_obj = json_object_get_object(object, current_name); 01645 } 01646 parson_free(current_name); 01647 return json_object_dotset_value(temp_obj, dot_pos + 1, value); 01648 } 01649 } 01650 01651 JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { 01652 JSON_Value *value = json_value_init_string(string); 01653 if (value == NULL) { 01654 return JSONFailure; 01655 } 01656 if (json_object_dotset_value(object, name, value) == JSONFailure) { 01657 json_value_free(value); 01658 return JSONFailure; 01659 } 01660 return JSONSuccess; 01661 } 01662 01663 JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) { 01664 JSON_Value *value = json_value_init_number(number); 01665 if (value == NULL) { 01666 return JSONFailure; 01667 } 01668 if (json_object_dotset_value(object, name, value) == JSONFailure) { 01669 json_value_free(value); 01670 return JSONFailure; 01671 } 01672 return JSONSuccess; 01673 } 01674 01675 JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) { 01676 JSON_Value *value = json_value_init_boolean(boolean); 01677 if (value == NULL) { 01678 return JSONFailure; 01679 } 01680 if (json_object_dotset_value(object, name, value) == JSONFailure) { 01681 json_value_free(value); 01682 return JSONFailure; 01683 } 01684 return JSONSuccess; 01685 } 01686 01687 JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { 01688 JSON_Value *value = json_value_init_null(); 01689 if (value == NULL) { 01690 return JSONFailure; 01691 } 01692 if (json_object_dotset_value(object, name, value) == JSONFailure) { 01693 json_value_free(value); 01694 return JSONFailure; 01695 } 01696 return JSONSuccess; 01697 } 01698 01699 JSON_Status json_object_remove(JSON_Object *object, const char *name) { 01700 size_t i = 0, last_item_index = 0; 01701 if (object == NULL || json_object_get_value(object, name) == NULL) { 01702 return JSONFailure; 01703 } 01704 last_item_index = json_object_get_count(object) - 1; 01705 for (i = 0; i < json_object_get_count(object); i++) { 01706 if (strcmp(object->names[i], name) == 0) { 01707 parson_free(object->names[i]); 01708 json_value_free(object->values[i]); 01709 if (i != last_item_index) { /* Replace key value pair with one from the end */ 01710 object->names[i] = object->names[last_item_index]; 01711 object->values[i] = object->values[last_item_index]; 01712 } 01713 object->count -= 1; 01714 return JSONSuccess; 01715 } 01716 } 01717 return JSONFailure; /* No execution path should end here */ 01718 } 01719 01720 JSON_Status json_object_dotremove(JSON_Object *object, const char *name) { 01721 const char *dot_pos = strchr(name, '.'); 01722 char *current_name = NULL; 01723 JSON_Object *temp_obj = NULL; 01724 if (dot_pos == NULL) { 01725 return json_object_remove(object, name); 01726 } else { 01727 current_name = parson_strndup(name, dot_pos - name); 01728 temp_obj = json_object_get_object(object, current_name); 01729 if (temp_obj == NULL) { 01730 parson_free(current_name); 01731 return JSONFailure; 01732 } 01733 parson_free(current_name); 01734 return json_object_dotremove(temp_obj, dot_pos + 1); 01735 } 01736 } 01737 01738 JSON_Status json_object_clear(JSON_Object *object) { 01739 size_t i = 0; 01740 if (object == NULL) { 01741 return JSONFailure; 01742 } 01743 for (i = 0; i < json_object_get_count(object); i++) { 01744 parson_free(object->names[i]); 01745 json_value_free(object->values[i]); 01746 } 01747 object->count = 0; 01748 return JSONSuccess; 01749 } 01750 01751 JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) { 01752 JSON_Value *temp_schema_value = NULL, *temp_value = NULL; 01753 JSON_Array *schema_array = NULL, *value_array = NULL; 01754 JSON_Object *schema_object = NULL, *value_object = NULL; 01755 JSON_Value_Type schema_type = JSONError, value_type = JSONError; 01756 const char *key = NULL; 01757 size_t i = 0, count = 0; 01758 if (schema == NULL || value == NULL) { 01759 return JSONFailure; 01760 } 01761 schema_type = json_value_get_type(schema); 01762 value_type = json_value_get_type(value); 01763 if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */ 01764 return JSONFailure; 01765 } 01766 switch (schema_type) { 01767 case JSONArray: 01768 schema_array = json_value_get_array(schema); 01769 value_array = json_value_get_array(value); 01770 count = json_array_get_count(schema_array); 01771 if (count == 0) { 01772 return JSONSuccess; /* Empty array allows all types */ 01773 } 01774 /* Get first value from array, rest is ignored */ 01775 temp_schema_value = json_array_get_value(schema_array, 0); 01776 for (i = 0; i < json_array_get_count(value_array); i++) { 01777 temp_value = json_array_get_value(value_array, i); 01778 if (json_validate(temp_schema_value, temp_value) == JSONFailure) { 01779 return JSONFailure; 01780 } 01781 } 01782 return JSONSuccess; 01783 case JSONObject: 01784 schema_object = json_value_get_object(schema); 01785 value_object = json_value_get_object(value); 01786 count = json_object_get_count(schema_object); 01787 if (count == 0) { 01788 return JSONSuccess; /* Empty object allows all objects */ 01789 } else if (json_object_get_count(value_object) < count) { 01790 return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */ 01791 } 01792 for (i = 0; i < count; i++) { 01793 key = json_object_get_name(schema_object, i); 01794 temp_schema_value = json_object_get_value(schema_object, key); 01795 temp_value = json_object_get_value(value_object, key); 01796 if (temp_value == NULL) { 01797 return JSONFailure; 01798 } 01799 if (json_validate(temp_schema_value, temp_value) == JSONFailure) { 01800 return JSONFailure; 01801 } 01802 } 01803 return JSONSuccess; 01804 case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: 01805 return JSONSuccess; /* equality already tested before switch */ 01806 case JSONError: default: 01807 return JSONFailure; 01808 } 01809 } 01810 01811 JSON_Status json_value_equals(const JSON_Value *a, const JSON_Value *b) { 01812 JSON_Object *a_object = NULL, *b_object = NULL; 01813 JSON_Array *a_array = NULL, *b_array = NULL; 01814 const char *a_string = NULL, *b_string = NULL; 01815 const char *key = NULL; 01816 size_t a_count = 0, b_count = 0, i = 0; 01817 JSON_Value_Type a_type, b_type; 01818 a_type = json_value_get_type(a); 01819 b_type = json_value_get_type(b); 01820 if (a_type != b_type) { 01821 return 0; 01822 } 01823 switch (a_type) { 01824 case JSONArray: 01825 a_array = json_value_get_array(a); 01826 b_array = json_value_get_array(b); 01827 a_count = json_array_get_count(a_array); 01828 b_count = json_array_get_count(b_array); 01829 if (a_count != b_count) { 01830 return 0; 01831 } 01832 for (i = 0; i < a_count; i++) { 01833 if (!json_value_equals(json_array_get_value(a_array, i), 01834 json_array_get_value(b_array, i))) { 01835 return 0; 01836 } 01837 } 01838 return 1; 01839 case JSONObject: 01840 a_object = json_value_get_object(a); 01841 b_object = json_value_get_object(b); 01842 a_count = json_object_get_count(a_object); 01843 b_count = json_object_get_count(b_object); 01844 if (a_count != b_count) { 01845 return 0; 01846 } 01847 for (i = 0; i < a_count; i++) { 01848 key = json_object_get_name(a_object, i); 01849 if (!json_value_equals(json_object_get_value(a_object, key), 01850 json_object_get_value(b_object, key))) { 01851 return 0; 01852 } 01853 } 01854 return 1; 01855 case JSONString: 01856 a_string = json_value_get_string(a); 01857 b_string = json_value_get_string(b); 01858 return strcmp(a_string, b_string) == 0; 01859 case JSONBoolean: 01860 return json_value_get_boolean(a) == json_value_get_boolean(b); 01861 case JSONNumber: 01862 return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */ 01863 case JSONError: 01864 return 1; 01865 case JSONNull: 01866 return 1; 01867 default: 01868 return 1; 01869 } 01870 } 01871 01872 JSON_Value_Type json_type(const JSON_Value *value) { 01873 return json_value_get_type(value); 01874 } 01875 01876 JSON_Object * json_object (const JSON_Value *value) { 01877 return json_value_get_object(value); 01878 } 01879 01880 JSON_Array * json_array (const JSON_Value *value) { 01881 return json_value_get_array(value); 01882 } 01883 01884 const char * json_string (const JSON_Value *value) { 01885 return json_value_get_string(value); 01886 } 01887 01888 double json_number (const JSON_Value *value) { 01889 return json_value_get_number(value); 01890 } 01891 01892 int json_boolean(const JSON_Value *value) { 01893 return json_value_get_boolean(value); 01894 } 01895 01896 void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun) { 01897 parson_malloc = malloc_fun; 01898 parson_free = free_fun; 01899 }
Generated on Wed Jul 13 2022 07:19:04 by
1.7.2