1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/byte_buf.h>
#include <aws/common/string.h>
#include <aws/common/json.h>
#include <aws/common/private/external_module_impl.h>
#include "external/cJSON.h"
static struct aws_allocator *s_aws_json_module_allocator = NULL;
static bool s_aws_json_module_initialized = false;
struct aws_json_value *aws_json_value_new_string(struct aws_allocator *allocator, struct aws_byte_cursor string) {
struct aws_string *tmp = aws_string_new_from_cursor(allocator, &string);
void *ret_val = cJSON_CreateString(aws_string_c_str(tmp));
aws_string_destroy_secure(tmp);
return ret_val;
}
struct aws_json_value *aws_json_value_new_string_from_c_str(struct aws_allocator *allocator, const char *string) {
(void)allocator; /* No need for allocator. It is overriden through hooks. */
void *ret_val = cJSON_CreateString(string);
return ret_val;
}
struct aws_json_value *aws_json_value_new_number(struct aws_allocator *allocator, double number) {
(void)allocator; // prevent warnings over unused parameter
return (void *)cJSON_CreateNumber(number);
}
struct aws_json_value *aws_json_value_new_array(struct aws_allocator *allocator) {
(void)allocator; // prevent warnings over unused parameter
return (void *)cJSON_CreateArray();
}
struct aws_json_value *aws_json_value_new_boolean(struct aws_allocator *allocator, bool boolean) {
(void)allocator; // prevent warnings over unused parameter
return (void *)cJSON_CreateBool(boolean);
}
struct aws_json_value *aws_json_value_new_null(struct aws_allocator *allocator) {
(void)allocator; // prevent warnings over unused parameter
return (void *)cJSON_CreateNull();
}
struct aws_json_value *aws_json_value_new_object(struct aws_allocator *allocator) {
(void)allocator; // prevent warnings over unused parameter
return (void *)cJSON_CreateObject();
}
int aws_json_value_get_string(const struct aws_json_value *value, struct aws_byte_cursor *output) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (!cJSON_IsString(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
*output = aws_byte_cursor_from_c_str(cJSON_GetStringValue(cjson));
return AWS_OP_SUCCESS;
}
int aws_json_value_get_number(const struct aws_json_value *value, double *output) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (!cJSON_IsNumber(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
*output = cjson->valuedouble;
return AWS_OP_SUCCESS;
}
int aws_json_value_get_boolean(const struct aws_json_value *value, bool *output) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (!cJSON_IsBool(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
*output = cjson->type == cJSON_True;
return AWS_OP_SUCCESS;
}
int aws_json_value_add_to_object(
struct aws_json_value *object,
struct aws_byte_cursor key,
struct aws_json_value *value) {
struct aws_string *tmp = aws_string_new_from_cursor(s_aws_json_module_allocator, &key);
int result = aws_json_value_add_to_object_c_str(object, aws_string_c_str(tmp), value);
aws_string_destroy_secure(tmp);
return result;
}
int aws_json_value_add_to_object_c_str(struct aws_json_value *object, const char *key, struct aws_json_value *value) {
struct cJSON *cjson = (struct cJSON *)object;
if (!cJSON_IsObject(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
struct cJSON *cjson_value = (struct cJSON *)value;
if (cJSON_IsInvalid(cjson_value)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
if (cJSON_HasObjectItem(cjson, key)) {
return AWS_OP_ERR;
}
cJSON_AddItemToObject(cjson, key, cjson_value);
return AWS_OP_SUCCESS;
}
struct aws_json_value *aws_json_value_get_from_object(const struct aws_json_value *object, struct aws_byte_cursor key) {
struct aws_string *tmp = aws_string_new_from_cursor(s_aws_json_module_allocator, &key);
void *return_value = aws_json_value_get_from_object_c_str(object, aws_string_c_str(tmp));
aws_string_destroy_secure(tmp);
return return_value;
}
struct aws_json_value *aws_json_value_get_from_object_c_str(const struct aws_json_value *object, const char *key) {
const struct cJSON *cjson = (const struct cJSON *)object;
if (!cJSON_IsObject(cjson)) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (!cJSON_HasObjectItem(cjson, key)) {
return NULL;
}
return (void *)cJSON_GetObjectItem(cjson, key);
}
bool aws_json_value_has_key(const struct aws_json_value *object, struct aws_byte_cursor key) {
struct aws_string *tmp = aws_string_new_from_cursor(s_aws_json_module_allocator, &key);
bool result = aws_json_value_has_key_c_str(object, aws_string_c_str(tmp));
aws_string_destroy_secure(tmp);
return result;
}
bool aws_json_value_has_key_c_str(const struct aws_json_value *object, const char *key) {
const struct cJSON *cjson = (const struct cJSON *)object;
if (!cJSON_IsObject(cjson)) {
return false;
}
if (!cJSON_HasObjectItem(cjson, key)) {
return false;
}
return true;
}
int aws_json_value_remove_from_object(struct aws_json_value *object, struct aws_byte_cursor key) {
struct aws_string *tmp = aws_string_new_from_cursor(s_aws_json_module_allocator, &key);
int result = aws_json_value_remove_from_object_c_str(object, aws_string_c_str(tmp));
aws_string_destroy_secure(tmp);
return result;
}
int aws_json_value_remove_from_object_c_str(struct aws_json_value *object, const char *key) {
struct cJSON *cjson = (struct cJSON *)object;
if (!cJSON_IsObject(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
if (!cJSON_HasObjectItem(cjson, key)) {
return AWS_OP_ERR;
}
cJSON_DeleteItemFromObject(cjson, key);
return AWS_OP_SUCCESS;
}
int aws_json_const_iterate_object(
const struct aws_json_value *object,
aws_json_on_member_encountered_const_fn *on_member,
void *user_data) {
int result = AWS_OP_ERR;
const struct cJSON *cjson = (const struct cJSON *)object;
if (!cJSON_IsObject(cjson)) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
goto done;
}
const cJSON *key = NULL;
cJSON_ArrayForEach(key, cjson) {
bool should_continue = true;
struct aws_byte_cursor key_cur = aws_byte_cursor_from_c_str(key->string);
if (on_member(&key_cur, (const struct aws_json_value *)key, &should_continue, user_data)) {
goto done;
}
if (!should_continue) {
break;
}
}
result = AWS_OP_SUCCESS;
done:
return result;
}
int aws_json_value_add_array_element(struct aws_json_value *array, const struct aws_json_value *value) {
struct cJSON *cjson = (struct cJSON *)array;
if (!cJSON_IsArray(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
struct cJSON *cjson_value = (struct cJSON *)value;
if (cJSON_IsInvalid(cjson_value)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
cJSON_AddItemToArray(cjson, cjson_value);
return AWS_OP_SUCCESS;
}
struct aws_json_value *aws_json_get_array_element(const struct aws_json_value *array, size_t index) {
const struct cJSON *cjson = (const struct cJSON *)array;
if (!cJSON_IsArray(cjson)) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
if (index > (size_t)cJSON_GetArraySize(cjson)) {
aws_raise_error(AWS_ERROR_INVALID_INDEX);
return NULL;
}
return (void *)cJSON_GetArrayItem(cjson, (int)index);
}
size_t aws_json_get_array_size(const struct aws_json_value *array) {
const struct cJSON *cjson = (const struct cJSON *)array;
if (!cJSON_IsArray(cjson)) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return 0;
}
return cJSON_GetArraySize(cjson);
}
int aws_json_value_remove_array_element(struct aws_json_value *array, size_t index) {
struct cJSON *cjson = (struct cJSON *)array;
if (!cJSON_IsArray(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
if (index > (size_t)cJSON_GetArraySize(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_INDEX);
}
cJSON_DeleteItemFromArray(cjson, (int)index);
return AWS_OP_SUCCESS;
}
int aws_json_const_iterate_array(
const struct aws_json_value *array,
aws_json_on_value_encountered_const_fn *on_value,
void *user_data) {
int result = AWS_OP_ERR;
const struct cJSON *cjson = (const struct cJSON *)array;
if (!cJSON_IsArray(cjson)) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
goto done;
}
size_t idx = 0;
const cJSON *value = NULL;
cJSON_ArrayForEach(value, cjson) {
bool should_continue = true;
if (on_value(idx, (const struct aws_json_value *)value, &should_continue, user_data)) {
goto done;
}
if (!should_continue) {
break;
}
++idx;
}
result = AWS_OP_SUCCESS;
done:
return result;
}
bool aws_json_value_compare(const struct aws_json_value *a, const struct aws_json_value *b, bool is_case_sensitive) {
const struct cJSON *cjson_a = (const struct cJSON *)a;
const struct cJSON *cjson_b = (const struct cJSON *)b;
return cJSON_Compare(cjson_a, cjson_b, is_case_sensitive);
}
struct aws_json_value *aws_json_value_duplicate(const struct aws_json_value *value) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
return NULL;
}
struct cJSON *ret = cJSON_Duplicate(cjson, true);
if (ret == NULL) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
return (void *)ret;
}
bool aws_json_value_is_string(const struct aws_json_value *value) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return false;
}
return cJSON_IsString(cjson);
}
bool aws_json_value_is_number(const struct aws_json_value *value) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return false;
}
return cJSON_IsNumber(cjson);
}
bool aws_json_value_is_array(const struct aws_json_value *value) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return false;
}
return cJSON_IsArray(cjson);
}
bool aws_json_value_is_boolean(const struct aws_json_value *value) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return false;
}
return cJSON_IsBool(cjson);
}
bool aws_json_value_is_null(const struct aws_json_value *value) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return false;
}
return cJSON_IsNull(cjson);
}
bool aws_json_value_is_object(const struct aws_json_value *value) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return false;
}
return cJSON_IsObject(cjson);
}
static void *s_aws_cJSON_alloc(size_t sz) {
return aws_mem_acquire(s_aws_json_module_allocator, sz);
}
static void s_aws_cJSON_free(void *ptr) {
aws_mem_release(s_aws_json_module_allocator, ptr);
}
void aws_json_module_init(struct aws_allocator *allocator) {
if (!s_aws_json_module_initialized) {
s_aws_json_module_allocator = allocator;
struct cJSON_Hooks allocation_hooks = {
.malloc_fn = s_aws_cJSON_alloc,
.free_fn = s_aws_cJSON_free,
};
cJSON_InitHooks(&allocation_hooks);
s_aws_json_module_initialized = true;
}
}
void aws_json_module_cleanup(void) {
if (s_aws_json_module_initialized) {
s_aws_json_module_allocator = NULL;
s_aws_json_module_initialized = false;
}
}
void aws_json_value_destroy(struct aws_json_value *value) {
struct cJSON *cjson = (struct cJSON *)value;
/* Note: cJSON_IsInvalid returns false for NULL values, so we need explicit
check for NULL to skip delete */
if (cjson != NULL && !cJSON_IsInvalid(cjson)) {
cJSON_Delete(cjson);
}
}
int aws_byte_buf_append_json_string(const struct aws_json_value *value, struct aws_byte_buf *output) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
char *tmp = cJSON_PrintUnformatted(cjson);
if (tmp == NULL) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
// Append the text to the byte buffer
struct aws_byte_cursor tmp_cursor = aws_byte_cursor_from_c_str(tmp);
int return_val = aws_byte_buf_append_dynamic_secure(output, &tmp_cursor);
s_aws_cJSON_free(tmp); // free the char* now that we do not need it
return return_val;
}
int aws_byte_buf_append_json_string_formatted(const struct aws_json_value *value, struct aws_byte_buf *output) {
const struct cJSON *cjson = (const struct cJSON *)value;
if (cJSON_IsInvalid(cjson)) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
char *tmp = cJSON_Print(cjson);
if (tmp == NULL) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
// Append the text to the byte buffer
struct aws_byte_cursor tmp_cursor = aws_byte_cursor_from_c_str(tmp);
int return_val = aws_byte_buf_append_dynamic_secure(output, &tmp_cursor);
s_aws_cJSON_free(tmp); // free the char* now that we do not need it
return return_val;
}
struct aws_json_value *aws_json_value_new_from_string(struct aws_allocator *allocator, struct aws_byte_cursor string) {
struct aws_string *tmp = aws_string_new_from_cursor(allocator, &string);
struct cJSON *cjson = cJSON_Parse(aws_string_c_str(tmp));
aws_string_destroy_secure(tmp);
return (void *)cjson;
}
|