| 1 | /* |
| 2 | * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org> |
| 3 | * |
| 4 | * Jansson is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the MIT license. See LICENSE for details. |
| 6 | */ |
| 7 | |
| 8 | #ifndef _GNU_SOURCE |
| 9 | #define _GNU_SOURCE |
| 10 | #endif |
| 11 | |
| 12 | #ifdef HAVE_CONFIG_H |
| 13 | #include <jansson_private_config.h> |
| 14 | #endif |
| 15 | |
| 16 | #include <stddef.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <math.h> |
| 20 | |
| 21 | #ifdef HAVE_STDINT_H |
| 22 | #include <stdint.h> |
| 23 | #endif |
| 24 | |
| 25 | #include "jansson.h" |
| 26 | #include "hashtable.h" |
| 27 | #include "jansson_private.h" |
| 28 | #include "utf.h" |
| 29 | |
| 30 | /* Work around nonstandard isnan() and isinf() implementations */ |
| 31 | #ifndef isnan |
| 32 | #ifndef __sun |
| 33 | static JSON_INLINE int isnan(double x) { return x != x; } |
| 34 | #endif |
| 35 | #endif |
| 36 | #ifndef isinf |
| 37 | static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); } |
| 38 | #endif |
| 39 | |
| 40 | static JSON_INLINE void json_init(json_t *json, json_type type) |
| 41 | { |
| 42 | json->type = type; |
| 43 | json->refcount = 1; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /*** object ***/ |
| 48 | |
| 49 | extern volatile uint32_t hashtable_seed; |
| 50 | |
| 51 | json_t *json_object(void) |
| 52 | { |
| 53 | json_object_t *object = jsonp_malloc(sizeof(json_object_t)); |
| 54 | if(!object) |
| 55 | return NULL; |
| 56 | |
| 57 | if (!hashtable_seed) { |
| 58 | /* Autoseed */ |
| 59 | json_object_seed(0); |
| 60 | } |
| 61 | |
| 62 | json_init(&object->json, JSON_OBJECT); |
| 63 | |
| 64 | if(hashtable_init(&object->hashtable)) |
| 65 | { |
| 66 | jsonp_free(object); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | return &object->json; |
| 71 | } |
| 72 | |
| 73 | static void json_delete_object(json_object_t *object) |
| 74 | { |
| 75 | hashtable_close(&object->hashtable); |
| 76 | jsonp_free(object); |
| 77 | } |
| 78 | |
| 79 | size_t json_object_size(const json_t *json) |
| 80 | { |
| 81 | json_object_t *object; |
| 82 | |
| 83 | if(!json_is_object(json)) |
| 84 | return 0; |
| 85 | |
| 86 | object = json_to_object(json); |
| 87 | return object->hashtable.size; |
| 88 | } |
| 89 | |
| 90 | json_t *json_object_get(const json_t *json, const char *key) |
| 91 | { |
| 92 | json_object_t *object; |
| 93 | |
| 94 | if(!key || !json_is_object(json)) |
| 95 | return NULL; |
| 96 | |
| 97 | object = json_to_object(json); |
| 98 | return hashtable_get(&object->hashtable, key); |
| 99 | } |
| 100 | |
| 101 | int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) |
| 102 | { |
| 103 | json_object_t *object; |
| 104 | |
| 105 | if(!value) |
| 106 | return -1; |
| 107 | |
| 108 | if(!key || !json_is_object(json) || json == value) |
| 109 | { |
| 110 | json_decref(value); |
| 111 | return -1; |
| 112 | } |
| 113 | object = json_to_object(json); |
| 114 | |
| 115 | if(hashtable_set(&object->hashtable, key, value)) |
| 116 | { |
| 117 | json_decref(value); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int json_object_set_new(json_t *json, const char *key, json_t *value) |
| 125 | { |
| 126 | if(!key || !utf8_check_string(key, strlen(key))) |
| 127 | { |
| 128 | json_decref(value); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | return json_object_set_new_nocheck(json, key, value); |
| 133 | } |
| 134 | |
| 135 | int json_object_del(json_t *json, const char *key) |
| 136 | { |
| 137 | json_object_t *object; |
| 138 | |
| 139 | if(!key || !json_is_object(json)) |
| 140 | return -1; |
| 141 | |
| 142 | object = json_to_object(json); |
| 143 | return hashtable_del(&object->hashtable, key); |
| 144 | } |
| 145 | |
| 146 | int json_object_clear(json_t *json) |
| 147 | { |
| 148 | json_object_t *object; |
| 149 | |
| 150 | if(!json_is_object(json)) |
| 151 | return -1; |
| 152 | |
| 153 | object = json_to_object(json); |
| 154 | hashtable_clear(&object->hashtable); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int json_object_update(json_t *object, json_t *other) |
| 160 | { |
| 161 | const char *key; |
| 162 | json_t *value; |
| 163 | |
| 164 | if(!json_is_object(object) || !json_is_object(other)) |
| 165 | return -1; |
| 166 | |
| 167 | json_object_foreach(other, key, value) { |
| 168 | if(json_object_set_nocheck(object, key, value)) |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int json_object_update_existing(json_t *object, json_t *other) |
| 176 | { |
| 177 | const char *key; |
| 178 | json_t *value; |
| 179 | |
| 180 | if(!json_is_object(object) || !json_is_object(other)) |
| 181 | return -1; |
| 182 | |
| 183 | json_object_foreach(other, key, value) { |
| 184 | if(json_object_get(object, key)) |
| 185 | json_object_set_nocheck(object, key, value); |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | int json_object_update_missing(json_t *object, json_t *other) |
| 192 | { |
| 193 | const char *key; |
| 194 | json_t *value; |
| 195 | |
| 196 | if(!json_is_object(object) || !json_is_object(other)) |
| 197 | return -1; |
| 198 | |
| 199 | json_object_foreach(other, key, value) { |
| 200 | if(!json_object_get(object, key)) |
| 201 | json_object_set_nocheck(object, key, value); |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | void *json_object_iter(json_t *json) |
| 208 | { |
| 209 | json_object_t *object; |
| 210 | |
| 211 | if(!json_is_object(json)) |
| 212 | return NULL; |
| 213 | |
| 214 | object = json_to_object(json); |
| 215 | return hashtable_iter(&object->hashtable); |
| 216 | } |
| 217 | |
| 218 | void *json_object_iter_at(json_t *json, const char *key) |
| 219 | { |
| 220 | json_object_t *object; |
| 221 | |
| 222 | if(!key || !json_is_object(json)) |
| 223 | return NULL; |
| 224 | |
| 225 | object = json_to_object(json); |
| 226 | return hashtable_iter_at(&object->hashtable, key); |
| 227 | } |
| 228 | |
| 229 | void *json_object_iter_next(json_t *json, void *iter) |
| 230 | { |
| 231 | json_object_t *object; |
| 232 | |
| 233 | if(!json_is_object(json) || iter == NULL) |
| 234 | return NULL; |
| 235 | |
| 236 | object = json_to_object(json); |
| 237 | return hashtable_iter_next(&object->hashtable, iter); |
| 238 | } |
| 239 | |
| 240 | const char *json_object_iter_key(void *iter) |
| 241 | { |
| 242 | if(!iter) |
| 243 | return NULL; |
| 244 | |
| 245 | return hashtable_iter_key(iter); |
| 246 | } |
| 247 | |
| 248 | json_t *json_object_iter_value(void *iter) |
| 249 | { |
| 250 | if(!iter) |
| 251 | return NULL; |
| 252 | |
| 253 | return (json_t *)hashtable_iter_value(iter); |
| 254 | } |
| 255 | |
| 256 | int json_object_iter_set_new(json_t *json, void *iter, json_t *value) |
| 257 | { |
| 258 | if(!json_is_object(json) || !iter || !value) |
| 259 | { |
| 260 | json_decref(value); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | hashtable_iter_set(iter, value); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | void *json_object_key_to_iter(const char *key) |
| 269 | { |
| 270 | if(!key) |
| 271 | return NULL; |
| 272 | |
| 273 | return hashtable_key_to_iter(key); |
| 274 | } |
| 275 | |
| 276 | static int json_object_equal(const json_t *object1, const json_t *object2) |
| 277 | { |
| 278 | const char *key; |
| 279 | const json_t *value1, *value2; |
| 280 | |
| 281 | if(json_object_size(object1) != json_object_size(object2)) |
| 282 | return 0; |
| 283 | |
| 284 | json_object_foreach((json_t *)object1, key, value1) { |
| 285 | value2 = json_object_get(object2, key); |
| 286 | |
| 287 | if(!json_equal(value1, value2)) |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | static json_t *json_object_copy(json_t *object) |
| 295 | { |
| 296 | json_t *result; |
| 297 | |
| 298 | const char *key; |
| 299 | json_t *value; |
| 300 | |
| 301 | result = json_object(); |
| 302 | if(!result) |
| 303 | return NULL; |
| 304 | |
| 305 | json_object_foreach(object, key, value) |
| 306 | json_object_set_nocheck(result, key, value); |
| 307 | |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | static json_t *json_object_deep_copy(const json_t *object) |
| 312 | { |
| 313 | json_t *result; |
| 314 | void *iter; |
| 315 | |
| 316 | result = json_object(); |
| 317 | if(!result) |
| 318 | return NULL; |
| 319 | |
| 320 | /* Cannot use json_object_foreach because object has to be cast |
| 321 | non-const */ |
| 322 | iter = json_object_iter((json_t *)object); |
| 323 | while(iter) { |
| 324 | const char *key; |
| 325 | const json_t *value; |
| 326 | key = json_object_iter_key(iter); |
| 327 | value = json_object_iter_value(iter); |
| 328 | |
| 329 | json_object_set_new_nocheck(result, key, json_deep_copy(value)); |
| 330 | iter = json_object_iter_next((json_t *)object, iter); |
| 331 | } |
| 332 | |
| 333 | return result; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /*** array ***/ |
| 338 | |
| 339 | json_t *json_array(void) |
| 340 | { |
| 341 | json_array_t *array = jsonp_malloc(sizeof(json_array_t)); |
| 342 | if(!array) |
| 343 | return NULL; |
| 344 | json_init(&array->json, JSON_ARRAY); |
| 345 | |
| 346 | array->entries = 0; |
| 347 | array->size = 8; |
| 348 | |
| 349 | array->table = jsonp_malloc(array->size * sizeof(json_t *)); |
| 350 | if(!array->table) { |
| 351 | jsonp_free(array); |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | return &array->json; |
| 356 | } |
| 357 | |
| 358 | static void json_delete_array(json_array_t *array) |
| 359 | { |
| 360 | size_t i; |
| 361 | |
| 362 | for(i = 0; i < array->entries; i++) |
| 363 | json_decref(array->table[i]); |
| 364 | |
| 365 | jsonp_free(array->table); |
| 366 | jsonp_free(array); |
| 367 | } |
| 368 | |
| 369 | size_t json_array_size(const json_t *json) |
| 370 | { |
| 371 | if(!json_is_array(json)) |
| 372 | return 0; |
| 373 | |
| 374 | return json_to_array(json)->entries; |
| 375 | } |
| 376 | |
| 377 | json_t *json_array_get(const json_t *json, size_t index) |
| 378 | { |
| 379 | json_array_t *array; |
| 380 | if(!json_is_array(json)) |
| 381 | return NULL; |
| 382 | array = json_to_array(json); |
| 383 | |
| 384 | if(index >= array->entries) |
| 385 | return NULL; |
| 386 | |
| 387 | return array->table[index]; |
| 388 | } |
| 389 | |
| 390 | int json_array_set_new(json_t *json, size_t index, json_t *value) |
| 391 | { |
| 392 | json_array_t *array; |
| 393 | |
| 394 | if(!value) |
| 395 | return -1; |
| 396 | |
| 397 | if(!json_is_array(json) || json == value) |
| 398 | { |
| 399 | json_decref(value); |
| 400 | return -1; |
| 401 | } |
| 402 | array = json_to_array(json); |
| 403 | |
| 404 | if(index >= array->entries) |
| 405 | { |
| 406 | json_decref(value); |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | json_decref(array->table[index]); |
| 411 | array->table[index] = value; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static void array_move(json_array_t *array, size_t dest, |
| 417 | size_t src, size_t count) |
| 418 | { |
| 419 | memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *)); |
| 420 | } |
| 421 | |
| 422 | static void array_copy(json_t **dest, size_t dpos, |
| 423 | json_t **src, size_t spos, |
| 424 | size_t count) |
| 425 | { |
| 426 | memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *)); |
| 427 | } |
| 428 | |
| 429 | static json_t **json_array_grow(json_array_t *array, |
| 430 | size_t amount, |
| 431 | int copy) |
| 432 | { |
| 433 | size_t new_size; |
| 434 | json_t **old_table, **new_table; |
| 435 | |
| 436 | if(array->entries + amount <= array->size) |
| 437 | return array->table; |
| 438 | |
| 439 | old_table = array->table; |
| 440 | |
| 441 | new_size = max(array->size + amount, array->size * 2); |
| 442 | new_table = jsonp_malloc(new_size * sizeof(json_t *)); |
| 443 | if(!new_table) |
| 444 | return NULL; |
| 445 | |
| 446 | array->size = new_size; |
| 447 | array->table = new_table; |
| 448 | |
| 449 | if(copy) { |
| 450 | array_copy(array->table, 0, old_table, 0, array->entries); |
| 451 | jsonp_free(old_table); |
| 452 | return array->table; |
| 453 | } |
| 454 | |
| 455 | return old_table; |
| 456 | } |
| 457 | |
| 458 | int json_array_append_new(json_t *json, json_t *value) |
| 459 | { |
| 460 | json_array_t *array; |
| 461 | |
| 462 | if(!value) |
| 463 | return -1; |
| 464 | |
| 465 | if(!json_is_array(json) || json == value) |
| 466 | { |
| 467 | json_decref(value); |
| 468 | return -1; |
| 469 | } |
| 470 | array = json_to_array(json); |
| 471 | |
| 472 | if(!json_array_grow(array, 1, 1)) { |
| 473 | json_decref(value); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | array->table[array->entries] = value; |
| 478 | array->entries++; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | int json_array_insert_new(json_t *json, size_t index, json_t *value) |
| 484 | { |
| 485 | json_array_t *array; |
| 486 | json_t **old_table; |
| 487 | |
| 488 | if(!value) |
| 489 | return -1; |
| 490 | |
| 491 | if(!json_is_array(json) || json == value) { |
| 492 | json_decref(value); |
| 493 | return -1; |
| 494 | } |
| 495 | array = json_to_array(json); |
| 496 | |
| 497 | if(index > array->entries) { |
| 498 | json_decref(value); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | old_table = json_array_grow(array, 1, 0); |
| 503 | if(!old_table) { |
| 504 | json_decref(value); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | if(old_table != array->table) { |
| 509 | array_copy(array->table, 0, old_table, 0, index); |
| 510 | array_copy(array->table, index + 1, old_table, index, |
| 511 | array->entries - index); |
| 512 | jsonp_free(old_table); |
| 513 | } |
| 514 | else |
| 515 | array_move(array, index + 1, index, array->entries - index); |
| 516 | |
| 517 | array->table[index] = value; |
| 518 | array->entries++; |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | int json_array_remove(json_t *json, size_t index) |
| 524 | { |
| 525 | json_array_t *array; |
| 526 | |
| 527 | if(!json_is_array(json)) |
| 528 | return -1; |
| 529 | array = json_to_array(json); |
| 530 | |
| 531 | if(index >= array->entries) |
| 532 | return -1; |
| 533 | |
| 534 | json_decref(array->table[index]); |
| 535 | |
| 536 | /* If we're removing the last element, nothing has to be moved */ |
| 537 | if(index < array->entries - 1) |
| 538 | array_move(array, index, index + 1, array->entries - index - 1); |
| 539 | |
| 540 | array->entries--; |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | int json_array_clear(json_t *json) |
| 546 | { |
| 547 | json_array_t *array; |
| 548 | size_t i; |
| 549 | |
| 550 | if(!json_is_array(json)) |
| 551 | return -1; |
| 552 | array = json_to_array(json); |
| 553 | |
| 554 | for(i = 0; i < array->entries; i++) |
| 555 | json_decref(array->table[i]); |
| 556 | |
| 557 | array->entries = 0; |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | int json_array_extend(json_t *json, json_t *other_json) |
| 562 | { |
| 563 | json_array_t *array, *other; |
| 564 | size_t i; |
| 565 | |
| 566 | if(!json_is_array(json) || !json_is_array(other_json)) |
| 567 | return -1; |
| 568 | array = json_to_array(json); |
| 569 | other = json_to_array(other_json); |
| 570 | |
| 571 | if(!json_array_grow(array, other->entries, 1)) |
| 572 | return -1; |
| 573 | |
| 574 | for(i = 0; i < other->entries; i++) |
| 575 | json_incref(other->table[i]); |
| 576 | |
| 577 | array_copy(array->table, array->entries, other->table, 0, other->entries); |
| 578 | |
| 579 | array->entries += other->entries; |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int json_array_equal(const json_t *array1, const json_t *array2) |
| 584 | { |
| 585 | size_t i, size; |
| 586 | |
| 587 | size = json_array_size(array1); |
| 588 | if(size != json_array_size(array2)) |
| 589 | return 0; |
| 590 | |
| 591 | for(i = 0; i < size; i++) |
| 592 | { |
| 593 | json_t *value1, *value2; |
| 594 | |
| 595 | value1 = json_array_get(array1, i); |
| 596 | value2 = json_array_get(array2, i); |
| 597 | |
| 598 | if(!json_equal(value1, value2)) |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | return 1; |
| 603 | } |
| 604 | |
| 605 | static json_t *json_array_copy(json_t *array) |
| 606 | { |
| 607 | json_t *result; |
| 608 | size_t i; |
| 609 | |
| 610 | result = json_array(); |
| 611 | if(!result) |
| 612 | return NULL; |
| 613 | |
| 614 | for(i = 0; i < json_array_size(array); i++) |
| 615 | json_array_append(result, json_array_get(array, i)); |
| 616 | |
| 617 | return result; |
| 618 | } |
| 619 | |
| 620 | static json_t *json_array_deep_copy(const json_t *array) |
| 621 | { |
| 622 | json_t *result; |
| 623 | size_t i; |
| 624 | |
| 625 | result = json_array(); |
| 626 | if(!result) |
| 627 | return NULL; |
| 628 | |
| 629 | for(i = 0; i < json_array_size(array); i++) |
| 630 | json_array_append_new(result, json_deep_copy(json_array_get(array, i))); |
| 631 | |
| 632 | return result; |
| 633 | } |
| 634 | |
| 635 | /*** string ***/ |
| 636 | |
| 637 | static json_t *string_create(const char *value, size_t len, int own) |
| 638 | { |
| 639 | char *v; |
| 640 | json_string_t *string; |
| 641 | |
| 642 | if(!value) |
| 643 | return NULL; |
| 644 | |
| 645 | if(own) |
| 646 | v = (char *)value; |
| 647 | else { |
| 648 | v = jsonp_strndup(value, len); |
| 649 | if(!v) |
| 650 | return NULL; |
| 651 | } |
| 652 | |
| 653 | string = jsonp_malloc(sizeof(json_string_t)); |
| 654 | if(!string) { |
| 655 | jsonp_free(v); |
| 656 | return NULL; |
| 657 | } |
| 658 | json_init(&string->json, JSON_STRING); |
| 659 | string->value = v; |
| 660 | string->length = len; |
| 661 | |
| 662 | return &string->json; |
| 663 | } |
| 664 | |
| 665 | json_t *json_string_nocheck(const char *value) |
| 666 | { |
| 667 | if(!value) |
| 668 | return NULL; |
| 669 | |
| 670 | return string_create(value, strlen(value), 0); |
| 671 | } |
| 672 | |
| 673 | json_t *json_stringn_nocheck(const char *value, size_t len) |
| 674 | { |
| 675 | return string_create(value, len, 0); |
| 676 | } |
| 677 | |
| 678 | /* this is private; "steal" is not a public API concept */ |
| 679 | json_t *jsonp_stringn_nocheck_own(const char *value, size_t len) |
| 680 | { |
| 681 | return string_create(value, len, 1); |
| 682 | } |
| 683 | |
| 684 | json_t *json_string(const char *value) |
| 685 | { |
| 686 | if(!value) |
| 687 | return NULL; |
| 688 | |
| 689 | return json_stringn(value, strlen(value)); |
| 690 | } |
| 691 | |
| 692 | json_t *json_stringn(const char *value, size_t len) |
| 693 | { |
| 694 | if(!value || !utf8_check_string(value, len)) |
| 695 | return NULL; |
| 696 | |
| 697 | return json_stringn_nocheck(value, len); |
| 698 | } |
| 699 | |
| 700 | const char *json_string_value(const json_t *json) |
| 701 | { |
| 702 | if(!json_is_string(json)) |
| 703 | return NULL; |
| 704 | |
| 705 | return json_to_string(json)->value; |
| 706 | } |
| 707 | |
| 708 | size_t json_string_length(const json_t *json) |
| 709 | { |
| 710 | if(!json_is_string(json)) |
| 711 | return 0; |
| 712 | |
| 713 | return json_to_string(json)->length; |
| 714 | } |
| 715 | |
| 716 | int json_string_set_nocheck(json_t *json, const char *value) |
| 717 | { |
| 718 | if(!value) |
| 719 | return -1; |
| 720 | |
| 721 | return json_string_setn_nocheck(json, value, strlen(value)); |
| 722 | } |
| 723 | |
| 724 | int json_string_setn_nocheck(json_t *json, const char *value, size_t len) |
| 725 | { |
| 726 | char *dup; |
| 727 | json_string_t *string; |
| 728 | |
| 729 | if(!json_is_string(json) || !value) |
| 730 | return -1; |
| 731 | |
| 732 | dup = jsonp_strndup(value, len); |
| 733 | if(!dup) |
| 734 | return -1; |
| 735 | |
| 736 | string = json_to_string(json); |
| 737 | jsonp_free(string->value); |
| 738 | string->value = dup; |
| 739 | string->length = len; |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | int json_string_set(json_t *json, const char *value) |
| 745 | { |
| 746 | if(!value) |
| 747 | return -1; |
| 748 | |
| 749 | return json_string_setn(json, value, strlen(value)); |
| 750 | } |
| 751 | |
| 752 | int json_string_setn(json_t *json, const char *value, size_t len) |
| 753 | { |
| 754 | if(!value || !utf8_check_string(value, len)) |
| 755 | return -1; |
| 756 | |
| 757 | return json_string_setn_nocheck(json, value, len); |
| 758 | } |
| 759 | |
| 760 | static void json_delete_string(json_string_t *string) |
| 761 | { |
| 762 | jsonp_free(string->value); |
| 763 | jsonp_free(string); |
| 764 | } |
| 765 | |
| 766 | static int json_string_equal(const json_t *string1, const json_t *string2) |
| 767 | { |
| 768 | json_string_t *s1, *s2; |
| 769 | |
| 770 | s1 = json_to_string(string1); |
| 771 | s2 = json_to_string(string2); |
| 772 | return s1->length == s2->length && !memcmp(s1->value, s2->value, s1->length); |
| 773 | } |
| 774 | |
| 775 | static json_t *json_string_copy(const json_t *string) |
| 776 | { |
| 777 | json_string_t *s; |
| 778 | |
| 779 | s = json_to_string(string); |
| 780 | return json_stringn_nocheck(s->value, s->length); |
| 781 | } |
| 782 | |
| 783 | json_t *json_vsprintf(const char *fmt, va_list ap) { |
| 784 | json_t *json = NULL; |
| 785 | int length; |
| 786 | char *buf; |
| 787 | va_list aq; |
| 788 | va_copy(aq, ap); |
| 789 | |
| 790 | length = vsnprintf(NULL, 0, fmt, ap); |
| 791 | if (length == 0) { |
| 792 | json = json_string(""); |
| 793 | goto out; |
| 794 | } |
| 795 | |
| 796 | buf = jsonp_malloc(length + 1); |
| 797 | if (!buf) |
| 798 | goto out; |
| 799 | |
| 800 | vsnprintf(buf, length + 1, fmt, aq); |
| 801 | if (!utf8_check_string(buf, length)) { |
| 802 | jsonp_free(buf); |
| 803 | goto out; |
| 804 | } |
| 805 | |
| 806 | json = jsonp_stringn_nocheck_own(buf, length); |
| 807 | |
| 808 | out: |
| 809 | va_end(aq); |
| 810 | return json; |
| 811 | } |
| 812 | |
| 813 | json_t *json_sprintf(const char *fmt, ...) { |
| 814 | json_t *result; |
| 815 | va_list ap; |
| 816 | |
| 817 | va_start(ap, fmt); |
| 818 | result = json_vsprintf(fmt, ap); |
| 819 | va_end(ap); |
| 820 | |
| 821 | return result; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | /*** integer ***/ |
| 826 | |
| 827 | json_t *json_integer(json_int_t value) |
| 828 | { |
| 829 | json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t)); |
| 830 | if(!integer) |
| 831 | return NULL; |
| 832 | json_init(&integer->json, JSON_INTEGER); |
| 833 | |
| 834 | integer->value = value; |
| 835 | return &integer->json; |
| 836 | } |
| 837 | |
| 838 | json_int_t json_integer_value(const json_t *json) |
| 839 | { |
| 840 | if(!json_is_integer(json)) |
| 841 | return 0; |
| 842 | |
| 843 | return json_to_integer(json)->value; |
| 844 | } |
| 845 | |
| 846 | int json_integer_set(json_t *json, json_int_t value) |
| 847 | { |
| 848 | if(!json_is_integer(json)) |
| 849 | return -1; |
| 850 | |
| 851 | json_to_integer(json)->value = value; |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static void json_delete_integer(json_integer_t *integer) |
| 857 | { |
| 858 | jsonp_free(integer); |
| 859 | } |
| 860 | |
| 861 | static int json_integer_equal(const json_t *integer1, const json_t *integer2) |
| 862 | { |
| 863 | return json_integer_value(integer1) == json_integer_value(integer2); |
| 864 | } |
| 865 | |
| 866 | static json_t *json_integer_copy(const json_t *integer) |
| 867 | { |
| 868 | return json_integer(json_integer_value(integer)); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | /*** real ***/ |
| 873 | |
| 874 | json_t *json_real(double value) |
| 875 | { |
| 876 | json_real_t *real; |
| 877 | |
| 878 | if(isnan(value) || isinf(value)) |
| 879 | return NULL; |
| 880 | |
| 881 | real = jsonp_malloc(sizeof(json_real_t)); |
| 882 | if(!real) |
| 883 | return NULL; |
| 884 | json_init(&real->json, JSON_REAL); |
| 885 | |
| 886 | real->value = value; |
| 887 | return &real->json; |
| 888 | } |
| 889 | |
| 890 | double json_real_value(const json_t *json) |
| 891 | { |
| 892 | if(!json_is_real(json)) |
| 893 | return 0; |
| 894 | |
| 895 | return json_to_real(json)->value; |
| 896 | } |
| 897 | |
| 898 | int json_real_set(json_t *json, double value) |
| 899 | { |
| 900 | if(!json_is_real(json) || isnan(value) || isinf(value)) |
| 901 | return -1; |
| 902 | |
| 903 | json_to_real(json)->value = value; |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static void json_delete_real(json_real_t *real) |
| 909 | { |
| 910 | jsonp_free(real); |
| 911 | } |
| 912 | |
| 913 | static int json_real_equal(const json_t *real1, const json_t *real2) |
| 914 | { |
| 915 | return json_real_value(real1) == json_real_value(real2); |
| 916 | } |
| 917 | |
| 918 | static json_t *json_real_copy(const json_t *real) |
| 919 | { |
| 920 | return json_real(json_real_value(real)); |
| 921 | } |
| 922 | |
| 923 | |
| 924 | /*** number ***/ |
| 925 | |
| 926 | double json_number_value(const json_t *json) |
| 927 | { |
| 928 | if(json_is_integer(json)) |
| 929 | return (double)json_integer_value(json); |
| 930 | else if(json_is_real(json)) |
| 931 | return json_real_value(json); |
| 932 | else |
| 933 | return 0.0; |
| 934 | } |
| 935 | |
| 936 | |
| 937 | /*** simple values ***/ |
| 938 | |
| 939 | json_t *json_true(void) |
| 940 | { |
| 941 | static json_t the_true = {JSON_TRUE, (size_t)-1}; |
| 942 | return &the_true; |
| 943 | } |
| 944 | |
| 945 | |
| 946 | json_t *json_false(void) |
| 947 | { |
| 948 | static json_t the_false = {JSON_FALSE, (size_t)-1}; |
| 949 | return &the_false; |
| 950 | } |
| 951 | |
| 952 | |
| 953 | json_t *json_null(void) |
| 954 | { |
| 955 | static json_t the_null = {JSON_NULL, (size_t)-1}; |
| 956 | return &the_null; |
| 957 | } |
| 958 | |
| 959 | |
| 960 | /*** deletion ***/ |
| 961 | |
| 962 | void json_delete(json_t *json) |
| 963 | { |
| 964 | if (!json) |
| 965 | return; |
| 966 | |
| 967 | switch(json_typeof(json)) { |
| 968 | case JSON_OBJECT: |
| 969 | json_delete_object(json_to_object(json)); |
| 970 | break; |
| 971 | case JSON_ARRAY: |
| 972 | json_delete_array(json_to_array(json)); |
| 973 | break; |
| 974 | case JSON_STRING: |
| 975 | json_delete_string(json_to_string(json)); |
| 976 | break; |
| 977 | case JSON_INTEGER: |
| 978 | json_delete_integer(json_to_integer(json)); |
| 979 | break; |
| 980 | case JSON_REAL: |
| 981 | json_delete_real(json_to_real(json)); |
| 982 | break; |
| 983 | default: |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | /* json_delete is not called for true, false or null */ |
| 988 | } |
| 989 | |
| 990 | |
| 991 | /*** equality ***/ |
| 992 | |
| 993 | int json_equal(const json_t *json1, const json_t *json2) |
| 994 | { |
| 995 | if(!json1 || !json2) |
| 996 | return 0; |
| 997 | |
| 998 | if(json_typeof(json1) != json_typeof(json2)) |
| 999 | return 0; |
| 1000 | |
| 1001 | /* this covers true, false and null as they are singletons */ |
| 1002 | if(json1 == json2) |
| 1003 | return 1; |
| 1004 | |
| 1005 | switch(json_typeof(json1)) { |
| 1006 | case JSON_OBJECT: |
| 1007 | return json_object_equal(json1, json2); |
| 1008 | case JSON_ARRAY: |
| 1009 | return json_array_equal(json1, json2); |
| 1010 | case JSON_STRING: |
| 1011 | return json_string_equal(json1, json2); |
| 1012 | case JSON_INTEGER: |
| 1013 | return json_integer_equal(json1, json2); |
| 1014 | case JSON_REAL: |
| 1015 | return json_real_equal(json1, json2); |
| 1016 | default: |
| 1017 | return 0; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | /*** copying ***/ |
| 1023 | |
| 1024 | json_t *json_copy(json_t *json) |
| 1025 | { |
| 1026 | if(!json) |
| 1027 | return NULL; |
| 1028 | |
| 1029 | switch(json_typeof(json)) { |
| 1030 | case JSON_OBJECT: |
| 1031 | return json_object_copy(json); |
| 1032 | case JSON_ARRAY: |
| 1033 | return json_array_copy(json); |
| 1034 | case JSON_STRING: |
| 1035 | return json_string_copy(json); |
| 1036 | case JSON_INTEGER: |
| 1037 | return json_integer_copy(json); |
| 1038 | case JSON_REAL: |
| 1039 | return json_real_copy(json); |
| 1040 | case JSON_TRUE: |
| 1041 | case JSON_FALSE: |
| 1042 | case JSON_NULL: |
| 1043 | return json; |
| 1044 | default: |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | json_t *json_deep_copy(const json_t *json) |
| 1050 | { |
| 1051 | if(!json) |
| 1052 | return NULL; |
| 1053 | |
| 1054 | switch(json_typeof(json)) { |
| 1055 | case JSON_OBJECT: |
| 1056 | return json_object_deep_copy(json); |
| 1057 | case JSON_ARRAY: |
| 1058 | return json_array_deep_copy(json); |
| 1059 | /* for the rest of the types, deep copying doesn't differ from |
| 1060 | shallow copying */ |
| 1061 | case JSON_STRING: |
| 1062 | return json_string_copy(json); |
| 1063 | case JSON_INTEGER: |
| 1064 | return json_integer_copy(json); |
| 1065 | case JSON_REAL: |
| 1066 | return json_real_copy(json); |
| 1067 | case JSON_TRUE: |
| 1068 | case JSON_FALSE: |
| 1069 | case JSON_NULL: |
| 1070 | return (json_t *)json; |
| 1071 | default: |
| 1072 | return NULL; |
| 1073 | } |
| 1074 | } |