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