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

Committer:
AzureIoTClient
Date:
Mon Sep 11 09:22:55 2017 -0700
Revision:
75:86205ca63a59
Parent:
61:8b85a4e797cf
Child:
80:db5f5237bc95
1.1.23

Who changed what in which revision?

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