| 1 | /*============================================================================== |
| 2 | * Created by Yaoyuan on 2019/3/9. |
| 3 | * Copyright (C) 2019 Yaoyuan <ibireme@gmail.com>. |
| 4 | * |
| 5 | * Released under the MIT License: |
| 6 | * https://github.com/ibireme/yyjson/blob/master/LICENSE |
| 7 | *============================================================================*/ |
| 8 | |
| 9 | /** @file yyjson.h */ |
| 10 | |
| 11 | #ifndef YYJSON_H |
| 12 | #define YYJSON_H |
| 13 | |
| 14 | |
| 15 | |
| 16 | /*============================================================================== |
| 17 | * Header Files |
| 18 | *============================================================================*/ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stddef.h> |
| 23 | #include <limits.h> |
| 24 | #include <string.h> |
| 25 | #include <float.h> |
| 26 | |
| 27 | |
| 28 | |
| 29 | /*============================================================================== |
| 30 | * Compile-time Options |
| 31 | *============================================================================*/ |
| 32 | |
| 33 | /* |
| 34 | Define as 1 to disable JSON reader if JSON parsing is not required. |
| 35 | |
| 36 | This will disable these functions at compile-time: |
| 37 | - yyjson_read_opts() |
| 38 | - yyjson_read_file() |
| 39 | - yyjson_read() |
| 40 | - yyjson_read_number() |
| 41 | - yyjson_mut_read_number() |
| 42 | |
| 43 | This will reduce the binary size by about 60%. |
| 44 | */ |
| 45 | #ifndef YYJSON_DISABLE_READER |
| 46 | #endif |
| 47 | |
| 48 | /* |
| 49 | Define as 1 to disable JSON writer if JSON serialization is not required. |
| 50 | |
| 51 | This will disable these functions at compile-time: |
| 52 | - yyjson_write() |
| 53 | - yyjson_write_file() |
| 54 | - yyjson_write_opts() |
| 55 | - yyjson_val_write() |
| 56 | - yyjson_val_write_file() |
| 57 | - yyjson_val_write_opts() |
| 58 | - yyjson_mut_write() |
| 59 | - yyjson_mut_write_file() |
| 60 | - yyjson_mut_write_opts() |
| 61 | - yyjson_mut_val_write() |
| 62 | - yyjson_mut_val_write_file() |
| 63 | - yyjson_mut_val_write_opts() |
| 64 | |
| 65 | This will reduce the binary size by about 30%. |
| 66 | */ |
| 67 | #ifndef YYJSON_DISABLE_WRITER |
| 68 | #endif |
| 69 | |
| 70 | /* |
| 71 | Define as 1 to disable JSON Pointer, JSON Patch and JSON Merge Patch supports. |
| 72 | |
| 73 | This will disable these functions at compile-time: |
| 74 | - yyjson_ptr_xxx() |
| 75 | - yyjson_mut_ptr_xxx() |
| 76 | - yyjson_doc_ptr_xxx() |
| 77 | - yyjson_mut_doc_ptr_xxx() |
| 78 | - yyjson_patch() |
| 79 | - yyjson_mut_patch() |
| 80 | - yyjson_merge_patch() |
| 81 | - yyjson_mut_merge_patch() |
| 82 | */ |
| 83 | #ifndef YYJSON_DISABLE_UTILS |
| 84 | #endif |
| 85 | |
| 86 | /* |
| 87 | Define as 1 to disable the fast floating-point number conversion in yyjson, |
| 88 | and use libc's `strtod/snprintf` instead. |
| 89 | |
| 90 | This will reduce the binary size by about 30%, but significantly slow down the |
| 91 | floating-point read/write speed. |
| 92 | */ |
| 93 | #ifndef YYJSON_DISABLE_FAST_FP_CONV |
| 94 | #endif |
| 95 | |
| 96 | /* |
| 97 | Define as 1 to disable non-standard JSON support at compile-time: |
| 98 | - Reading and writing inf/nan literal, such as `NaN`, `-Infinity`. |
| 99 | - Single line and multiple line comments. |
| 100 | - Single trailing comma at the end of an object or array. |
| 101 | - Invalid unicode in string value. |
| 102 | |
| 103 | This will also invalidate these run-time options: |
| 104 | - YYJSON_READ_ALLOW_INF_AND_NAN |
| 105 | - YYJSON_READ_ALLOW_COMMENTS |
| 106 | - YYJSON_READ_ALLOW_TRAILING_COMMAS |
| 107 | - YYJSON_READ_ALLOW_INVALID_UNICODE |
| 108 | - YYJSON_WRITE_ALLOW_INF_AND_NAN |
| 109 | - YYJSON_WRITE_ALLOW_INVALID_UNICODE |
| 110 | |
| 111 | This will reduce the binary size by about 10%, and slightly improve the JSON |
| 112 | read/write speed. |
| 113 | */ |
| 114 | #ifndef YYJSON_DISABLE_NON_STANDARD |
| 115 | #endif |
| 116 | |
| 117 | /* |
| 118 | Define as 1 to disable unaligned memory access if target architecture does not |
| 119 | support unaligned memory access (such as some embedded processors). |
| 120 | |
| 121 | If this value is not defined, yyjson will perform some automatic detection. |
| 122 | The wrong definition of this option may cause some performance degradation, |
| 123 | but will not cause any run-time errors. |
| 124 | */ |
| 125 | #ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS |
| 126 | #endif |
| 127 | |
| 128 | /* Define as 1 to export symbols when building this library as Windows DLL. */ |
| 129 | #ifndef YYJSON_EXPORTS |
| 130 | #endif |
| 131 | |
| 132 | /* Define as 1 to import symbols when using this library as Windows DLL. */ |
| 133 | #ifndef YYJSON_IMPORTS |
| 134 | #endif |
| 135 | |
| 136 | /* Define as 1 to include <stdint.h> for compiler which doesn't support C99. */ |
| 137 | #ifndef YYJSON_HAS_STDINT_H |
| 138 | #endif |
| 139 | |
| 140 | /* Define as 1 to include <stdbool.h> for compiler which doesn't support C99. */ |
| 141 | #ifndef YYJSON_HAS_STDBOOL_H |
| 142 | #endif |
| 143 | |
| 144 | |
| 145 | |
| 146 | /*============================================================================== |
| 147 | * Compiler Macros |
| 148 | *============================================================================*/ |
| 149 | |
| 150 | /** compiler version (MSVC) */ |
| 151 | #ifdef _MSC_VER |
| 152 | # define YYJSON_MSC_VER _MSC_VER |
| 153 | #else |
| 154 | # define YYJSON_MSC_VER 0 |
| 155 | #endif |
| 156 | |
| 157 | /** compiler version (GCC) */ |
| 158 | #ifdef __GNUC__ |
| 159 | # define YYJSON_GCC_VER __GNUC__ |
| 160 | #else |
| 161 | # define YYJSON_GCC_VER 0 |
| 162 | #endif |
| 163 | |
| 164 | /** C version (STDC) */ |
| 165 | #if defined(__STDC__) && (__STDC__ >= 1) && defined(__STDC_VERSION__) |
| 166 | # define YYJSON_STDC_VER __STDC_VERSION__ |
| 167 | #else |
| 168 | # define YYJSON_STDC_VER 0 |
| 169 | #endif |
| 170 | |
| 171 | /** C++ version */ |
| 172 | #if defined(__cplusplus) |
| 173 | # define YYJSON_CPP_VER __cplusplus |
| 174 | #else |
| 175 | # define YYJSON_CPP_VER 0 |
| 176 | #endif |
| 177 | |
| 178 | /** compiler builtin check (since gcc 10.0, clang 2.6, icc 2021) */ |
| 179 | #ifndef yyjson_has_builtin |
| 180 | # ifdef __has_builtin |
| 181 | # define yyjson_has_builtin(x) __has_builtin(x) |
| 182 | # else |
| 183 | # define yyjson_has_builtin(x) 0 |
| 184 | # endif |
| 185 | #endif |
| 186 | |
| 187 | /** compiler attribute check (since gcc 5.0, clang 2.9, icc 17) */ |
| 188 | #ifndef yyjson_has_attribute |
| 189 | # ifdef __has_attribute |
| 190 | # define yyjson_has_attribute(x) __has_attribute(x) |
| 191 | # else |
| 192 | # define yyjson_has_attribute(x) 0 |
| 193 | # endif |
| 194 | #endif |
| 195 | |
| 196 | /** compiler feature check (since clang 2.6, icc 17) */ |
| 197 | #ifndef yyjson_has_feature |
| 198 | # ifdef __has_feature |
| 199 | # define yyjson_has_feature(x) __has_feature(x) |
| 200 | # else |
| 201 | # define yyjson_has_feature(x) 0 |
| 202 | # endif |
| 203 | #endif |
| 204 | |
| 205 | /** include check (since gcc 5.0, clang 2.7, icc 16, msvc 2017 15.3) */ |
| 206 | #ifndef yyjson_has_include |
| 207 | # ifdef __has_include |
| 208 | # define yyjson_has_include(x) __has_include(x) |
| 209 | # else |
| 210 | # define yyjson_has_include(x) 0 |
| 211 | # endif |
| 212 | #endif |
| 213 | |
| 214 | /** inline for compiler */ |
| 215 | #ifndef yyjson_inline |
| 216 | # if YYJSON_MSC_VER >= 1200 |
| 217 | # define yyjson_inline __forceinline |
| 218 | # elif defined(_MSC_VER) |
| 219 | # define yyjson_inline __inline |
| 220 | # elif yyjson_has_attribute(always_inline) || YYJSON_GCC_VER >= 4 |
| 221 | # define yyjson_inline __inline__ __attribute__((always_inline)) |
| 222 | # elif defined(__clang__) || defined(__GNUC__) |
| 223 | # define yyjson_inline __inline__ |
| 224 | # elif defined(__cplusplus) || YYJSON_STDC_VER >= 199901L |
| 225 | # define yyjson_inline inline |
| 226 | # else |
| 227 | # define yyjson_inline |
| 228 | # endif |
| 229 | #endif |
| 230 | |
| 231 | /** noinline for compiler */ |
| 232 | #ifndef yyjson_noinline |
| 233 | # if YYJSON_MSC_VER >= 1400 |
| 234 | # define yyjson_noinline __declspec(noinline) |
| 235 | # elif yyjson_has_attribute(noinline) || YYJSON_GCC_VER >= 4 |
| 236 | # define yyjson_noinline __attribute__((noinline)) |
| 237 | # else |
| 238 | # define yyjson_noinline |
| 239 | # endif |
| 240 | #endif |
| 241 | |
| 242 | /** align for compiler */ |
| 243 | #ifndef yyjson_align |
| 244 | # if YYJSON_MSC_VER >= 1300 |
| 245 | # define yyjson_align(x) __declspec(align(x)) |
| 246 | # elif yyjson_has_attribute(aligned) || defined(__GNUC__) |
| 247 | # define yyjson_align(x) __attribute__((aligned(x))) |
| 248 | # elif YYJSON_CPP_VER >= 201103L |
| 249 | # define yyjson_align(x) alignas(x) |
| 250 | # else |
| 251 | # define yyjson_align(x) |
| 252 | # endif |
| 253 | #endif |
| 254 | |
| 255 | /** likely for compiler */ |
| 256 | #ifndef yyjson_likely |
| 257 | # if yyjson_has_builtin(__builtin_expect) || \ |
| 258 | (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) |
| 259 | # define yyjson_likely(expr) __builtin_expect(!!(expr), 1) |
| 260 | # else |
| 261 | # define yyjson_likely(expr) (expr) |
| 262 | # endif |
| 263 | #endif |
| 264 | |
| 265 | /** unlikely for compiler */ |
| 266 | #ifndef yyjson_unlikely |
| 267 | # if yyjson_has_builtin(__builtin_expect) || \ |
| 268 | (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) |
| 269 | # define yyjson_unlikely(expr) __builtin_expect(!!(expr), 0) |
| 270 | # else |
| 271 | # define yyjson_unlikely(expr) (expr) |
| 272 | # endif |
| 273 | #endif |
| 274 | |
| 275 | /** compile-time constant check for compiler */ |
| 276 | #ifndef yyjson_constant_p |
| 277 | # if yyjson_has_builtin(__builtin_constant_p) || (YYJSON_GCC_VER >= 3) |
| 278 | # define YYJSON_HAS_CONSTANT_P 1 |
| 279 | # define yyjson_constant_p(value) __builtin_constant_p(value) |
| 280 | # else |
| 281 | # define YYJSON_HAS_CONSTANT_P 0 |
| 282 | # define yyjson_constant_p(value) 0 |
| 283 | # endif |
| 284 | #endif |
| 285 | |
| 286 | /** deprecate warning */ |
| 287 | #ifndef yyjson_deprecated |
| 288 | # if YYJSON_MSC_VER >= 1400 |
| 289 | # define yyjson_deprecated(msg) __declspec(deprecated(msg)) |
| 290 | # elif yyjson_has_feature(attribute_deprecated_with_message) || \ |
| 291 | (YYJSON_GCC_VER > 4 || (YYJSON_GCC_VER == 4 && __GNUC_MINOR__ >= 5)) |
| 292 | # define yyjson_deprecated(msg) __attribute__((deprecated(msg))) |
| 293 | # elif YYJSON_GCC_VER >= 3 |
| 294 | # define yyjson_deprecated(msg) __attribute__((deprecated)) |
| 295 | # else |
| 296 | # define yyjson_deprecated(msg) |
| 297 | # endif |
| 298 | #endif |
| 299 | |
| 300 | /** function export */ |
| 301 | #ifndef yyjson_api |
| 302 | # if defined(_WIN32) |
| 303 | # if defined(YYJSON_EXPORTS) && YYJSON_EXPORTS |
| 304 | # define yyjson_api __declspec(dllexport) |
| 305 | # elif defined(YYJSON_IMPORTS) && YYJSON_IMPORTS |
| 306 | # define yyjson_api __declspec(dllimport) |
| 307 | # else |
| 308 | # define yyjson_api |
| 309 | # endif |
| 310 | # elif yyjson_has_attribute(visibility) || YYJSON_GCC_VER >= 4 |
| 311 | # define yyjson_api __attribute__((visibility("default"))) |
| 312 | # else |
| 313 | # define yyjson_api |
| 314 | # endif |
| 315 | #endif |
| 316 | |
| 317 | /** inline function export */ |
| 318 | #ifndef yyjson_api_inline |
| 319 | # define yyjson_api_inline static yyjson_inline |
| 320 | #endif |
| 321 | |
| 322 | /** stdint (C89 compatible) */ |
| 323 | #if (defined(YYJSON_HAS_STDINT_H) && YYJSON_HAS_STDINT_H) || \ |
| 324 | YYJSON_MSC_VER >= 1600 || YYJSON_STDC_VER >= 199901L || \ |
| 325 | defined(_STDINT_H) || defined(_STDINT_H_) || \ |
| 326 | defined(__CLANG_STDINT_H) || defined(_STDINT_H_INCLUDED) || \ |
| 327 | yyjson_has_include(<stdint.h>) |
| 328 | # include <stdint.h> |
| 329 | #elif defined(_MSC_VER) |
| 330 | # if _MSC_VER < 1300 |
| 331 | typedef signed char int8_t; |
| 332 | typedef signed short int16_t; |
| 333 | typedef signed int int32_t; |
| 334 | typedef unsigned char uint8_t; |
| 335 | typedef unsigned short uint16_t; |
| 336 | typedef unsigned int uint32_t; |
| 337 | typedef signed __int64 int64_t; |
| 338 | typedef unsigned __int64 uint64_t; |
| 339 | # else |
| 340 | typedef signed __int8 int8_t; |
| 341 | typedef signed __int16 int16_t; |
| 342 | typedef signed __int32 int32_t; |
| 343 | typedef unsigned __int8 uint8_t; |
| 344 | typedef unsigned __int16 uint16_t; |
| 345 | typedef unsigned __int32 uint32_t; |
| 346 | typedef signed __int64 int64_t; |
| 347 | typedef unsigned __int64 uint64_t; |
| 348 | # endif |
| 349 | #else |
| 350 | # if UCHAR_MAX == 0xFFU |
| 351 | typedef signed char int8_t; |
| 352 | typedef unsigned char uint8_t; |
| 353 | # else |
| 354 | # error cannot find 8-bit integer type |
| 355 | # endif |
| 356 | # if USHRT_MAX == 0xFFFFU |
| 357 | typedef unsigned short uint16_t; |
| 358 | typedef signed short int16_t; |
| 359 | # elif UINT_MAX == 0xFFFFU |
| 360 | typedef unsigned int uint16_t; |
| 361 | typedef signed int int16_t; |
| 362 | # else |
| 363 | # error cannot find 16-bit integer type |
| 364 | # endif |
| 365 | # if UINT_MAX == 0xFFFFFFFFUL |
| 366 | typedef unsigned int uint32_t; |
| 367 | typedef signed int int32_t; |
| 368 | # elif ULONG_MAX == 0xFFFFFFFFUL |
| 369 | typedef unsigned long uint32_t; |
| 370 | typedef signed long int32_t; |
| 371 | # elif USHRT_MAX == 0xFFFFFFFFUL |
| 372 | typedef unsigned short uint32_t; |
| 373 | typedef signed short int32_t; |
| 374 | # else |
| 375 | # error cannot find 32-bit integer type |
| 376 | # endif |
| 377 | # if defined(__INT64_TYPE__) && defined(__UINT64_TYPE__) |
| 378 | typedef __INT64_TYPE__ int64_t; |
| 379 | typedef __UINT64_TYPE__ uint64_t; |
| 380 | # elif defined(__GNUC__) || defined(__clang__) |
| 381 | # if !defined(_SYS_TYPES_H) && !defined(__int8_t_defined) |
| 382 | __extension__ typedef long long int64_t; |
| 383 | # endif |
| 384 | __extension__ typedef unsigned long long uint64_t; |
| 385 | # elif defined(_LONG_LONG) || defined(__MWERKS__) || defined(_CRAYC) || \ |
| 386 | defined(__SUNPRO_C) || defined(__SUNPRO_CC) |
| 387 | typedef long long int64_t; |
| 388 | typedef unsigned long long uint64_t; |
| 389 | # elif (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || \ |
| 390 | defined(__WATCOM_INT64__) || defined (__alpha) || defined (__DECC) |
| 391 | typedef __int64 int64_t; |
| 392 | typedef unsigned __int64 uint64_t; |
| 393 | # else |
| 394 | # error cannot find 64-bit integer type |
| 395 | # endif |
| 396 | #endif |
| 397 | |
| 398 | /** stdbool (C89 compatible) */ |
| 399 | #if (defined(YYJSON_HAS_STDBOOL_H) && YYJSON_HAS_STDBOOL_H) || \ |
| 400 | (yyjson_has_include(<stdbool.h>) && !defined(__STRICT_ANSI__)) || \ |
| 401 | YYJSON_MSC_VER >= 1800 || YYJSON_STDC_VER >= 199901L |
| 402 | # include <stdbool.h> |
| 403 | #elif !defined(__bool_true_false_are_defined) |
| 404 | # define __bool_true_false_are_defined 1 |
| 405 | # if defined(__cplusplus) |
| 406 | # if defined(__GNUC__) && !defined(__STRICT_ANSI__) |
| 407 | # define _Bool bool |
| 408 | # if __cplusplus < 201103L |
| 409 | # define bool bool |
| 410 | # define false false |
| 411 | # define true true |
| 412 | # endif |
| 413 | # endif |
| 414 | # else |
| 415 | # define bool unsigned char |
| 416 | # define true 1 |
| 417 | # define false 0 |
| 418 | # endif |
| 419 | #endif |
| 420 | |
| 421 | /** char bit check */ |
| 422 | #if defined(CHAR_BIT) |
| 423 | # if CHAR_BIT != 8 |
| 424 | # error non 8-bit char is not supported |
| 425 | # endif |
| 426 | #endif |
| 427 | |
| 428 | /** |
| 429 | Microsoft Visual C++ 6.0 doesn't support converting number from u64 to f64: |
| 430 | error C2520: conversion from unsigned __int64 to double not implemented. |
| 431 | */ |
| 432 | #ifndef YYJSON_U64_TO_F64_NO_IMPL |
| 433 | # if (0 < YYJSON_MSC_VER) && (YYJSON_MSC_VER <= 1200) |
| 434 | # define YYJSON_U64_TO_F64_NO_IMPL 1 |
| 435 | # else |
| 436 | # define YYJSON_U64_TO_F64_NO_IMPL 0 |
| 437 | # endif |
| 438 | #endif |
| 439 | |
| 440 | |
| 441 | |
| 442 | /*============================================================================== |
| 443 | * Compile Hint Begin |
| 444 | *============================================================================*/ |
| 445 | |
| 446 | /* extern "C" begin */ |
| 447 | #ifdef __cplusplus |
| 448 | extern "C" { |
| 449 | #endif |
| 450 | |
| 451 | /* warning suppress begin */ |
| 452 | #if defined(__clang__) |
| 453 | # pragma clang diagnostic push |
| 454 | # pragma clang diagnostic ignored "-Wunused-function" |
| 455 | # pragma clang diagnostic ignored "-Wunused-parameter" |
| 456 | #elif defined(__GNUC__) |
| 457 | # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 458 | # pragma GCC diagnostic push |
| 459 | # endif |
| 460 | # pragma GCC diagnostic ignored "-Wunused-function" |
| 461 | # pragma GCC diagnostic ignored "-Wunused-parameter" |
| 462 | #elif defined(_MSC_VER) |
| 463 | # pragma warning(push) |
| 464 | # pragma warning(disable:4800) /* 'int': forcing value to 'true' or 'false' */ |
| 465 | #endif |
| 466 | |
| 467 | |
| 468 | |
| 469 | /*============================================================================== |
| 470 | * Version |
| 471 | *============================================================================*/ |
| 472 | |
| 473 | /** The major version of yyjson. */ |
| 474 | #define YYJSON_VERSION_MAJOR 0 |
| 475 | |
| 476 | /** The minor version of yyjson. */ |
| 477 | #define YYJSON_VERSION_MINOR 7 |
| 478 | |
| 479 | /** The patch version of yyjson. */ |
| 480 | #define YYJSON_VERSION_PATCH 0 |
| 481 | |
| 482 | /** The version of yyjson in hex: `(major << 16) | (minor << 8) | (patch)`. */ |
| 483 | #define YYJSON_VERSION_HEX 0x000700 |
| 484 | |
| 485 | /** The version string of yyjson. */ |
| 486 | #define YYJSON_VERSION_STRING "0.7.0" |
| 487 | |
| 488 | /** The version of yyjson in hex, same as `YYJSON_VERSION_HEX`. */ |
| 489 | yyjson_api uint32_t yyjson_version(void); |
| 490 | |
| 491 | |
| 492 | |
| 493 | /*============================================================================== |
| 494 | * JSON Types |
| 495 | *============================================================================*/ |
| 496 | |
| 497 | /** Type of a JSON value (3 bit). */ |
| 498 | typedef uint8_t yyjson_type; |
| 499 | /** No type, invalid. */ |
| 500 | #define YYJSON_TYPE_NONE ((uint8_t)0) /* _____000 */ |
| 501 | /** Raw string type, no subtype. */ |
| 502 | #define YYJSON_TYPE_RAW ((uint8_t)1) /* _____001 */ |
| 503 | /** Null type: `null` literal, no subtype. */ |
| 504 | #define YYJSON_TYPE_NULL ((uint8_t)2) /* _____010 */ |
| 505 | /** Boolean type, subtype: TRUE, FALSE. */ |
| 506 | #define YYJSON_TYPE_BOOL ((uint8_t)3) /* _____011 */ |
| 507 | /** Number type, subtype: UINT, SINT, REAL. */ |
| 508 | #define YYJSON_TYPE_NUM ((uint8_t)4) /* _____100 */ |
| 509 | /** String type, subtype: NONE, NOESC. */ |
| 510 | #define YYJSON_TYPE_STR ((uint8_t)5) /* _____101 */ |
| 511 | /** Array type, no subtype. */ |
| 512 | #define YYJSON_TYPE_ARR ((uint8_t)6) /* _____110 */ |
| 513 | /** Object type, no subtype. */ |
| 514 | #define YYJSON_TYPE_OBJ ((uint8_t)7) /* _____111 */ |
| 515 | |
| 516 | /** Subtype of a JSON value (2 bit). */ |
| 517 | typedef uint8_t yyjson_subtype; |
| 518 | /** No subtype. */ |
| 519 | #define YYJSON_SUBTYPE_NONE ((uint8_t)(0 << 3)) /* ___00___ */ |
| 520 | /** False subtype: `false` literal. */ |
| 521 | #define YYJSON_SUBTYPE_FALSE ((uint8_t)(0 << 3)) /* ___00___ */ |
| 522 | /** True subtype: `true` literal. */ |
| 523 | #define YYJSON_SUBTYPE_TRUE ((uint8_t)(1 << 3)) /* ___01___ */ |
| 524 | /** Unsigned integer subtype: `uint64_t`. */ |
| 525 | #define YYJSON_SUBTYPE_UINT ((uint8_t)(0 << 3)) /* ___00___ */ |
| 526 | /** Signed integer subtype: `int64_t`. */ |
| 527 | #define YYJSON_SUBTYPE_SINT ((uint8_t)(1 << 3)) /* ___01___ */ |
| 528 | /** Real number subtype: `double`. */ |
| 529 | #define YYJSON_SUBTYPE_REAL ((uint8_t)(2 << 3)) /* ___10___ */ |
| 530 | /** String that do not need to be escaped for writing (internal use). */ |
| 531 | #define YYJSON_SUBTYPE_NOESC ((uint8_t)(1 << 3)) /* ___01___ */ |
| 532 | |
| 533 | /** The mask used to extract the type of a JSON value. */ |
| 534 | #define YYJSON_TYPE_MASK ((uint8_t)0x07) /* _____111 */ |
| 535 | /** The number of bits used by the type. */ |
| 536 | #define YYJSON_TYPE_BIT ((uint8_t)3) |
| 537 | /** The mask used to extract the subtype of a JSON value. */ |
| 538 | #define YYJSON_SUBTYPE_MASK ((uint8_t)0x18) /* ___11___ */ |
| 539 | /** The number of bits used by the subtype. */ |
| 540 | #define YYJSON_SUBTYPE_BIT ((uint8_t)2) |
| 541 | /** The mask used to extract the reserved bits of a JSON value. */ |
| 542 | #define YYJSON_RESERVED_MASK ((uint8_t)0xE0) /* 111_____ */ |
| 543 | /** The number of reserved bits. */ |
| 544 | #define YYJSON_RESERVED_BIT ((uint8_t)3) |
| 545 | /** The mask used to extract the tag of a JSON value. */ |
| 546 | #define YYJSON_TAG_MASK ((uint8_t)0xFF) /* 11111111 */ |
| 547 | /** The number of bits used by the tag. */ |
| 548 | #define YYJSON_TAG_BIT ((uint8_t)8) |
| 549 | |
| 550 | /** Padding size for JSON reader. */ |
| 551 | #define YYJSON_PADDING_SIZE 4 |
| 552 | |
| 553 | |
| 554 | |
| 555 | /*============================================================================== |
| 556 | * Allocator |
| 557 | *============================================================================*/ |
| 558 | |
| 559 | /** |
| 560 | A memory allocator. |
| 561 | |
| 562 | Typically you don't need to use it, unless you want to customize your own |
| 563 | memory allocator. |
| 564 | */ |
| 565 | typedef struct yyjson_alc { |
| 566 | /** Same as libc's malloc(size), should not be NULL. */ |
| 567 | void *(*malloc)(void *ctx, size_t size); |
| 568 | /** Same as libc's realloc(ptr, size), should not be NULL. */ |
| 569 | void *(*realloc)(void *ctx, void *ptr, size_t old_size, size_t size); |
| 570 | /** Same as libc's free(ptr), should not be NULL. */ |
| 571 | void (*free)(void *ctx, void *ptr); |
| 572 | /** A context for malloc/realloc/free, can be NULL. */ |
| 573 | void *ctx; |
| 574 | } yyjson_alc; |
| 575 | |
| 576 | /** |
| 577 | A pool allocator uses fixed length pre-allocated memory. |
| 578 | |
| 579 | This allocator may be used to avoid malloc/realloc calls. The pre-allocated |
| 580 | memory should be held by the caller. The maximum amount of memory required to |
| 581 | read a JSON can be calculated using the `yyjson_read_max_memory_usage()` |
| 582 | function, but the amount of memory required to write a JSON cannot be directly |
| 583 | calculated. |
| 584 | |
| 585 | This is not a general-purpose allocator. If used to read multiple JSON |
| 586 | documents and only some of them are released, it may cause memory |
| 587 | fragmentation, leading to performance degradation and memory waste. Therefore, |
| 588 | it is recommended to use this allocator only for reading or writing a single |
| 589 | JSON document. |
| 590 | |
| 591 | @param alc The allocator to be initialized. |
| 592 | If this parameter is NULL, the function will fail and return false. |
| 593 | If `buf` or `size` is invalid, this will be set to an empty allocator. |
| 594 | @param buf The buffer memory for this allocator. |
| 595 | If this parameter is NULL, the function will fail and return false. |
| 596 | @param size The size of `buf`, in bytes. |
| 597 | If this parameter is less than 8 words (32/64 bytes on 32/64-bit OS), the |
| 598 | function will fail and return false. |
| 599 | @return true if the `alc` has been successfully initialized. |
| 600 | |
| 601 | @par Example |
| 602 | @code |
| 603 | // parse JSON with stack memory |
| 604 | char buf[1024]; |
| 605 | yyjson_alc alc; |
| 606 | yyjson_alc_pool_init(&alc, buf, 1024); |
| 607 | |
| 608 | const char *json = "{\"name\":\"Helvetica\",\"size\":16}" |
| 609 | yyjson_doc *doc = yyjson_read_opts(json, strlen(json), 0, &alc, NULL); |
| 610 | // the memory of `doc` is on the stack |
| 611 | @endcode |
| 612 | */ |
| 613 | yyjson_api bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, size_t size); |
| 614 | |
| 615 | |
| 616 | |
| 617 | /*============================================================================== |
| 618 | * JSON Structure |
| 619 | *============================================================================*/ |
| 620 | |
| 621 | /** |
| 622 | An immutable document for reading JSON. |
| 623 | This document holds memory for all its JSON values and strings. When it is no |
| 624 | longer used, the user should call `yyjson_doc_free()` to free its memory. |
| 625 | */ |
| 626 | typedef struct yyjson_doc yyjson_doc; |
| 627 | |
| 628 | /** |
| 629 | An immutable value for reading JSON. |
| 630 | A JSON Value has the same lifetime as its document. The memory is held by its |
| 631 | document and and cannot be freed alone. |
| 632 | */ |
| 633 | typedef struct yyjson_val yyjson_val; |
| 634 | |
| 635 | /** |
| 636 | A mutable document for building JSON. |
| 637 | This document holds memory for all its JSON values and strings. When it is no |
| 638 | longer used, the user should call `yyjson_mut_doc_free()` to free its memory. |
| 639 | */ |
| 640 | typedef struct yyjson_mut_doc yyjson_mut_doc; |
| 641 | |
| 642 | /** |
| 643 | A mutable value for building JSON. |
| 644 | A JSON Value has the same lifetime as its document. The memory is held by its |
| 645 | document and and cannot be freed alone. |
| 646 | */ |
| 647 | typedef struct yyjson_mut_val yyjson_mut_val; |
| 648 | |
| 649 | |
| 650 | |
| 651 | /*============================================================================== |
| 652 | * JSON Reader API |
| 653 | *============================================================================*/ |
| 654 | |
| 655 | /** Run-time options for JSON reader. */ |
| 656 | typedef uint32_t yyjson_read_flag; |
| 657 | |
| 658 | /** Default option (RFC 8259 compliant): |
| 659 | - Read positive integer as uint64_t. |
| 660 | - Read negative integer as int64_t. |
| 661 | - Read floating-point number as double with round-to-nearest mode. |
| 662 | - Read integer which cannot fit in uint64_t or int64_t as double. |
| 663 | - Report error if double number is infinity. |
| 664 | - Report error if string contains invalid UTF-8 character or BOM. |
| 665 | - Report error on trailing commas, comments, inf and nan literals. */ |
| 666 | static const yyjson_read_flag YYJSON_READ_NOFLAG = 0 << 0; |
| 667 | |
| 668 | /** Read the input data in-situ. |
| 669 | This option allows the reader to modify and use input data to store string |
| 670 | values, which can increase reading speed slightly. |
| 671 | The caller should hold the input data before free the document. |
| 672 | The input data must be padded by at least `YYJSON_PADDING_SIZE` bytes. |
| 673 | For example: `[1,2]` should be `[1,2]\0\0\0\0`, input length should be 5. */ |
| 674 | static const yyjson_read_flag YYJSON_READ_INSITU = 1 << 0; |
| 675 | |
| 676 | /** Stop when done instead of issuing an error if there's additional content |
| 677 | after a JSON document. This option may be used to parse small pieces of JSON |
| 678 | in larger data, such as `NDJSON`. */ |
| 679 | static const yyjson_read_flag YYJSON_READ_STOP_WHEN_DONE = 1 << 1; |
| 680 | |
| 681 | /** Allow single trailing comma at the end of an object or array, |
| 682 | such as `[1,2,3,]`, `{"a":1,"b":2,}` (non-standard). */ |
| 683 | static const yyjson_read_flag YYJSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2; |
| 684 | |
| 685 | /** Allow C-style single line and multiple line comments (non-standard). */ |
| 686 | static const yyjson_read_flag = 1 << 3; |
| 687 | |
| 688 | /** Allow inf/nan number and literal, case-insensitive, |
| 689 | such as 1e999, NaN, inf, -Infinity (non-standard). */ |
| 690 | static const yyjson_read_flag YYJSON_READ_ALLOW_INF_AND_NAN = 1 << 4; |
| 691 | |
| 692 | /** Read all numbers as raw strings (value with `YYJSON_TYPE_RAW` type), |
| 693 | inf/nan literal is also read as raw with `ALLOW_INF_AND_NAN` flag. */ |
| 694 | static const yyjson_read_flag YYJSON_READ_NUMBER_AS_RAW = 1 << 5; |
| 695 | |
| 696 | /** Allow reading invalid unicode when parsing string values (non-standard). |
| 697 | Invalid characters will be allowed to appear in the string values, but |
| 698 | invalid escape sequences will still be reported as errors. |
| 699 | This flag does not affect the performance of correctly encoded strings. |
| 700 | |
| 701 | @warning Strings in JSON values may contain incorrect encoding when this |
| 702 | option is used, you need to handle these strings carefully to avoid security |
| 703 | risks. */ |
| 704 | static const yyjson_read_flag YYJSON_READ_ALLOW_INVALID_UNICODE = 1 << 6; |
| 705 | |
| 706 | /** Read big numbers as raw strings. These big numbers include integers that |
| 707 | cannot be represented by `int64_t` and `uint64_t`, and floating-point |
| 708 | numbers that cannot be represented by finite `double`. |
| 709 | The flag will be overridden by `YYJSON_READ_NUMBER_AS_RAW` flag. */ |
| 710 | static const yyjson_read_flag YYJSON_READ_BIGNUM_AS_RAW = 1 << 7; |
| 711 | |
| 712 | |
| 713 | |
| 714 | /** Result code for JSON reader. */ |
| 715 | typedef uint32_t yyjson_read_code; |
| 716 | |
| 717 | /** Success, no error. */ |
| 718 | static const yyjson_read_code YYJSON_READ_SUCCESS = 0; |
| 719 | |
| 720 | /** Invalid parameter, such as NULL input string or 0 input length. */ |
| 721 | static const yyjson_read_code YYJSON_READ_ERROR_INVALID_PARAMETER = 1; |
| 722 | |
| 723 | /** Memory allocation failure occurs. */ |
| 724 | static const yyjson_read_code YYJSON_READ_ERROR_MEMORY_ALLOCATION = 2; |
| 725 | |
| 726 | /** Input JSON string is empty. */ |
| 727 | static const yyjson_read_code YYJSON_READ_ERROR_EMPTY_CONTENT = 3; |
| 728 | |
| 729 | /** Unexpected content after document, such as `[123]abc`. */ |
| 730 | static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CONTENT = 4; |
| 731 | |
| 732 | /** Unexpected ending, such as `[123`. */ |
| 733 | static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_END = 5; |
| 734 | |
| 735 | /** Unexpected character inside the document, such as `[abc]`. */ |
| 736 | static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CHARACTER = 6; |
| 737 | |
| 738 | /** Invalid JSON structure, such as `[1,]`. */ |
| 739 | static const yyjson_read_code YYJSON_READ_ERROR_JSON_STRUCTURE = 7; |
| 740 | |
| 741 | /** Invalid comment, such as unclosed multi-line comment. */ |
| 742 | static const yyjson_read_code = 8; |
| 743 | |
| 744 | /** Invalid number, such as `123.e12`, `000`. */ |
| 745 | static const yyjson_read_code YYJSON_READ_ERROR_INVALID_NUMBER = 9; |
| 746 | |
| 747 | /** Invalid string, such as invalid escaped character inside a string. */ |
| 748 | static const yyjson_read_code YYJSON_READ_ERROR_INVALID_STRING = 10; |
| 749 | |
| 750 | /** Invalid JSON literal, such as `truu`. */ |
| 751 | static const yyjson_read_code YYJSON_READ_ERROR_LITERAL = 11; |
| 752 | |
| 753 | /** Failed to open a file. */ |
| 754 | static const yyjson_read_code YYJSON_READ_ERROR_FILE_OPEN = 12; |
| 755 | |
| 756 | /** Failed to read a file. */ |
| 757 | static const yyjson_read_code YYJSON_READ_ERROR_FILE_READ = 13; |
| 758 | |
| 759 | /** Error information for JSON reader. */ |
| 760 | typedef struct yyjson_read_err { |
| 761 | /** Error code, see `yyjson_read_code` for all possible values. */ |
| 762 | yyjson_read_code code; |
| 763 | /** Error message, constant, no need to free (NULL if success). */ |
| 764 | const char *msg; |
| 765 | /** Error byte position for input data (0 if success). */ |
| 766 | size_t pos; |
| 767 | } yyjson_read_err; |
| 768 | |
| 769 | |
| 770 | |
| 771 | /** |
| 772 | Read JSON with options. |
| 773 | |
| 774 | This function is thread-safe when: |
| 775 | 1. The `dat` is not modified by other threads. |
| 776 | 2. The `alc` is thread-safe or NULL. |
| 777 | |
| 778 | @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. |
| 779 | If this parameter is NULL, the function will fail and return NULL. |
| 780 | The `dat` will not be modified without the flag `YYJSON_READ_INSITU`, so you |
| 781 | can pass a `const char *` string and case it to `char *` if you don't use |
| 782 | the `YYJSON_READ_INSITU` flag. |
| 783 | @param len The length of JSON data in bytes. |
| 784 | If this parameter is 0, the function will fail and return NULL. |
| 785 | @param flg The JSON read options. |
| 786 | Multiple options can be combined with `|` operator. 0 means no options. |
| 787 | @param alc The memory allocator used by JSON reader. |
| 788 | Pass NULL to use the libc's default allocator. |
| 789 | @param err A pointer to receive error information. |
| 790 | Pass NULL if you don't need error information. |
| 791 | @return A new JSON document, or NULL if an error occurs. |
| 792 | When it's no longer needed, it should be freed with `yyjson_doc_free()`. |
| 793 | */ |
| 794 | yyjson_api yyjson_doc *yyjson_read_opts(char *dat, |
| 795 | size_t len, |
| 796 | yyjson_read_flag flg, |
| 797 | const yyjson_alc *alc, |
| 798 | yyjson_read_err *err); |
| 799 | |
| 800 | /** |
| 801 | Read a JSON file. |
| 802 | |
| 803 | This function is thread-safe when: |
| 804 | 1. The file is not modified by other threads. |
| 805 | 2. The `alc` is thread-safe or NULL. |
| 806 | |
| 807 | @param path The JSON file's path. |
| 808 | If this path is NULL or invalid, the function will fail and return NULL. |
| 809 | @param flg The JSON read options. |
| 810 | Multiple options can be combined with `|` operator. 0 means no options. |
| 811 | @param alc The memory allocator used by JSON reader. |
| 812 | Pass NULL to use the libc's default allocator. |
| 813 | @param err A pointer to receive error information. |
| 814 | Pass NULL if you don't need error information. |
| 815 | @return A new JSON document, or NULL if an error occurs. |
| 816 | When it's no longer needed, it should be freed with `yyjson_doc_free()`. |
| 817 | |
| 818 | @warning On 32-bit operating system, files larger than 2GB may fail to read. |
| 819 | */ |
| 820 | yyjson_api yyjson_doc *yyjson_read_file(const char *path, |
| 821 | yyjson_read_flag flg, |
| 822 | const yyjson_alc *alc, |
| 823 | yyjson_read_err *err); |
| 824 | |
| 825 | /** |
| 826 | Read JSON from a file pointer. |
| 827 | |
| 828 | @param fp The file pointer. |
| 829 | The data will be read from the current position of the FILE to the end. |
| 830 | If this fp is NULL or invalid, the function will fail and return NULL. |
| 831 | @param flg The JSON read options. |
| 832 | Multiple options can be combined with `|` operator. 0 means no options. |
| 833 | @param alc The memory allocator used by JSON reader. |
| 834 | Pass NULL to use the libc's default allocator. |
| 835 | @param err A pointer to receive error information. |
| 836 | Pass NULL if you don't need error information. |
| 837 | @return A new JSON document, or NULL if an error occurs. |
| 838 | When it's no longer needed, it should be freed with `yyjson_doc_free()`. |
| 839 | |
| 840 | @warning On 32-bit operating system, files larger than 2GB may fail to read. |
| 841 | */ |
| 842 | yyjson_api yyjson_doc *yyjson_read_fp(FILE *fp, |
| 843 | yyjson_read_flag flg, |
| 844 | const yyjson_alc *alc, |
| 845 | yyjson_read_err *err); |
| 846 | |
| 847 | /** |
| 848 | Read a JSON string. |
| 849 | |
| 850 | This function is thread-safe. |
| 851 | |
| 852 | @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. |
| 853 | If this parameter is NULL, the function will fail and return NULL. |
| 854 | @param len The length of JSON data in bytes. |
| 855 | If this parameter is 0, the function will fail and return NULL. |
| 856 | @param flg The JSON read options. |
| 857 | Multiple options can be combined with `|` operator. 0 means no options. |
| 858 | @return A new JSON document, or NULL if an error occurs. |
| 859 | When it's no longer needed, it should be freed with `yyjson_doc_free()`. |
| 860 | */ |
| 861 | yyjson_api_inline yyjson_doc *yyjson_read(const char *dat, |
| 862 | size_t len, |
| 863 | yyjson_read_flag flg) { |
| 864 | flg &= ~YYJSON_READ_INSITU; /* const string cannot be modified */ |
| 865 | return yyjson_read_opts((char *)(void *)(size_t)(const void *)dat, |
| 866 | len, flg, NULL, NULL); |
| 867 | } |
| 868 | |
| 869 | /** |
| 870 | Returns the size of maximum memory usage to read a JSON data. |
| 871 | |
| 872 | You may use this value to avoid malloc() or calloc() call inside the reader |
| 873 | to get better performance, or read multiple JSON with one piece of memory. |
| 874 | |
| 875 | @param len The length of JSON data in bytes. |
| 876 | @param flg The JSON read options. |
| 877 | @return The maximum memory size to read this JSON, or 0 if overflow. |
| 878 | |
| 879 | @par Example |
| 880 | @code |
| 881 | // read multiple JSON with same pre-allocated memory |
| 882 | |
| 883 | char *dat1, *dat2, *dat3; // JSON data |
| 884 | size_t len1, len2, len3; // JSON length |
| 885 | size_t max_len = MAX(len1, MAX(len2, len3)); |
| 886 | yyjson_doc *doc; |
| 887 | |
| 888 | // use one allocator for multiple JSON |
| 889 | size_t size = yyjson_read_max_memory_usage(max_len, 0); |
| 890 | void *buf = malloc(size); |
| 891 | yyjson_alc alc; |
| 892 | yyjson_alc_pool_init(&alc, buf, size); |
| 893 | |
| 894 | // no more alloc() or realloc() call during reading |
| 895 | doc = yyjson_read_opts(dat1, len1, 0, &alc, NULL); |
| 896 | yyjson_doc_free(doc); |
| 897 | doc = yyjson_read_opts(dat2, len2, 0, &alc, NULL); |
| 898 | yyjson_doc_free(doc); |
| 899 | doc = yyjson_read_opts(dat3, len3, 0, &alc, NULL); |
| 900 | yyjson_doc_free(doc); |
| 901 | |
| 902 | free(buf); |
| 903 | @endcode |
| 904 | @see yyjson_alc_pool_init() |
| 905 | */ |
| 906 | yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len, |
| 907 | yyjson_read_flag flg) { |
| 908 | /* |
| 909 | 1. The max value count is (json_size / 2 + 1), |
| 910 | for example: "[1,2,3,4]" size is 9, value count is 5. |
| 911 | 2. Some broken JSON may cost more memory during reading, but fail at end, |
| 912 | for example: "[[[[[[[[". |
| 913 | 3. yyjson use 16 bytes per value, see struct yyjson_val. |
| 914 | 4. yyjson use dynamic memory with a growth factor of 1.5. |
| 915 | |
| 916 | The max memory size is (json_size / 2 * 16 * 1.5 + padding). |
| 917 | */ |
| 918 | size_t mul = (size_t)12 + !(flg & YYJSON_READ_INSITU); |
| 919 | size_t pad = 256; |
| 920 | size_t max = (size_t)(~(size_t)0); |
| 921 | if (flg & YYJSON_READ_STOP_WHEN_DONE) len = len < 256 ? 256 : len; |
| 922 | if (len >= (max - pad - mul) / mul) return 0; |
| 923 | return len * mul + pad; |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | Read a JSON number. |
| 928 | |
| 929 | This function is thread-safe when data is not modified by other threads. |
| 930 | |
| 931 | @param dat The JSON data (UTF-8 without BOM), null-terminator is required. |
| 932 | If this parameter is NULL, the function will fail and return NULL. |
| 933 | @param val The output value where result is stored. |
| 934 | If this parameter is NULL, the function will fail and return NULL. |
| 935 | The value will hold either UINT or SINT or REAL number; |
| 936 | @param flg The JSON read options. |
| 937 | Multiple options can be combined with `|` operator. 0 means no options. |
| 938 | Supports `YYJSON_READ_NUMBER_AS_RAW` and `YYJSON_READ_ALLOW_INF_AND_NAN`. |
| 939 | @param alc The memory allocator used for long number. |
| 940 | It is only used when the built-in floating point reader is disabled. |
| 941 | Pass NULL to use the libc's default allocator. |
| 942 | @param err A pointer to receive error information. |
| 943 | Pass NULL if you don't need error information. |
| 944 | @return If successful, a pointer to the character after the last character |
| 945 | used in the conversion, NULL if an error occurs. |
| 946 | */ |
| 947 | yyjson_api const char *yyjson_read_number(const char *dat, |
| 948 | yyjson_val *val, |
| 949 | yyjson_read_flag flg, |
| 950 | const yyjson_alc *alc, |
| 951 | yyjson_read_err *err); |
| 952 | |
| 953 | /** |
| 954 | Read a JSON number. |
| 955 | |
| 956 | This function is thread-safe when data is not modified by other threads. |
| 957 | |
| 958 | @param dat The JSON data (UTF-8 without BOM), null-terminator is required. |
| 959 | If this parameter is NULL, the function will fail and return NULL. |
| 960 | @param val The output value where result is stored. |
| 961 | If this parameter is NULL, the function will fail and return NULL. |
| 962 | The value will hold either UINT or SINT or REAL number; |
| 963 | @param flg The JSON read options. |
| 964 | Multiple options can be combined with `|` operator. 0 means no options. |
| 965 | Supports `YYJSON_READ_NUMBER_AS_RAW` and `YYJSON_READ_ALLOW_INF_AND_NAN`. |
| 966 | @param alc The memory allocator used for long number. |
| 967 | It is only used when the built-in floating point reader is disabled. |
| 968 | Pass NULL to use the libc's default allocator. |
| 969 | @param err A pointer to receive error information. |
| 970 | Pass NULL if you don't need error information. |
| 971 | @return If successful, a pointer to the character after the last character |
| 972 | used in the conversion, NULL if an error occurs. |
| 973 | */ |
| 974 | yyjson_api_inline const char *yyjson_mut_read_number(const char *dat, |
| 975 | yyjson_mut_val *val, |
| 976 | yyjson_read_flag flg, |
| 977 | const yyjson_alc *alc, |
| 978 | yyjson_read_err *err) { |
| 979 | return yyjson_read_number(dat, (yyjson_val *)val, flg, alc, err); |
| 980 | } |
| 981 | |
| 982 | |
| 983 | /*============================================================================== |
| 984 | * JSON Writer API |
| 985 | *============================================================================*/ |
| 986 | |
| 987 | /** Run-time options for JSON writer. */ |
| 988 | typedef uint32_t yyjson_write_flag; |
| 989 | |
| 990 | /** Default option: |
| 991 | - Write JSON minify. |
| 992 | - Report error on inf or nan number. |
| 993 | - Report error on invalid UTF-8 string. |
| 994 | - Do not escape unicode or slash. */ |
| 995 | static const yyjson_write_flag YYJSON_WRITE_NOFLAG = 0 << 0; |
| 996 | |
| 997 | /** Write JSON pretty with 4 space indent. */ |
| 998 | static const yyjson_write_flag YYJSON_WRITE_PRETTY = 1 << 0; |
| 999 | |
| 1000 | /** Escape unicode as `uXXXX`, make the output ASCII only. */ |
| 1001 | static const yyjson_write_flag YYJSON_WRITE_ESCAPE_UNICODE = 1 << 1; |
| 1002 | |
| 1003 | /** Escape '/' as '\/'. */ |
| 1004 | static const yyjson_write_flag YYJSON_WRITE_ESCAPE_SLASHES = 1 << 2; |
| 1005 | |
| 1006 | /** Write inf and nan number as 'Infinity' and 'NaN' literal (non-standard). */ |
| 1007 | static const yyjson_write_flag YYJSON_WRITE_ALLOW_INF_AND_NAN = 1 << 3; |
| 1008 | |
| 1009 | /** Write inf and nan number as null literal. |
| 1010 | This flag will override `YYJSON_WRITE_ALLOW_INF_AND_NAN` flag. */ |
| 1011 | static const yyjson_write_flag YYJSON_WRITE_INF_AND_NAN_AS_NULL = 1 << 4; |
| 1012 | |
| 1013 | /** Allow invalid unicode when encoding string values (non-standard). |
| 1014 | Invalid characters in string value will be copied byte by byte. |
| 1015 | If `YYJSON_WRITE_ESCAPE_UNICODE` flag is also set, invalid character will be |
| 1016 | escaped as `U+FFFD` (replacement character). |
| 1017 | This flag does not affect the performance of correctly encoded strings. */ |
| 1018 | static const yyjson_write_flag YYJSON_WRITE_ALLOW_INVALID_UNICODE = 1 << 5; |
| 1019 | |
| 1020 | /** Write JSON pretty with 2 space indent. |
| 1021 | This flag will override `YYJSON_WRITE_PRETTY` flag. */ |
| 1022 | static const yyjson_write_flag YYJSON_WRITE_PRETTY_TWO_SPACES = 1 << 6; |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | /** Result code for JSON writer */ |
| 1027 | typedef uint32_t yyjson_write_code; |
| 1028 | |
| 1029 | /** Success, no error. */ |
| 1030 | static const yyjson_write_code YYJSON_WRITE_SUCCESS = 0; |
| 1031 | |
| 1032 | /** Invalid parameter, such as NULL document. */ |
| 1033 | static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_PARAMETER = 1; |
| 1034 | |
| 1035 | /** Memory allocation failure occurs. */ |
| 1036 | static const yyjson_write_code YYJSON_WRITE_ERROR_MEMORY_ALLOCATION = 2; |
| 1037 | |
| 1038 | /** Invalid value type in JSON document. */ |
| 1039 | static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_VALUE_TYPE = 3; |
| 1040 | |
| 1041 | /** NaN or Infinity number occurs. */ |
| 1042 | static const yyjson_write_code YYJSON_WRITE_ERROR_NAN_OR_INF = 4; |
| 1043 | |
| 1044 | /** Failed to open a file. */ |
| 1045 | static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_OPEN = 5; |
| 1046 | |
| 1047 | /** Failed to write a file. */ |
| 1048 | static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_WRITE = 6; |
| 1049 | |
| 1050 | /** Invalid unicode in string. */ |
| 1051 | static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_STRING = 7; |
| 1052 | |
| 1053 | /** Error information for JSON writer. */ |
| 1054 | typedef struct yyjson_write_err { |
| 1055 | /** Error code, see `yyjson_write_code` for all possible values. */ |
| 1056 | yyjson_write_code code; |
| 1057 | /** Error message, constant, no need to free (NULL if success). */ |
| 1058 | const char *msg; |
| 1059 | } yyjson_write_err; |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | /*============================================================================== |
| 1064 | * JSON Document Writer API |
| 1065 | *============================================================================*/ |
| 1066 | |
| 1067 | /** |
| 1068 | Write a document to JSON string with options. |
| 1069 | |
| 1070 | This function is thread-safe when: |
| 1071 | The `alc` is thread-safe or NULL. |
| 1072 | |
| 1073 | @param doc The JSON document. |
| 1074 | If this doc is NULL or has no root, the function will fail and return false. |
| 1075 | @param flg The JSON write options. |
| 1076 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1077 | @param alc The memory allocator used by JSON writer. |
| 1078 | Pass NULL to use the libc's default allocator. |
| 1079 | @param len A pointer to receive output length in bytes (not including the |
| 1080 | null-terminator). Pass NULL if you don't need length information. |
| 1081 | @param err A pointer to receive error information. |
| 1082 | Pass NULL if you don't need error information. |
| 1083 | @return A new JSON string, or NULL if an error occurs. |
| 1084 | This string is encoded as UTF-8 with a null-terminator. |
| 1085 | When it's no longer needed, it should be freed with free() or alc->free(). |
| 1086 | */ |
| 1087 | yyjson_api char *yyjson_write_opts(const yyjson_doc *doc, |
| 1088 | yyjson_write_flag flg, |
| 1089 | const yyjson_alc *alc, |
| 1090 | size_t *len, |
| 1091 | yyjson_write_err *err); |
| 1092 | |
| 1093 | /** |
| 1094 | Write a document to JSON file with options. |
| 1095 | |
| 1096 | This function is thread-safe when: |
| 1097 | 1. The file is not accessed by other threads. |
| 1098 | 2. The `alc` is thread-safe or NULL. |
| 1099 | |
| 1100 | @param path The JSON file's path. |
| 1101 | If this path is NULL or invalid, the function will fail and return false. |
| 1102 | If this file is not empty, the content will be discarded. |
| 1103 | @param doc The JSON document. |
| 1104 | If this doc is NULL or has no root, the function will fail and return false. |
| 1105 | @param flg The JSON write options. |
| 1106 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1107 | @param alc The memory allocator used by JSON writer. |
| 1108 | Pass NULL to use the libc's default allocator. |
| 1109 | @param err A pointer to receive error information. |
| 1110 | Pass NULL if you don't need error information. |
| 1111 | @return true if successful, false if an error occurs. |
| 1112 | |
| 1113 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1114 | */ |
| 1115 | yyjson_api bool yyjson_write_file(const char *path, |
| 1116 | const yyjson_doc *doc, |
| 1117 | yyjson_write_flag flg, |
| 1118 | const yyjson_alc *alc, |
| 1119 | yyjson_write_err *err); |
| 1120 | |
| 1121 | /** |
| 1122 | Write a document to file pointer with options. |
| 1123 | |
| 1124 | @param fp The file pointer. |
| 1125 | The data will be written to the current position of the file. |
| 1126 | If this fp is NULL or invalid, the function will fail and return false. |
| 1127 | @param doc The JSON document. |
| 1128 | If this doc is NULL or has no root, the function will fail and return false. |
| 1129 | @param flg The JSON write options. |
| 1130 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1131 | @param alc The memory allocator used by JSON writer. |
| 1132 | Pass NULL to use the libc's default allocator. |
| 1133 | @param err A pointer to receive error information. |
| 1134 | Pass NULL if you don't need error information. |
| 1135 | @return true if successful, false if an error occurs. |
| 1136 | |
| 1137 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1138 | */ |
| 1139 | yyjson_api bool yyjson_write_fp(FILE *fp, |
| 1140 | const yyjson_doc *doc, |
| 1141 | yyjson_write_flag flg, |
| 1142 | const yyjson_alc *alc, |
| 1143 | yyjson_write_err *err); |
| 1144 | |
| 1145 | /** |
| 1146 | Write a document to JSON string. |
| 1147 | |
| 1148 | This function is thread-safe. |
| 1149 | |
| 1150 | @param doc The JSON document. |
| 1151 | If this doc is NULL or has no root, the function will fail and return false. |
| 1152 | @param flg The JSON write options. |
| 1153 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1154 | @param len A pointer to receive output length in bytes (not including the |
| 1155 | null-terminator). Pass NULL if you don't need length information. |
| 1156 | @return A new JSON string, or NULL if an error occurs. |
| 1157 | This string is encoded as UTF-8 with a null-terminator. |
| 1158 | When it's no longer needed, it should be freed with free(). |
| 1159 | */ |
| 1160 | yyjson_api_inline char *yyjson_write(const yyjson_doc *doc, |
| 1161 | yyjson_write_flag flg, |
| 1162 | size_t *len) { |
| 1163 | return yyjson_write_opts(doc, flg, NULL, len, NULL); |
| 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | /** |
| 1169 | Write a document to JSON string with options. |
| 1170 | |
| 1171 | This function is thread-safe when: |
| 1172 | 1. The `doc` is not modified by other threads. |
| 1173 | 2. The `alc` is thread-safe or NULL. |
| 1174 | |
| 1175 | @param doc The mutable JSON document. |
| 1176 | If this doc is NULL or has no root, the function will fail and return false. |
| 1177 | @param flg The JSON write options. |
| 1178 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1179 | @param alc The memory allocator used by JSON writer. |
| 1180 | Pass NULL to use the libc's default allocator. |
| 1181 | @param len A pointer to receive output length in bytes (not including the |
| 1182 | null-terminator). Pass NULL if you don't need length information. |
| 1183 | @param err A pointer to receive error information. |
| 1184 | Pass NULL if you don't need error information. |
| 1185 | @return A new JSON string, or NULL if an error occurs. |
| 1186 | This string is encoded as UTF-8 with a null-terminator. |
| 1187 | When it's no longer needed, it should be freed with free() or alc->free(). |
| 1188 | */ |
| 1189 | yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, |
| 1190 | yyjson_write_flag flg, |
| 1191 | const yyjson_alc *alc, |
| 1192 | size_t *len, |
| 1193 | yyjson_write_err *err); |
| 1194 | |
| 1195 | /** |
| 1196 | Write a document to JSON file with options. |
| 1197 | |
| 1198 | This function is thread-safe when: |
| 1199 | 1. The file is not accessed by other threads. |
| 1200 | 2. The `doc` is not modified by other threads. |
| 1201 | 3. The `alc` is thread-safe or NULL. |
| 1202 | |
| 1203 | @param path The JSON file's path. |
| 1204 | If this path is NULL or invalid, the function will fail and return false. |
| 1205 | If this file is not empty, the content will be discarded. |
| 1206 | @param doc The mutable JSON document. |
| 1207 | If this doc is NULL or has no root, the function will fail and return false. |
| 1208 | @param flg The JSON write options. |
| 1209 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1210 | @param alc The memory allocator used by JSON writer. |
| 1211 | Pass NULL to use the libc's default allocator. |
| 1212 | @param err A pointer to receive error information. |
| 1213 | Pass NULL if you don't need error information. |
| 1214 | @return true if successful, false if an error occurs. |
| 1215 | |
| 1216 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1217 | */ |
| 1218 | yyjson_api bool yyjson_mut_write_file(const char *path, |
| 1219 | const yyjson_mut_doc *doc, |
| 1220 | yyjson_write_flag flg, |
| 1221 | const yyjson_alc *alc, |
| 1222 | yyjson_write_err *err); |
| 1223 | |
| 1224 | /** |
| 1225 | Write a document to file pointer with options. |
| 1226 | |
| 1227 | @param fp The file pointer. |
| 1228 | The data will be written to the current position of the file. |
| 1229 | If this fp is NULL or invalid, the function will fail and return false. |
| 1230 | @param doc The mutable JSON document. |
| 1231 | If this doc is NULL or has no root, the function will fail and return false. |
| 1232 | @param flg The JSON write options. |
| 1233 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1234 | @param alc The memory allocator used by JSON writer. |
| 1235 | Pass NULL to use the libc's default allocator. |
| 1236 | @param err A pointer to receive error information. |
| 1237 | Pass NULL if you don't need error information. |
| 1238 | @return true if successful, false if an error occurs. |
| 1239 | |
| 1240 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1241 | */ |
| 1242 | yyjson_api bool yyjson_mut_write_fp(FILE *fp, |
| 1243 | const yyjson_mut_doc *doc, |
| 1244 | yyjson_write_flag flg, |
| 1245 | const yyjson_alc *alc, |
| 1246 | yyjson_write_err *err); |
| 1247 | |
| 1248 | /** |
| 1249 | Write a document to JSON string. |
| 1250 | |
| 1251 | This function is thread-safe when: |
| 1252 | The `doc` is not modified by other threads. |
| 1253 | |
| 1254 | @param doc The JSON document. |
| 1255 | If this doc is NULL or has no root, the function will fail and return false. |
| 1256 | @param flg The JSON write options. |
| 1257 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1258 | @param len A pointer to receive output length in bytes (not including the |
| 1259 | null-terminator). Pass NULL if you don't need length information. |
| 1260 | @return A new JSON string, or NULL if an error occurs. |
| 1261 | This string is encoded as UTF-8 with a null-terminator. |
| 1262 | When it's no longer needed, it should be freed with free(). |
| 1263 | */ |
| 1264 | yyjson_api_inline char *yyjson_mut_write(const yyjson_mut_doc *doc, |
| 1265 | yyjson_write_flag flg, |
| 1266 | size_t *len) { |
| 1267 | return yyjson_mut_write_opts(doc, flg, NULL, len, NULL); |
| 1268 | } |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | /*============================================================================== |
| 1273 | * JSON Value Writer API |
| 1274 | *============================================================================*/ |
| 1275 | |
| 1276 | /** |
| 1277 | Write a value to JSON string with options. |
| 1278 | |
| 1279 | This function is thread-safe when: |
| 1280 | The `alc` is thread-safe or NULL. |
| 1281 | |
| 1282 | @param val The JSON root value. |
| 1283 | If this parameter is NULL, the function will fail and return NULL. |
| 1284 | @param flg The JSON write options. |
| 1285 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1286 | @param alc The memory allocator used by JSON writer. |
| 1287 | Pass NULL to use the libc's default allocator. |
| 1288 | @param len A pointer to receive output length in bytes (not including the |
| 1289 | null-terminator). Pass NULL if you don't need length information. |
| 1290 | @param err A pointer to receive error information. |
| 1291 | Pass NULL if you don't need error information. |
| 1292 | @return A new JSON string, or NULL if an error occurs. |
| 1293 | This string is encoded as UTF-8 with a null-terminator. |
| 1294 | When it's no longer needed, it should be freed with free() or alc->free(). |
| 1295 | */ |
| 1296 | yyjson_api char *yyjson_val_write_opts(const yyjson_val *val, |
| 1297 | yyjson_write_flag flg, |
| 1298 | const yyjson_alc *alc, |
| 1299 | size_t *len, |
| 1300 | yyjson_write_err *err); |
| 1301 | |
| 1302 | /** |
| 1303 | Write a value to JSON file with options. |
| 1304 | |
| 1305 | This function is thread-safe when: |
| 1306 | 1. The file is not accessed by other threads. |
| 1307 | 2. The `alc` is thread-safe or NULL. |
| 1308 | |
| 1309 | @param path The JSON file's path. |
| 1310 | If this path is NULL or invalid, the function will fail and return false. |
| 1311 | If this file is not empty, the content will be discarded. |
| 1312 | @param val The JSON root value. |
| 1313 | If this parameter is NULL, the function will fail and return NULL. |
| 1314 | @param flg The JSON write options. |
| 1315 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1316 | @param alc The memory allocator used by JSON writer. |
| 1317 | Pass NULL to use the libc's default allocator. |
| 1318 | @param err A pointer to receive error information. |
| 1319 | Pass NULL if you don't need error information. |
| 1320 | @return true if successful, false if an error occurs. |
| 1321 | |
| 1322 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1323 | */ |
| 1324 | yyjson_api bool yyjson_val_write_file(const char *path, |
| 1325 | const yyjson_val *val, |
| 1326 | yyjson_write_flag flg, |
| 1327 | const yyjson_alc *alc, |
| 1328 | yyjson_write_err *err); |
| 1329 | |
| 1330 | /** |
| 1331 | Write a value to file pointer with options. |
| 1332 | |
| 1333 | @param fp The file pointer. |
| 1334 | The data will be written to the current position of the file. |
| 1335 | If this path is NULL or invalid, the function will fail and return false. |
| 1336 | @param val The JSON root value. |
| 1337 | If this parameter is NULL, the function will fail and return NULL. |
| 1338 | @param flg The JSON write options. |
| 1339 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1340 | @param alc The memory allocator used by JSON writer. |
| 1341 | Pass NULL to use the libc's default allocator. |
| 1342 | @param err A pointer to receive error information. |
| 1343 | Pass NULL if you don't need error information. |
| 1344 | @return true if successful, false if an error occurs. |
| 1345 | |
| 1346 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1347 | */ |
| 1348 | yyjson_api bool yyjson_val_write_fp(FILE *fp, |
| 1349 | const yyjson_val *val, |
| 1350 | yyjson_write_flag flg, |
| 1351 | const yyjson_alc *alc, |
| 1352 | yyjson_write_err *err); |
| 1353 | |
| 1354 | /** |
| 1355 | Write a value to JSON string. |
| 1356 | |
| 1357 | This function is thread-safe. |
| 1358 | |
| 1359 | @param val The JSON root value. |
| 1360 | If this parameter is NULL, the function will fail and return NULL. |
| 1361 | @param flg The JSON write options. |
| 1362 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1363 | @param len A pointer to receive output length in bytes (not including the |
| 1364 | null-terminator). Pass NULL if you don't need length information. |
| 1365 | @return A new JSON string, or NULL if an error occurs. |
| 1366 | This string is encoded as UTF-8 with a null-terminator. |
| 1367 | When it's no longer needed, it should be freed with free(). |
| 1368 | */ |
| 1369 | yyjson_api_inline char *yyjson_val_write(const yyjson_val *val, |
| 1370 | yyjson_write_flag flg, |
| 1371 | size_t *len) { |
| 1372 | return yyjson_val_write_opts(val, flg, NULL, len, NULL); |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | Write a value to JSON string with options. |
| 1377 | |
| 1378 | This function is thread-safe when: |
| 1379 | 1. The `val` is not modified by other threads. |
| 1380 | 2. The `alc` is thread-safe or NULL. |
| 1381 | |
| 1382 | @param val The mutable JSON root value. |
| 1383 | If this parameter is NULL, the function will fail and return NULL. |
| 1384 | @param flg The JSON write options. |
| 1385 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1386 | @param alc The memory allocator used by JSON writer. |
| 1387 | Pass NULL to use the libc's default allocator. |
| 1388 | @param len A pointer to receive output length in bytes (not including the |
| 1389 | null-terminator). Pass NULL if you don't need length information. |
| 1390 | @param err A pointer to receive error information. |
| 1391 | Pass NULL if you don't need error information. |
| 1392 | @return A new JSON string, or NULL if an error occurs. |
| 1393 | This string is encoded as UTF-8 with a null-terminator. |
| 1394 | When it's no longer needed, it should be freed with free() or alc->free(). |
| 1395 | */ |
| 1396 | yyjson_api char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, |
| 1397 | yyjson_write_flag flg, |
| 1398 | const yyjson_alc *alc, |
| 1399 | size_t *len, |
| 1400 | yyjson_write_err *err); |
| 1401 | |
| 1402 | /** |
| 1403 | Write a value to JSON file with options. |
| 1404 | |
| 1405 | This function is thread-safe when: |
| 1406 | 1. The file is not accessed by other threads. |
| 1407 | 2. The `val` is not modified by other threads. |
| 1408 | 3. The `alc` is thread-safe or NULL. |
| 1409 | |
| 1410 | @param path The JSON file's path. |
| 1411 | If this path is NULL or invalid, the function will fail and return false. |
| 1412 | If this file is not empty, the content will be discarded. |
| 1413 | @param val The mutable JSON root value. |
| 1414 | If this parameter is NULL, the function will fail and return NULL. |
| 1415 | @param flg The JSON write options. |
| 1416 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1417 | @param alc The memory allocator used by JSON writer. |
| 1418 | Pass NULL to use the libc's default allocator. |
| 1419 | @param err A pointer to receive error information. |
| 1420 | Pass NULL if you don't need error information. |
| 1421 | @return true if successful, false if an error occurs. |
| 1422 | |
| 1423 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1424 | */ |
| 1425 | yyjson_api bool yyjson_mut_val_write_file(const char *path, |
| 1426 | const yyjson_mut_val *val, |
| 1427 | yyjson_write_flag flg, |
| 1428 | const yyjson_alc *alc, |
| 1429 | yyjson_write_err *err); |
| 1430 | |
| 1431 | /** |
| 1432 | Write a value to JSON file with options. |
| 1433 | |
| 1434 | @param fp The file pointer. |
| 1435 | The data will be written to the current position of the file. |
| 1436 | If this path is NULL or invalid, the function will fail and return false. |
| 1437 | @param val The mutable JSON root value. |
| 1438 | If this parameter is NULL, the function will fail and return NULL. |
| 1439 | @param flg The JSON write options. |
| 1440 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1441 | @param alc The memory allocator used by JSON writer. |
| 1442 | Pass NULL to use the libc's default allocator. |
| 1443 | @param err A pointer to receive error information. |
| 1444 | Pass NULL if you don't need error information. |
| 1445 | @return true if successful, false if an error occurs. |
| 1446 | |
| 1447 | @warning On 32-bit operating system, files larger than 2GB may fail to write. |
| 1448 | */ |
| 1449 | yyjson_api bool yyjson_mut_val_write_fp(FILE *fp, |
| 1450 | const yyjson_mut_val *val, |
| 1451 | yyjson_write_flag flg, |
| 1452 | const yyjson_alc *alc, |
| 1453 | yyjson_write_err *err); |
| 1454 | |
| 1455 | /** |
| 1456 | Write a value to JSON string. |
| 1457 | |
| 1458 | This function is thread-safe when: |
| 1459 | The `val` is not modified by other threads. |
| 1460 | |
| 1461 | @param val The JSON root value. |
| 1462 | If this parameter is NULL, the function will fail and return NULL. |
| 1463 | @param flg The JSON write options. |
| 1464 | Multiple options can be combined with `|` operator. 0 means no options. |
| 1465 | @param len A pointer to receive output length in bytes (not including the |
| 1466 | null-terminator). Pass NULL if you don't need length information. |
| 1467 | @return A new JSON string, or NULL if an error occurs. |
| 1468 | This string is encoded as UTF-8 with a null-terminator. |
| 1469 | When it's no longer needed, it should be freed with free(). |
| 1470 | */ |
| 1471 | yyjson_api_inline char *yyjson_mut_val_write(const yyjson_mut_val *val, |
| 1472 | yyjson_write_flag flg, |
| 1473 | size_t *len) { |
| 1474 | return yyjson_mut_val_write_opts(val, flg, NULL, len, NULL); |
| 1475 | } |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | /*============================================================================== |
| 1480 | * JSON Document API |
| 1481 | *============================================================================*/ |
| 1482 | |
| 1483 | /** Returns the root value of this JSON document. |
| 1484 | Returns NULL if `doc` is NULL. */ |
| 1485 | yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc); |
| 1486 | |
| 1487 | /** Returns read size of input JSON data. |
| 1488 | Returns 0 if `doc` is NULL. |
| 1489 | For example: the read size of `[1,2,3]` is 7 bytes. */ |
| 1490 | yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc); |
| 1491 | |
| 1492 | /** Returns total value count in this JSON document. |
| 1493 | Returns 0 if `doc` is NULL. |
| 1494 | For example: the value count of `[1,2,3]` is 4. */ |
| 1495 | yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc); |
| 1496 | |
| 1497 | /** Release the JSON document and free the memory. |
| 1498 | After calling this function, the `doc` and all values from the `doc` are no |
| 1499 | longer available. This function will do nothing if the `doc` is NULL. */ |
| 1500 | yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc); |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | /*============================================================================== |
| 1505 | * JSON Value Type API |
| 1506 | *============================================================================*/ |
| 1507 | |
| 1508 | /** Returns whether the JSON value is raw. |
| 1509 | Returns false if `val` is NULL. */ |
| 1510 | yyjson_api_inline bool yyjson_is_raw(yyjson_val *val); |
| 1511 | |
| 1512 | /** Returns whether the JSON value is `null`. |
| 1513 | Returns false if `val` is NULL. */ |
| 1514 | yyjson_api_inline bool yyjson_is_null(yyjson_val *val); |
| 1515 | |
| 1516 | /** Returns whether the JSON value is `true`. |
| 1517 | Returns false if `val` is NULL. */ |
| 1518 | yyjson_api_inline bool yyjson_is_true(yyjson_val *val); |
| 1519 | |
| 1520 | /** Returns whether the JSON value is `false`. |
| 1521 | Returns false if `val` is NULL. */ |
| 1522 | yyjson_api_inline bool yyjson_is_false(yyjson_val *val); |
| 1523 | |
| 1524 | /** Returns whether the JSON value is bool (true/false). |
| 1525 | Returns false if `val` is NULL. */ |
| 1526 | yyjson_api_inline bool yyjson_is_bool(yyjson_val *val); |
| 1527 | |
| 1528 | /** Returns whether the JSON value is unsigned integer (uint64_t). |
| 1529 | Returns false if `val` is NULL. */ |
| 1530 | yyjson_api_inline bool yyjson_is_uint(yyjson_val *val); |
| 1531 | |
| 1532 | /** Returns whether the JSON value is signed integer (int64_t). |
| 1533 | Returns false if `val` is NULL. */ |
| 1534 | yyjson_api_inline bool yyjson_is_sint(yyjson_val *val); |
| 1535 | |
| 1536 | /** Returns whether the JSON value is integer (uint64_t/int64_t). |
| 1537 | Returns false if `val` is NULL. */ |
| 1538 | yyjson_api_inline bool yyjson_is_int(yyjson_val *val); |
| 1539 | |
| 1540 | /** Returns whether the JSON value is real number (double). |
| 1541 | Returns false if `val` is NULL. */ |
| 1542 | yyjson_api_inline bool yyjson_is_real(yyjson_val *val); |
| 1543 | |
| 1544 | /** Returns whether the JSON value is number (uint64_t/int64_t/double). |
| 1545 | Returns false if `val` is NULL. */ |
| 1546 | yyjson_api_inline bool yyjson_is_num(yyjson_val *val); |
| 1547 | |
| 1548 | /** Returns whether the JSON value is string. |
| 1549 | Returns false if `val` is NULL. */ |
| 1550 | yyjson_api_inline bool yyjson_is_str(yyjson_val *val); |
| 1551 | |
| 1552 | /** Returns whether the JSON value is array. |
| 1553 | Returns false if `val` is NULL. */ |
| 1554 | yyjson_api_inline bool yyjson_is_arr(yyjson_val *val); |
| 1555 | |
| 1556 | /** Returns whether the JSON value is object. |
| 1557 | Returns false if `val` is NULL. */ |
| 1558 | yyjson_api_inline bool yyjson_is_obj(yyjson_val *val); |
| 1559 | |
| 1560 | /** Returns whether the JSON value is container (array/object). |
| 1561 | Returns false if `val` is NULL. */ |
| 1562 | yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val); |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | /*============================================================================== |
| 1567 | * JSON Value Content API |
| 1568 | *============================================================================*/ |
| 1569 | |
| 1570 | /** Returns the JSON value's type. |
| 1571 | Returns YYJSON_TYPE_NONE if `val` is NULL. */ |
| 1572 | yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val); |
| 1573 | |
| 1574 | /** Returns the JSON value's subtype. |
| 1575 | Returns YYJSON_SUBTYPE_NONE if `val` is NULL. */ |
| 1576 | yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val); |
| 1577 | |
| 1578 | /** Returns the JSON value's tag. |
| 1579 | Returns 0 if `val` is NULL. */ |
| 1580 | yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val); |
| 1581 | |
| 1582 | /** Returns the JSON value's type description. |
| 1583 | The return value should be one of these strings: "raw", "null", "string", |
| 1584 | "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ |
| 1585 | yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val); |
| 1586 | |
| 1587 | /** Returns the content if the value is raw. |
| 1588 | Returns NULL if `val` is NULL or type is not raw. */ |
| 1589 | yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val); |
| 1590 | |
| 1591 | /** Returns the content if the value is bool. |
| 1592 | Returns NULL if `val` is NULL or type is not bool. */ |
| 1593 | yyjson_api_inline bool yyjson_get_bool(yyjson_val *val); |
| 1594 | |
| 1595 | /** Returns the content and cast to uint64_t. |
| 1596 | Returns 0 if `val` is NULL or type is not integer(sint/uint). */ |
| 1597 | yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val); |
| 1598 | |
| 1599 | /** Returns the content and cast to int64_t. |
| 1600 | Returns 0 if `val` is NULL or type is not integer(sint/uint). */ |
| 1601 | yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val); |
| 1602 | |
| 1603 | /** Returns the content and cast to int. |
| 1604 | Returns 0 if `val` is NULL or type is not integer(sint/uint). */ |
| 1605 | yyjson_api_inline int yyjson_get_int(yyjson_val *val); |
| 1606 | |
| 1607 | /** Returns the content if the value is real number, or 0.0 on error. |
| 1608 | Returns 0.0 if `val` is NULL or type is not real(double). */ |
| 1609 | yyjson_api_inline double yyjson_get_real(yyjson_val *val); |
| 1610 | |
| 1611 | /** Returns the content and typecast to `double` if the value is number. |
| 1612 | Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ |
| 1613 | yyjson_api_inline double yyjson_get_num(yyjson_val *val); |
| 1614 | |
| 1615 | /** Returns the content if the value is string. |
| 1616 | Returns NULL if `val` is NULL or type is not string. */ |
| 1617 | yyjson_api_inline const char *yyjson_get_str(yyjson_val *val); |
| 1618 | |
| 1619 | /** Returns the content length (string length, array size, object size. |
| 1620 | Returns 0 if `val` is NULL or type is not string/array/object. */ |
| 1621 | yyjson_api_inline size_t yyjson_get_len(yyjson_val *val); |
| 1622 | |
| 1623 | /** Returns whether the JSON value is equals to a string. |
| 1624 | Returns false if input is NULL or type is not string. */ |
| 1625 | yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str); |
| 1626 | |
| 1627 | /** Returns whether the JSON value is equals to a string. |
| 1628 | The `str` should be a UTF-8 string, null-terminator is not required. |
| 1629 | Returns false if input is NULL or type is not string. */ |
| 1630 | yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, |
| 1631 | size_t len); |
| 1632 | |
| 1633 | /** Returns whether two JSON values are equal (deep compare). |
| 1634 | Returns false if input is NULL. |
| 1635 | @note the result may be inaccurate if object has duplicate keys. |
| 1636 | @warning This function is recursive and may cause a stack overflow |
| 1637 | if the object level is too deep. */ |
| 1638 | yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); |
| 1639 | |
| 1640 | /** Set the value to raw. |
| 1641 | Returns false if input is NULL or `val` is object or array. |
| 1642 | @warning This will modify the `immutable` value, use with caution. */ |
| 1643 | yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, |
| 1644 | const char *raw, size_t len); |
| 1645 | |
| 1646 | /** Set the value to null. |
| 1647 | Returns false if input is NULL or `val` is object or array. |
| 1648 | @warning This will modify the `immutable` value, use with caution. */ |
| 1649 | yyjson_api_inline bool yyjson_set_null(yyjson_val *val); |
| 1650 | |
| 1651 | /** Set the value to bool. |
| 1652 | Returns false if input is NULL or `val` is object or array. |
| 1653 | @warning This will modify the `immutable` value, use with caution. */ |
| 1654 | yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num); |
| 1655 | |
| 1656 | /** Set the value to uint. |
| 1657 | Returns false if input is NULL or `val` is object or array. |
| 1658 | @warning This will modify the `immutable` value, use with caution. */ |
| 1659 | yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num); |
| 1660 | |
| 1661 | /** Set the value to sint. |
| 1662 | Returns false if input is NULL or `val` is object or array. |
| 1663 | @warning This will modify the `immutable` value, use with caution. */ |
| 1664 | yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num); |
| 1665 | |
| 1666 | /** Set the value to int. |
| 1667 | Returns false if input is NULL or `val` is object or array. |
| 1668 | @warning This will modify the `immutable` value, use with caution. */ |
| 1669 | yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num); |
| 1670 | |
| 1671 | /** Set the value to real. |
| 1672 | Returns false if input is NULL or `val` is object or array. |
| 1673 | @warning This will modify the `immutable` value, use with caution. */ |
| 1674 | yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num); |
| 1675 | |
| 1676 | /** Set the value to string (null-terminated). |
| 1677 | Returns false if input is NULL or `val` is object or array. |
| 1678 | @warning This will modify the `immutable` value, use with caution. */ |
| 1679 | yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str); |
| 1680 | |
| 1681 | /** Set the value to string (with length). |
| 1682 | Returns false if input is NULL or `val` is object or array. |
| 1683 | @warning This will modify the `immutable` value, use with caution. */ |
| 1684 | yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, |
| 1685 | const char *str, size_t len); |
| 1686 | |
| 1687 | |
| 1688 | |
| 1689 | /*============================================================================== |
| 1690 | * JSON Array API |
| 1691 | *============================================================================*/ |
| 1692 | |
| 1693 | /** Returns the number of elements in this array. |
| 1694 | Returns 0 if `arr` is NULL or type is not array. */ |
| 1695 | yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr); |
| 1696 | |
| 1697 | /** Returns the element at the specified position in this array. |
| 1698 | Returns NULL if array is NULL/empty or the index is out of bounds. |
| 1699 | @warning This function takes a linear search time if array is not flat. |
| 1700 | For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat. */ |
| 1701 | yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx); |
| 1702 | |
| 1703 | /** Returns the first element of this array. |
| 1704 | Returns NULL if `arr` is NULL/empty or type is not array. */ |
| 1705 | yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr); |
| 1706 | |
| 1707 | /** Returns the last element of this array. |
| 1708 | Returns NULL if `arr` is NULL/empty or type is not array. |
| 1709 | @warning This function takes a linear search time if array is not flat. |
| 1710 | For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat.*/ |
| 1711 | yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr); |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | /*============================================================================== |
| 1716 | * JSON Array Iterator API |
| 1717 | *============================================================================*/ |
| 1718 | |
| 1719 | /** |
| 1720 | A JSON array iterator. |
| 1721 | |
| 1722 | @par Example |
| 1723 | @code |
| 1724 | yyjson_val *val; |
| 1725 | yyjson_arr_iter iter = yyjson_arr_iter_with(arr); |
| 1726 | while ((val = yyjson_arr_iter_next(&iter))) { |
| 1727 | your_func(val); |
| 1728 | } |
| 1729 | @endcode |
| 1730 | */ |
| 1731 | typedef struct yyjson_arr_iter { |
| 1732 | size_t idx; /**< next value's index */ |
| 1733 | size_t max; /**< maximum index (arr.size) */ |
| 1734 | yyjson_val *cur; /**< next value */ |
| 1735 | } yyjson_arr_iter; |
| 1736 | |
| 1737 | /** |
| 1738 | Initialize an iterator for this array. |
| 1739 | |
| 1740 | @param arr The array to be iterated over. |
| 1741 | If this parameter is NULL or not an array, `iter` will be set to empty. |
| 1742 | @param iter The iterator to be initialized. |
| 1743 | If this parameter is NULL, the function will fail and return false. |
| 1744 | @return true if the `iter` has been successfully initialized. |
| 1745 | |
| 1746 | @note The iterator does not need to be destroyed. |
| 1747 | */ |
| 1748 | yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, |
| 1749 | yyjson_arr_iter *iter); |
| 1750 | |
| 1751 | /** |
| 1752 | Create an iterator with an array , same as `yyjson_arr_iter_init()`. |
| 1753 | |
| 1754 | @param arr The array to be iterated over. |
| 1755 | If this parameter is NULL or not an array, an empty iterator will returned. |
| 1756 | @return A new iterator for the array. |
| 1757 | |
| 1758 | @note The iterator does not need to be destroyed. |
| 1759 | */ |
| 1760 | yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr); |
| 1761 | |
| 1762 | /** |
| 1763 | Returns whether the iteration has more elements. |
| 1764 | If `iter` is NULL, this function will return false. |
| 1765 | */ |
| 1766 | yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter); |
| 1767 | |
| 1768 | /** |
| 1769 | Returns the next element in the iteration, or NULL on end. |
| 1770 | If `iter` is NULL, this function will return NULL. |
| 1771 | */ |
| 1772 | yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter); |
| 1773 | |
| 1774 | /** |
| 1775 | Macro for iterating over an array. |
| 1776 | It works like iterator, but with a more intuitive API. |
| 1777 | |
| 1778 | @par Example |
| 1779 | @code |
| 1780 | size_t idx, max; |
| 1781 | yyjson_val *val; |
| 1782 | yyjson_arr_foreach(arr, idx, max, val) { |
| 1783 | your_func(idx, val); |
| 1784 | } |
| 1785 | @endcode |
| 1786 | */ |
| 1787 | #define yyjson_arr_foreach(arr, idx, max, val) \ |
| 1788 | for ((idx) = 0, \ |
| 1789 | (max) = yyjson_arr_size(arr), \ |
| 1790 | (val) = yyjson_arr_get_first(arr); \ |
| 1791 | (idx) < (max); \ |
| 1792 | (idx)++, \ |
| 1793 | (val) = unsafe_yyjson_get_next(val)) |
| 1794 | |
| 1795 | |
| 1796 | |
| 1797 | /*============================================================================== |
| 1798 | * JSON Object API |
| 1799 | *============================================================================*/ |
| 1800 | |
| 1801 | /** Returns the number of key-value pairs in this object. |
| 1802 | Returns 0 if `obj` is NULL or type is not object. */ |
| 1803 | yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj); |
| 1804 | |
| 1805 | /** Returns the value to which the specified key is mapped. |
| 1806 | Returns NULL if this object contains no mapping for the key. |
| 1807 | Returns NULL if `obj/key` is NULL, or type is not object. |
| 1808 | |
| 1809 | The `key` should be a null-terminated UTF-8 string. |
| 1810 | |
| 1811 | @warning This function takes a linear search time. */ |
| 1812 | yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, const char *key); |
| 1813 | |
| 1814 | /** Returns the value to which the specified key is mapped. |
| 1815 | Returns NULL if this object contains no mapping for the key. |
| 1816 | Returns NULL if `obj/key` is NULL, or type is not object. |
| 1817 | |
| 1818 | The `key` should be a UTF-8 string, null-terminator is not required. |
| 1819 | The `key_len` should be the length of the key, in bytes. |
| 1820 | |
| 1821 | @warning This function takes a linear search time. */ |
| 1822 | yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *key, |
| 1823 | size_t key_len); |
| 1824 | |
| 1825 | |
| 1826 | |
| 1827 | /*============================================================================== |
| 1828 | * JSON Object Iterator API |
| 1829 | *============================================================================*/ |
| 1830 | |
| 1831 | /** |
| 1832 | A JSON object iterator. |
| 1833 | |
| 1834 | @par Example |
| 1835 | @code |
| 1836 | yyjson_val *key, *val; |
| 1837 | yyjson_obj_iter iter = yyjson_obj_iter_with(obj); |
| 1838 | while ((key = yyjson_obj_iter_next(&iter))) { |
| 1839 | val = yyjson_obj_iter_get_val(key); |
| 1840 | your_func(key, val); |
| 1841 | } |
| 1842 | @endcode |
| 1843 | |
| 1844 | If the ordering of the keys is known at compile-time, you can use this method |
| 1845 | to speed up value lookups: |
| 1846 | @code |
| 1847 | // {"k1":1, "k2": 3, "k3": 3} |
| 1848 | yyjson_val *key, *val; |
| 1849 | yyjson_obj_iter iter = yyjson_obj_iter_with(obj); |
| 1850 | yyjson_val *v1 = yyjson_obj_iter_get(&iter, "k1"); |
| 1851 | yyjson_val *v3 = yyjson_obj_iter_get(&iter, "k3"); |
| 1852 | @endcode |
| 1853 | @see yyjson_obj_iter_get() and yyjson_obj_iter_getn() |
| 1854 | */ |
| 1855 | typedef struct yyjson_obj_iter { |
| 1856 | size_t idx; /**< next key's index */ |
| 1857 | size_t max; /**< maximum key index (obj.size) */ |
| 1858 | yyjson_val *cur; /**< next key */ |
| 1859 | yyjson_val *obj; /**< the object being iterated */ |
| 1860 | } yyjson_obj_iter; |
| 1861 | |
| 1862 | /** |
| 1863 | Initialize an iterator for this object. |
| 1864 | |
| 1865 | @param obj The object to be iterated over. |
| 1866 | If this parameter is NULL or not an object, `iter` will be set to empty. |
| 1867 | @param iter The iterator to be initialized. |
| 1868 | If this parameter is NULL, the function will fail and return false. |
| 1869 | @return true if the `iter` has been successfully initialized. |
| 1870 | |
| 1871 | @note The iterator does not need to be destroyed. |
| 1872 | */ |
| 1873 | yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, |
| 1874 | yyjson_obj_iter *iter); |
| 1875 | |
| 1876 | /** |
| 1877 | Create an iterator with an object, same as `yyjson_obj_iter_init()`. |
| 1878 | |
| 1879 | @param obj The object to be iterated over. |
| 1880 | If this parameter is NULL or not an object, an empty iterator will returned. |
| 1881 | @return A new iterator for the object. |
| 1882 | |
| 1883 | @note The iterator does not need to be destroyed. |
| 1884 | */ |
| 1885 | yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj); |
| 1886 | |
| 1887 | /** |
| 1888 | Returns whether the iteration has more elements. |
| 1889 | If `iter` is NULL, this function will return false. |
| 1890 | */ |
| 1891 | yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter); |
| 1892 | |
| 1893 | /** |
| 1894 | Returns the next key in the iteration, or NULL on end. |
| 1895 | If `iter` is NULL, this function will return NULL. |
| 1896 | */ |
| 1897 | yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter); |
| 1898 | |
| 1899 | /** |
| 1900 | Returns the value for key inside the iteration. |
| 1901 | If `iter` is NULL, this function will return NULL. |
| 1902 | */ |
| 1903 | yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key); |
| 1904 | |
| 1905 | /** |
| 1906 | Iterates to a specified key and returns the value. |
| 1907 | |
| 1908 | This function does the same thing as `yyjson_obj_get()`, but is much faster |
| 1909 | if the ordering of the keys is known at compile-time and you are using the same |
| 1910 | order to look up the values. If the key exists in this object, then the |
| 1911 | iterator will stop at the next key, otherwise the iterator will not change and |
| 1912 | NULL is returned. |
| 1913 | |
| 1914 | @param iter The object iterator, should not be NULL. |
| 1915 | @param key The key, should be a UTF-8 string with null-terminator. |
| 1916 | @return The value to which the specified key is mapped. |
| 1917 | NULL if this object contains no mapping for the key or input is invalid. |
| 1918 | |
| 1919 | @warning This function takes a linear search time if the key is not nearby. |
| 1920 | */ |
| 1921 | yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, |
| 1922 | const char *key); |
| 1923 | |
| 1924 | /** |
| 1925 | Iterates to a specified key and returns the value. |
| 1926 | |
| 1927 | This function does the same thing as `yyjson_obj_getn()`, but is much faster |
| 1928 | if the ordering of the keys is known at compile-time and you are using the same |
| 1929 | order to look up the values. If the key exists in this object, then the |
| 1930 | iterator will stop at the next key, otherwise the iterator will not change and |
| 1931 | NULL is returned. |
| 1932 | |
| 1933 | @param iter The object iterator, should not be NULL. |
| 1934 | @param key The key, should be a UTF-8 string, null-terminator is not required. |
| 1935 | @param key_len The the length of `key`, in bytes. |
| 1936 | @return The value to which the specified key is mapped. |
| 1937 | NULL if this object contains no mapping for the key or input is invalid. |
| 1938 | |
| 1939 | @warning This function takes a linear search time if the key is not nearby. |
| 1940 | */ |
| 1941 | yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, |
| 1942 | const char *key, |
| 1943 | size_t key_len); |
| 1944 | |
| 1945 | /** |
| 1946 | Macro for iterating over an object. |
| 1947 | It works like iterator, but with a more intuitive API. |
| 1948 | |
| 1949 | @par Example |
| 1950 | @code |
| 1951 | size_t idx, max; |
| 1952 | yyjson_val *key, *val; |
| 1953 | yyjson_obj_foreach(obj, idx, max, key, val) { |
| 1954 | your_func(key, val); |
| 1955 | } |
| 1956 | @endcode |
| 1957 | */ |
| 1958 | #define yyjson_obj_foreach(obj, idx, max, key, val) \ |
| 1959 | for ((idx) = 0, \ |
| 1960 | (max) = yyjson_obj_size(obj), \ |
| 1961 | (key) = (obj) ? unsafe_yyjson_get_first(obj) : NULL, \ |
| 1962 | (val) = (key) + 1; \ |
| 1963 | (idx) < (max); \ |
| 1964 | (idx)++, \ |
| 1965 | (key) = unsafe_yyjson_get_next(val), \ |
| 1966 | (val) = (key) + 1) |
| 1967 | |
| 1968 | |
| 1969 | |
| 1970 | /*============================================================================== |
| 1971 | * Mutable JSON Document API |
| 1972 | *============================================================================*/ |
| 1973 | |
| 1974 | /** Returns the root value of this JSON document. |
| 1975 | Returns NULL if `doc` is NULL. */ |
| 1976 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc); |
| 1977 | |
| 1978 | /** Sets the root value of this JSON document. |
| 1979 | Pass NULL to clear root value of the document. */ |
| 1980 | yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, |
| 1981 | yyjson_mut_val *root); |
| 1982 | |
| 1983 | /** |
| 1984 | Set the string pool size for a mutable document. |
| 1985 | This function does not allocate memory immediately, but uses the size when |
| 1986 | the next memory allocation is needed. |
| 1987 | |
| 1988 | If the caller knows the approximate bytes of strings that the document needs to |
| 1989 | store (e.g. copy string with `yyjson_mut_strcpy` function), setting a larger |
| 1990 | size can avoid multiple memory allocations and improve performance. |
| 1991 | |
| 1992 | @param doc The mutable document. |
| 1993 | @param len The desired string pool size in bytes (total string length). |
| 1994 | @return true if successful, false if size is 0 or overflow. |
| 1995 | */ |
| 1996 | yyjson_api bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, |
| 1997 | size_t len); |
| 1998 | |
| 1999 | /** |
| 2000 | Set the value pool size for a mutable document. |
| 2001 | This function does not allocate memory immediately, but uses the size when |
| 2002 | the next memory allocation is needed. |
| 2003 | |
| 2004 | If the caller knows the approximate number of values that the document needs to |
| 2005 | store (e.g. create new value with `yyjson_mut_xxx` functions), setting a larger |
| 2006 | size can avoid multiple memory allocations and improve performance. |
| 2007 | |
| 2008 | @param doc The mutable document. |
| 2009 | @param count The desired value pool size (number of `yyjson_mut_val`). |
| 2010 | @return true if successful, false if size is 0 or overflow. |
| 2011 | */ |
| 2012 | yyjson_api bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, |
| 2013 | size_t count); |
| 2014 | |
| 2015 | /** Release the JSON document and free the memory. |
| 2016 | After calling this function, the `doc` and all values from the `doc` are no |
| 2017 | longer available. This function will do nothing if the `doc` is NULL. */ |
| 2018 | yyjson_api void yyjson_mut_doc_free(yyjson_mut_doc *doc); |
| 2019 | |
| 2020 | /** Creates and returns a new mutable JSON document, returns NULL on error. |
| 2021 | If allocator is NULL, the default allocator will be used. */ |
| 2022 | yyjson_api yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc); |
| 2023 | |
| 2024 | /** Copies and returns a new mutable document from input, returns NULL on error. |
| 2025 | This makes a `deep-copy` on the immutable document. |
| 2026 | If allocator is NULL, the default allocator will be used. |
| 2027 | @note `imut_doc` -> `mut_doc`. */ |
| 2028 | yyjson_api yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, |
| 2029 | const yyjson_alc *alc); |
| 2030 | |
| 2031 | /** Copies and returns a new mutable document from input, returns NULL on error. |
| 2032 | This makes a `deep-copy` on the mutable document. |
| 2033 | If allocator is NULL, the default allocator will be used. |
| 2034 | @note `mut_doc` -> `mut_doc`. */ |
| 2035 | yyjson_api yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc, |
| 2036 | const yyjson_alc *alc); |
| 2037 | |
| 2038 | /** Copies and returns a new mutable value from input, returns NULL on error. |
| 2039 | This makes a `deep-copy` on the immutable value. |
| 2040 | The memory was managed by mutable document. |
| 2041 | @note `imut_val` -> `mut_val`. */ |
| 2042 | yyjson_api yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *doc, |
| 2043 | yyjson_val *val); |
| 2044 | |
| 2045 | /** Copies and returns a new mutable value from input, returns NULL on error. |
| 2046 | This makes a `deep-copy` on the mutable value. |
| 2047 | The memory was managed by mutable document. |
| 2048 | @note `mut_val` -> `mut_val`. |
| 2049 | @warning This function is recursive and may cause a stack overflow |
| 2050 | if the object level is too deep. */ |
| 2051 | yyjson_api yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, |
| 2052 | yyjson_mut_val *val); |
| 2053 | |
| 2054 | /** Copies and returns a new immutable document from input, |
| 2055 | returns NULL on error. This makes a `deep-copy` on the mutable document. |
| 2056 | The returned document should be freed with `yyjson_doc_free()`. |
| 2057 | @note `mut_doc` -> `imut_doc`. |
| 2058 | @warning This function is recursive and may cause a stack overflow |
| 2059 | if the object level is too deep. */ |
| 2060 | yyjson_api yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *doc, |
| 2061 | const yyjson_alc *alc); |
| 2062 | |
| 2063 | /** Copies and returns a new immutable document from input, |
| 2064 | returns NULL on error. This makes a `deep-copy` on the mutable value. |
| 2065 | The returned document should be freed with `yyjson_doc_free()`. |
| 2066 | @note `mut_val` -> `imut_doc`. |
| 2067 | @warning This function is recursive and may cause a stack overflow |
| 2068 | if the object level is too deep. */ |
| 2069 | yyjson_api yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *val, |
| 2070 | const yyjson_alc *alc); |
| 2071 | |
| 2072 | |
| 2073 | |
| 2074 | /*============================================================================== |
| 2075 | * Mutable JSON Value Type API |
| 2076 | *============================================================================*/ |
| 2077 | |
| 2078 | /** Returns whether the JSON value is raw. |
| 2079 | Returns false if `val` is NULL. */ |
| 2080 | yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val); |
| 2081 | |
| 2082 | /** Returns whether the JSON value is `null`. |
| 2083 | Returns false if `val` is NULL. */ |
| 2084 | yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val); |
| 2085 | |
| 2086 | /** Returns whether the JSON value is `true`. |
| 2087 | Returns false if `val` is NULL. */ |
| 2088 | yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val); |
| 2089 | |
| 2090 | /** Returns whether the JSON value is `false`. |
| 2091 | Returns false if `val` is NULL. */ |
| 2092 | yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val); |
| 2093 | |
| 2094 | /** Returns whether the JSON value is bool (true/false). |
| 2095 | Returns false if `val` is NULL. */ |
| 2096 | yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val); |
| 2097 | |
| 2098 | /** Returns whether the JSON value is unsigned integer (uint64_t). |
| 2099 | Returns false if `val` is NULL. */ |
| 2100 | yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val); |
| 2101 | |
| 2102 | /** Returns whether the JSON value is signed integer (int64_t). |
| 2103 | Returns false if `val` is NULL. */ |
| 2104 | yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val); |
| 2105 | |
| 2106 | /** Returns whether the JSON value is integer (uint64_t/int64_t). |
| 2107 | Returns false if `val` is NULL. */ |
| 2108 | yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val); |
| 2109 | |
| 2110 | /** Returns whether the JSON value is real number (double). |
| 2111 | Returns false if `val` is NULL. */ |
| 2112 | yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val); |
| 2113 | |
| 2114 | /** Returns whether the JSON value is number (uint/sint/real). |
| 2115 | Returns false if `val` is NULL. */ |
| 2116 | yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val); |
| 2117 | |
| 2118 | /** Returns whether the JSON value is string. |
| 2119 | Returns false if `val` is NULL. */ |
| 2120 | yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val); |
| 2121 | |
| 2122 | /** Returns whether the JSON value is array. |
| 2123 | Returns false if `val` is NULL. */ |
| 2124 | yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val); |
| 2125 | |
| 2126 | /** Returns whether the JSON value is object. |
| 2127 | Returns false if `val` is NULL. */ |
| 2128 | yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val); |
| 2129 | |
| 2130 | /** Returns whether the JSON value is container (array/object). |
| 2131 | Returns false if `val` is NULL. */ |
| 2132 | yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val); |
| 2133 | |
| 2134 | |
| 2135 | |
| 2136 | /*============================================================================== |
| 2137 | * Mutable JSON Value Content API |
| 2138 | *============================================================================*/ |
| 2139 | |
| 2140 | /** Returns the JSON value's type. |
| 2141 | Returns `YYJSON_TYPE_NONE` if `val` is NULL. */ |
| 2142 | yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val); |
| 2143 | |
| 2144 | /** Returns the JSON value's subtype. |
| 2145 | Returns `YYJSON_SUBTYPE_NONE` if `val` is NULL. */ |
| 2146 | yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val); |
| 2147 | |
| 2148 | /** Returns the JSON value's tag. |
| 2149 | Returns 0 if `val` is NULL. */ |
| 2150 | yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val); |
| 2151 | |
| 2152 | /** Returns the JSON value's type description. |
| 2153 | The return value should be one of these strings: "raw", "null", "string", |
| 2154 | "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ |
| 2155 | yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val); |
| 2156 | |
| 2157 | /** Returns the content if the value is raw. |
| 2158 | Returns NULL if `val` is NULL or type is not raw. */ |
| 2159 | yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val); |
| 2160 | |
| 2161 | /** Returns the content if the value is bool. |
| 2162 | Returns NULL if `val` is NULL or type is not bool. */ |
| 2163 | yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val); |
| 2164 | |
| 2165 | /** Returns the content and cast to uint64_t. |
| 2166 | Returns 0 if `val` is NULL or type is not integer(sint/uint). */ |
| 2167 | yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val); |
| 2168 | |
| 2169 | /** Returns the content and cast to int64_t. |
| 2170 | Returns 0 if `val` is NULL or type is not integer(sint/uint). */ |
| 2171 | yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val); |
| 2172 | |
| 2173 | /** Returns the content and cast to int. |
| 2174 | Returns 0 if `val` is NULL or type is not integer(sint/uint). */ |
| 2175 | yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val); |
| 2176 | |
| 2177 | /** Returns the content if the value is real number. |
| 2178 | Returns 0.0 if `val` is NULL or type is not real(double). */ |
| 2179 | yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val); |
| 2180 | |
| 2181 | /** Returns the content and typecast to `double` if the value is number. |
| 2182 | Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ |
| 2183 | yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val); |
| 2184 | |
| 2185 | /** Returns the content if the value is string. |
| 2186 | Returns NULL if `val` is NULL or type is not string. */ |
| 2187 | yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val); |
| 2188 | |
| 2189 | /** Returns the content length (string length, array size, object size. |
| 2190 | Returns 0 if `val` is NULL or type is not string/array/object. */ |
| 2191 | yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val); |
| 2192 | |
| 2193 | /** Returns whether the JSON value is equals to a string. |
| 2194 | The `str` should be a null-terminated UTF-8 string. |
| 2195 | Returns false if input is NULL or type is not string. */ |
| 2196 | yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, |
| 2197 | const char *str); |
| 2198 | |
| 2199 | /** Returns whether the JSON value is equals to a string. |
| 2200 | The `str` should be a UTF-8 string, null-terminator is not required. |
| 2201 | Returns false if input is NULL or type is not string. */ |
| 2202 | yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, |
| 2203 | const char *str, size_t len); |
| 2204 | |
| 2205 | /** Returns whether two JSON values are equal (deep compare). |
| 2206 | Returns false if input is NULL. |
| 2207 | @note the result may be inaccurate if object has duplicate keys. |
| 2208 | @warning This function is recursive and may cause a stack overflow |
| 2209 | if the object level is too deep. */ |
| 2210 | yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, |
| 2211 | yyjson_mut_val *rhs); |
| 2212 | |
| 2213 | /** Set the value to raw. |
| 2214 | Returns false if input is NULL. |
| 2215 | @warning This function should not be used on an existing object or array. */ |
| 2216 | yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, |
| 2217 | const char *raw, size_t len); |
| 2218 | |
| 2219 | /** Set the value to null. |
| 2220 | Returns false if input is NULL. |
| 2221 | @warning This function should not be used on an existing object or array. */ |
| 2222 | yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val); |
| 2223 | |
| 2224 | /** Set the value to bool. |
| 2225 | Returns false if input is NULL. |
| 2226 | @warning This function should not be used on an existing object or array. */ |
| 2227 | yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num); |
| 2228 | |
| 2229 | /** Set the value to uint. |
| 2230 | Returns false if input is NULL. |
| 2231 | @warning This function should not be used on an existing object or array. */ |
| 2232 | yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num); |
| 2233 | |
| 2234 | /** Set the value to sint. |
| 2235 | Returns false if input is NULL. |
| 2236 | @warning This function should not be used on an existing object or array. */ |
| 2237 | yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num); |
| 2238 | |
| 2239 | /** Set the value to int. |
| 2240 | Returns false if input is NULL. |
| 2241 | @warning This function should not be used on an existing object or array. */ |
| 2242 | yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num); |
| 2243 | |
| 2244 | /** Set the value to real. |
| 2245 | Returns false if input is NULL. |
| 2246 | @warning This function should not be used on an existing object or array. */ |
| 2247 | yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num); |
| 2248 | |
| 2249 | /** Set the value to string (null-terminated). |
| 2250 | Returns false if input is NULL. |
| 2251 | @warning This function should not be used on an existing object or array. */ |
| 2252 | yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, const char *str); |
| 2253 | |
| 2254 | /** Set the value to string (with length). |
| 2255 | Returns false if input is NULL. |
| 2256 | @warning This function should not be used on an existing object or array. */ |
| 2257 | yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, |
| 2258 | const char *str, size_t len); |
| 2259 | |
| 2260 | /** Set the value to array. |
| 2261 | Returns false if input is NULL. |
| 2262 | @warning This function should not be used on an existing object or array. */ |
| 2263 | yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val); |
| 2264 | |
| 2265 | /** Set the value to array. |
| 2266 | Returns false if input is NULL. |
| 2267 | @warning This function should not be used on an existing object or array. */ |
| 2268 | yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val); |
| 2269 | |
| 2270 | |
| 2271 | |
| 2272 | /*============================================================================== |
| 2273 | * Mutable JSON Value Creation API |
| 2274 | *============================================================================*/ |
| 2275 | |
| 2276 | /** Creates and returns a raw value, returns NULL on error. |
| 2277 | The `str` should be a null-terminated UTF-8 string. |
| 2278 | |
| 2279 | @warning The input string is not copied, you should keep this string |
| 2280 | unmodified for the lifetime of this JSON document. */ |
| 2281 | yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, |
| 2282 | const char *str); |
| 2283 | |
| 2284 | /** Creates and returns a raw value, returns NULL on error. |
| 2285 | The `str` should be a UTF-8 string, null-terminator is not required. |
| 2286 | |
| 2287 | @warning The input string is not copied, you should keep this string |
| 2288 | unmodified for the lifetime of this JSON document. */ |
| 2289 | yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, |
| 2290 | const char *str, |
| 2291 | size_t len); |
| 2292 | |
| 2293 | /** Creates and returns a raw value, returns NULL on error. |
| 2294 | The `str` should be a null-terminated UTF-8 string. |
| 2295 | The input string is copied and held by the document. */ |
| 2296 | yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, |
| 2297 | const char *str); |
| 2298 | |
| 2299 | /** Creates and returns a raw value, returns NULL on error. |
| 2300 | The `str` should be a UTF-8 string, null-terminator is not required. |
| 2301 | The input string is copied and held by the document. */ |
| 2302 | yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, |
| 2303 | const char *str, |
| 2304 | size_t len); |
| 2305 | |
| 2306 | /** Creates and returns a null value, returns NULL on error. */ |
| 2307 | yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc); |
| 2308 | |
| 2309 | /** Creates and returns a true value, returns NULL on error. */ |
| 2310 | yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc); |
| 2311 | |
| 2312 | /** Creates and returns a false value, returns NULL on error. */ |
| 2313 | yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc); |
| 2314 | |
| 2315 | /** Creates and returns a bool value, returns NULL on error. */ |
| 2316 | yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, |
| 2317 | bool val); |
| 2318 | |
| 2319 | /** Creates and returns an unsigned integer value, returns NULL on error. */ |
| 2320 | yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, |
| 2321 | uint64_t num); |
| 2322 | |
| 2323 | /** Creates and returns a signed integer value, returns NULL on error. */ |
| 2324 | yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, |
| 2325 | int64_t num); |
| 2326 | |
| 2327 | /** Creates and returns a signed integer value, returns NULL on error. */ |
| 2328 | yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, |
| 2329 | int64_t num); |
| 2330 | |
| 2331 | /** Creates and returns an real number value, returns NULL on error. */ |
| 2332 | yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, |
| 2333 | double num); |
| 2334 | |
| 2335 | /** Creates and returns a string value, returns NULL on error. |
| 2336 | The `str` should be a null-terminated UTF-8 string. |
| 2337 | @warning The input string is not copied, you should keep this string |
| 2338 | unmodified for the lifetime of this JSON document. */ |
| 2339 | yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, |
| 2340 | const char *str); |
| 2341 | |
| 2342 | /** Creates and returns a string value, returns NULL on error. |
| 2343 | The `str` should be a UTF-8 string, null-terminator is not required. |
| 2344 | @warning The input string is not copied, you should keep this string |
| 2345 | unmodified for the lifetime of this JSON document. */ |
| 2346 | yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, |
| 2347 | const char *str, |
| 2348 | size_t len); |
| 2349 | |
| 2350 | /** Creates and returns a string value, returns NULL on error. |
| 2351 | The `str` should be a null-terminated UTF-8 string. |
| 2352 | The input string is copied and held by the document. */ |
| 2353 | yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, |
| 2354 | const char *str); |
| 2355 | |
| 2356 | /** Creates and returns a string value, returns NULL on error. |
| 2357 | The `str` should be a UTF-8 string, null-terminator is not required. |
| 2358 | The input string is copied and held by the document. */ |
| 2359 | yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, |
| 2360 | const char *str, |
| 2361 | size_t len); |
| 2362 | |
| 2363 | |
| 2364 | |
| 2365 | /*============================================================================== |
| 2366 | * Mutable JSON Array API |
| 2367 | *============================================================================*/ |
| 2368 | |
| 2369 | /** Returns the number of elements in this array. |
| 2370 | Returns 0 if `arr` is NULL or type is not array. */ |
| 2371 | yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr); |
| 2372 | |
| 2373 | /** Returns the element at the specified position in this array. |
| 2374 | Returns NULL if array is NULL/empty or the index is out of bounds. |
| 2375 | @warning This function takes a linear search time. */ |
| 2376 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, |
| 2377 | size_t idx); |
| 2378 | |
| 2379 | /** Returns the first element of this array. |
| 2380 | Returns NULL if `arr` is NULL/empty or type is not array. */ |
| 2381 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first(yyjson_mut_val *arr); |
| 2382 | |
| 2383 | /** Returns the last element of this array. |
| 2384 | Returns NULL if `arr` is NULL/empty or type is not array. */ |
| 2385 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last(yyjson_mut_val *arr); |
| 2386 | |
| 2387 | |
| 2388 | |
| 2389 | /*============================================================================== |
| 2390 | * Mutable JSON Array Iterator API |
| 2391 | *============================================================================*/ |
| 2392 | |
| 2393 | /** |
| 2394 | A mutable JSON array iterator. |
| 2395 | |
| 2396 | @warning You should not modify the array while iterating over it, but you can |
| 2397 | use `yyjson_mut_arr_iter_remove()` to remove current value. |
| 2398 | |
| 2399 | @par Example |
| 2400 | @code |
| 2401 | yyjson_mut_val *val; |
| 2402 | yyjson_mut_arr_iter iter = yyjson_mut_arr_iter_with(arr); |
| 2403 | while ((val = yyjson_mut_arr_iter_next(&iter))) { |
| 2404 | your_func(val); |
| 2405 | if (your_val_is_unused(val)) { |
| 2406 | yyjson_mut_arr_iter_remove(&iter); |
| 2407 | } |
| 2408 | } |
| 2409 | @endcode |
| 2410 | */ |
| 2411 | typedef struct yyjson_mut_arr_iter { |
| 2412 | size_t idx; /**< next value's index */ |
| 2413 | size_t max; /**< maximum index (arr.size) */ |
| 2414 | yyjson_mut_val *cur; /**< current value */ |
| 2415 | yyjson_mut_val *pre; /**< previous value */ |
| 2416 | yyjson_mut_val *arr; /**< the array being iterated */ |
| 2417 | } yyjson_mut_arr_iter; |
| 2418 | |
| 2419 | /** |
| 2420 | Initialize an iterator for this array. |
| 2421 | |
| 2422 | @param arr The array to be iterated over. |
| 2423 | If this parameter is NULL or not an array, `iter` will be set to empty. |
| 2424 | @param iter The iterator to be initialized. |
| 2425 | If this parameter is NULL, the function will fail and return false. |
| 2426 | @return true if the `iter` has been successfully initialized. |
| 2427 | |
| 2428 | @note The iterator does not need to be destroyed. |
| 2429 | */ |
| 2430 | yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, |
| 2431 | yyjson_mut_arr_iter *iter); |
| 2432 | |
| 2433 | /** |
| 2434 | Create an iterator with an array , same as `yyjson_mut_arr_iter_init()`. |
| 2435 | |
| 2436 | @param arr The array to be iterated over. |
| 2437 | If this parameter is NULL or not an array, an empty iterator will returned. |
| 2438 | @return A new iterator for the array. |
| 2439 | |
| 2440 | @note The iterator does not need to be destroyed. |
| 2441 | */ |
| 2442 | yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( |
| 2443 | yyjson_mut_val *arr); |
| 2444 | |
| 2445 | /** |
| 2446 | Returns whether the iteration has more elements. |
| 2447 | If `iter` is NULL, this function will return false. |
| 2448 | */ |
| 2449 | yyjson_api_inline bool yyjson_mut_arr_iter_has_next( |
| 2450 | yyjson_mut_arr_iter *iter); |
| 2451 | |
| 2452 | /** |
| 2453 | Returns the next element in the iteration, or NULL on end. |
| 2454 | If `iter` is NULL, this function will return NULL. |
| 2455 | */ |
| 2456 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( |
| 2457 | yyjson_mut_arr_iter *iter); |
| 2458 | |
| 2459 | /** |
| 2460 | Removes and returns current element in the iteration. |
| 2461 | If `iter` is NULL, this function will return NULL. |
| 2462 | */ |
| 2463 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( |
| 2464 | yyjson_mut_arr_iter *iter); |
| 2465 | |
| 2466 | /** |
| 2467 | Macro for iterating over an array. |
| 2468 | It works like iterator, but with a more intuitive API. |
| 2469 | |
| 2470 | @warning You should not modify the array while iterating over it. |
| 2471 | |
| 2472 | @par Example |
| 2473 | @code |
| 2474 | size_t idx, max; |
| 2475 | yyjson_mut_val *val; |
| 2476 | yyjson_mut_arr_foreach(arr, idx, max, val) { |
| 2477 | your_func(idx, val); |
| 2478 | } |
| 2479 | @endcode |
| 2480 | */ |
| 2481 | #define yyjson_mut_arr_foreach(arr, idx, max, val) \ |
| 2482 | for ((idx) = 0, \ |
| 2483 | (max) = yyjson_mut_arr_size(arr), \ |
| 2484 | (val) = yyjson_mut_arr_get_first(arr); \ |
| 2485 | (idx) < (max); \ |
| 2486 | (idx)++, \ |
| 2487 | (val) = (val)->next) |
| 2488 | |
| 2489 | |
| 2490 | |
| 2491 | /*============================================================================== |
| 2492 | * Mutable JSON Array Creation API |
| 2493 | *============================================================================*/ |
| 2494 | |
| 2495 | /** |
| 2496 | Creates and returns an empty mutable array. |
| 2497 | @param doc A mutable document, used for memory allocation only. |
| 2498 | @return The new array. NULL if input is NULL or memory allocation failed. |
| 2499 | */ |
| 2500 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc); |
| 2501 | |
| 2502 | /** |
| 2503 | Creates and returns a new mutable array with the given boolean values. |
| 2504 | |
| 2505 | @param doc A mutable document, used for memory allocation only. |
| 2506 | If this parameter is NULL, the function will fail and return NULL. |
| 2507 | @param vals A C array of boolean values. |
| 2508 | @param count The value count. If this value is 0, an empty array will return. |
| 2509 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2510 | |
| 2511 | @par Example |
| 2512 | @code |
| 2513 | const bool vals[3] = { true, false, true }; |
| 2514 | yyjson_mut_val *arr = yyjson_mut_arr_with_bool(doc, vals, 3); |
| 2515 | @endcode |
| 2516 | */ |
| 2517 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( |
| 2518 | yyjson_mut_doc *doc, const bool *vals, size_t count); |
| 2519 | |
| 2520 | /** |
| 2521 | Creates and returns a new mutable array with the given sint numbers. |
| 2522 | |
| 2523 | @param doc A mutable document, used for memory allocation only. |
| 2524 | If this parameter is NULL, the function will fail and return NULL. |
| 2525 | @param vals A C array of sint numbers. |
| 2526 | @param count The number count. If this value is 0, an empty array will return. |
| 2527 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2528 | |
| 2529 | @par Example |
| 2530 | @code |
| 2531 | const int64_t vals[3] = { -1, 0, 1 }; |
| 2532 | yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); |
| 2533 | @endcode |
| 2534 | */ |
| 2535 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( |
| 2536 | yyjson_mut_doc *doc, const int64_t *vals, size_t count); |
| 2537 | |
| 2538 | /** |
| 2539 | Creates and returns a new mutable array with the given uint numbers. |
| 2540 | |
| 2541 | @param doc A mutable document, used for memory allocation only. |
| 2542 | If this parameter is NULL, the function will fail and return NULL. |
| 2543 | @param vals A C array of uint numbers. |
| 2544 | @param count The number count. If this value is 0, an empty array will return. |
| 2545 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2546 | |
| 2547 | @par Example |
| 2548 | @code |
| 2549 | const uint64_t vals[3] = { 0, 1, 0 }; |
| 2550 | yyjson_mut_val *arr = yyjson_mut_arr_with_uint(doc, vals, 3); |
| 2551 | @endcode |
| 2552 | */ |
| 2553 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( |
| 2554 | yyjson_mut_doc *doc, const uint64_t *vals, size_t count); |
| 2555 | |
| 2556 | /** |
| 2557 | Creates and returns a new mutable array with the given real numbers. |
| 2558 | |
| 2559 | @param doc A mutable document, used for memory allocation only. |
| 2560 | If this parameter is NULL, the function will fail and return NULL. |
| 2561 | @param vals A C array of real numbers. |
| 2562 | @param count The number count. If this value is 0, an empty array will return. |
| 2563 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2564 | |
| 2565 | @par Example |
| 2566 | @code |
| 2567 | const double vals[3] = { 0.1, 0.2, 0.3 }; |
| 2568 | yyjson_mut_val *arr = yyjson_mut_arr_with_real(doc, vals, 3); |
| 2569 | @endcode |
| 2570 | */ |
| 2571 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( |
| 2572 | yyjson_mut_doc *doc, const double *vals, size_t count); |
| 2573 | |
| 2574 | /** |
| 2575 | Creates and returns a new mutable array with the given int8 numbers. |
| 2576 | |
| 2577 | @param doc A mutable document, used for memory allocation only. |
| 2578 | If this parameter is NULL, the function will fail and return NULL. |
| 2579 | @param vals A C array of int8 numbers. |
| 2580 | @param count The number count. If this value is 0, an empty array will return. |
| 2581 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2582 | |
| 2583 | @par Example |
| 2584 | @code |
| 2585 | const int8_t vals[3] = { -1, 0, 1 }; |
| 2586 | yyjson_mut_val *arr = yyjson_mut_arr_with_sint8(doc, vals, 3); |
| 2587 | @endcode |
| 2588 | */ |
| 2589 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( |
| 2590 | yyjson_mut_doc *doc, const int8_t *vals, size_t count); |
| 2591 | |
| 2592 | /** |
| 2593 | Creates and returns a new mutable array with the given int16 numbers. |
| 2594 | |
| 2595 | @param doc A mutable document, used for memory allocation only. |
| 2596 | If this parameter is NULL, the function will fail and return NULL. |
| 2597 | @param vals A C array of int16 numbers. |
| 2598 | @param count The number count. If this value is 0, an empty array will return. |
| 2599 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2600 | |
| 2601 | @par Example |
| 2602 | @code |
| 2603 | const int16_t vals[3] = { -1, 0, 1 }; |
| 2604 | yyjson_mut_val *arr = yyjson_mut_arr_with_sint16(doc, vals, 3); |
| 2605 | @endcode |
| 2606 | */ |
| 2607 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( |
| 2608 | yyjson_mut_doc *doc, const int16_t *vals, size_t count); |
| 2609 | |
| 2610 | /** |
| 2611 | Creates and returns a new mutable array with the given int32 numbers. |
| 2612 | |
| 2613 | @param doc A mutable document, used for memory allocation only. |
| 2614 | If this parameter is NULL, the function will fail and return NULL. |
| 2615 | @param vals A C array of int32 numbers. |
| 2616 | @param count The number count. If this value is 0, an empty array will return. |
| 2617 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2618 | |
| 2619 | @par Example |
| 2620 | @code |
| 2621 | const int32_t vals[3] = { -1, 0, 1 }; |
| 2622 | yyjson_mut_val *arr = yyjson_mut_arr_with_sint32(doc, vals, 3); |
| 2623 | @endcode |
| 2624 | */ |
| 2625 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( |
| 2626 | yyjson_mut_doc *doc, const int32_t *vals, size_t count); |
| 2627 | |
| 2628 | /** |
| 2629 | Creates and returns a new mutable array with the given int64 numbers. |
| 2630 | |
| 2631 | @param doc A mutable document, used for memory allocation only. |
| 2632 | If this parameter is NULL, the function will fail and return NULL. |
| 2633 | @param vals A C array of int64 numbers. |
| 2634 | @param count The number count. If this value is 0, an empty array will return. |
| 2635 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2636 | |
| 2637 | @par Example |
| 2638 | @code |
| 2639 | const int64_t vals[3] = { -1, 0, 1 }; |
| 2640 | yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); |
| 2641 | @endcode |
| 2642 | */ |
| 2643 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( |
| 2644 | yyjson_mut_doc *doc, const int64_t *vals, size_t count); |
| 2645 | |
| 2646 | /** |
| 2647 | Creates and returns a new mutable array with the given uint8 numbers. |
| 2648 | |
| 2649 | @param doc A mutable document, used for memory allocation only. |
| 2650 | If this parameter is NULL, the function will fail and return NULL. |
| 2651 | @param vals A C array of uint8 numbers. |
| 2652 | @param count The number count. If this value is 0, an empty array will return. |
| 2653 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2654 | |
| 2655 | @par Example |
| 2656 | @code |
| 2657 | const uint8_t vals[3] = { 0, 1, 0 }; |
| 2658 | yyjson_mut_val *arr = yyjson_mut_arr_with_uint8(doc, vals, 3); |
| 2659 | @endcode |
| 2660 | */ |
| 2661 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( |
| 2662 | yyjson_mut_doc *doc, const uint8_t *vals, size_t count); |
| 2663 | |
| 2664 | /** |
| 2665 | Creates and returns a new mutable array with the given uint16 numbers. |
| 2666 | |
| 2667 | @param doc A mutable document, used for memory allocation only. |
| 2668 | If this parameter is NULL, the function will fail and return NULL. |
| 2669 | @param vals A C array of uint16 numbers. |
| 2670 | @param count The number count. If this value is 0, an empty array will return. |
| 2671 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2672 | |
| 2673 | @par Example |
| 2674 | @code |
| 2675 | const uint16_t vals[3] = { 0, 1, 0 }; |
| 2676 | yyjson_mut_val *arr = yyjson_mut_arr_with_uint16(doc, vals, 3); |
| 2677 | @endcode |
| 2678 | */ |
| 2679 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( |
| 2680 | yyjson_mut_doc *doc, const uint16_t *vals, size_t count); |
| 2681 | |
| 2682 | /** |
| 2683 | Creates and returns a new mutable array with the given uint32 numbers. |
| 2684 | |
| 2685 | @param doc A mutable document, used for memory allocation only. |
| 2686 | If this parameter is NULL, the function will fail and return NULL. |
| 2687 | @param vals A C array of uint32 numbers. |
| 2688 | @param count The number count. If this value is 0, an empty array will return. |
| 2689 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2690 | |
| 2691 | @par Example |
| 2692 | @code |
| 2693 | const uint32_t vals[3] = { 0, 1, 0 }; |
| 2694 | yyjson_mut_val *arr = yyjson_mut_arr_with_uint32(doc, vals, 3); |
| 2695 | @endcode |
| 2696 | */ |
| 2697 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( |
| 2698 | yyjson_mut_doc *doc, const uint32_t *vals, size_t count); |
| 2699 | |
| 2700 | /** |
| 2701 | Creates and returns a new mutable array with the given uint64 numbers. |
| 2702 | |
| 2703 | @param doc A mutable document, used for memory allocation only. |
| 2704 | If this parameter is NULL, the function will fail and return NULL. |
| 2705 | @param vals A C array of uint64 numbers. |
| 2706 | @param count The number count. If this value is 0, an empty array will return. |
| 2707 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2708 | |
| 2709 | @par Example |
| 2710 | @code |
| 2711 | const uint64_t vals[3] = { 0, 1, 0 }; |
| 2712 | yyjson_mut_val *arr = yyjson_mut_arr_with_uint64(doc, vals, 3); |
| 2713 | @endcode |
| 2714 | */ |
| 2715 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( |
| 2716 | yyjson_mut_doc *doc, const uint64_t *vals, size_t count); |
| 2717 | |
| 2718 | /** |
| 2719 | Creates and returns a new mutable array with the given float numbers. |
| 2720 | |
| 2721 | @param doc A mutable document, used for memory allocation only. |
| 2722 | If this parameter is NULL, the function will fail and return NULL. |
| 2723 | @param vals A C array of float numbers. |
| 2724 | @param count The number count. If this value is 0, an empty array will return. |
| 2725 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2726 | |
| 2727 | @par Example |
| 2728 | @code |
| 2729 | const float vals[3] = { -1.0f, 0.0f, 1.0f }; |
| 2730 | yyjson_mut_val *arr = yyjson_mut_arr_with_float(doc, vals, 3); |
| 2731 | @endcode |
| 2732 | */ |
| 2733 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( |
| 2734 | yyjson_mut_doc *doc, const float *vals, size_t count); |
| 2735 | |
| 2736 | /** |
| 2737 | Creates and returns a new mutable array with the given double numbers. |
| 2738 | |
| 2739 | @param doc A mutable document, used for memory allocation only. |
| 2740 | If this parameter is NULL, the function will fail and return NULL. |
| 2741 | @param vals A C array of double numbers. |
| 2742 | @param count The number count. If this value is 0, an empty array will return. |
| 2743 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2744 | |
| 2745 | @par Example |
| 2746 | @code |
| 2747 | const double vals[3] = { -1.0, 0.0, 1.0 }; |
| 2748 | yyjson_mut_val *arr = yyjson_mut_arr_with_double(doc, vals, 3); |
| 2749 | @endcode |
| 2750 | */ |
| 2751 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( |
| 2752 | yyjson_mut_doc *doc, const double *vals, size_t count); |
| 2753 | |
| 2754 | /** |
| 2755 | Creates and returns a new mutable array with the given strings, these strings |
| 2756 | will not be copied. |
| 2757 | |
| 2758 | @param doc A mutable document, used for memory allocation only. |
| 2759 | If this parameter is NULL, the function will fail and return NULL. |
| 2760 | @param vals A C array of UTF-8 null-terminator strings. |
| 2761 | If this array contains NULL, the function will fail and return NULL. |
| 2762 | @param count The number of values in `vals`. |
| 2763 | If this value is 0, an empty array will return. |
| 2764 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2765 | |
| 2766 | @warning The input strings are not copied, you should keep these strings |
| 2767 | unmodified for the lifetime of this JSON document. If these strings will be |
| 2768 | modified, you should use `yyjson_mut_arr_with_strcpy()` instead. |
| 2769 | |
| 2770 | @par Example |
| 2771 | @code |
| 2772 | const char *vals[3] = { "a", "b", "c" }; |
| 2773 | yyjson_mut_val *arr = yyjson_mut_arr_with_str(doc, vals, 3); |
| 2774 | @endcode |
| 2775 | */ |
| 2776 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( |
| 2777 | yyjson_mut_doc *doc, const char **vals, size_t count); |
| 2778 | |
| 2779 | /** |
| 2780 | Creates and returns a new mutable array with the given strings and string |
| 2781 | lengths, these strings will not be copied. |
| 2782 | |
| 2783 | @param doc A mutable document, used for memory allocation only. |
| 2784 | If this parameter is NULL, the function will fail and return NULL. |
| 2785 | @param vals A C array of UTF-8 strings, null-terminator is not required. |
| 2786 | If this array contains NULL, the function will fail and return NULL. |
| 2787 | @param lens A C array of string lengths, in bytes. |
| 2788 | @param count The number of strings in `vals`. |
| 2789 | If this value is 0, an empty array will return. |
| 2790 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2791 | |
| 2792 | @warning The input strings are not copied, you should keep these strings |
| 2793 | unmodified for the lifetime of this JSON document. If these strings will be |
| 2794 | modified, you should use `yyjson_mut_arr_with_strncpy()` instead. |
| 2795 | |
| 2796 | @par Example |
| 2797 | @code |
| 2798 | const char *vals[3] = { "a", "bb", "c" }; |
| 2799 | const size_t lens[3] = { 1, 2, 1 }; |
| 2800 | yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); |
| 2801 | @endcode |
| 2802 | */ |
| 2803 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( |
| 2804 | yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); |
| 2805 | |
| 2806 | /** |
| 2807 | Creates and returns a new mutable array with the given strings, these strings |
| 2808 | will be copied. |
| 2809 | |
| 2810 | @param doc A mutable document, used for memory allocation only. |
| 2811 | If this parameter is NULL, the function will fail and return NULL. |
| 2812 | @param vals A C array of UTF-8 null-terminator strings. |
| 2813 | If this array contains NULL, the function will fail and return NULL. |
| 2814 | @param count The number of values in `vals`. |
| 2815 | If this value is 0, an empty array will return. |
| 2816 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2817 | |
| 2818 | @par Example |
| 2819 | @code |
| 2820 | const char *vals[3] = { "a", "b", "c" }; |
| 2821 | yyjson_mut_val *arr = yyjson_mut_arr_with_strcpy(doc, vals, 3); |
| 2822 | @endcode |
| 2823 | */ |
| 2824 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( |
| 2825 | yyjson_mut_doc *doc, const char **vals, size_t count); |
| 2826 | |
| 2827 | /** |
| 2828 | Creates and returns a new mutable array with the given strings and string |
| 2829 | lengths, these strings will be copied. |
| 2830 | |
| 2831 | @param doc A mutable document, used for memory allocation only. |
| 2832 | If this parameter is NULL, the function will fail and return NULL. |
| 2833 | @param vals A C array of UTF-8 strings, null-terminator is not required. |
| 2834 | If this array contains NULL, the function will fail and return NULL. |
| 2835 | @param lens A C array of string lengths, in bytes. |
| 2836 | @param count The number of strings in `vals`. |
| 2837 | If this value is 0, an empty array will return. |
| 2838 | @return The new array. NULL if input is invalid or memory allocation failed. |
| 2839 | |
| 2840 | @par Example |
| 2841 | @code |
| 2842 | const char *vals[3] = { "a", "bb", "c" }; |
| 2843 | const size_t lens[3] = { 1, 2, 1 }; |
| 2844 | yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); |
| 2845 | @endcode |
| 2846 | */ |
| 2847 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( |
| 2848 | yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); |
| 2849 | |
| 2850 | |
| 2851 | |
| 2852 | /*============================================================================== |
| 2853 | * Mutable JSON Array Modification API |
| 2854 | *============================================================================*/ |
| 2855 | |
| 2856 | /** |
| 2857 | Inserts a value into an array at a given index. |
| 2858 | @param arr The array to which the value is to be inserted. |
| 2859 | Returns false if it is NULL or not an array. |
| 2860 | @param val The value to be inserted. Returns false if it is NULL. |
| 2861 | @param idx The index to which to insert the new value. |
| 2862 | Returns false if the index is out of range. |
| 2863 | @return Whether successful. |
| 2864 | @warning This function takes a linear search time. |
| 2865 | */ |
| 2866 | yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, |
| 2867 | yyjson_mut_val *val, size_t idx); |
| 2868 | |
| 2869 | /** |
| 2870 | Inserts a value at the end of the array. |
| 2871 | @param arr The array to which the value is to be inserted. |
| 2872 | Returns false if it is NULL or not an array. |
| 2873 | @param val The value to be inserted. Returns false if it is NULL. |
| 2874 | @return Whether successful. |
| 2875 | */ |
| 2876 | yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, |
| 2877 | yyjson_mut_val *val); |
| 2878 | |
| 2879 | /** |
| 2880 | Inserts a value at the head of the array. |
| 2881 | @param arr The array to which the value is to be inserted. |
| 2882 | Returns false if it is NULL or not an array. |
| 2883 | @param val The value to be inserted. Returns false if it is NULL. |
| 2884 | @return Whether successful. |
| 2885 | */ |
| 2886 | yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, |
| 2887 | yyjson_mut_val *val); |
| 2888 | |
| 2889 | /** |
| 2890 | Replaces a value at index and returns old value. |
| 2891 | @param arr The array to which the value is to be replaced. |
| 2892 | Returns false if it is NULL or not an array. |
| 2893 | @param idx The index to which to replace the value. |
| 2894 | Returns false if the index is out of range. |
| 2895 | @param val The new value to replace. Returns false if it is NULL. |
| 2896 | @return Old value, or NULL on error. |
| 2897 | @warning This function takes a linear search time. |
| 2898 | */ |
| 2899 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, |
| 2900 | size_t idx, |
| 2901 | yyjson_mut_val *val); |
| 2902 | |
| 2903 | /** |
| 2904 | Removes and returns a value at index. |
| 2905 | @param arr The array from which the value is to be removed. |
| 2906 | Returns false if it is NULL or not an array. |
| 2907 | @param idx The index from which to remove the value. |
| 2908 | Returns false if the index is out of range. |
| 2909 | @return Old value, or NULL on error. |
| 2910 | @warning This function takes a linear search time. |
| 2911 | */ |
| 2912 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, |
| 2913 | size_t idx); |
| 2914 | |
| 2915 | /** |
| 2916 | Removes and returns the first value in this array. |
| 2917 | @param arr The array from which the value is to be removed. |
| 2918 | Returns false if it is NULL or not an array. |
| 2919 | @return The first value, or NULL on error. |
| 2920 | */ |
| 2921 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( |
| 2922 | yyjson_mut_val *arr); |
| 2923 | |
| 2924 | /** |
| 2925 | Removes and returns the last value in this array. |
| 2926 | @param arr The array from which the value is to be removed. |
| 2927 | Returns false if it is NULL or not an array. |
| 2928 | @return The last value, or NULL on error. |
| 2929 | */ |
| 2930 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( |
| 2931 | yyjson_mut_val *arr); |
| 2932 | |
| 2933 | /** |
| 2934 | Removes all values within a specified range in the array. |
| 2935 | @param arr The array from which the value is to be removed. |
| 2936 | Returns false if it is NULL or not an array. |
| 2937 | @param idx The start index of the range (0 is the first). |
| 2938 | @param len The number of items in the range (can be 0). |
| 2939 | @return Whether successful. |
| 2940 | @warning This function takes a linear search time. |
| 2941 | */ |
| 2942 | yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, |
| 2943 | size_t idx, size_t len); |
| 2944 | |
| 2945 | /** |
| 2946 | Removes all values in this array. |
| 2947 | @param arr The array from which all of the values are to be removed. |
| 2948 | Returns false if it is NULL or not an array. |
| 2949 | @return Whether successful. |
| 2950 | */ |
| 2951 | yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr); |
| 2952 | |
| 2953 | /** |
| 2954 | Rotates values in this array for the given number of times. |
| 2955 | For example: `[1,2,3,4,5]` rotate 2 is `[3,4,5,1,2]`. |
| 2956 | @param arr The array to be rotated. |
| 2957 | @param idx Index (or times) to rotate. |
| 2958 | @warning This function takes a linear search time. |
| 2959 | */ |
| 2960 | yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, |
| 2961 | size_t idx); |
| 2962 | |
| 2963 | |
| 2964 | |
| 2965 | /*============================================================================== |
| 2966 | * Mutable JSON Array Modification Convenience API |
| 2967 | *============================================================================*/ |
| 2968 | |
| 2969 | /** |
| 2970 | Adds a value at the end of the array. |
| 2971 | @param arr The array to which the value is to be inserted. |
| 2972 | Returns false if it is NULL or not an array. |
| 2973 | @param val The value to be inserted. Returns false if it is NULL. |
| 2974 | @return Whether successful. |
| 2975 | */ |
| 2976 | yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, |
| 2977 | yyjson_mut_val *val); |
| 2978 | |
| 2979 | /** |
| 2980 | Adds a `null` value at the end of the array. |
| 2981 | @param doc The `doc` is only used for memory allocation. |
| 2982 | @param arr The array to which the value is to be inserted. |
| 2983 | Returns false if it is NULL or not an array. |
| 2984 | @return Whether successful. |
| 2985 | */ |
| 2986 | yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, |
| 2987 | yyjson_mut_val *arr); |
| 2988 | |
| 2989 | /** |
| 2990 | Adds a `true` value at the end of the array. |
| 2991 | @param doc The `doc` is only used for memory allocation. |
| 2992 | @param arr The array to which the value is to be inserted. |
| 2993 | Returns false if it is NULL or not an array. |
| 2994 | @return Whether successful. |
| 2995 | */ |
| 2996 | yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, |
| 2997 | yyjson_mut_val *arr); |
| 2998 | |
| 2999 | /** |
| 3000 | Adds a `false` value at the end of the array. |
| 3001 | @param doc The `doc` is only used for memory allocation. |
| 3002 | @param arr The array to which the value is to be inserted. |
| 3003 | Returns false if it is NULL or not an array. |
| 3004 | @return Whether successful. |
| 3005 | */ |
| 3006 | yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, |
| 3007 | yyjson_mut_val *arr); |
| 3008 | |
| 3009 | /** |
| 3010 | Adds a bool value at the end of the array. |
| 3011 | @param doc The `doc` is only used for memory allocation. |
| 3012 | @param arr The array to which the value is to be inserted. |
| 3013 | Returns false if it is NULL or not an array. |
| 3014 | @param val The bool value to be added. |
| 3015 | @return Whether successful. |
| 3016 | */ |
| 3017 | yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, |
| 3018 | yyjson_mut_val *arr, |
| 3019 | bool val); |
| 3020 | |
| 3021 | /** |
| 3022 | Adds an unsigned integer value at the end of the array. |
| 3023 | @param doc The `doc` is only used for memory allocation. |
| 3024 | @param arr The array to which the value is to be inserted. |
| 3025 | Returns false if it is NULL or not an array. |
| 3026 | @param num The number to be added. |
| 3027 | @return Whether successful. |
| 3028 | */ |
| 3029 | yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, |
| 3030 | yyjson_mut_val *arr, |
| 3031 | uint64_t num); |
| 3032 | |
| 3033 | /** |
| 3034 | Adds a signed integer value at the end of the array. |
| 3035 | @param doc The `doc` is only used for memory allocation. |
| 3036 | @param arr The array to which the value is to be inserted. |
| 3037 | Returns false if it is NULL or not an array. |
| 3038 | @param num The number to be added. |
| 3039 | @return Whether successful. |
| 3040 | */ |
| 3041 | yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, |
| 3042 | yyjson_mut_val *arr, |
| 3043 | int64_t num); |
| 3044 | |
| 3045 | /** |
| 3046 | Adds a integer value at the end of the array. |
| 3047 | @param doc The `doc` is only used for memory allocation. |
| 3048 | @param arr The array to which the value is to be inserted. |
| 3049 | Returns false if it is NULL or not an array. |
| 3050 | @param num The number to be added. |
| 3051 | @return Whether successful. |
| 3052 | */ |
| 3053 | yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, |
| 3054 | yyjson_mut_val *arr, |
| 3055 | int64_t num); |
| 3056 | |
| 3057 | /** |
| 3058 | Adds a double value at the end of the array. |
| 3059 | @param doc The `doc` is only used for memory allocation. |
| 3060 | @param arr The array to which the value is to be inserted. |
| 3061 | Returns false if it is NULL or not an array. |
| 3062 | @param num The number to be added. |
| 3063 | @return Whether successful. |
| 3064 | */ |
| 3065 | yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, |
| 3066 | yyjson_mut_val *arr, |
| 3067 | double num); |
| 3068 | |
| 3069 | /** |
| 3070 | Adds a string value at the end of the array (no copy). |
| 3071 | @param doc The `doc` is only used for memory allocation. |
| 3072 | @param arr The array to which the value is to be inserted. |
| 3073 | Returns false if it is NULL or not an array. |
| 3074 | @param str A null-terminated UTF-8 string. |
| 3075 | @return Whether successful. |
| 3076 | @warning The input string is not copied, you should keep this string unmodified |
| 3077 | for the lifetime of this JSON document. |
| 3078 | */ |
| 3079 | yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, |
| 3080 | yyjson_mut_val *arr, |
| 3081 | const char *str); |
| 3082 | |
| 3083 | /** |
| 3084 | Adds a string value at the end of the array (no copy). |
| 3085 | @param doc The `doc` is only used for memory allocation. |
| 3086 | @param arr The array to which the value is to be inserted. |
| 3087 | Returns false if it is NULL or not an array. |
| 3088 | @param str A UTF-8 string, null-terminator is not required. |
| 3089 | @param len The length of the string, in bytes. |
| 3090 | @return Whether successful. |
| 3091 | @warning The input string is not copied, you should keep this string unmodified |
| 3092 | for the lifetime of this JSON document. |
| 3093 | */ |
| 3094 | yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, |
| 3095 | yyjson_mut_val *arr, |
| 3096 | const char *str, |
| 3097 | size_t len); |
| 3098 | |
| 3099 | /** |
| 3100 | Adds a string value at the end of the array (copied). |
| 3101 | @param doc The `doc` is only used for memory allocation. |
| 3102 | @param arr The array to which the value is to be inserted. |
| 3103 | Returns false if it is NULL or not an array. |
| 3104 | @param str A null-terminated UTF-8 string. |
| 3105 | @return Whether successful. |
| 3106 | */ |
| 3107 | yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, |
| 3108 | yyjson_mut_val *arr, |
| 3109 | const char *str); |
| 3110 | |
| 3111 | /** |
| 3112 | Adds a string value at the end of the array (copied). |
| 3113 | @param doc The `doc` is only used for memory allocation. |
| 3114 | @param arr The array to which the value is to be inserted. |
| 3115 | Returns false if it is NULL or not an array. |
| 3116 | @param str A UTF-8 string, null-terminator is not required. |
| 3117 | @param len The length of the string, in bytes. |
| 3118 | @return Whether successful. |
| 3119 | */ |
| 3120 | yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, |
| 3121 | yyjson_mut_val *arr, |
| 3122 | const char *str, |
| 3123 | size_t len); |
| 3124 | |
| 3125 | /** |
| 3126 | Creates and adds a new array at the end of the array. |
| 3127 | @param doc The `doc` is only used for memory allocation. |
| 3128 | @param arr The array to which the value is to be inserted. |
| 3129 | Returns false if it is NULL or not an array. |
| 3130 | @return The new array, or NULL on error. |
| 3131 | */ |
| 3132 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, |
| 3133 | yyjson_mut_val *arr); |
| 3134 | |
| 3135 | /** |
| 3136 | Creates and adds a new object at the end of the array. |
| 3137 | @param doc The `doc` is only used for memory allocation. |
| 3138 | @param arr The array to which the value is to be inserted. |
| 3139 | Returns false if it is NULL or not an array. |
| 3140 | @return The new object, or NULL on error. |
| 3141 | */ |
| 3142 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, |
| 3143 | yyjson_mut_val *arr); |
| 3144 | |
| 3145 | |
| 3146 | |
| 3147 | /*============================================================================== |
| 3148 | * Mutable JSON Object API |
| 3149 | *============================================================================*/ |
| 3150 | |
| 3151 | /** Returns the number of key-value pairs in this object. |
| 3152 | Returns 0 if `obj` is NULL or type is not object. */ |
| 3153 | yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj); |
| 3154 | |
| 3155 | /** Returns the value to which the specified key is mapped. |
| 3156 | Returns NULL if this object contains no mapping for the key. |
| 3157 | Returns NULL if `obj/key` is NULL, or type is not object. |
| 3158 | |
| 3159 | The `key` should be a null-terminated UTF-8 string. |
| 3160 | |
| 3161 | @warning This function takes a linear search time. */ |
| 3162 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, |
| 3163 | const char *key); |
| 3164 | |
| 3165 | /** Returns the value to which the specified key is mapped. |
| 3166 | Returns NULL if this object contains no mapping for the key. |
| 3167 | Returns NULL if `obj/key` is NULL, or type is not object. |
| 3168 | |
| 3169 | The `key` should be a UTF-8 string, null-terminator is not required. |
| 3170 | The `key_len` should be the length of the key, in bytes. |
| 3171 | |
| 3172 | @warning This function takes a linear search time. */ |
| 3173 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, |
| 3174 | const char *key, |
| 3175 | size_t key_len); |
| 3176 | |
| 3177 | |
| 3178 | |
| 3179 | /*============================================================================== |
| 3180 | * Mutable JSON Object Iterator API |
| 3181 | *============================================================================*/ |
| 3182 | |
| 3183 | /** |
| 3184 | A mutable JSON object iterator. |
| 3185 | |
| 3186 | @warning You should not modify the object while iterating over it, but you can |
| 3187 | use `yyjson_mut_obj_iter_remove()` to remove current value. |
| 3188 | |
| 3189 | @par Example |
| 3190 | @code |
| 3191 | yyjson_mut_val *key, *val; |
| 3192 | yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); |
| 3193 | while ((key = yyjson_mut_obj_iter_next(&iter))) { |
| 3194 | val = yyjson_mut_obj_iter_get_val(key); |
| 3195 | your_func(key, val); |
| 3196 | if (your_val_is_unused(key, val)) { |
| 3197 | yyjson_mut_obj_iter_remove(&iter); |
| 3198 | } |
| 3199 | } |
| 3200 | @endcode |
| 3201 | |
| 3202 | If the ordering of the keys is known at compile-time, you can use this method |
| 3203 | to speed up value lookups: |
| 3204 | @code |
| 3205 | // {"k1":1, "k2": 3, "k3": 3} |
| 3206 | yyjson_mut_val *key, *val; |
| 3207 | yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); |
| 3208 | yyjson_mut_val *v1 = yyjson_mut_obj_iter_get(&iter, "k1"); |
| 3209 | yyjson_mut_val *v3 = yyjson_mut_obj_iter_get(&iter, "k3"); |
| 3210 | @endcode |
| 3211 | @see `yyjson_mut_obj_iter_get()` and `yyjson_mut_obj_iter_getn()` |
| 3212 | */ |
| 3213 | typedef struct yyjson_mut_obj_iter { |
| 3214 | size_t idx; /**< next key's index */ |
| 3215 | size_t max; /**< maximum key index (obj.size) */ |
| 3216 | yyjson_mut_val *cur; /**< current key */ |
| 3217 | yyjson_mut_val *pre; /**< previous key */ |
| 3218 | yyjson_mut_val *obj; /**< the object being iterated */ |
| 3219 | } yyjson_mut_obj_iter; |
| 3220 | |
| 3221 | /** |
| 3222 | Initialize an iterator for this object. |
| 3223 | |
| 3224 | @param obj The object to be iterated over. |
| 3225 | If this parameter is NULL or not an array, `iter` will be set to empty. |
| 3226 | @param iter The iterator to be initialized. |
| 3227 | If this parameter is NULL, the function will fail and return false. |
| 3228 | @return true if the `iter` has been successfully initialized. |
| 3229 | |
| 3230 | @note The iterator does not need to be destroyed. |
| 3231 | */ |
| 3232 | yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, |
| 3233 | yyjson_mut_obj_iter *iter); |
| 3234 | |
| 3235 | /** |
| 3236 | Create an iterator with an object, same as `yyjson_obj_iter_init()`. |
| 3237 | |
| 3238 | @param obj The object to be iterated over. |
| 3239 | If this parameter is NULL or not an object, an empty iterator will returned. |
| 3240 | @return A new iterator for the object. |
| 3241 | |
| 3242 | @note The iterator does not need to be destroyed. |
| 3243 | */ |
| 3244 | yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( |
| 3245 | yyjson_mut_val *obj); |
| 3246 | |
| 3247 | /** |
| 3248 | Returns whether the iteration has more elements. |
| 3249 | If `iter` is NULL, this function will return false. |
| 3250 | */ |
| 3251 | yyjson_api_inline bool yyjson_mut_obj_iter_has_next( |
| 3252 | yyjson_mut_obj_iter *iter); |
| 3253 | |
| 3254 | /** |
| 3255 | Returns the next key in the iteration, or NULL on end. |
| 3256 | If `iter` is NULL, this function will return NULL. |
| 3257 | */ |
| 3258 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( |
| 3259 | yyjson_mut_obj_iter *iter); |
| 3260 | |
| 3261 | /** |
| 3262 | Returns the value for key inside the iteration. |
| 3263 | If `iter` is NULL, this function will return NULL. |
| 3264 | */ |
| 3265 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( |
| 3266 | yyjson_mut_val *key); |
| 3267 | |
| 3268 | /** |
| 3269 | Removes current key-value pair in the iteration, returns the removed value. |
| 3270 | If `iter` is NULL, this function will return NULL. |
| 3271 | */ |
| 3272 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( |
| 3273 | yyjson_mut_obj_iter *iter); |
| 3274 | |
| 3275 | /** |
| 3276 | Iterates to a specified key and returns the value. |
| 3277 | |
| 3278 | This function does the same thing as `yyjson_mut_obj_get()`, but is much faster |
| 3279 | if the ordering of the keys is known at compile-time and you are using the same |
| 3280 | order to look up the values. If the key exists in this object, then the |
| 3281 | iterator will stop at the next key, otherwise the iterator will not change and |
| 3282 | NULL is returned. |
| 3283 | |
| 3284 | @param iter The object iterator, should not be NULL. |
| 3285 | @param key The key, should be a UTF-8 string with null-terminator. |
| 3286 | @return The value to which the specified key is mapped. |
| 3287 | NULL if this object contains no mapping for the key or input is invalid. |
| 3288 | |
| 3289 | @warning This function takes a linear search time if the key is not nearby. |
| 3290 | */ |
| 3291 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( |
| 3292 | yyjson_mut_obj_iter *iter, const char *key); |
| 3293 | |
| 3294 | /** |
| 3295 | Iterates to a specified key and returns the value. |
| 3296 | |
| 3297 | This function does the same thing as `yyjson_mut_obj_getn()` but is much faster |
| 3298 | if the ordering of the keys is known at compile-time and you are using the same |
| 3299 | order to look up the values. If the key exists in this object, then the |
| 3300 | iterator will stop at the next key, otherwise the iterator will not change and |
| 3301 | NULL is returned. |
| 3302 | |
| 3303 | @param iter The object iterator, should not be NULL. |
| 3304 | @param key The key, should be a UTF-8 string, null-terminator is not required. |
| 3305 | @param key_len The the length of `key`, in bytes. |
| 3306 | @return The value to which the specified key is mapped. |
| 3307 | NULL if this object contains no mapping for the key or input is invalid. |
| 3308 | |
| 3309 | @warning This function takes a linear search time if the key is not nearby. |
| 3310 | */ |
| 3311 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( |
| 3312 | yyjson_mut_obj_iter *iter, const char *key, size_t key_len); |
| 3313 | |
| 3314 | /** |
| 3315 | Macro for iterating over an object. |
| 3316 | It works like iterator, but with a more intuitive API. |
| 3317 | |
| 3318 | @warning You should not modify the object while iterating over it. |
| 3319 | |
| 3320 | @par Example |
| 3321 | @code |
| 3322 | size_t idx, max; |
| 3323 | yyjson_val *key, *val; |
| 3324 | yyjson_obj_foreach(obj, idx, max, key, val) { |
| 3325 | your_func(key, val); |
| 3326 | } |
| 3327 | @endcode |
| 3328 | */ |
| 3329 | #define yyjson_mut_obj_foreach(obj, idx, max, key, val) \ |
| 3330 | for ((idx) = 0, \ |
| 3331 | (max) = yyjson_mut_obj_size(obj), \ |
| 3332 | (key) = (max) ? ((yyjson_mut_val *)(obj)->uni.ptr)->next->next : NULL, \ |
| 3333 | (val) = (key) ? (key)->next : NULL; \ |
| 3334 | (idx) < (max); \ |
| 3335 | (idx)++, \ |
| 3336 | (key) = (val)->next, \ |
| 3337 | (val) = (key)->next) |
| 3338 | |
| 3339 | |
| 3340 | |
| 3341 | /*============================================================================== |
| 3342 | * Mutable JSON Object Creation API |
| 3343 | *============================================================================*/ |
| 3344 | |
| 3345 | /** Creates and returns a mutable object, returns NULL on error. */ |
| 3346 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc); |
| 3347 | |
| 3348 | /** |
| 3349 | Creates and returns a mutable object with keys and values, returns NULL on |
| 3350 | error. The keys and values are not copied. The strings should be a |
| 3351 | null-terminated UTF-8 string. |
| 3352 | |
| 3353 | @warning The input string is not copied, you should keep this string |
| 3354 | unmodified for the lifetime of this JSON document. |
| 3355 | |
| 3356 | @par Example |
| 3357 | @code |
| 3358 | const char *keys[2] = { "id", "name" }; |
| 3359 | const char *vals[2] = { "01", "Harry" }; |
| 3360 | yyjson_mut_val *obj = yyjson_mut_obj_with_str(doc, keys, vals, 2); |
| 3361 | @endcode |
| 3362 | */ |
| 3363 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, |
| 3364 | const char **keys, |
| 3365 | const char **vals, |
| 3366 | size_t count); |
| 3367 | |
| 3368 | /** |
| 3369 | Creates and returns a mutable object with key-value pairs and pair count, |
| 3370 | returns NULL on error. The keys and values are not copied. The strings should |
| 3371 | be a null-terminated UTF-8 string. |
| 3372 | |
| 3373 | @warning The input string is not copied, you should keep this string |
| 3374 | unmodified for the lifetime of this JSON document. |
| 3375 | |
| 3376 | @par Example |
| 3377 | @code |
| 3378 | const char *kv_pairs[4] = { "id", "01", "name", "Harry" }; |
| 3379 | yyjson_mut_val *obj = yyjson_mut_obj_with_kv(doc, kv_pairs, 2); |
| 3380 | @endcode |
| 3381 | */ |
| 3382 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, |
| 3383 | const char **kv_pairs, |
| 3384 | size_t pair_count); |
| 3385 | |
| 3386 | |
| 3387 | |
| 3388 | /*============================================================================== |
| 3389 | * Mutable JSON Object Modification API |
| 3390 | *============================================================================*/ |
| 3391 | |
| 3392 | /** |
| 3393 | Adds a key-value pair at the end of the object. |
| 3394 | This function allows duplicated key in one object. |
| 3395 | @param obj The object to which the new key-value pair is to be added. |
| 3396 | @param key The key, should be a string which is created by `yyjson_mut_str()`, |
| 3397 | `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. |
| 3398 | @param val The value to add to the object. |
| 3399 | @return Whether successful. |
| 3400 | */ |
| 3401 | yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, |
| 3402 | yyjson_mut_val *key, |
| 3403 | yyjson_mut_val *val); |
| 3404 | /** |
| 3405 | Sets a key-value pair at the end of the object. |
| 3406 | This function may remove all key-value pairs for the given key before add. |
| 3407 | @param obj The object to which the new key-value pair is to be added. |
| 3408 | @param key The key, should be a string which is created by `yyjson_mut_str()`, |
| 3409 | `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. |
| 3410 | @param val The value to add to the object. If this value is null, the behavior |
| 3411 | is same as `yyjson_mut_obj_remove()`. |
| 3412 | @return Whether successful. |
| 3413 | */ |
| 3414 | yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, |
| 3415 | yyjson_mut_val *key, |
| 3416 | yyjson_mut_val *val); |
| 3417 | |
| 3418 | /** |
| 3419 | Inserts a key-value pair to the object at the given position. |
| 3420 | This function allows duplicated key in one object. |
| 3421 | @param obj The object to which the new key-value pair is to be added. |
| 3422 | @param key The key, should be a string which is created by `yyjson_mut_str()`, |
| 3423 | `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. |
| 3424 | @param val The value to add to the object. |
| 3425 | @param idx The index to which to insert the new pair. |
| 3426 | @return Whether successful. |
| 3427 | */ |
| 3428 | yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, |
| 3429 | yyjson_mut_val *key, |
| 3430 | yyjson_mut_val *val, |
| 3431 | size_t idx); |
| 3432 | |
| 3433 | /** |
| 3434 | Removes all key-value pair from the object with given key. |
| 3435 | @param obj The object from which the key-value pair is to be removed. |
| 3436 | @param key The key, should be a string value. |
| 3437 | @return The first matched value, or NULL if no matched value. |
| 3438 | @warning This function takes a linear search time. |
| 3439 | */ |
| 3440 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, |
| 3441 | yyjson_mut_val *key); |
| 3442 | |
| 3443 | /** |
| 3444 | Removes all key-value pair from the object with given key. |
| 3445 | @param obj The object from which the key-value pair is to be removed. |
| 3446 | @param key The key, should be a UTF-8 string with null-terminator. |
| 3447 | @return The first matched value, or NULL if no matched value. |
| 3448 | @warning This function takes a linear search time. |
| 3449 | */ |
| 3450 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( |
| 3451 | yyjson_mut_val *obj, const char *key); |
| 3452 | |
| 3453 | /** |
| 3454 | Removes all key-value pair from the object with given key. |
| 3455 | @param obj The object from which the key-value pair is to be removed. |
| 3456 | @param key The key, should be a UTF-8 string, null-terminator is not required. |
| 3457 | @param key_len The length of the key. |
| 3458 | @return The first matched value, or NULL if no matched value. |
| 3459 | @warning This function takes a linear search time. |
| 3460 | */ |
| 3461 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( |
| 3462 | yyjson_mut_val *obj, const char *key, size_t key_len); |
| 3463 | |
| 3464 | /** |
| 3465 | Removes all key-value pairs in this object. |
| 3466 | @param obj The object from which all of the values are to be removed. |
| 3467 | @return Whether successful. |
| 3468 | */ |
| 3469 | yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj); |
| 3470 | |
| 3471 | /** |
| 3472 | Replaces value from the object with given key. |
| 3473 | If the key is not exist, or the value is NULL, it will fail. |
| 3474 | @param obj The object to which the value is to be replaced. |
| 3475 | @param key The key, should be a string value. |
| 3476 | @param val The value to replace into the object. |
| 3477 | @return Whether successful. |
| 3478 | @warning This function takes a linear search time. |
| 3479 | */ |
| 3480 | yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, |
| 3481 | yyjson_mut_val *key, |
| 3482 | yyjson_mut_val *val); |
| 3483 | |
| 3484 | /** |
| 3485 | Rotates key-value pairs in the object for the given number of times. |
| 3486 | For example: `{"a":1,"b":2,"c":3,"d":4}` rotate 1 is |
| 3487 | `{"b":2,"c":3,"d":4,"a":1}`. |
| 3488 | @param obj The object to be rotated. |
| 3489 | @param idx Index (or times) to rotate. |
| 3490 | @return Whether successful. |
| 3491 | @warning This function takes a linear search time. |
| 3492 | */ |
| 3493 | yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, |
| 3494 | size_t idx); |
| 3495 | |
| 3496 | |
| 3497 | |
| 3498 | /*============================================================================== |
| 3499 | * Mutable JSON Object Modification Convenience API |
| 3500 | *============================================================================*/ |
| 3501 | |
| 3502 | /** Adds a `null` value at the end of the object. |
| 3503 | The `key` should be a null-terminated UTF-8 string. |
| 3504 | This function allows duplicated key in one object. |
| 3505 | |
| 3506 | @warning The key string are not copied, you should keep the string |
| 3507 | unmodified for the lifetime of this JSON document. */ |
| 3508 | yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, |
| 3509 | yyjson_mut_val *obj, |
| 3510 | const char *key); |
| 3511 | |
| 3512 | /** Adds a `true` value at the end of the object. |
| 3513 | The `key` should be a null-terminated UTF-8 string. |
| 3514 | This function allows duplicated key in one object. |
| 3515 | |
| 3516 | @warning The key string are not copied, you should keep the string |
| 3517 | unmodified for the lifetime of this JSON document. */ |
| 3518 | yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, |
| 3519 | yyjson_mut_val *obj, |
| 3520 | const char *key); |
| 3521 | |
| 3522 | /** Adds a `false` value at the end of the object. |
| 3523 | The `key` should be a null-terminated UTF-8 string. |
| 3524 | This function allows duplicated key in one object. |
| 3525 | |
| 3526 | @warning The key string are not copied, you should keep the string |
| 3527 | unmodified for the lifetime of this JSON document. */ |
| 3528 | yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, |
| 3529 | yyjson_mut_val *obj, |
| 3530 | const char *key); |
| 3531 | |
| 3532 | /** Adds a bool value at the end of the object. |
| 3533 | The `key` should be a null-terminated UTF-8 string. |
| 3534 | This function allows duplicated key in one object. |
| 3535 | |
| 3536 | @warning The key string are not copied, you should keep the string |
| 3537 | unmodified for the lifetime of this JSON document. */ |
| 3538 | yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, |
| 3539 | yyjson_mut_val *obj, |
| 3540 | const char *key, bool val); |
| 3541 | |
| 3542 | /** Adds an unsigned integer value at the end of the object. |
| 3543 | The `key` should be a null-terminated UTF-8 string. |
| 3544 | This function allows duplicated key in one object. |
| 3545 | |
| 3546 | @warning The key string are not copied, you should keep the string |
| 3547 | unmodified for the lifetime of this JSON document. */ |
| 3548 | yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, |
| 3549 | yyjson_mut_val *obj, |
| 3550 | const char *key, uint64_t val); |
| 3551 | |
| 3552 | /** Adds a signed integer value at the end of the object. |
| 3553 | The `key` should be a null-terminated UTF-8 string. |
| 3554 | This function allows duplicated key in one object. |
| 3555 | |
| 3556 | @warning The key string are not copied, you should keep the string |
| 3557 | unmodified for the lifetime of this JSON document. */ |
| 3558 | yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, |
| 3559 | yyjson_mut_val *obj, |
| 3560 | const char *key, int64_t val); |
| 3561 | |
| 3562 | /** Adds an int value at the end of the object. |
| 3563 | The `key` should be a null-terminated UTF-8 string. |
| 3564 | This function allows duplicated key in one object. |
| 3565 | |
| 3566 | @warning The key string are not copied, you should keep the string |
| 3567 | unmodified for the lifetime of this JSON document. */ |
| 3568 | yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, |
| 3569 | yyjson_mut_val *obj, |
| 3570 | const char *key, int64_t val); |
| 3571 | |
| 3572 | /** Adds a double value at the end of the object. |
| 3573 | The `key` should be a null-terminated UTF-8 string. |
| 3574 | This function allows duplicated key in one object. |
| 3575 | |
| 3576 | @warning The key string are not copied, you should keep the string |
| 3577 | unmodified for the lifetime of this JSON document. */ |
| 3578 | yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, |
| 3579 | yyjson_mut_val *obj, |
| 3580 | const char *key, double val); |
| 3581 | |
| 3582 | /** Adds a string value at the end of the object. |
| 3583 | The `key` and `val` should be null-terminated UTF-8 strings. |
| 3584 | This function allows duplicated key in one object. |
| 3585 | |
| 3586 | @warning The key/value string are not copied, you should keep these strings |
| 3587 | unmodified for the lifetime of this JSON document. */ |
| 3588 | yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, |
| 3589 | yyjson_mut_val *obj, |
| 3590 | const char *key, const char *val); |
| 3591 | |
| 3592 | /** Adds a string value at the end of the object. |
| 3593 | The `key` should be a null-terminated UTF-8 string. |
| 3594 | The `val` should be a UTF-8 string, null-terminator is not required. |
| 3595 | The `len` should be the length of the `val`, in bytes. |
| 3596 | This function allows duplicated key in one object. |
| 3597 | |
| 3598 | @warning The key/value string are not copied, you should keep these strings |
| 3599 | unmodified for the lifetime of this JSON document. */ |
| 3600 | yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, |
| 3601 | yyjson_mut_val *obj, |
| 3602 | const char *key, |
| 3603 | const char *val, size_t len); |
| 3604 | |
| 3605 | /** Adds a string value at the end of the object. |
| 3606 | The `key` and `val` should be null-terminated UTF-8 strings. |
| 3607 | The value string is copied. |
| 3608 | This function allows duplicated key in one object. |
| 3609 | |
| 3610 | @warning The key string are not copied, you should keep the string |
| 3611 | unmodified for the lifetime of this JSON document. */ |
| 3612 | yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, |
| 3613 | yyjson_mut_val *obj, |
| 3614 | const char *key, |
| 3615 | const char *val); |
| 3616 | |
| 3617 | /** Adds a string value at the end of the object. |
| 3618 | The `key` should be a null-terminated UTF-8 string. |
| 3619 | The `val` should be a UTF-8 string, null-terminator is not required. |
| 3620 | The `len` should be the length of the `val`, in bytes. |
| 3621 | This function allows duplicated key in one object. |
| 3622 | |
| 3623 | @warning The key/value string are not copied, you should keep these strings |
| 3624 | unmodified for the lifetime of this JSON document. */ |
| 3625 | yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, |
| 3626 | yyjson_mut_val *obj, |
| 3627 | const char *key, |
| 3628 | const char *val, size_t len); |
| 3629 | |
| 3630 | /** Adds a JSON value at the end of the object. |
| 3631 | The `key` should be a null-terminated UTF-8 string. |
| 3632 | This function allows duplicated key in one object. |
| 3633 | |
| 3634 | @warning The key string are not copied, you should keep the string |
| 3635 | unmodified for the lifetime of this JSON document. */ |
| 3636 | yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, |
| 3637 | yyjson_mut_val *obj, |
| 3638 | const char *key, |
| 3639 | yyjson_mut_val *val); |
| 3640 | |
| 3641 | /** Removes all key-value pairs for the given key. |
| 3642 | Returns the first value to which the specified key is mapped or NULL if this |
| 3643 | object contains no mapping for the key. |
| 3644 | The `key` should be a null-terminated UTF-8 string. |
| 3645 | |
| 3646 | @warning This function takes a linear search time. */ |
| 3647 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str( |
| 3648 | yyjson_mut_val *obj, const char *key); |
| 3649 | |
| 3650 | /** Removes all key-value pairs for the given key. |
| 3651 | Returns the first value to which the specified key is mapped or NULL if this |
| 3652 | object contains no mapping for the key. |
| 3653 | The `key` should be a UTF-8 string, null-terminator is not required. |
| 3654 | The `len` should be the length of the key, in bytes. |
| 3655 | |
| 3656 | @warning This function takes a linear search time. */ |
| 3657 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( |
| 3658 | yyjson_mut_val *obj, const char *key, size_t len); |
| 3659 | |
| 3660 | /** Replaces all matching keys with the new key. |
| 3661 | Returns true if at least one key was renamed. |
| 3662 | The `key` and `new_key` should be a null-terminated UTF-8 string. |
| 3663 | The `new_key` is copied and held by doc. |
| 3664 | |
| 3665 | @warning This function takes a linear search time. |
| 3666 | If `new_key` already exists, it will cause duplicate keys. |
| 3667 | */ |
| 3668 | yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, |
| 3669 | yyjson_mut_val *obj, |
| 3670 | const char *key, |
| 3671 | const char *new_key); |
| 3672 | |
| 3673 | /** Replaces all matching keys with the new key. |
| 3674 | Returns true if at least one key was renamed. |
| 3675 | The `key` and `new_key` should be a UTF-8 string, |
| 3676 | null-terminator is not required. The `new_key` is copied and held by doc. |
| 3677 | |
| 3678 | @warning This function takes a linear search time. |
| 3679 | If `new_key` already exists, it will cause duplicate keys. |
| 3680 | */ |
| 3681 | yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, |
| 3682 | yyjson_mut_val *obj, |
| 3683 | const char *key, |
| 3684 | size_t len, |
| 3685 | const char *new_key, |
| 3686 | size_t new_len); |
| 3687 | |
| 3688 | |
| 3689 | |
| 3690 | /*============================================================================== |
| 3691 | * JSON Pointer API (RFC 6901) |
| 3692 | * https://tools.ietf.org/html/rfc6901 |
| 3693 | *============================================================================*/ |
| 3694 | |
| 3695 | /** JSON Pointer error code. */ |
| 3696 | typedef uint32_t yyjson_ptr_code; |
| 3697 | |
| 3698 | /** No JSON pointer error. */ |
| 3699 | static const yyjson_ptr_code YYJSON_PTR_ERR_NONE = 0; |
| 3700 | |
| 3701 | /** Invalid input parameter, such as NULL input. */ |
| 3702 | static const yyjson_ptr_code YYJSON_PTR_ERR_PARAMETER = 1; |
| 3703 | |
| 3704 | /** JSON pointer syntax error, such as invalid escape, token no prefix. */ |
| 3705 | static const yyjson_ptr_code YYJSON_PTR_ERR_SYNTAX = 2; |
| 3706 | |
| 3707 | /** JSON pointer resolve failed, such as index out of range, key not found. */ |
| 3708 | static const yyjson_ptr_code YYJSON_PTR_ERR_RESOLVE = 3; |
| 3709 | |
| 3710 | /** Document's root is NULL, but it is required for the function call. */ |
| 3711 | static const yyjson_ptr_code YYJSON_PTR_ERR_NULL_ROOT = 4; |
| 3712 | |
| 3713 | /** Cannot set root as the target is not a document. */ |
| 3714 | static const yyjson_ptr_code YYJSON_PTR_ERR_SET_ROOT = 5; |
| 3715 | |
| 3716 | /** The memory allocation failed and a new value could not be created. */ |
| 3717 | static const yyjson_ptr_code YYJSON_PTR_ERR_MEMORY_ALLOCATION = 6; |
| 3718 | |
| 3719 | /** Error information for JSON pointer. */ |
| 3720 | typedef struct yyjson_ptr_err { |
| 3721 | /** Error code, see `yyjson_ptr_code` for all possible values. */ |
| 3722 | yyjson_ptr_code code; |
| 3723 | /** Error message, constant, no need to free (NULL if no error). */ |
| 3724 | const char *msg; |
| 3725 | /** Error byte position for input JSON pointer (0 if no error). */ |
| 3726 | size_t pos; |
| 3727 | } yyjson_ptr_err; |
| 3728 | |
| 3729 | /** |
| 3730 | A context for JSON pointer operation. |
| 3731 | |
| 3732 | This struct stores the context of JSON Pointer operation result. The struct |
| 3733 | can be used with three helper functions: `ctx_append()`, `ctx_replace()`, and |
| 3734 | `ctx_remove()`, which perform the corresponding operations on the container |
| 3735 | without re-parsing the JSON Pointer. |
| 3736 | |
| 3737 | For example: |
| 3738 | @code |
| 3739 | // doc before: {"a":[0,1,null]} |
| 3740 | // ptr: "/a/2" |
| 3741 | val = yyjson_mut_doc_ptr_getx(doc, ptr, strlen(ptr), &ctx, &err); |
| 3742 | if (yyjson_is_null(val)) { |
| 3743 | yyjson_ptr_ctx_remove(&ctx); |
| 3744 | } |
| 3745 | // doc after: {"a":[0,1]} |
| 3746 | @endcode |
| 3747 | */ |
| 3748 | typedef struct yyjson_ptr_ctx { |
| 3749 | /** |
| 3750 | The container (parent) of the target value. It can be either an array or |
| 3751 | an object. If the target location has no value, but all its parent |
| 3752 | containers exist, and the target location can be used to insert a new |
| 3753 | value, then `ctn` is the parent container of the target location. |
| 3754 | Otherwise, `ctn` is NULL. |
| 3755 | */ |
| 3756 | yyjson_mut_val *ctn; |
| 3757 | /** |
| 3758 | The previous sibling of the target value. It can be either a value in an |
| 3759 | array or a key in an object. As the container is a `circular linked list` |
| 3760 | of elements, `pre` is the previous node of the target value. If the |
| 3761 | operation is `add` or `set`, then `pre` is the previous node of the new |
| 3762 | value, not the original target value. If the target value does not exist, |
| 3763 | `pre` is NULL. |
| 3764 | */ |
| 3765 | yyjson_mut_val *pre; |
| 3766 | /** |
| 3767 | The removed value if the operation is `set`, `replace` or `remove`. It can |
| 3768 | be used to restore the original state of the document if needed. |
| 3769 | */ |
| 3770 | yyjson_mut_val *old; |
| 3771 | } yyjson_ptr_ctx; |
| 3772 | |
| 3773 | /** |
| 3774 | Get value by a JSON Pointer. |
| 3775 | @param doc The JSON document to be queried. |
| 3776 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 3777 | @return The value referenced by the JSON pointer. |
| 3778 | NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3779 | */ |
| 3780 | yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, |
| 3781 | const char *ptr); |
| 3782 | |
| 3783 | /** |
| 3784 | Get value by a JSON Pointer. |
| 3785 | @param doc The JSON document to be queried. |
| 3786 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3787 | @param len The length of `ptr` in bytes. |
| 3788 | @return The value referenced by the JSON pointer. |
| 3789 | NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3790 | */ |
| 3791 | yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, |
| 3792 | const char *ptr, size_t len); |
| 3793 | |
| 3794 | /** |
| 3795 | Get value by a JSON Pointer. |
| 3796 | @param doc The JSON document to be queried. |
| 3797 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3798 | @param len The length of `ptr` in bytes. |
| 3799 | @param err A pointer to store the error information, or NULL if not needed. |
| 3800 | @return The value referenced by the JSON pointer. |
| 3801 | NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3802 | */ |
| 3803 | yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, |
| 3804 | const char *ptr, size_t len, |
| 3805 | yyjson_ptr_err *err); |
| 3806 | |
| 3807 | /** |
| 3808 | Get value by a JSON Pointer. |
| 3809 | @param val The JSON value to be queried. |
| 3810 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 3811 | @return The value referenced by the JSON pointer. |
| 3812 | NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3813 | */ |
| 3814 | yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, |
| 3815 | const char *ptr); |
| 3816 | |
| 3817 | /** |
| 3818 | Get value by a JSON Pointer. |
| 3819 | @param val The JSON value to be queried. |
| 3820 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3821 | @param len The length of `ptr` in bytes. |
| 3822 | @return The value referenced by the JSON pointer. |
| 3823 | NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3824 | */ |
| 3825 | yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, |
| 3826 | const char *ptr, size_t len); |
| 3827 | |
| 3828 | /** |
| 3829 | Get value by a JSON Pointer. |
| 3830 | @param val The JSON value to be queried. |
| 3831 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3832 | @param len The length of `ptr` in bytes. |
| 3833 | @param err A pointer to store the error information, or NULL if not needed. |
| 3834 | @return The value referenced by the JSON pointer. |
| 3835 | NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3836 | */ |
| 3837 | yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, |
| 3838 | const char *ptr, size_t len, |
| 3839 | yyjson_ptr_err *err); |
| 3840 | |
| 3841 | /** |
| 3842 | Get value by a JSON Pointer. |
| 3843 | @param doc The JSON document to be queried. |
| 3844 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 3845 | @return The value referenced by the JSON pointer. |
| 3846 | NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3847 | */ |
| 3848 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, |
| 3849 | const char *ptr); |
| 3850 | |
| 3851 | /** |
| 3852 | Get value by a JSON Pointer. |
| 3853 | @param doc The JSON document to be queried. |
| 3854 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3855 | @param len The length of `ptr` in bytes. |
| 3856 | @return The value referenced by the JSON pointer. |
| 3857 | NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3858 | */ |
| 3859 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, |
| 3860 | const char *ptr, |
| 3861 | size_t len); |
| 3862 | |
| 3863 | /** |
| 3864 | Get value by a JSON Pointer. |
| 3865 | @param doc The JSON document to be queried. |
| 3866 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3867 | @param len The length of `ptr` in bytes. |
| 3868 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 3869 | @param err A pointer to store the error information, or NULL if not needed. |
| 3870 | @return The value referenced by the JSON pointer. |
| 3871 | NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3872 | */ |
| 3873 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, |
| 3874 | const char *ptr, |
| 3875 | size_t len, |
| 3876 | yyjson_ptr_ctx *ctx, |
| 3877 | yyjson_ptr_err *err); |
| 3878 | |
| 3879 | /** |
| 3880 | Get value by a JSON Pointer. |
| 3881 | @param val The JSON value to be queried. |
| 3882 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 3883 | @return The value referenced by the JSON pointer. |
| 3884 | NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3885 | */ |
| 3886 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, |
| 3887 | const char *ptr); |
| 3888 | |
| 3889 | /** |
| 3890 | Get value by a JSON Pointer. |
| 3891 | @param val The JSON value to be queried. |
| 3892 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3893 | @param len The length of `ptr` in bytes. |
| 3894 | @return The value referenced by the JSON pointer. |
| 3895 | NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3896 | */ |
| 3897 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, |
| 3898 | const char *ptr, |
| 3899 | size_t len); |
| 3900 | |
| 3901 | /** |
| 3902 | Get value by a JSON Pointer. |
| 3903 | @param val The JSON value to be queried. |
| 3904 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3905 | @param len The length of `ptr` in bytes. |
| 3906 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 3907 | @param err A pointer to store the error information, or NULL if not needed. |
| 3908 | @return The value referenced by the JSON pointer. |
| 3909 | NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. |
| 3910 | */ |
| 3911 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, |
| 3912 | const char *ptr, |
| 3913 | size_t len, |
| 3914 | yyjson_ptr_ctx *ctx, |
| 3915 | yyjson_ptr_err *err); |
| 3916 | |
| 3917 | /** |
| 3918 | Add (insert) value by a JSON pointer. |
| 3919 | @param doc The target JSON document. |
| 3920 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 3921 | @param new_val The value to be added. |
| 3922 | @return true if JSON pointer is valid and new value is added, false otherwise. |
| 3923 | @note The parent nodes will be created if they do not exist. |
| 3924 | */ |
| 3925 | yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, |
| 3926 | const char *ptr, |
| 3927 | yyjson_mut_val *new_val); |
| 3928 | |
| 3929 | /** |
| 3930 | Add (insert) value by a JSON pointer. |
| 3931 | @param doc The target JSON document. |
| 3932 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3933 | @param len The length of `ptr` in bytes. |
| 3934 | @param new_val The value to be added. |
| 3935 | @return true if JSON pointer is valid and new value is added, false otherwise. |
| 3936 | @note The parent nodes will be created if they do not exist. |
| 3937 | */ |
| 3938 | yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, |
| 3939 | const char *ptr, size_t len, |
| 3940 | yyjson_mut_val *new_val); |
| 3941 | |
| 3942 | /** |
| 3943 | Add (insert) value by a JSON pointer. |
| 3944 | @param doc The target JSON document. |
| 3945 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3946 | @param len The length of `ptr` in bytes. |
| 3947 | @param new_val The value to be added. |
| 3948 | @param create_parent Whether to create parent nodes if not exist. |
| 3949 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 3950 | @param err A pointer to store the error information, or NULL if not needed. |
| 3951 | @return true if JSON pointer is valid and new value is added, false otherwise. |
| 3952 | */ |
| 3953 | yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, |
| 3954 | const char *ptr, size_t len, |
| 3955 | yyjson_mut_val *new_val, |
| 3956 | bool create_parent, |
| 3957 | yyjson_ptr_ctx *ctx, |
| 3958 | yyjson_ptr_err *err); |
| 3959 | |
| 3960 | /** |
| 3961 | Add (insert) value by a JSON pointer. |
| 3962 | @param val The target JSON value. |
| 3963 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 3964 | @param doc Only used to create new values when needed. |
| 3965 | @param new_val The value to be added. |
| 3966 | @return true if JSON pointer is valid and new value is added, false otherwise. |
| 3967 | @note The parent nodes will be created if they do not exist. |
| 3968 | */ |
| 3969 | yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, |
| 3970 | const char *ptr, |
| 3971 | yyjson_mut_val *new_val, |
| 3972 | yyjson_mut_doc *doc); |
| 3973 | |
| 3974 | /** |
| 3975 | Add (insert) value by a JSON pointer. |
| 3976 | @param val The target JSON value. |
| 3977 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3978 | @param len The length of `ptr` in bytes. |
| 3979 | @param doc Only used to create new values when needed. |
| 3980 | @param new_val The value to be added. |
| 3981 | @return true if JSON pointer is valid and new value is added, false otherwise. |
| 3982 | @note The parent nodes will be created if they do not exist. |
| 3983 | */ |
| 3984 | yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, |
| 3985 | const char *ptr, size_t len, |
| 3986 | yyjson_mut_val *new_val, |
| 3987 | yyjson_mut_doc *doc); |
| 3988 | |
| 3989 | /** |
| 3990 | Add (insert) value by a JSON pointer. |
| 3991 | @param val The target JSON value. |
| 3992 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 3993 | @param len The length of `ptr` in bytes. |
| 3994 | @param doc Only used to create new values when needed. |
| 3995 | @param new_val The value to be added. |
| 3996 | @param create_parent Whether to create parent nodes if not exist. |
| 3997 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 3998 | @param err A pointer to store the error information, or NULL if not needed. |
| 3999 | @return true if JSON pointer is valid and new value is added, false otherwise. |
| 4000 | */ |
| 4001 | yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, |
| 4002 | const char *ptr, size_t len, |
| 4003 | yyjson_mut_val *new_val, |
| 4004 | yyjson_mut_doc *doc, |
| 4005 | bool create_parent, |
| 4006 | yyjson_ptr_ctx *ctx, |
| 4007 | yyjson_ptr_err *err); |
| 4008 | |
| 4009 | /** |
| 4010 | Set value by a JSON pointer. |
| 4011 | @param doc The target JSON document. |
| 4012 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 4013 | @param new_val The value to be set, pass NULL to remove. |
| 4014 | @return true if JSON pointer is valid and new value is set, false otherwise. |
| 4015 | @note The parent nodes will be created if they do not exist. |
| 4016 | If the target value already exists, it will be replaced by the new value. |
| 4017 | */ |
| 4018 | yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, |
| 4019 | const char *ptr, |
| 4020 | yyjson_mut_val *new_val); |
| 4021 | |
| 4022 | /** |
| 4023 | Set value by a JSON pointer. |
| 4024 | @param doc The target JSON document. |
| 4025 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4026 | @param len The length of `ptr` in bytes. |
| 4027 | @param new_val The value to be set, pass NULL to remove. |
| 4028 | @return true if JSON pointer is valid and new value is set, false otherwise. |
| 4029 | @note The parent nodes will be created if they do not exist. |
| 4030 | If the target value already exists, it will be replaced by the new value. |
| 4031 | */ |
| 4032 | yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, |
| 4033 | const char *ptr, size_t len, |
| 4034 | yyjson_mut_val *new_val); |
| 4035 | |
| 4036 | /** |
| 4037 | Set value by a JSON pointer. |
| 4038 | @param doc The target JSON document. |
| 4039 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4040 | @param len The length of `ptr` in bytes. |
| 4041 | @param new_val The value to be set, pass NULL to remove. |
| 4042 | @param create_parent Whether to create parent nodes if not exist. |
| 4043 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 4044 | @param err A pointer to store the error information, or NULL if not needed. |
| 4045 | @return true if JSON pointer is valid and new value is set, false otherwise. |
| 4046 | @note If the target value already exists, it will be replaced by the new value. |
| 4047 | */ |
| 4048 | yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, |
| 4049 | const char *ptr, size_t len, |
| 4050 | yyjson_mut_val *new_val, |
| 4051 | bool create_parent, |
| 4052 | yyjson_ptr_ctx *ctx, |
| 4053 | yyjson_ptr_err *err); |
| 4054 | |
| 4055 | /** |
| 4056 | Set value by a JSON pointer. |
| 4057 | @param val The target JSON value. |
| 4058 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 4059 | @param new_val The value to be set, pass NULL to remove. |
| 4060 | @param doc Only used to create new values when needed. |
| 4061 | @return true if JSON pointer is valid and new value is set, false otherwise. |
| 4062 | @note The parent nodes will be created if they do not exist. |
| 4063 | If the target value already exists, it will be replaced by the new value. |
| 4064 | */ |
| 4065 | yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, |
| 4066 | const char *ptr, |
| 4067 | yyjson_mut_val *new_val, |
| 4068 | yyjson_mut_doc *doc); |
| 4069 | |
| 4070 | /** |
| 4071 | Set value by a JSON pointer. |
| 4072 | @param val The target JSON value. |
| 4073 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4074 | @param len The length of `ptr` in bytes. |
| 4075 | @param new_val The value to be set, pass NULL to remove. |
| 4076 | @param doc Only used to create new values when needed. |
| 4077 | @return true if JSON pointer is valid and new value is set, false otherwise. |
| 4078 | @note The parent nodes will be created if they do not exist. |
| 4079 | If the target value already exists, it will be replaced by the new value. |
| 4080 | */ |
| 4081 | yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, |
| 4082 | const char *ptr, size_t len, |
| 4083 | yyjson_mut_val *new_val, |
| 4084 | yyjson_mut_doc *doc); |
| 4085 | |
| 4086 | /** |
| 4087 | Set value by a JSON pointer. |
| 4088 | @param val The target JSON value. |
| 4089 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4090 | @param len The length of `ptr` in bytes. |
| 4091 | @param new_val The value to be set, pass NULL to remove. |
| 4092 | @param doc Only used to create new values when needed. |
| 4093 | @param create_parent Whether to create parent nodes if not exist. |
| 4094 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 4095 | @param err A pointer to store the error information, or NULL if not needed. |
| 4096 | @return true if JSON pointer is valid and new value is set, false otherwise. |
| 4097 | @note If the target value already exists, it will be replaced by the new value. |
| 4098 | */ |
| 4099 | yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, |
| 4100 | const char *ptr, size_t len, |
| 4101 | yyjson_mut_val *new_val, |
| 4102 | yyjson_mut_doc *doc, |
| 4103 | bool create_parent, |
| 4104 | yyjson_ptr_ctx *ctx, |
| 4105 | yyjson_ptr_err *err); |
| 4106 | |
| 4107 | /** |
| 4108 | Replace value by a JSON pointer. |
| 4109 | @param doc The target JSON document. |
| 4110 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 4111 | @param new_val The new value to replace the old one. |
| 4112 | @return The old value that was replaced, or NULL if not found. |
| 4113 | */ |
| 4114 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( |
| 4115 | yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val); |
| 4116 | |
| 4117 | /** |
| 4118 | Replace value by a JSON pointer. |
| 4119 | @param doc The target JSON document. |
| 4120 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4121 | @param len The length of `ptr` in bytes. |
| 4122 | @param new_val The new value to replace the old one. |
| 4123 | @return The old value that was replaced, or NULL if not found. |
| 4124 | */ |
| 4125 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( |
| 4126 | yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val); |
| 4127 | |
| 4128 | /** |
| 4129 | Replace value by a JSON pointer. |
| 4130 | @param doc The target JSON document. |
| 4131 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4132 | @param len The length of `ptr` in bytes. |
| 4133 | @param new_val The new value to replace the old one. |
| 4134 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 4135 | @param err A pointer to store the error information, or NULL if not needed. |
| 4136 | @return The old value that was replaced, or NULL if not found. |
| 4137 | */ |
| 4138 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( |
| 4139 | yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, |
| 4140 | yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); |
| 4141 | |
| 4142 | /** |
| 4143 | Replace value by a JSON pointer. |
| 4144 | @param val The target JSON value. |
| 4145 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 4146 | @param new_val The new value to replace the old one. |
| 4147 | @return The old value that was replaced, or NULL if not found. |
| 4148 | */ |
| 4149 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( |
| 4150 | yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val); |
| 4151 | |
| 4152 | /** |
| 4153 | Replace value by a JSON pointer. |
| 4154 | @param val The target JSON value. |
| 4155 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4156 | @param len The length of `ptr` in bytes. |
| 4157 | @param new_val The new value to replace the old one. |
| 4158 | @return The old value that was replaced, or NULL if not found. |
| 4159 | */ |
| 4160 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( |
| 4161 | yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val); |
| 4162 | |
| 4163 | /** |
| 4164 | Replace value by a JSON pointer. |
| 4165 | @param val The target JSON value. |
| 4166 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4167 | @param len The length of `ptr` in bytes. |
| 4168 | @param new_val The new value to replace the old one. |
| 4169 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 4170 | @param err A pointer to store the error information, or NULL if not needed. |
| 4171 | @return The old value that was replaced, or NULL if not found. |
| 4172 | */ |
| 4173 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( |
| 4174 | yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, |
| 4175 | yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); |
| 4176 | |
| 4177 | /** |
| 4178 | Remove value by a JSON pointer. |
| 4179 | @param doc The target JSON document. |
| 4180 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 4181 | @return The removed value, or NULL on error. |
| 4182 | */ |
| 4183 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( |
| 4184 | yyjson_mut_doc *doc, const char *ptr); |
| 4185 | |
| 4186 | /** |
| 4187 | Remove value by a JSON pointer. |
| 4188 | @param doc The target JSON document. |
| 4189 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4190 | @param len The length of `ptr` in bytes. |
| 4191 | @return The removed value, or NULL on error. |
| 4192 | */ |
| 4193 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( |
| 4194 | yyjson_mut_doc *doc, const char *ptr, size_t len); |
| 4195 | |
| 4196 | /** |
| 4197 | Remove value by a JSON pointer. |
| 4198 | @param doc The target JSON document. |
| 4199 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4200 | @param len The length of `ptr` in bytes. |
| 4201 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 4202 | @param err A pointer to store the error information, or NULL if not needed. |
| 4203 | @return The removed value, or NULL on error. |
| 4204 | */ |
| 4205 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( |
| 4206 | yyjson_mut_doc *doc, const char *ptr, size_t len, |
| 4207 | yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); |
| 4208 | |
| 4209 | /** |
| 4210 | Remove value by a JSON pointer. |
| 4211 | @param val The target JSON value. |
| 4212 | @param ptr The JSON pointer string (UTF-8 with null-terminator). |
| 4213 | @return The removed value, or NULL on error. |
| 4214 | */ |
| 4215 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, |
| 4216 | const char *ptr); |
| 4217 | |
| 4218 | /** |
| 4219 | Remove value by a JSON pointer. |
| 4220 | @param val The target JSON value. |
| 4221 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4222 | @param len The length of `ptr` in bytes. |
| 4223 | @return The removed value, or NULL on error. |
| 4224 | */ |
| 4225 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, |
| 4226 | const char *ptr, |
| 4227 | size_t len); |
| 4228 | |
| 4229 | /** |
| 4230 | Remove value by a JSON pointer. |
| 4231 | @param val The target JSON value. |
| 4232 | @param ptr The JSON pointer string (UTF-8, null-terminator is not required). |
| 4233 | @param len The length of `ptr` in bytes. |
| 4234 | @param ctx A pointer to store the result context, or NULL if not needed. |
| 4235 | @param err A pointer to store the error information, or NULL if not needed. |
| 4236 | @return The removed value, or NULL on error. |
| 4237 | */ |
| 4238 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, |
| 4239 | const char *ptr, |
| 4240 | size_t len, |
| 4241 | yyjson_ptr_ctx *ctx, |
| 4242 | yyjson_ptr_err *err); |
| 4243 | |
| 4244 | /** |
| 4245 | Append value by JSON pointer context. |
| 4246 | @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. |
| 4247 | @param key New key if `ctx->ctn` is object, or NULL if `ctx->ctn` is array. |
| 4248 | @param val New value to be added. |
| 4249 | @return true on success or false on fail. |
| 4250 | */ |
| 4251 | yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, |
| 4252 | yyjson_mut_val *key, |
| 4253 | yyjson_mut_val *val); |
| 4254 | |
| 4255 | /** |
| 4256 | Replace value by JSON pointer context. |
| 4257 | @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. |
| 4258 | @param val New value to be replaced. |
| 4259 | @return true on success or false on fail. |
| 4260 | @note If success, the old value will be returned via `ctx->old`. |
| 4261 | */ |
| 4262 | yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, |
| 4263 | yyjson_mut_val *val); |
| 4264 | |
| 4265 | /** |
| 4266 | Remove value by JSON pointer context. |
| 4267 | @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. |
| 4268 | @return true on success or false on fail. |
| 4269 | @note If success, the old value will be returned via `ctx->old`. |
| 4270 | */ |
| 4271 | yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx); |
| 4272 | |
| 4273 | |
| 4274 | |
| 4275 | /*============================================================================== |
| 4276 | * JSON Patch API (RFC 6902) |
| 4277 | * https://tools.ietf.org/html/rfc6902 |
| 4278 | *============================================================================*/ |
| 4279 | |
| 4280 | /** Result code for JSON patch. */ |
| 4281 | typedef uint32_t yyjson_patch_code; |
| 4282 | |
| 4283 | /** Success, no error. */ |
| 4284 | static const yyjson_patch_code YYJSON_PATCH_SUCCESS = 0; |
| 4285 | |
| 4286 | /** Invalid parameter, such as NULL input or non-array patch. */ |
| 4287 | static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_PARAMETER = 1; |
| 4288 | |
| 4289 | /** Memory allocation failure occurs. */ |
| 4290 | static const yyjson_patch_code YYJSON_PATCH_ERROR_MEMORY_ALLOCATION = 2; |
| 4291 | |
| 4292 | /** JSON patch operation is not object type. */ |
| 4293 | static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_OPERATION = 3; |
| 4294 | |
| 4295 | /** JSON patch operation is missing a required key. */ |
| 4296 | static const yyjson_patch_code YYJSON_PATCH_ERROR_MISSING_KEY = 4; |
| 4297 | |
| 4298 | /** JSON patch operation member is invalid. */ |
| 4299 | static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_MEMBER = 5; |
| 4300 | |
| 4301 | /** JSON patch operation `test` not equal. */ |
| 4302 | static const yyjson_patch_code YYJSON_PATCH_ERROR_EQUAL = 6; |
| 4303 | |
| 4304 | /** JSON patch operation failed on JSON pointer. */ |
| 4305 | static const yyjson_patch_code YYJSON_PATCH_ERROR_POINTER = 7; |
| 4306 | |
| 4307 | /** Error information for JSON patch. */ |
| 4308 | typedef struct yyjson_patch_err { |
| 4309 | /** Error code, see `yyjson_patch_code` for all possible values. */ |
| 4310 | yyjson_patch_code code; |
| 4311 | /** Index of the error operation (0 if no error). */ |
| 4312 | size_t idx; |
| 4313 | /** Error message, constant, no need to free (NULL if no error). */ |
| 4314 | const char *msg; |
| 4315 | /** JSON pointer error if `code == YYJSON_PATCH_ERROR_POINTER`. */ |
| 4316 | yyjson_ptr_err ptr; |
| 4317 | } yyjson_patch_err; |
| 4318 | |
| 4319 | /** |
| 4320 | Creates and returns a patched JSON value (RFC 6902). |
| 4321 | The memory of the returned value is allocated by the `doc`. |
| 4322 | The `err` is used to receive error information, pass NULL if not needed. |
| 4323 | Returns NULL if the patch could not be applied. |
| 4324 | */ |
| 4325 | yyjson_api yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, |
| 4326 | yyjson_val *orig, |
| 4327 | yyjson_val *patch, |
| 4328 | yyjson_patch_err *err); |
| 4329 | |
| 4330 | /** |
| 4331 | Creates and returns a patched JSON value (RFC 6902). |
| 4332 | The memory of the returned value is allocated by the `doc`. |
| 4333 | The `err` is used to receive error information, pass NULL if not needed. |
| 4334 | Returns NULL if the patch could not be applied. |
| 4335 | */ |
| 4336 | yyjson_api yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, |
| 4337 | yyjson_mut_val *orig, |
| 4338 | yyjson_mut_val *patch, |
| 4339 | yyjson_patch_err *err); |
| 4340 | |
| 4341 | |
| 4342 | |
| 4343 | /*============================================================================== |
| 4344 | * JSON Merge-Patch API (RFC 7386) |
| 4345 | * https://tools.ietf.org/html/rfc7386 |
| 4346 | *============================================================================*/ |
| 4347 | |
| 4348 | /** |
| 4349 | Creates and returns a merge-patched JSON value (RFC 7386). |
| 4350 | The memory of the returned value is allocated by the `doc`. |
| 4351 | Returns NULL if the patch could not be applied. |
| 4352 | |
| 4353 | @warning This function is recursive and may cause a stack overflow if the |
| 4354 | object level is too deep. |
| 4355 | */ |
| 4356 | yyjson_api yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, |
| 4357 | yyjson_val *orig, |
| 4358 | yyjson_val *patch); |
| 4359 | |
| 4360 | /** |
| 4361 | Creates and returns a merge-patched JSON value (RFC 7386). |
| 4362 | The memory of the returned value is allocated by the `doc`. |
| 4363 | Returns NULL if the patch could not be applied. |
| 4364 | |
| 4365 | @warning This function is recursive and may cause a stack overflow if the |
| 4366 | object level is too deep. |
| 4367 | */ |
| 4368 | yyjson_api yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, |
| 4369 | yyjson_mut_val *orig, |
| 4370 | yyjson_mut_val *patch); |
| 4371 | |
| 4372 | |
| 4373 | |
| 4374 | /*============================================================================== |
| 4375 | * JSON Structure (Implementation) |
| 4376 | *============================================================================*/ |
| 4377 | |
| 4378 | /** Payload of a JSON value (8 bytes). */ |
| 4379 | typedef union yyjson_val_uni { |
| 4380 | uint64_t u64; |
| 4381 | int64_t i64; |
| 4382 | double f64; |
| 4383 | const char *str; |
| 4384 | void *ptr; |
| 4385 | size_t ofs; |
| 4386 | } yyjson_val_uni; |
| 4387 | |
| 4388 | /** |
| 4389 | Immutable JSON value, 16 bytes. |
| 4390 | */ |
| 4391 | struct yyjson_val { |
| 4392 | uint64_t tag; /**< type, subtype and length */ |
| 4393 | yyjson_val_uni uni; /**< payload */ |
| 4394 | }; |
| 4395 | |
| 4396 | struct yyjson_doc { |
| 4397 | /** Root value of the document (nonnull). */ |
| 4398 | yyjson_val *root; |
| 4399 | /** Allocator used by document (nonnull). */ |
| 4400 | yyjson_alc alc; |
| 4401 | /** The total number of bytes read when parsing JSON (nonzero). */ |
| 4402 | size_t dat_read; |
| 4403 | /** The total number of value read when parsing JSON (nonzero). */ |
| 4404 | size_t val_read; |
| 4405 | /** The string pool used by JSON values (nullable). */ |
| 4406 | char *str_pool; |
| 4407 | }; |
| 4408 | |
| 4409 | |
| 4410 | |
| 4411 | /*============================================================================== |
| 4412 | * Unsafe JSON Value API (Implementation) |
| 4413 | *============================================================================*/ |
| 4414 | |
| 4415 | /* |
| 4416 | Whether the string does not need to be escaped for serialization. |
| 4417 | This function is used to optimize the writing speed of small constant strings. |
| 4418 | This function works only if the compiler can evaluate it at compile time. |
| 4419 | |
| 4420 | Clang supports it since v8.0, |
| 4421 | earlier versions do not support constant_p(strlen) and return false. |
| 4422 | GCC supports it since at least v4.4, |
| 4423 | earlier versions may compile it as run-time instructions. |
| 4424 | ICC supports it since at least v16, |
| 4425 | earlier versions are uncertain. |
| 4426 | |
| 4427 | @param str The C string. |
| 4428 | @param len The returnd value from strlen(str). |
| 4429 | */ |
| 4430 | yyjson_api_inline bool unsafe_yyjson_is_str_noesc(const char *str, size_t len) { |
| 4431 | #if YYJSON_HAS_CONSTANT_P && (YYJSON_GCC_VER == 0 || YYJSON_GCC_VER >= 4) |
| 4432 | if (yyjson_constant_p(len) && len <= 32) { |
| 4433 | /* |
| 4434 | Same as the following loop: |
| 4435 | |
| 4436 | for (size_t i = 0; i < len; i++) { |
| 4437 | char c = str[i]; |
| 4438 | if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; |
| 4439 | } |
| 4440 | |
| 4441 | GCC evaluates it at compile time only if the string length is within 17 |
| 4442 | and -O3 (which turns on the -fpeel-loops flag) is used. |
| 4443 | So the loop is unrolled for GCC. |
| 4444 | */ |
| 4445 | # define yyjson_repeat32_incr(x) \ |
| 4446 | x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \ |
| 4447 | x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) \ |
| 4448 | x(16) x(17) x(18) x(19) x(20) x(21) x(22) x(23) \ |
| 4449 | x(24) x(25) x(26) x(27) x(28) x(29) x(30) x(31) |
| 4450 | # define yyjson_check_char_noesc(i) \ |
| 4451 | if (i < len) { \ |
| 4452 | char c = str[i]; \ |
| 4453 | if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; } |
| 4454 | yyjson_repeat32_incr(yyjson_check_char_noesc) |
| 4455 | # undef yyjson_repeat32_incr |
| 4456 | # undef yyjson_check_char_noesc |
| 4457 | return true; |
| 4458 | } |
| 4459 | #endif |
| 4460 | return false; |
| 4461 | } |
| 4462 | |
| 4463 | yyjson_api_inline yyjson_type unsafe_yyjson_get_type(void *val) { |
| 4464 | uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; |
| 4465 | return (yyjson_type)(tag & YYJSON_TYPE_MASK); |
| 4466 | } |
| 4467 | |
| 4468 | yyjson_api_inline yyjson_subtype unsafe_yyjson_get_subtype(void *val) { |
| 4469 | uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; |
| 4470 | return (yyjson_subtype)(tag & YYJSON_SUBTYPE_MASK); |
| 4471 | } |
| 4472 | |
| 4473 | yyjson_api_inline uint8_t unsafe_yyjson_get_tag(void *val) { |
| 4474 | uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; |
| 4475 | return (uint8_t)(tag & YYJSON_TAG_MASK); |
| 4476 | } |
| 4477 | |
| 4478 | yyjson_api_inline bool unsafe_yyjson_is_raw(void *val) { |
| 4479 | return unsafe_yyjson_get_type(val) == YYJSON_TYPE_RAW; |
| 4480 | } |
| 4481 | |
| 4482 | yyjson_api_inline bool unsafe_yyjson_is_null(void *val) { |
| 4483 | return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NULL; |
| 4484 | } |
| 4485 | |
| 4486 | yyjson_api_inline bool unsafe_yyjson_is_bool(void *val) { |
| 4487 | return unsafe_yyjson_get_type(val) == YYJSON_TYPE_BOOL; |
| 4488 | } |
| 4489 | |
| 4490 | yyjson_api_inline bool unsafe_yyjson_is_num(void *val) { |
| 4491 | return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NUM; |
| 4492 | } |
| 4493 | |
| 4494 | yyjson_api_inline bool unsafe_yyjson_is_str(void *val) { |
| 4495 | return unsafe_yyjson_get_type(val) == YYJSON_TYPE_STR; |
| 4496 | } |
| 4497 | |
| 4498 | yyjson_api_inline bool unsafe_yyjson_is_arr(void *val) { |
| 4499 | return unsafe_yyjson_get_type(val) == YYJSON_TYPE_ARR; |
| 4500 | } |
| 4501 | |
| 4502 | yyjson_api_inline bool unsafe_yyjson_is_obj(void *val) { |
| 4503 | return unsafe_yyjson_get_type(val) == YYJSON_TYPE_OBJ; |
| 4504 | } |
| 4505 | |
| 4506 | yyjson_api_inline bool unsafe_yyjson_is_ctn(void *val) { |
| 4507 | uint8_t mask = YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ; |
| 4508 | return (unsafe_yyjson_get_tag(val) & mask) == mask; |
| 4509 | } |
| 4510 | |
| 4511 | yyjson_api_inline bool unsafe_yyjson_is_uint(void *val) { |
| 4512 | const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 4513 | return unsafe_yyjson_get_tag(val) == patt; |
| 4514 | } |
| 4515 | |
| 4516 | yyjson_api_inline bool unsafe_yyjson_is_sint(void *val) { |
| 4517 | const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 4518 | return unsafe_yyjson_get_tag(val) == patt; |
| 4519 | } |
| 4520 | |
| 4521 | yyjson_api_inline bool unsafe_yyjson_is_int(void *val) { |
| 4522 | const uint8_t mask = YYJSON_TAG_MASK & (~YYJSON_SUBTYPE_SINT); |
| 4523 | const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 4524 | return (unsafe_yyjson_get_tag(val) & mask) == patt; |
| 4525 | } |
| 4526 | |
| 4527 | yyjson_api_inline bool unsafe_yyjson_is_real(void *val) { |
| 4528 | const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; |
| 4529 | return unsafe_yyjson_get_tag(val) == patt; |
| 4530 | } |
| 4531 | |
| 4532 | yyjson_api_inline bool unsafe_yyjson_is_true(void *val) { |
| 4533 | const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; |
| 4534 | return unsafe_yyjson_get_tag(val) == patt; |
| 4535 | } |
| 4536 | |
| 4537 | yyjson_api_inline bool unsafe_yyjson_is_false(void *val) { |
| 4538 | const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; |
| 4539 | return unsafe_yyjson_get_tag(val) == patt; |
| 4540 | } |
| 4541 | |
| 4542 | yyjson_api_inline bool unsafe_yyjson_arr_is_flat(yyjson_val *val) { |
| 4543 | size_t ofs = val->uni.ofs; |
| 4544 | size_t len = (size_t)(val->tag >> YYJSON_TAG_BIT); |
| 4545 | return len * sizeof(yyjson_val) + sizeof(yyjson_val) == ofs; |
| 4546 | } |
| 4547 | |
| 4548 | yyjson_api_inline const char *unsafe_yyjson_get_raw(void *val) { |
| 4549 | return ((yyjson_val *)val)->uni.str; |
| 4550 | } |
| 4551 | |
| 4552 | yyjson_api_inline bool unsafe_yyjson_get_bool(void *val) { |
| 4553 | uint8_t tag = unsafe_yyjson_get_tag(val); |
| 4554 | return (bool)((tag & YYJSON_SUBTYPE_MASK) >> YYJSON_TYPE_BIT); |
| 4555 | } |
| 4556 | |
| 4557 | yyjson_api_inline uint64_t unsafe_yyjson_get_uint(void *val) { |
| 4558 | return ((yyjson_val *)val)->uni.u64; |
| 4559 | } |
| 4560 | |
| 4561 | yyjson_api_inline int64_t unsafe_yyjson_get_sint(void *val) { |
| 4562 | return ((yyjson_val *)val)->uni.i64; |
| 4563 | } |
| 4564 | |
| 4565 | yyjson_api_inline int unsafe_yyjson_get_int(void *val) { |
| 4566 | return (int)((yyjson_val *)val)->uni.i64; |
| 4567 | } |
| 4568 | |
| 4569 | yyjson_api_inline double unsafe_yyjson_get_real(void *val) { |
| 4570 | return ((yyjson_val *)val)->uni.f64; |
| 4571 | } |
| 4572 | |
| 4573 | yyjson_api_inline double unsafe_yyjson_get_num(void *val) { |
| 4574 | uint8_t tag = unsafe_yyjson_get_tag(val); |
| 4575 | if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL)) { |
| 4576 | return ((yyjson_val *)val)->uni.f64; |
| 4577 | } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT)) { |
| 4578 | return (double)((yyjson_val *)val)->uni.i64; |
| 4579 | } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT)) { |
| 4580 | #if YYJSON_U64_TO_F64_NO_IMPL |
| 4581 | uint64_t msb = ((uint64_t)1) << 63; |
| 4582 | uint64_t num = ((yyjson_val *)val)->uni.u64; |
| 4583 | if ((num & msb) == 0) { |
| 4584 | return (double)(int64_t)num; |
| 4585 | } else { |
| 4586 | return ((double)(int64_t)((num >> 1) | (num & 1))) * (double)2.0; |
| 4587 | } |
| 4588 | #else |
| 4589 | return (double)((yyjson_val *)val)->uni.u64; |
| 4590 | #endif |
| 4591 | } |
| 4592 | return 0.0; |
| 4593 | } |
| 4594 | |
| 4595 | yyjson_api_inline const char *unsafe_yyjson_get_str(void *val) { |
| 4596 | return ((yyjson_val *)val)->uni.str; |
| 4597 | } |
| 4598 | |
| 4599 | yyjson_api_inline size_t unsafe_yyjson_get_len(void *val) { |
| 4600 | return (size_t)(((yyjson_val *)val)->tag >> YYJSON_TAG_BIT); |
| 4601 | } |
| 4602 | |
| 4603 | yyjson_api_inline yyjson_val *unsafe_yyjson_get_first(yyjson_val *ctn) { |
| 4604 | return ctn + 1; |
| 4605 | } |
| 4606 | |
| 4607 | yyjson_api_inline yyjson_val *unsafe_yyjson_get_next(yyjson_val *val) { |
| 4608 | bool is_ctn = unsafe_yyjson_is_ctn(val); |
| 4609 | size_t ctn_ofs = val->uni.ofs; |
| 4610 | size_t ofs = (is_ctn ? ctn_ofs : sizeof(yyjson_val)); |
| 4611 | return (yyjson_val *)(void *)((uint8_t *)val + ofs); |
| 4612 | } |
| 4613 | |
| 4614 | yyjson_api_inline bool unsafe_yyjson_equals_strn(void *val, const char *str, |
| 4615 | size_t len) { |
| 4616 | return unsafe_yyjson_get_len(val) == len && |
| 4617 | memcmp(((yyjson_val *)val)->uni.str, str, len) == 0; |
| 4618 | } |
| 4619 | |
| 4620 | yyjson_api_inline bool unsafe_yyjson_equals_str(void *val, const char *str) { |
| 4621 | return unsafe_yyjson_equals_strn(val, str, strlen(str)); |
| 4622 | } |
| 4623 | |
| 4624 | yyjson_api_inline void unsafe_yyjson_set_type(void *val, yyjson_type type, |
| 4625 | yyjson_subtype subtype) { |
| 4626 | uint8_t tag = (type | subtype); |
| 4627 | uint64_t new_tag = ((yyjson_val *)val)->tag; |
| 4628 | new_tag = (new_tag & (~(uint64_t)YYJSON_TAG_MASK)) | (uint64_t)tag; |
| 4629 | ((yyjson_val *)val)->tag = new_tag; |
| 4630 | } |
| 4631 | |
| 4632 | yyjson_api_inline void unsafe_yyjson_set_len(void *val, size_t len) { |
| 4633 | uint64_t tag = ((yyjson_val *)val)->tag & YYJSON_TAG_MASK; |
| 4634 | tag |= (uint64_t)len << YYJSON_TAG_BIT; |
| 4635 | ((yyjson_val *)val)->tag = tag; |
| 4636 | } |
| 4637 | |
| 4638 | yyjson_api_inline void unsafe_yyjson_inc_len(void *val) { |
| 4639 | uint64_t tag = ((yyjson_val *)val)->tag; |
| 4640 | tag += (uint64_t)(1 << YYJSON_TAG_BIT); |
| 4641 | ((yyjson_val *)val)->tag = tag; |
| 4642 | } |
| 4643 | |
| 4644 | yyjson_api_inline void unsafe_yyjson_set_raw(void *val, const char *raw, |
| 4645 | size_t len) { |
| 4646 | unsafe_yyjson_set_type(val, YYJSON_TYPE_RAW, YYJSON_SUBTYPE_NONE); |
| 4647 | unsafe_yyjson_set_len(val, len); |
| 4648 | ((yyjson_val *)val)->uni.str = raw; |
| 4649 | } |
| 4650 | |
| 4651 | yyjson_api_inline void unsafe_yyjson_set_null(void *val) { |
| 4652 | unsafe_yyjson_set_type(val, YYJSON_TYPE_NULL, YYJSON_SUBTYPE_NONE); |
| 4653 | unsafe_yyjson_set_len(val, 0); |
| 4654 | } |
| 4655 | |
| 4656 | yyjson_api_inline void unsafe_yyjson_set_bool(void *val, bool num) { |
| 4657 | yyjson_subtype subtype = num ? YYJSON_SUBTYPE_TRUE : YYJSON_SUBTYPE_FALSE; |
| 4658 | unsafe_yyjson_set_type(val, YYJSON_TYPE_BOOL, subtype); |
| 4659 | unsafe_yyjson_set_len(val, 0); |
| 4660 | } |
| 4661 | |
| 4662 | yyjson_api_inline void unsafe_yyjson_set_uint(void *val, uint64_t num) { |
| 4663 | unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_UINT); |
| 4664 | unsafe_yyjson_set_len(val, 0); |
| 4665 | ((yyjson_val *)val)->uni.u64 = num; |
| 4666 | } |
| 4667 | |
| 4668 | yyjson_api_inline void unsafe_yyjson_set_sint(void *val, int64_t num) { |
| 4669 | unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_SINT); |
| 4670 | unsafe_yyjson_set_len(val, 0); |
| 4671 | ((yyjson_val *)val)->uni.i64 = num; |
| 4672 | } |
| 4673 | |
| 4674 | yyjson_api_inline void unsafe_yyjson_set_real(void *val, double num) { |
| 4675 | unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_REAL); |
| 4676 | unsafe_yyjson_set_len(val, 0); |
| 4677 | ((yyjson_val *)val)->uni.f64 = num; |
| 4678 | } |
| 4679 | |
| 4680 | yyjson_api_inline void unsafe_yyjson_set_str(void *val, const char *str) { |
| 4681 | size_t len = strlen(str); |
| 4682 | bool noesc = unsafe_yyjson_is_str_noesc(str, len); |
| 4683 | yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; |
| 4684 | unsafe_yyjson_set_type(val, YYJSON_TYPE_STR, sub); |
| 4685 | unsafe_yyjson_set_len(val, len); |
| 4686 | ((yyjson_val *)val)->uni.str = str; |
| 4687 | } |
| 4688 | |
| 4689 | yyjson_api_inline void unsafe_yyjson_set_strn(void *val, const char *str, |
| 4690 | size_t len) { |
| 4691 | unsafe_yyjson_set_type(val, YYJSON_TYPE_STR, YYJSON_SUBTYPE_NONE); |
| 4692 | unsafe_yyjson_set_len(val, len); |
| 4693 | ((yyjson_val *)val)->uni.str = str; |
| 4694 | } |
| 4695 | |
| 4696 | yyjson_api_inline void unsafe_yyjson_set_arr(void *val, size_t size) { |
| 4697 | unsafe_yyjson_set_type(val, YYJSON_TYPE_ARR, YYJSON_SUBTYPE_NONE); |
| 4698 | unsafe_yyjson_set_len(val, size); |
| 4699 | } |
| 4700 | |
| 4701 | yyjson_api_inline void unsafe_yyjson_set_obj(void *val, size_t size) { |
| 4702 | unsafe_yyjson_set_type(val, YYJSON_TYPE_OBJ, YYJSON_SUBTYPE_NONE); |
| 4703 | unsafe_yyjson_set_len(val, size); |
| 4704 | } |
| 4705 | |
| 4706 | |
| 4707 | |
| 4708 | /*============================================================================== |
| 4709 | * JSON Document API (Implementation) |
| 4710 | *============================================================================*/ |
| 4711 | |
| 4712 | yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc) { |
| 4713 | return doc ? doc->root : NULL; |
| 4714 | } |
| 4715 | |
| 4716 | yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc) { |
| 4717 | return doc ? doc->dat_read : 0; |
| 4718 | } |
| 4719 | |
| 4720 | yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) { |
| 4721 | return doc ? doc->val_read : 0; |
| 4722 | } |
| 4723 | |
| 4724 | yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc) { |
| 4725 | if (doc) { |
| 4726 | yyjson_alc alc = doc->alc; |
| 4727 | if (doc->str_pool) alc.free(alc.ctx, doc->str_pool); |
| 4728 | alc.free(alc.ctx, doc); |
| 4729 | } |
| 4730 | } |
| 4731 | |
| 4732 | |
| 4733 | |
| 4734 | /*============================================================================== |
| 4735 | * JSON Value Type API (Implementation) |
| 4736 | *============================================================================*/ |
| 4737 | |
| 4738 | yyjson_api_inline bool yyjson_is_raw(yyjson_val *val) { |
| 4739 | return val ? unsafe_yyjson_is_raw(val) : false; |
| 4740 | } |
| 4741 | |
| 4742 | yyjson_api_inline bool yyjson_is_null(yyjson_val *val) { |
| 4743 | return val ? unsafe_yyjson_is_null(val) : false; |
| 4744 | } |
| 4745 | |
| 4746 | yyjson_api_inline bool yyjson_is_true(yyjson_val *val) { |
| 4747 | return val ? unsafe_yyjson_is_true(val) : false; |
| 4748 | } |
| 4749 | |
| 4750 | yyjson_api_inline bool yyjson_is_false(yyjson_val *val) { |
| 4751 | return val ? unsafe_yyjson_is_false(val) : false; |
| 4752 | } |
| 4753 | |
| 4754 | yyjson_api_inline bool yyjson_is_bool(yyjson_val *val) { |
| 4755 | return val ? unsafe_yyjson_is_bool(val) : false; |
| 4756 | } |
| 4757 | |
| 4758 | yyjson_api_inline bool yyjson_is_uint(yyjson_val *val) { |
| 4759 | return val ? unsafe_yyjson_is_uint(val) : false; |
| 4760 | } |
| 4761 | |
| 4762 | yyjson_api_inline bool yyjson_is_sint(yyjson_val *val) { |
| 4763 | return val ? unsafe_yyjson_is_sint(val) : false; |
| 4764 | } |
| 4765 | |
| 4766 | yyjson_api_inline bool yyjson_is_int(yyjson_val *val) { |
| 4767 | return val ? unsafe_yyjson_is_int(val) : false; |
| 4768 | } |
| 4769 | |
| 4770 | yyjson_api_inline bool yyjson_is_real(yyjson_val *val) { |
| 4771 | return val ? unsafe_yyjson_is_real(val) : false; |
| 4772 | } |
| 4773 | |
| 4774 | yyjson_api_inline bool yyjson_is_num(yyjson_val *val) { |
| 4775 | return val ? unsafe_yyjson_is_num(val) : false; |
| 4776 | } |
| 4777 | |
| 4778 | yyjson_api_inline bool yyjson_is_str(yyjson_val *val) { |
| 4779 | return val ? unsafe_yyjson_is_str(val) : false; |
| 4780 | } |
| 4781 | |
| 4782 | yyjson_api_inline bool yyjson_is_arr(yyjson_val *val) { |
| 4783 | return val ? unsafe_yyjson_is_arr(val) : false; |
| 4784 | } |
| 4785 | |
| 4786 | yyjson_api_inline bool yyjson_is_obj(yyjson_val *val) { |
| 4787 | return val ? unsafe_yyjson_is_obj(val) : false; |
| 4788 | } |
| 4789 | |
| 4790 | yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val) { |
| 4791 | return val ? unsafe_yyjson_is_ctn(val) : false; |
| 4792 | } |
| 4793 | |
| 4794 | |
| 4795 | |
| 4796 | /*============================================================================== |
| 4797 | * JSON Value Content API (Implementation) |
| 4798 | *============================================================================*/ |
| 4799 | |
| 4800 | yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val) { |
| 4801 | return val ? unsafe_yyjson_get_type(val) : YYJSON_TYPE_NONE; |
| 4802 | } |
| 4803 | |
| 4804 | yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val) { |
| 4805 | return val ? unsafe_yyjson_get_subtype(val) : YYJSON_SUBTYPE_NONE; |
| 4806 | } |
| 4807 | |
| 4808 | yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val) { |
| 4809 | return val ? unsafe_yyjson_get_tag(val) : 0; |
| 4810 | } |
| 4811 | |
| 4812 | yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val) { |
| 4813 | switch (yyjson_get_tag(val)) { |
| 4814 | case YYJSON_TYPE_RAW | YYJSON_SUBTYPE_NONE: return "raw" ; |
| 4815 | case YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE: return "null" ; |
| 4816 | case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NONE: return "string" ; |
| 4817 | case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC: return "string" ; |
| 4818 | case YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE: return "array" ; |
| 4819 | case YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE: return "object" ; |
| 4820 | case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE: return "true" ; |
| 4821 | case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE: return "false" ; |
| 4822 | case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT: return "uint" ; |
| 4823 | case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT: return "sint" ; |
| 4824 | case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL: return "real" ; |
| 4825 | default: return "unknown" ; |
| 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val) { |
| 4830 | return yyjson_is_raw(val) ? unsafe_yyjson_get_raw(val) : NULL; |
| 4831 | } |
| 4832 | |
| 4833 | yyjson_api_inline bool yyjson_get_bool(yyjson_val *val) { |
| 4834 | return yyjson_is_bool(val) ? unsafe_yyjson_get_bool(val) : false; |
| 4835 | } |
| 4836 | |
| 4837 | yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val) { |
| 4838 | return yyjson_is_int(val) ? unsafe_yyjson_get_uint(val) : 0; |
| 4839 | } |
| 4840 | |
| 4841 | yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val) { |
| 4842 | return yyjson_is_int(val) ? unsafe_yyjson_get_sint(val) : 0; |
| 4843 | } |
| 4844 | |
| 4845 | yyjson_api_inline int yyjson_get_int(yyjson_val *val) { |
| 4846 | return yyjson_is_int(val) ? unsafe_yyjson_get_int(val) : 0; |
| 4847 | } |
| 4848 | |
| 4849 | yyjson_api_inline double yyjson_get_real(yyjson_val *val) { |
| 4850 | return yyjson_is_real(val) ? unsafe_yyjson_get_real(val) : 0.0; |
| 4851 | } |
| 4852 | |
| 4853 | yyjson_api_inline double yyjson_get_num(yyjson_val *val) { |
| 4854 | return val ? unsafe_yyjson_get_num(val) : 0.0; |
| 4855 | } |
| 4856 | |
| 4857 | yyjson_api_inline const char *yyjson_get_str(yyjson_val *val) { |
| 4858 | return yyjson_is_str(val) ? unsafe_yyjson_get_str(val) : NULL; |
| 4859 | } |
| 4860 | |
| 4861 | yyjson_api_inline size_t yyjson_get_len(yyjson_val *val) { |
| 4862 | return val ? unsafe_yyjson_get_len(val) : 0; |
| 4863 | } |
| 4864 | |
| 4865 | yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str) { |
| 4866 | if (yyjson_likely(val && str)) { |
| 4867 | return unsafe_yyjson_is_str(val) && |
| 4868 | unsafe_yyjson_equals_str(val, str); |
| 4869 | } |
| 4870 | return false; |
| 4871 | } |
| 4872 | |
| 4873 | yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, |
| 4874 | size_t len) { |
| 4875 | if (yyjson_likely(val && str)) { |
| 4876 | return unsafe_yyjson_is_str(val) && |
| 4877 | unsafe_yyjson_equals_strn(val, str, len); |
| 4878 | } |
| 4879 | return false; |
| 4880 | } |
| 4881 | |
| 4882 | yyjson_api bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); |
| 4883 | |
| 4884 | yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { |
| 4885 | if (yyjson_unlikely(!lhs || !rhs)) return false; |
| 4886 | return unsafe_yyjson_equals(lhs, rhs); |
| 4887 | } |
| 4888 | |
| 4889 | yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, |
| 4890 | const char *raw, size_t len) { |
| 4891 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4892 | unsafe_yyjson_set_raw(val, raw, len); |
| 4893 | return true; |
| 4894 | } |
| 4895 | |
| 4896 | yyjson_api_inline bool yyjson_set_null(yyjson_val *val) { |
| 4897 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4898 | unsafe_yyjson_set_null(val); |
| 4899 | return true; |
| 4900 | } |
| 4901 | |
| 4902 | yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num) { |
| 4903 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4904 | unsafe_yyjson_set_bool(val, num); |
| 4905 | return true; |
| 4906 | } |
| 4907 | |
| 4908 | yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num) { |
| 4909 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4910 | unsafe_yyjson_set_uint(val, num); |
| 4911 | return true; |
| 4912 | } |
| 4913 | |
| 4914 | yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num) { |
| 4915 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4916 | unsafe_yyjson_set_sint(val, num); |
| 4917 | return true; |
| 4918 | } |
| 4919 | |
| 4920 | yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num) { |
| 4921 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4922 | unsafe_yyjson_set_sint(val, (int64_t)num); |
| 4923 | return true; |
| 4924 | } |
| 4925 | |
| 4926 | yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num) { |
| 4927 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4928 | unsafe_yyjson_set_real(val, num); |
| 4929 | return true; |
| 4930 | } |
| 4931 | |
| 4932 | yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str) { |
| 4933 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4934 | if (yyjson_unlikely(!str)) return false; |
| 4935 | unsafe_yyjson_set_str(val, str); |
| 4936 | return true; |
| 4937 | } |
| 4938 | |
| 4939 | yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, |
| 4940 | const char *str, size_t len) { |
| 4941 | if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; |
| 4942 | if (yyjson_unlikely(!str)) return false; |
| 4943 | unsafe_yyjson_set_strn(val, str, len); |
| 4944 | return true; |
| 4945 | } |
| 4946 | |
| 4947 | |
| 4948 | |
| 4949 | /*============================================================================== |
| 4950 | * JSON Array API (Implementation) |
| 4951 | *============================================================================*/ |
| 4952 | |
| 4953 | yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr) { |
| 4954 | return yyjson_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; |
| 4955 | } |
| 4956 | |
| 4957 | yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx) { |
| 4958 | if (yyjson_likely(yyjson_is_arr(arr))) { |
| 4959 | if (yyjson_likely(unsafe_yyjson_get_len(arr) > idx)) { |
| 4960 | yyjson_val *val = unsafe_yyjson_get_first(arr); |
| 4961 | if (unsafe_yyjson_arr_is_flat(arr)) { |
| 4962 | return val + idx; |
| 4963 | } else { |
| 4964 | while (idx-- > 0) val = unsafe_yyjson_get_next(val); |
| 4965 | return val; |
| 4966 | } |
| 4967 | } |
| 4968 | } |
| 4969 | return NULL; |
| 4970 | } |
| 4971 | |
| 4972 | yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr) { |
| 4973 | if (yyjson_likely(yyjson_is_arr(arr))) { |
| 4974 | if (yyjson_likely(unsafe_yyjson_get_len(arr) > 0)) { |
| 4975 | return unsafe_yyjson_get_first(arr); |
| 4976 | } |
| 4977 | } |
| 4978 | return NULL; |
| 4979 | } |
| 4980 | |
| 4981 | yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr) { |
| 4982 | if (yyjson_likely(yyjson_is_arr(arr))) { |
| 4983 | size_t len = unsafe_yyjson_get_len(arr); |
| 4984 | if (yyjson_likely(len > 0)) { |
| 4985 | yyjson_val *val = unsafe_yyjson_get_first(arr); |
| 4986 | if (unsafe_yyjson_arr_is_flat(arr)) { |
| 4987 | return val + (len - 1); |
| 4988 | } else { |
| 4989 | while (len-- > 1) val = unsafe_yyjson_get_next(val); |
| 4990 | return val; |
| 4991 | } |
| 4992 | } |
| 4993 | } |
| 4994 | return NULL; |
| 4995 | } |
| 4996 | |
| 4997 | |
| 4998 | |
| 4999 | /*============================================================================== |
| 5000 | * JSON Array Iterator API (Implementation) |
| 5001 | *============================================================================*/ |
| 5002 | |
| 5003 | yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, |
| 5004 | yyjson_arr_iter *iter) { |
| 5005 | if (yyjson_likely(yyjson_is_arr(arr) && iter)) { |
| 5006 | iter->idx = 0; |
| 5007 | iter->max = unsafe_yyjson_get_len(arr); |
| 5008 | iter->cur = unsafe_yyjson_get_first(arr); |
| 5009 | return true; |
| 5010 | } |
| 5011 | if (iter) memset(iter, 0, sizeof(yyjson_arr_iter)); |
| 5012 | return false; |
| 5013 | } |
| 5014 | |
| 5015 | yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr) { |
| 5016 | yyjson_arr_iter iter; |
| 5017 | yyjson_arr_iter_init(arr, &iter); |
| 5018 | return iter; |
| 5019 | } |
| 5020 | |
| 5021 | yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter) { |
| 5022 | return iter ? iter->idx < iter->max : false; |
| 5023 | } |
| 5024 | |
| 5025 | yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter) { |
| 5026 | yyjson_val *val; |
| 5027 | if (iter && iter->idx < iter->max) { |
| 5028 | val = iter->cur; |
| 5029 | iter->cur = unsafe_yyjson_get_next(val); |
| 5030 | iter->idx++; |
| 5031 | return val; |
| 5032 | } |
| 5033 | return NULL; |
| 5034 | } |
| 5035 | |
| 5036 | |
| 5037 | |
| 5038 | /*============================================================================== |
| 5039 | * JSON Object API (Implementation) |
| 5040 | *============================================================================*/ |
| 5041 | |
| 5042 | yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj) { |
| 5043 | return yyjson_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; |
| 5044 | } |
| 5045 | |
| 5046 | yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, |
| 5047 | const char *key) { |
| 5048 | return yyjson_obj_getn(obj, key, key ? strlen(key) : 0); |
| 5049 | } |
| 5050 | |
| 5051 | yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, |
| 5052 | const char *_key, |
| 5053 | size_t key_len) { |
| 5054 | if (yyjson_likely(yyjson_is_obj(obj) && _key)) { |
| 5055 | size_t len = unsafe_yyjson_get_len(obj); |
| 5056 | yyjson_val *key = unsafe_yyjson_get_first(obj); |
| 5057 | while (len-- > 0) { |
| 5058 | if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key + 1; |
| 5059 | key = unsafe_yyjson_get_next(key + 1); |
| 5060 | } |
| 5061 | } |
| 5062 | return NULL; |
| 5063 | } |
| 5064 | |
| 5065 | |
| 5066 | |
| 5067 | /*============================================================================== |
| 5068 | * JSON Object Iterator API (Implementation) |
| 5069 | *============================================================================*/ |
| 5070 | |
| 5071 | yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, |
| 5072 | yyjson_obj_iter *iter) { |
| 5073 | if (yyjson_likely(yyjson_is_obj(obj) && iter)) { |
| 5074 | iter->idx = 0; |
| 5075 | iter->max = unsafe_yyjson_get_len(obj); |
| 5076 | iter->cur = unsafe_yyjson_get_first(obj); |
| 5077 | iter->obj = obj; |
| 5078 | return true; |
| 5079 | } |
| 5080 | if (iter) memset(iter, 0, sizeof(yyjson_obj_iter)); |
| 5081 | return false; |
| 5082 | } |
| 5083 | |
| 5084 | yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj) { |
| 5085 | yyjson_obj_iter iter; |
| 5086 | yyjson_obj_iter_init(obj, &iter); |
| 5087 | return iter; |
| 5088 | } |
| 5089 | |
| 5090 | yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter) { |
| 5091 | return iter ? iter->idx < iter->max : false; |
| 5092 | } |
| 5093 | |
| 5094 | yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter) { |
| 5095 | if (iter && iter->idx < iter->max) { |
| 5096 | yyjson_val *key = iter->cur; |
| 5097 | iter->idx++; |
| 5098 | iter->cur = unsafe_yyjson_get_next(key + 1); |
| 5099 | return key; |
| 5100 | } |
| 5101 | return NULL; |
| 5102 | } |
| 5103 | |
| 5104 | yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key) { |
| 5105 | return key ? key + 1 : NULL; |
| 5106 | } |
| 5107 | |
| 5108 | yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, |
| 5109 | const char *key) { |
| 5110 | return yyjson_obj_iter_getn(iter, key, key ? strlen(key) : 0); |
| 5111 | } |
| 5112 | |
| 5113 | yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, |
| 5114 | const char *key, |
| 5115 | size_t key_len) { |
| 5116 | if (iter && key) { |
| 5117 | size_t idx = iter->idx; |
| 5118 | size_t max = iter->max; |
| 5119 | yyjson_val *cur = iter->cur; |
| 5120 | if (yyjson_unlikely(idx == max)) { |
| 5121 | idx = 0; |
| 5122 | cur = unsafe_yyjson_get_first(iter->obj); |
| 5123 | } |
| 5124 | while (idx++ < max) { |
| 5125 | yyjson_val *next = unsafe_yyjson_get_next(cur + 1); |
| 5126 | if (unsafe_yyjson_equals_strn(cur, key, key_len)) { |
| 5127 | iter->idx = idx; |
| 5128 | iter->cur = next; |
| 5129 | return cur + 1; |
| 5130 | } |
| 5131 | cur = next; |
| 5132 | if (idx == iter->max && iter->idx < iter->max) { |
| 5133 | idx = 0; |
| 5134 | max = iter->idx; |
| 5135 | cur = unsafe_yyjson_get_first(iter->obj); |
| 5136 | } |
| 5137 | } |
| 5138 | } |
| 5139 | return NULL; |
| 5140 | } |
| 5141 | |
| 5142 | |
| 5143 | |
| 5144 | /*============================================================================== |
| 5145 | * Mutable JSON Structure (Implementation) |
| 5146 | *============================================================================*/ |
| 5147 | |
| 5148 | /** |
| 5149 | Mutable JSON value, 24 bytes. |
| 5150 | The 'tag' and 'uni' field is same as immutable value. |
| 5151 | The 'next' field links all elements inside the container to be a cycle. |
| 5152 | */ |
| 5153 | struct yyjson_mut_val { |
| 5154 | uint64_t tag; /**< type, subtype and length */ |
| 5155 | yyjson_val_uni uni; /**< payload */ |
| 5156 | yyjson_mut_val *next; /**< the next value in circular linked list */ |
| 5157 | }; |
| 5158 | |
| 5159 | /** |
| 5160 | A memory chunk in string memory pool. |
| 5161 | */ |
| 5162 | typedef struct yyjson_str_chunk { |
| 5163 | struct yyjson_str_chunk *next; /* next chunk linked list */ |
| 5164 | size_t chunk_size; /* chunk size in bytes */ |
| 5165 | /* char str[]; flexible array member */ |
| 5166 | } yyjson_str_chunk; |
| 5167 | |
| 5168 | /** |
| 5169 | A memory pool to hold all strings in a mutable document. |
| 5170 | */ |
| 5171 | typedef struct yyjson_str_pool { |
| 5172 | char *cur; /* cursor inside current chunk */ |
| 5173 | char *end; /* the end of current chunk */ |
| 5174 | size_t chunk_size; /* chunk size in bytes while creating new chunk */ |
| 5175 | size_t chunk_size_max; /* maximum chunk size in bytes */ |
| 5176 | yyjson_str_chunk *chunks; /* a linked list of chunks, nullable */ |
| 5177 | } yyjson_str_pool; |
| 5178 | |
| 5179 | /** |
| 5180 | A memory chunk in value memory pool. |
| 5181 | `sizeof(yyjson_val_chunk)` should not larger than `sizeof(yyjson_mut_val)`. |
| 5182 | */ |
| 5183 | typedef struct yyjson_val_chunk { |
| 5184 | struct yyjson_val_chunk *next; /* next chunk linked list */ |
| 5185 | size_t chunk_size; /* chunk size in bytes */ |
| 5186 | /* char pad[sizeof(yyjson_mut_val) - sizeof(yyjson_val_chunk)]; padding */ |
| 5187 | /* yyjson_mut_val vals[]; flexible array member */ |
| 5188 | } yyjson_val_chunk; |
| 5189 | |
| 5190 | /** |
| 5191 | A memory pool to hold all values in a mutable document. |
| 5192 | */ |
| 5193 | typedef struct yyjson_val_pool { |
| 5194 | yyjson_mut_val *cur; /* cursor inside current chunk */ |
| 5195 | yyjson_mut_val *end; /* the end of current chunk */ |
| 5196 | size_t chunk_size; /* chunk size in bytes while creating new chunk */ |
| 5197 | size_t chunk_size_max; /* maximum chunk size in bytes */ |
| 5198 | yyjson_val_chunk *chunks; /* a linked list of chunks, nullable */ |
| 5199 | } yyjson_val_pool; |
| 5200 | |
| 5201 | struct yyjson_mut_doc { |
| 5202 | yyjson_mut_val *root; /**< root value of the JSON document, nullable */ |
| 5203 | yyjson_alc alc; /**< a valid allocator, nonnull */ |
| 5204 | yyjson_str_pool str_pool; /**< string memory pool */ |
| 5205 | yyjson_val_pool val_pool; /**< value memory pool */ |
| 5206 | }; |
| 5207 | |
| 5208 | /* Ensures the capacity to at least equal to the specified byte length. */ |
| 5209 | yyjson_api bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, |
| 5210 | const yyjson_alc *alc, |
| 5211 | size_t len); |
| 5212 | |
| 5213 | /* Ensures the capacity to at least equal to the specified value count. */ |
| 5214 | yyjson_api bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, |
| 5215 | const yyjson_alc *alc, |
| 5216 | size_t count); |
| 5217 | |
| 5218 | /* Allocate memory for string. */ |
| 5219 | yyjson_api_inline char *unsafe_yyjson_mut_str_alc(yyjson_mut_doc *doc, |
| 5220 | size_t len) { |
| 5221 | char *mem; |
| 5222 | const yyjson_alc *alc = &doc->alc; |
| 5223 | yyjson_str_pool *pool = &doc->str_pool; |
| 5224 | if (yyjson_unlikely((size_t)(pool->end - pool->cur) <= len)) { |
| 5225 | if (yyjson_unlikely(!unsafe_yyjson_str_pool_grow(pool, alc, len + 1))) { |
| 5226 | return NULL; |
| 5227 | } |
| 5228 | } |
| 5229 | mem = pool->cur; |
| 5230 | pool->cur = mem + len + 1; |
| 5231 | return mem; |
| 5232 | } |
| 5233 | |
| 5234 | yyjson_api_inline char *unsafe_yyjson_mut_strncpy(yyjson_mut_doc *doc, |
| 5235 | const char *str, size_t len) { |
| 5236 | char *mem = unsafe_yyjson_mut_str_alc(doc, len); |
| 5237 | if (yyjson_unlikely(!mem)) return NULL; |
| 5238 | memcpy((void *)mem, (const void *)str, len); |
| 5239 | mem[len] = '\0'; |
| 5240 | return mem; |
| 5241 | } |
| 5242 | |
| 5243 | yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_val(yyjson_mut_doc *doc, |
| 5244 | size_t count) { |
| 5245 | yyjson_mut_val *val; |
| 5246 | yyjson_alc *alc = &doc->alc; |
| 5247 | yyjson_val_pool *pool = &doc->val_pool; |
| 5248 | if (yyjson_unlikely((size_t)(pool->end - pool->cur) < count)) { |
| 5249 | if (yyjson_unlikely(!unsafe_yyjson_val_pool_grow(pool, alc, count))) { |
| 5250 | return NULL; |
| 5251 | } |
| 5252 | } |
| 5253 | val = pool->cur; |
| 5254 | pool->cur += count; |
| 5255 | return val; |
| 5256 | } |
| 5257 | |
| 5258 | |
| 5259 | |
| 5260 | /*============================================================================== |
| 5261 | * Mutable JSON Document API (Implementation) |
| 5262 | *============================================================================*/ |
| 5263 | |
| 5264 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc) { |
| 5265 | return doc ? doc->root : NULL; |
| 5266 | } |
| 5267 | |
| 5268 | yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, |
| 5269 | yyjson_mut_val *root) { |
| 5270 | if (doc) doc->root = root; |
| 5271 | } |
| 5272 | |
| 5273 | |
| 5274 | |
| 5275 | /*============================================================================== |
| 5276 | * Mutable JSON Value Type API (Implementation) |
| 5277 | *============================================================================*/ |
| 5278 | |
| 5279 | yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val) { |
| 5280 | return val ? unsafe_yyjson_is_raw(val) : false; |
| 5281 | } |
| 5282 | |
| 5283 | yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val) { |
| 5284 | return val ? unsafe_yyjson_is_null(val) : false; |
| 5285 | } |
| 5286 | |
| 5287 | yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val) { |
| 5288 | return val ? unsafe_yyjson_is_true(val) : false; |
| 5289 | } |
| 5290 | |
| 5291 | yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val) { |
| 5292 | return val ? unsafe_yyjson_is_false(val) : false; |
| 5293 | } |
| 5294 | |
| 5295 | yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val) { |
| 5296 | return val ? unsafe_yyjson_is_bool(val) : false; |
| 5297 | } |
| 5298 | |
| 5299 | yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val) { |
| 5300 | return val ? unsafe_yyjson_is_uint(val) : false; |
| 5301 | } |
| 5302 | |
| 5303 | yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val) { |
| 5304 | return val ? unsafe_yyjson_is_sint(val) : false; |
| 5305 | } |
| 5306 | |
| 5307 | yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val) { |
| 5308 | return val ? unsafe_yyjson_is_int(val) : false; |
| 5309 | } |
| 5310 | |
| 5311 | yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val) { |
| 5312 | return val ? unsafe_yyjson_is_real(val) : false; |
| 5313 | } |
| 5314 | |
| 5315 | yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val) { |
| 5316 | return val ? unsafe_yyjson_is_num(val) : false; |
| 5317 | } |
| 5318 | |
| 5319 | yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val) { |
| 5320 | return val ? unsafe_yyjson_is_str(val) : false; |
| 5321 | } |
| 5322 | |
| 5323 | yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val) { |
| 5324 | return val ? unsafe_yyjson_is_arr(val) : false; |
| 5325 | } |
| 5326 | |
| 5327 | yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val) { |
| 5328 | return val ? unsafe_yyjson_is_obj(val) : false; |
| 5329 | } |
| 5330 | |
| 5331 | yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val) { |
| 5332 | return val ? unsafe_yyjson_is_ctn(val) : false; |
| 5333 | } |
| 5334 | |
| 5335 | |
| 5336 | |
| 5337 | /*============================================================================== |
| 5338 | * Mutable JSON Value Content API (Implementation) |
| 5339 | *============================================================================*/ |
| 5340 | |
| 5341 | yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val) { |
| 5342 | return yyjson_get_type((yyjson_val *)val); |
| 5343 | } |
| 5344 | |
| 5345 | yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val) { |
| 5346 | return yyjson_get_subtype((yyjson_val *)val); |
| 5347 | } |
| 5348 | |
| 5349 | yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val) { |
| 5350 | return yyjson_get_tag((yyjson_val *)val); |
| 5351 | } |
| 5352 | |
| 5353 | yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val) { |
| 5354 | return yyjson_get_type_desc((yyjson_val *)val); |
| 5355 | } |
| 5356 | |
| 5357 | yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val) { |
| 5358 | return yyjson_get_raw((yyjson_val *)val); |
| 5359 | } |
| 5360 | |
| 5361 | yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val) { |
| 5362 | return yyjson_get_bool((yyjson_val *)val); |
| 5363 | } |
| 5364 | |
| 5365 | yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val) { |
| 5366 | return yyjson_get_uint((yyjson_val *)val); |
| 5367 | } |
| 5368 | |
| 5369 | yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val) { |
| 5370 | return yyjson_get_sint((yyjson_val *)val); |
| 5371 | } |
| 5372 | |
| 5373 | yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val) { |
| 5374 | return yyjson_get_int((yyjson_val *)val); |
| 5375 | } |
| 5376 | |
| 5377 | yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val) { |
| 5378 | return yyjson_get_real((yyjson_val *)val); |
| 5379 | } |
| 5380 | |
| 5381 | yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val) { |
| 5382 | return yyjson_get_num((yyjson_val *)val); |
| 5383 | } |
| 5384 | |
| 5385 | yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val) { |
| 5386 | return yyjson_get_str((yyjson_val *)val); |
| 5387 | } |
| 5388 | |
| 5389 | yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val) { |
| 5390 | return yyjson_get_len((yyjson_val *)val); |
| 5391 | } |
| 5392 | |
| 5393 | yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, |
| 5394 | const char *str) { |
| 5395 | return yyjson_equals_str((yyjson_val *)val, str); |
| 5396 | } |
| 5397 | |
| 5398 | yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, |
| 5399 | const char *str, size_t len) { |
| 5400 | return yyjson_equals_strn((yyjson_val *)val, str, len); |
| 5401 | } |
| 5402 | |
| 5403 | yyjson_api bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, |
| 5404 | yyjson_mut_val *rhs); |
| 5405 | |
| 5406 | yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, |
| 5407 | yyjson_mut_val *rhs) { |
| 5408 | if (yyjson_unlikely(!lhs || !rhs)) return false; |
| 5409 | return unsafe_yyjson_mut_equals(lhs, rhs); |
| 5410 | } |
| 5411 | |
| 5412 | yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, |
| 5413 | const char *raw, size_t len) { |
| 5414 | if (yyjson_unlikely(!val || !raw)) return false; |
| 5415 | unsafe_yyjson_set_raw(val, raw, len); |
| 5416 | return true; |
| 5417 | } |
| 5418 | |
| 5419 | yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val) { |
| 5420 | if (yyjson_unlikely(!val)) return false; |
| 5421 | unsafe_yyjson_set_null(val); |
| 5422 | return true; |
| 5423 | } |
| 5424 | |
| 5425 | yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num) { |
| 5426 | if (yyjson_unlikely(!val)) return false; |
| 5427 | unsafe_yyjson_set_bool(val, num); |
| 5428 | return true; |
| 5429 | } |
| 5430 | |
| 5431 | yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num) { |
| 5432 | if (yyjson_unlikely(!val)) return false; |
| 5433 | unsafe_yyjson_set_uint(val, num); |
| 5434 | return true; |
| 5435 | } |
| 5436 | |
| 5437 | yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num) { |
| 5438 | if (yyjson_unlikely(!val)) return false; |
| 5439 | unsafe_yyjson_set_sint(val, num); |
| 5440 | return true; |
| 5441 | } |
| 5442 | |
| 5443 | yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num) { |
| 5444 | if (yyjson_unlikely(!val)) return false; |
| 5445 | unsafe_yyjson_set_sint(val, (int64_t)num); |
| 5446 | return true; |
| 5447 | } |
| 5448 | |
| 5449 | yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num) { |
| 5450 | if (yyjson_unlikely(!val)) return false; |
| 5451 | unsafe_yyjson_set_real(val, num); |
| 5452 | return true; |
| 5453 | } |
| 5454 | |
| 5455 | yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, |
| 5456 | const char *str) { |
| 5457 | if (yyjson_unlikely(!val || !str)) return false; |
| 5458 | unsafe_yyjson_set_str(val, str); |
| 5459 | return true; |
| 5460 | } |
| 5461 | |
| 5462 | yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, |
| 5463 | const char *str, size_t len) { |
| 5464 | if (yyjson_unlikely(!val || !str)) return false; |
| 5465 | unsafe_yyjson_set_strn(val, str, len); |
| 5466 | return true; |
| 5467 | } |
| 5468 | |
| 5469 | yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val) { |
| 5470 | if (yyjson_unlikely(!val)) return false; |
| 5471 | unsafe_yyjson_set_arr(val, 0); |
| 5472 | return true; |
| 5473 | } |
| 5474 | |
| 5475 | yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val) { |
| 5476 | if (yyjson_unlikely(!val)) return false; |
| 5477 | unsafe_yyjson_set_obj(val, 0); |
| 5478 | return true; |
| 5479 | } |
| 5480 | |
| 5481 | |
| 5482 | |
| 5483 | /*============================================================================== |
| 5484 | * Mutable JSON Value Creation API (Implementation) |
| 5485 | *============================================================================*/ |
| 5486 | |
| 5487 | yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, |
| 5488 | const char *str) { |
| 5489 | if (yyjson_likely(str)) return yyjson_mut_rawn(doc, str, strlen(str)); |
| 5490 | return NULL; |
| 5491 | } |
| 5492 | |
| 5493 | yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, |
| 5494 | const char *str, |
| 5495 | size_t len) { |
| 5496 | if (yyjson_likely(doc && str)) { |
| 5497 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5498 | if (yyjson_likely(val)) { |
| 5499 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; |
| 5500 | val->uni.str = str; |
| 5501 | return val; |
| 5502 | } |
| 5503 | } |
| 5504 | return NULL; |
| 5505 | } |
| 5506 | |
| 5507 | yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, |
| 5508 | const char *str) { |
| 5509 | if (yyjson_likely(str)) return yyjson_mut_rawncpy(doc, str, strlen(str)); |
| 5510 | return NULL; |
| 5511 | } |
| 5512 | |
| 5513 | yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, |
| 5514 | const char *str, |
| 5515 | size_t len) { |
| 5516 | if (yyjson_likely(doc && str)) { |
| 5517 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5518 | char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); |
| 5519 | if (yyjson_likely(val && new_str)) { |
| 5520 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; |
| 5521 | val->uni.str = new_str; |
| 5522 | return val; |
| 5523 | } |
| 5524 | } |
| 5525 | return NULL; |
| 5526 | } |
| 5527 | |
| 5528 | yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc) { |
| 5529 | if (yyjson_likely(doc)) { |
| 5530 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5531 | if (yyjson_likely(val)) { |
| 5532 | val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE; |
| 5533 | return val; |
| 5534 | } |
| 5535 | } |
| 5536 | return NULL; |
| 5537 | } |
| 5538 | |
| 5539 | yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc) { |
| 5540 | if (yyjson_likely(doc)) { |
| 5541 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5542 | if (yyjson_likely(val)) { |
| 5543 | val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; |
| 5544 | return val; |
| 5545 | } |
| 5546 | } |
| 5547 | return NULL; |
| 5548 | } |
| 5549 | |
| 5550 | yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc) { |
| 5551 | if (yyjson_likely(doc)) { |
| 5552 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5553 | if (yyjson_likely(val)) { |
| 5554 | val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; |
| 5555 | return val; |
| 5556 | } |
| 5557 | } |
| 5558 | return NULL; |
| 5559 | } |
| 5560 | |
| 5561 | yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, |
| 5562 | bool _val) { |
| 5563 | if (yyjson_likely(doc)) { |
| 5564 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5565 | if (yyjson_likely(val)) { |
| 5566 | val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3); |
| 5567 | return val; |
| 5568 | } |
| 5569 | } |
| 5570 | return NULL; |
| 5571 | } |
| 5572 | |
| 5573 | yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, |
| 5574 | uint64_t num) { |
| 5575 | if (yyjson_likely(doc)) { |
| 5576 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5577 | if (yyjson_likely(val)) { |
| 5578 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 5579 | val->uni.u64 = num; |
| 5580 | return val; |
| 5581 | } |
| 5582 | } |
| 5583 | return NULL; |
| 5584 | } |
| 5585 | |
| 5586 | yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, |
| 5587 | int64_t num) { |
| 5588 | if (yyjson_likely(doc)) { |
| 5589 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5590 | if (yyjson_likely(val)) { |
| 5591 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 5592 | val->uni.i64 = num; |
| 5593 | return val; |
| 5594 | } |
| 5595 | } |
| 5596 | return NULL; |
| 5597 | } |
| 5598 | |
| 5599 | yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, |
| 5600 | int64_t num) { |
| 5601 | return yyjson_mut_sint(doc, num); |
| 5602 | } |
| 5603 | |
| 5604 | yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, |
| 5605 | double num) { |
| 5606 | if (yyjson_likely(doc)) { |
| 5607 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5608 | if (yyjson_likely(val)) { |
| 5609 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; |
| 5610 | val->uni.f64 = num; |
| 5611 | return val; |
| 5612 | } |
| 5613 | } |
| 5614 | return NULL; |
| 5615 | } |
| 5616 | |
| 5617 | yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, |
| 5618 | const char *str) { |
| 5619 | if (yyjson_likely(doc && str)) { |
| 5620 | size_t len = strlen(str); |
| 5621 | bool noesc = unsafe_yyjson_is_str_noesc(str, len); |
| 5622 | yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; |
| 5623 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5624 | if (yyjson_likely(val)) { |
| 5625 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | |
| 5626 | (uint64_t)(YYJSON_TYPE_STR | sub); |
| 5627 | val->uni.str = str; |
| 5628 | return val; |
| 5629 | } |
| 5630 | } |
| 5631 | return NULL; |
| 5632 | } |
| 5633 | |
| 5634 | yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, |
| 5635 | const char *str, |
| 5636 | size_t len) { |
| 5637 | if (yyjson_likely(doc && str)) { |
| 5638 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5639 | if (yyjson_likely(val)) { |
| 5640 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 5641 | val->uni.str = str; |
| 5642 | return val; |
| 5643 | } |
| 5644 | } |
| 5645 | return NULL; |
| 5646 | } |
| 5647 | |
| 5648 | yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, |
| 5649 | const char *str) { |
| 5650 | if (yyjson_likely(doc && str)) { |
| 5651 | size_t len = strlen(str); |
| 5652 | bool noesc = unsafe_yyjson_is_str_noesc(str, len); |
| 5653 | yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; |
| 5654 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5655 | char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); |
| 5656 | if (yyjson_likely(val && new_str)) { |
| 5657 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | |
| 5658 | (uint64_t)(YYJSON_TYPE_STR | sub); |
| 5659 | val->uni.str = new_str; |
| 5660 | return val; |
| 5661 | } |
| 5662 | } |
| 5663 | return NULL; |
| 5664 | } |
| 5665 | |
| 5666 | yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, |
| 5667 | const char *str, |
| 5668 | size_t len) { |
| 5669 | if (yyjson_likely(doc && str)) { |
| 5670 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5671 | char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); |
| 5672 | if (yyjson_likely(val && new_str)) { |
| 5673 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 5674 | val->uni.str = new_str; |
| 5675 | return val; |
| 5676 | } |
| 5677 | } |
| 5678 | return NULL; |
| 5679 | } |
| 5680 | |
| 5681 | |
| 5682 | |
| 5683 | /*============================================================================== |
| 5684 | * Mutable JSON Array API (Implementation) |
| 5685 | *============================================================================*/ |
| 5686 | |
| 5687 | yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr) { |
| 5688 | return yyjson_mut_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; |
| 5689 | } |
| 5690 | |
| 5691 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, |
| 5692 | size_t idx) { |
| 5693 | if (yyjson_likely(idx < yyjson_mut_arr_size(arr))) { |
| 5694 | yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; |
| 5695 | while (idx-- > 0) val = val->next; |
| 5696 | return val->next; |
| 5697 | } |
| 5698 | return NULL; |
| 5699 | } |
| 5700 | |
| 5701 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first( |
| 5702 | yyjson_mut_val *arr) { |
| 5703 | if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { |
| 5704 | return ((yyjson_mut_val *)arr->uni.ptr)->next; |
| 5705 | } |
| 5706 | return NULL; |
| 5707 | } |
| 5708 | |
| 5709 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last( |
| 5710 | yyjson_mut_val *arr) { |
| 5711 | if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { |
| 5712 | return ((yyjson_mut_val *)arr->uni.ptr); |
| 5713 | } |
| 5714 | return NULL; |
| 5715 | } |
| 5716 | |
| 5717 | |
| 5718 | |
| 5719 | /*============================================================================== |
| 5720 | * Mutable JSON Array Iterator API (Implementation) |
| 5721 | *============================================================================*/ |
| 5722 | |
| 5723 | yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, |
| 5724 | yyjson_mut_arr_iter *iter) { |
| 5725 | if (yyjson_likely(yyjson_mut_is_arr(arr) && iter)) { |
| 5726 | iter->idx = 0; |
| 5727 | iter->max = unsafe_yyjson_get_len(arr); |
| 5728 | iter->cur = iter->max ? (yyjson_mut_val *)arr->uni.ptr : NULL; |
| 5729 | iter->pre = NULL; |
| 5730 | iter->arr = arr; |
| 5731 | return true; |
| 5732 | } |
| 5733 | if (iter) memset(iter, 0, sizeof(yyjson_mut_arr_iter)); |
| 5734 | return false; |
| 5735 | } |
| 5736 | |
| 5737 | yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( |
| 5738 | yyjson_mut_val *arr) { |
| 5739 | yyjson_mut_arr_iter iter; |
| 5740 | yyjson_mut_arr_iter_init(arr, &iter); |
| 5741 | return iter; |
| 5742 | } |
| 5743 | |
| 5744 | yyjson_api_inline bool yyjson_mut_arr_iter_has_next(yyjson_mut_arr_iter *iter) { |
| 5745 | return iter ? iter->idx < iter->max : false; |
| 5746 | } |
| 5747 | |
| 5748 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( |
| 5749 | yyjson_mut_arr_iter *iter) { |
| 5750 | if (iter && iter->idx < iter->max) { |
| 5751 | yyjson_mut_val *val = iter->cur; |
| 5752 | iter->pre = val; |
| 5753 | iter->cur = val->next; |
| 5754 | iter->idx++; |
| 5755 | return iter->cur; |
| 5756 | } |
| 5757 | return NULL; |
| 5758 | } |
| 5759 | |
| 5760 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( |
| 5761 | yyjson_mut_arr_iter *iter) { |
| 5762 | if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { |
| 5763 | yyjson_mut_val *prev = iter->pre; |
| 5764 | yyjson_mut_val *cur = iter->cur; |
| 5765 | yyjson_mut_val *next = cur->next; |
| 5766 | if (yyjson_unlikely(iter->idx == iter->max)) iter->arr->uni.ptr = prev; |
| 5767 | iter->idx--; |
| 5768 | iter->max--; |
| 5769 | unsafe_yyjson_set_len(iter->arr, iter->max); |
| 5770 | prev->next = next; |
| 5771 | iter->cur = next; |
| 5772 | return cur; |
| 5773 | } |
| 5774 | return NULL; |
| 5775 | } |
| 5776 | |
| 5777 | |
| 5778 | |
| 5779 | /*============================================================================== |
| 5780 | * Mutable JSON Array Creation API (Implementation) |
| 5781 | *============================================================================*/ |
| 5782 | |
| 5783 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc) { |
| 5784 | if (yyjson_likely(doc)) { |
| 5785 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 5786 | if (yyjson_likely(val)) { |
| 5787 | val->tag = YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE; |
| 5788 | return val; |
| 5789 | } |
| 5790 | } |
| 5791 | return NULL; |
| 5792 | } |
| 5793 | |
| 5794 | #define yyjson_mut_arr_with_func(func) \ |
| 5795 | if (yyjson_likely(doc && ((0 < count && count < \ |
| 5796 | (~(size_t)0) / sizeof(yyjson_mut_val) && vals) || count == 0))) { \ |
| 5797 | yyjson_mut_val *arr = unsafe_yyjson_mut_val(doc, 1 + count); \ |
| 5798 | if (yyjson_likely(arr)) { \ |
| 5799 | arr->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; \ |
| 5800 | if (count > 0) { \ |
| 5801 | size_t i; \ |
| 5802 | for (i = 0; i < count; i++) { \ |
| 5803 | yyjson_mut_val *val = arr + i + 1; \ |
| 5804 | func \ |
| 5805 | val->next = val + 1; \ |
| 5806 | } \ |
| 5807 | arr[count].next = arr + 1; \ |
| 5808 | arr->uni.ptr = arr + count; \ |
| 5809 | } \ |
| 5810 | return arr; \ |
| 5811 | } \ |
| 5812 | } \ |
| 5813 | return NULL |
| 5814 | |
| 5815 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( |
| 5816 | yyjson_mut_doc *doc, const bool *vals, size_t count) { |
| 5817 | yyjson_mut_arr_with_func({ |
| 5818 | val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)vals[i] << 3); |
| 5819 | }); |
| 5820 | } |
| 5821 | |
| 5822 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( |
| 5823 | yyjson_mut_doc *doc, const int64_t *vals, size_t count) { |
| 5824 | return yyjson_mut_arr_with_sint64(doc, vals, count); |
| 5825 | } |
| 5826 | |
| 5827 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( |
| 5828 | yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { |
| 5829 | return yyjson_mut_arr_with_uint64(doc, vals, count); |
| 5830 | } |
| 5831 | |
| 5832 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( |
| 5833 | yyjson_mut_doc *doc, const double *vals, size_t count) { |
| 5834 | return yyjson_mut_arr_with_double(doc, vals, count); |
| 5835 | } |
| 5836 | |
| 5837 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( |
| 5838 | yyjson_mut_doc *doc, const int8_t *vals, size_t count) { |
| 5839 | yyjson_mut_arr_with_func({ |
| 5840 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 5841 | val->uni.i64 = (int64_t)vals[i]; |
| 5842 | }); |
| 5843 | } |
| 5844 | |
| 5845 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( |
| 5846 | yyjson_mut_doc *doc, const int16_t *vals, size_t count) { |
| 5847 | yyjson_mut_arr_with_func({ |
| 5848 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 5849 | val->uni.i64 = vals[i]; |
| 5850 | }); |
| 5851 | } |
| 5852 | |
| 5853 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( |
| 5854 | yyjson_mut_doc *doc, const int32_t *vals, size_t count) { |
| 5855 | yyjson_mut_arr_with_func({ |
| 5856 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 5857 | val->uni.i64 = vals[i]; |
| 5858 | }); |
| 5859 | } |
| 5860 | |
| 5861 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( |
| 5862 | yyjson_mut_doc *doc, const int64_t *vals, size_t count) { |
| 5863 | yyjson_mut_arr_with_func({ |
| 5864 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 5865 | val->uni.i64 = vals[i]; |
| 5866 | }); |
| 5867 | } |
| 5868 | |
| 5869 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( |
| 5870 | yyjson_mut_doc *doc, const uint8_t *vals, size_t count) { |
| 5871 | yyjson_mut_arr_with_func({ |
| 5872 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 5873 | val->uni.u64 = vals[i]; |
| 5874 | }); |
| 5875 | } |
| 5876 | |
| 5877 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( |
| 5878 | yyjson_mut_doc *doc, const uint16_t *vals, size_t count) { |
| 5879 | yyjson_mut_arr_with_func({ |
| 5880 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 5881 | val->uni.u64 = vals[i]; |
| 5882 | }); |
| 5883 | } |
| 5884 | |
| 5885 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( |
| 5886 | yyjson_mut_doc *doc, const uint32_t *vals, size_t count) { |
| 5887 | yyjson_mut_arr_with_func({ |
| 5888 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 5889 | val->uni.u64 = vals[i]; |
| 5890 | }); |
| 5891 | } |
| 5892 | |
| 5893 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( |
| 5894 | yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { |
| 5895 | yyjson_mut_arr_with_func({ |
| 5896 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 5897 | val->uni.u64 = vals[i]; |
| 5898 | }); |
| 5899 | } |
| 5900 | |
| 5901 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( |
| 5902 | yyjson_mut_doc *doc, const float *vals, size_t count) { |
| 5903 | yyjson_mut_arr_with_func({ |
| 5904 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; |
| 5905 | val->uni.f64 = (double)vals[i]; |
| 5906 | }); |
| 5907 | } |
| 5908 | |
| 5909 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( |
| 5910 | yyjson_mut_doc *doc, const double *vals, size_t count) { |
| 5911 | yyjson_mut_arr_with_func({ |
| 5912 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; |
| 5913 | val->uni.f64 = vals[i]; |
| 5914 | }); |
| 5915 | } |
| 5916 | |
| 5917 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( |
| 5918 | yyjson_mut_doc *doc, const char **vals, size_t count) { |
| 5919 | yyjson_mut_arr_with_func({ |
| 5920 | uint64_t len = (uint64_t)strlen(vals[i]); |
| 5921 | val->tag = (len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 5922 | val->uni.str = vals[i]; |
| 5923 | if (yyjson_unlikely(!val->uni.str)) return NULL; |
| 5924 | }); |
| 5925 | } |
| 5926 | |
| 5927 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( |
| 5928 | yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { |
| 5929 | if (yyjson_unlikely(count > 0 && !lens)) return NULL; |
| 5930 | yyjson_mut_arr_with_func({ |
| 5931 | val->tag = ((uint64_t)lens[i] << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 5932 | val->uni.str = vals[i]; |
| 5933 | if (yyjson_unlikely(!val->uni.str)) return NULL; |
| 5934 | }); |
| 5935 | } |
| 5936 | |
| 5937 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( |
| 5938 | yyjson_mut_doc *doc, const char **vals, size_t count) { |
| 5939 | size_t len; |
| 5940 | const char *str; |
| 5941 | yyjson_mut_arr_with_func({ |
| 5942 | str = vals[i]; |
| 5943 | if (!str) return NULL; |
| 5944 | len = strlen(str); |
| 5945 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 5946 | val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); |
| 5947 | if (yyjson_unlikely(!val->uni.str)) return NULL; |
| 5948 | }); |
| 5949 | } |
| 5950 | |
| 5951 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( |
| 5952 | yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { |
| 5953 | size_t len; |
| 5954 | const char *str; |
| 5955 | if (yyjson_unlikely(count > 0 && !lens)) return NULL; |
| 5956 | yyjson_mut_arr_with_func({ |
| 5957 | str = vals[i]; |
| 5958 | len = lens[i]; |
| 5959 | val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 5960 | val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); |
| 5961 | if (yyjson_unlikely(!val->uni.str)) return NULL; |
| 5962 | }); |
| 5963 | } |
| 5964 | |
| 5965 | #undef yyjson_mut_arr_with_func |
| 5966 | |
| 5967 | |
| 5968 | |
| 5969 | /*============================================================================== |
| 5970 | * Mutable JSON Array Modification API (Implementation) |
| 5971 | *============================================================================*/ |
| 5972 | |
| 5973 | yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, |
| 5974 | yyjson_mut_val *val, size_t idx) { |
| 5975 | if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { |
| 5976 | size_t len = unsafe_yyjson_get_len(arr); |
| 5977 | if (yyjson_likely(idx <= len)) { |
| 5978 | unsafe_yyjson_set_len(arr, len + 1); |
| 5979 | if (len == 0) { |
| 5980 | val->next = val; |
| 5981 | arr->uni.ptr = val; |
| 5982 | } else { |
| 5983 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 5984 | yyjson_mut_val *next = prev->next; |
| 5985 | if (idx == len) { |
| 5986 | prev->next = val; |
| 5987 | val->next = next; |
| 5988 | arr->uni.ptr = val; |
| 5989 | } else { |
| 5990 | while (idx-- > 0) { |
| 5991 | prev = next; |
| 5992 | next = next->next; |
| 5993 | } |
| 5994 | prev->next = val; |
| 5995 | val->next = next; |
| 5996 | } |
| 5997 | } |
| 5998 | return true; |
| 5999 | } |
| 6000 | } |
| 6001 | return false; |
| 6002 | } |
| 6003 | |
| 6004 | yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, |
| 6005 | yyjson_mut_val *val) { |
| 6006 | if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { |
| 6007 | size_t len = unsafe_yyjson_get_len(arr); |
| 6008 | unsafe_yyjson_set_len(arr, len + 1); |
| 6009 | if (len == 0) { |
| 6010 | val->next = val; |
| 6011 | } else { |
| 6012 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6013 | yyjson_mut_val *next = prev->next; |
| 6014 | prev->next = val; |
| 6015 | val->next = next; |
| 6016 | } |
| 6017 | arr->uni.ptr = val; |
| 6018 | return true; |
| 6019 | } |
| 6020 | return false; |
| 6021 | } |
| 6022 | |
| 6023 | yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, |
| 6024 | yyjson_mut_val *val) { |
| 6025 | if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { |
| 6026 | size_t len = unsafe_yyjson_get_len(arr); |
| 6027 | unsafe_yyjson_set_len(arr, len + 1); |
| 6028 | if (len == 0) { |
| 6029 | val->next = val; |
| 6030 | arr->uni.ptr = val; |
| 6031 | } else { |
| 6032 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6033 | yyjson_mut_val *next = prev->next; |
| 6034 | prev->next = val; |
| 6035 | val->next = next; |
| 6036 | } |
| 6037 | return true; |
| 6038 | } |
| 6039 | return false; |
| 6040 | } |
| 6041 | |
| 6042 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, |
| 6043 | size_t idx, |
| 6044 | yyjson_mut_val *val) { |
| 6045 | if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { |
| 6046 | size_t len = unsafe_yyjson_get_len(arr); |
| 6047 | if (yyjson_likely(idx < len)) { |
| 6048 | if (yyjson_likely(len > 1)) { |
| 6049 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6050 | yyjson_mut_val *next = prev->next; |
| 6051 | while (idx-- > 0) { |
| 6052 | prev = next; |
| 6053 | next = next->next; |
| 6054 | } |
| 6055 | prev->next = val; |
| 6056 | val->next = next->next; |
| 6057 | if ((void *)next == arr->uni.ptr) arr->uni.ptr = val; |
| 6058 | return next; |
| 6059 | } else { |
| 6060 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6061 | val->next = val; |
| 6062 | arr->uni.ptr = val; |
| 6063 | return prev; |
| 6064 | } |
| 6065 | } |
| 6066 | } |
| 6067 | return NULL; |
| 6068 | } |
| 6069 | |
| 6070 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, |
| 6071 | size_t idx) { |
| 6072 | if (yyjson_likely(yyjson_mut_is_arr(arr))) { |
| 6073 | size_t len = unsafe_yyjson_get_len(arr); |
| 6074 | if (yyjson_likely(idx < len)) { |
| 6075 | unsafe_yyjson_set_len(arr, len - 1); |
| 6076 | if (yyjson_likely(len > 1)) { |
| 6077 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6078 | yyjson_mut_val *next = prev->next; |
| 6079 | while (idx-- > 0) { |
| 6080 | prev = next; |
| 6081 | next = next->next; |
| 6082 | } |
| 6083 | prev->next = next->next; |
| 6084 | if ((void *)next == arr->uni.ptr) arr->uni.ptr = prev; |
| 6085 | return next; |
| 6086 | } else { |
| 6087 | return ((yyjson_mut_val *)arr->uni.ptr); |
| 6088 | } |
| 6089 | } |
| 6090 | } |
| 6091 | return NULL; |
| 6092 | } |
| 6093 | |
| 6094 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( |
| 6095 | yyjson_mut_val *arr) { |
| 6096 | if (yyjson_likely(yyjson_mut_is_arr(arr))) { |
| 6097 | size_t len = unsafe_yyjson_get_len(arr); |
| 6098 | if (len > 1) { |
| 6099 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6100 | yyjson_mut_val *next = prev->next; |
| 6101 | prev->next = next->next; |
| 6102 | unsafe_yyjson_set_len(arr, len - 1); |
| 6103 | return next; |
| 6104 | } else if (len == 1) { |
| 6105 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6106 | unsafe_yyjson_set_len(arr, 0); |
| 6107 | return prev; |
| 6108 | } |
| 6109 | } |
| 6110 | return NULL; |
| 6111 | } |
| 6112 | |
| 6113 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( |
| 6114 | yyjson_mut_val *arr) { |
| 6115 | if (yyjson_likely(yyjson_mut_is_arr(arr))) { |
| 6116 | size_t len = unsafe_yyjson_get_len(arr); |
| 6117 | if (yyjson_likely(len > 1)) { |
| 6118 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6119 | yyjson_mut_val *next = prev->next; |
| 6120 | unsafe_yyjson_set_len(arr, len - 1); |
| 6121 | while (--len > 0) prev = prev->next; |
| 6122 | prev->next = next; |
| 6123 | next = (yyjson_mut_val *)arr->uni.ptr; |
| 6124 | arr->uni.ptr = prev; |
| 6125 | return next; |
| 6126 | } else if (len == 1) { |
| 6127 | yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6128 | unsafe_yyjson_set_len(arr, 0); |
| 6129 | return prev; |
| 6130 | } |
| 6131 | } |
| 6132 | return NULL; |
| 6133 | } |
| 6134 | |
| 6135 | yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, |
| 6136 | size_t _idx, size_t _len) { |
| 6137 | if (yyjson_likely(yyjson_mut_is_arr(arr))) { |
| 6138 | yyjson_mut_val *prev, *next; |
| 6139 | bool tail_removed; |
| 6140 | size_t len = unsafe_yyjson_get_len(arr); |
| 6141 | if (yyjson_unlikely(_idx + _len > len)) return false; |
| 6142 | if (yyjson_unlikely(_len == 0)) return true; |
| 6143 | unsafe_yyjson_set_len(arr, len - _len); |
| 6144 | if (yyjson_unlikely(len == _len)) return true; |
| 6145 | tail_removed = (_idx + _len == len); |
| 6146 | prev = ((yyjson_mut_val *)arr->uni.ptr); |
| 6147 | while (_idx-- > 0) prev = prev->next; |
| 6148 | next = prev->next; |
| 6149 | while (_len-- > 0) next = next->next; |
| 6150 | prev->next = next; |
| 6151 | if (yyjson_unlikely(tail_removed)) arr->uni.ptr = prev; |
| 6152 | return true; |
| 6153 | } |
| 6154 | return false; |
| 6155 | } |
| 6156 | |
| 6157 | yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr) { |
| 6158 | if (yyjson_likely(yyjson_mut_is_arr(arr))) { |
| 6159 | unsafe_yyjson_set_len(arr, 0); |
| 6160 | return true; |
| 6161 | } |
| 6162 | return false; |
| 6163 | } |
| 6164 | |
| 6165 | yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, |
| 6166 | size_t idx) { |
| 6167 | if (yyjson_likely(yyjson_mut_is_arr(arr) && |
| 6168 | unsafe_yyjson_get_len(arr) > idx)) { |
| 6169 | yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; |
| 6170 | while (idx-- > 0) val = val->next; |
| 6171 | arr->uni.ptr = (void *)val; |
| 6172 | return true; |
| 6173 | } |
| 6174 | return false; |
| 6175 | } |
| 6176 | |
| 6177 | |
| 6178 | |
| 6179 | /*============================================================================== |
| 6180 | * Mutable JSON Array Modification Convenience API (Implementation) |
| 6181 | *============================================================================*/ |
| 6182 | |
| 6183 | yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, |
| 6184 | yyjson_mut_val *val) { |
| 6185 | return yyjson_mut_arr_append(arr, val); |
| 6186 | } |
| 6187 | |
| 6188 | yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, |
| 6189 | yyjson_mut_val *arr) { |
| 6190 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6191 | yyjson_mut_val *val = yyjson_mut_null(doc); |
| 6192 | return yyjson_mut_arr_append(arr, val); |
| 6193 | } |
| 6194 | return false; |
| 6195 | } |
| 6196 | |
| 6197 | yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, |
| 6198 | yyjson_mut_val *arr) { |
| 6199 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6200 | yyjson_mut_val *val = yyjson_mut_true(doc); |
| 6201 | return yyjson_mut_arr_append(arr, val); |
| 6202 | } |
| 6203 | return false; |
| 6204 | } |
| 6205 | |
| 6206 | yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, |
| 6207 | yyjson_mut_val *arr) { |
| 6208 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6209 | yyjson_mut_val *val = yyjson_mut_false(doc); |
| 6210 | return yyjson_mut_arr_append(arr, val); |
| 6211 | } |
| 6212 | return false; |
| 6213 | } |
| 6214 | |
| 6215 | yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, |
| 6216 | yyjson_mut_val *arr, |
| 6217 | bool _val) { |
| 6218 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6219 | yyjson_mut_val *val = yyjson_mut_bool(doc, _val); |
| 6220 | return yyjson_mut_arr_append(arr, val); |
| 6221 | } |
| 6222 | return false; |
| 6223 | } |
| 6224 | |
| 6225 | yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, |
| 6226 | yyjson_mut_val *arr, |
| 6227 | uint64_t num) { |
| 6228 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6229 | yyjson_mut_val *val = yyjson_mut_uint(doc, num); |
| 6230 | return yyjson_mut_arr_append(arr, val); |
| 6231 | } |
| 6232 | return false; |
| 6233 | } |
| 6234 | |
| 6235 | yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, |
| 6236 | yyjson_mut_val *arr, |
| 6237 | int64_t num) { |
| 6238 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6239 | yyjson_mut_val *val = yyjson_mut_sint(doc, num); |
| 6240 | return yyjson_mut_arr_append(arr, val); |
| 6241 | } |
| 6242 | return false; |
| 6243 | } |
| 6244 | |
| 6245 | yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, |
| 6246 | yyjson_mut_val *arr, |
| 6247 | int64_t num) { |
| 6248 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6249 | yyjson_mut_val *val = yyjson_mut_sint(doc, num); |
| 6250 | return yyjson_mut_arr_append(arr, val); |
| 6251 | } |
| 6252 | return false; |
| 6253 | } |
| 6254 | |
| 6255 | yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, |
| 6256 | yyjson_mut_val *arr, |
| 6257 | double num) { |
| 6258 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6259 | yyjson_mut_val *val = yyjson_mut_real(doc, num); |
| 6260 | return yyjson_mut_arr_append(arr, val); |
| 6261 | } |
| 6262 | return false; |
| 6263 | } |
| 6264 | |
| 6265 | yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, |
| 6266 | yyjson_mut_val *arr, |
| 6267 | const char *str) { |
| 6268 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6269 | yyjson_mut_val *val = yyjson_mut_str(doc, str); |
| 6270 | return yyjson_mut_arr_append(arr, val); |
| 6271 | } |
| 6272 | return false; |
| 6273 | } |
| 6274 | |
| 6275 | yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, |
| 6276 | yyjson_mut_val *arr, |
| 6277 | const char *str, size_t len) { |
| 6278 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6279 | yyjson_mut_val *val = yyjson_mut_strn(doc, str, len); |
| 6280 | return yyjson_mut_arr_append(arr, val); |
| 6281 | } |
| 6282 | return false; |
| 6283 | } |
| 6284 | |
| 6285 | yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, |
| 6286 | yyjson_mut_val *arr, |
| 6287 | const char *str) { |
| 6288 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6289 | yyjson_mut_val *val = yyjson_mut_strcpy(doc, str); |
| 6290 | return yyjson_mut_arr_append(arr, val); |
| 6291 | } |
| 6292 | return false; |
| 6293 | } |
| 6294 | |
| 6295 | yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, |
| 6296 | yyjson_mut_val *arr, |
| 6297 | const char *str, size_t len) { |
| 6298 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6299 | yyjson_mut_val *val = yyjson_mut_strncpy(doc, str, len); |
| 6300 | return yyjson_mut_arr_append(arr, val); |
| 6301 | } |
| 6302 | return false; |
| 6303 | } |
| 6304 | |
| 6305 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, |
| 6306 | yyjson_mut_val *arr) { |
| 6307 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6308 | yyjson_mut_val *val = yyjson_mut_arr(doc); |
| 6309 | return yyjson_mut_arr_append(arr, val) ? val : NULL; |
| 6310 | } |
| 6311 | return NULL; |
| 6312 | } |
| 6313 | |
| 6314 | yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, |
| 6315 | yyjson_mut_val *arr) { |
| 6316 | if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { |
| 6317 | yyjson_mut_val *val = yyjson_mut_obj(doc); |
| 6318 | return yyjson_mut_arr_append(arr, val) ? val : NULL; |
| 6319 | } |
| 6320 | return NULL; |
| 6321 | } |
| 6322 | |
| 6323 | |
| 6324 | |
| 6325 | /*============================================================================== |
| 6326 | * Mutable JSON Object API (Implementation) |
| 6327 | *============================================================================*/ |
| 6328 | |
| 6329 | yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj) { |
| 6330 | return yyjson_mut_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; |
| 6331 | } |
| 6332 | |
| 6333 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, |
| 6334 | const char *key) { |
| 6335 | return yyjson_mut_obj_getn(obj, key, key ? strlen(key) : 0); |
| 6336 | } |
| 6337 | |
| 6338 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, |
| 6339 | const char *_key, |
| 6340 | size_t key_len) { |
| 6341 | size_t len = yyjson_mut_obj_size(obj); |
| 6342 | if (yyjson_likely(len && _key)) { |
| 6343 | yyjson_mut_val *key = ((yyjson_mut_val *)obj->uni.ptr)->next->next; |
| 6344 | while (len-- > 0) { |
| 6345 | if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key->next; |
| 6346 | key = key->next->next; |
| 6347 | } |
| 6348 | } |
| 6349 | return NULL; |
| 6350 | } |
| 6351 | |
| 6352 | |
| 6353 | |
| 6354 | /*============================================================================== |
| 6355 | * Mutable JSON Object Iterator API (Implementation) |
| 6356 | *============================================================================*/ |
| 6357 | |
| 6358 | yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, |
| 6359 | yyjson_mut_obj_iter *iter) { |
| 6360 | if (yyjson_likely(yyjson_mut_is_obj(obj) && iter)) { |
| 6361 | iter->idx = 0; |
| 6362 | iter->max = unsafe_yyjson_get_len(obj); |
| 6363 | iter->cur = iter->max ? (yyjson_mut_val *)obj->uni.ptr : NULL; |
| 6364 | iter->pre = NULL; |
| 6365 | iter->obj = obj; |
| 6366 | return true; |
| 6367 | } |
| 6368 | if (iter) memset(iter, 0, sizeof(yyjson_mut_obj_iter)); |
| 6369 | return false; |
| 6370 | } |
| 6371 | |
| 6372 | yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( |
| 6373 | yyjson_mut_val *obj) { |
| 6374 | yyjson_mut_obj_iter iter; |
| 6375 | yyjson_mut_obj_iter_init(obj, &iter); |
| 6376 | return iter; |
| 6377 | } |
| 6378 | |
| 6379 | yyjson_api_inline bool yyjson_mut_obj_iter_has_next(yyjson_mut_obj_iter *iter) { |
| 6380 | return iter ? iter->idx < iter->max : false; |
| 6381 | } |
| 6382 | |
| 6383 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( |
| 6384 | yyjson_mut_obj_iter *iter) { |
| 6385 | if (iter && iter->idx < iter->max) { |
| 6386 | yyjson_mut_val *key = iter->cur; |
| 6387 | iter->pre = key; |
| 6388 | iter->cur = key->next->next; |
| 6389 | iter->idx++; |
| 6390 | return iter->cur; |
| 6391 | } |
| 6392 | return NULL; |
| 6393 | } |
| 6394 | |
| 6395 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( |
| 6396 | yyjson_mut_val *key) { |
| 6397 | return key ? key->next : NULL; |
| 6398 | } |
| 6399 | |
| 6400 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( |
| 6401 | yyjson_mut_obj_iter *iter) { |
| 6402 | if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { |
| 6403 | yyjson_mut_val *prev = iter->pre; |
| 6404 | yyjson_mut_val *cur = iter->cur; |
| 6405 | yyjson_mut_val *next = cur->next->next; |
| 6406 | if (yyjson_unlikely(iter->idx == iter->max)) iter->obj->uni.ptr = prev; |
| 6407 | iter->idx--; |
| 6408 | iter->max--; |
| 6409 | unsafe_yyjson_set_len(iter->obj, iter->max); |
| 6410 | prev->next->next = next; |
| 6411 | iter->cur = prev; |
| 6412 | return cur->next; |
| 6413 | } |
| 6414 | return NULL; |
| 6415 | } |
| 6416 | |
| 6417 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( |
| 6418 | yyjson_mut_obj_iter *iter, const char *key) { |
| 6419 | return yyjson_mut_obj_iter_getn(iter, key, key ? strlen(key) : 0); |
| 6420 | } |
| 6421 | |
| 6422 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( |
| 6423 | yyjson_mut_obj_iter *iter, const char *key, size_t key_len) { |
| 6424 | if (iter && key) { |
| 6425 | size_t idx = 0; |
| 6426 | size_t max = iter->max; |
| 6427 | yyjson_mut_val *pre, *cur = iter->cur; |
| 6428 | while (idx++ < max) { |
| 6429 | pre = cur; |
| 6430 | cur = cur->next->next; |
| 6431 | if (unsafe_yyjson_equals_strn(cur, key, key_len)) { |
| 6432 | iter->idx += idx; |
| 6433 | if (iter->idx > max) iter->idx -= max + 1; |
| 6434 | iter->pre = pre; |
| 6435 | iter->cur = cur; |
| 6436 | return cur->next; |
| 6437 | } |
| 6438 | } |
| 6439 | } |
| 6440 | return NULL; |
| 6441 | } |
| 6442 | |
| 6443 | |
| 6444 | |
| 6445 | /*============================================================================== |
| 6446 | * Mutable JSON Object Creation API (Implementation) |
| 6447 | *============================================================================*/ |
| 6448 | |
| 6449 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc) { |
| 6450 | if (yyjson_likely(doc)) { |
| 6451 | yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); |
| 6452 | if (yyjson_likely(val)) { |
| 6453 | val->tag = YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE; |
| 6454 | return val; |
| 6455 | } |
| 6456 | } |
| 6457 | return NULL; |
| 6458 | } |
| 6459 | |
| 6460 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, |
| 6461 | const char **keys, |
| 6462 | const char **vals, |
| 6463 | size_t count) { |
| 6464 | if (yyjson_likely(doc && ((count > 0 && keys && vals) || (count == 0)))) { |
| 6465 | yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); |
| 6466 | if (yyjson_likely(obj)) { |
| 6467 | obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; |
| 6468 | if (count > 0) { |
| 6469 | size_t i; |
| 6470 | for (i = 0; i < count; i++) { |
| 6471 | yyjson_mut_val *key = obj + (i * 2 + 1); |
| 6472 | yyjson_mut_val *val = obj + (i * 2 + 2); |
| 6473 | uint64_t key_len = (uint64_t)strlen(keys[i]); |
| 6474 | uint64_t val_len = (uint64_t)strlen(vals[i]); |
| 6475 | key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6476 | val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6477 | key->uni.str = keys[i]; |
| 6478 | val->uni.str = vals[i]; |
| 6479 | key->next = val; |
| 6480 | val->next = val + 1; |
| 6481 | } |
| 6482 | obj[count * 2].next = obj + 1; |
| 6483 | obj->uni.ptr = obj + (count * 2 - 1); |
| 6484 | } |
| 6485 | return obj; |
| 6486 | } |
| 6487 | } |
| 6488 | return NULL; |
| 6489 | } |
| 6490 | |
| 6491 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, |
| 6492 | const char **pairs, |
| 6493 | size_t count) { |
| 6494 | if (yyjson_likely(doc && ((count > 0 && pairs) || (count == 0)))) { |
| 6495 | yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); |
| 6496 | if (yyjson_likely(obj)) { |
| 6497 | obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; |
| 6498 | if (count > 0) { |
| 6499 | size_t i; |
| 6500 | for (i = 0; i < count; i++) { |
| 6501 | yyjson_mut_val *key = obj + (i * 2 + 1); |
| 6502 | yyjson_mut_val *val = obj + (i * 2 + 2); |
| 6503 | const char *key_str = pairs[i * 2 + 0]; |
| 6504 | const char *val_str = pairs[i * 2 + 1]; |
| 6505 | uint64_t key_len = (uint64_t)strlen(key_str); |
| 6506 | uint64_t val_len = (uint64_t)strlen(val_str); |
| 6507 | key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6508 | val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6509 | key->uni.str = key_str; |
| 6510 | val->uni.str = val_str; |
| 6511 | key->next = val; |
| 6512 | val->next = val + 1; |
| 6513 | } |
| 6514 | obj[count * 2].next = obj + 1; |
| 6515 | obj->uni.ptr = obj + (count * 2 - 1); |
| 6516 | } |
| 6517 | return obj; |
| 6518 | } |
| 6519 | } |
| 6520 | return NULL; |
| 6521 | } |
| 6522 | |
| 6523 | |
| 6524 | |
| 6525 | /*============================================================================== |
| 6526 | * Mutable JSON Object Modification API (Implementation) |
| 6527 | *============================================================================*/ |
| 6528 | |
| 6529 | yyjson_api_inline void unsafe_yyjson_mut_obj_add(yyjson_mut_val *obj, |
| 6530 | yyjson_mut_val *key, |
| 6531 | yyjson_mut_val *val, |
| 6532 | size_t len) { |
| 6533 | if (yyjson_likely(len)) { |
| 6534 | yyjson_mut_val *prev_val = ((yyjson_mut_val *)obj->uni.ptr)->next; |
| 6535 | yyjson_mut_val *next_key = prev_val->next; |
| 6536 | prev_val->next = key; |
| 6537 | val->next = next_key; |
| 6538 | } else { |
| 6539 | val->next = key; |
| 6540 | } |
| 6541 | key->next = val; |
| 6542 | obj->uni.ptr = (void *)key; |
| 6543 | unsafe_yyjson_set_len(obj, len + 1); |
| 6544 | } |
| 6545 | |
| 6546 | yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_obj_remove( |
| 6547 | yyjson_mut_val *obj, const char *key, size_t key_len) { |
| 6548 | size_t obj_len = unsafe_yyjson_get_len(obj); |
| 6549 | if (obj_len) { |
| 6550 | yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; |
| 6551 | yyjson_mut_val *cur_key = pre_key->next->next; |
| 6552 | yyjson_mut_val *removed_item = NULL; |
| 6553 | size_t i; |
| 6554 | for (i = 0; i < obj_len; i++) { |
| 6555 | if (unsafe_yyjson_equals_strn(cur_key, key, key_len)) { |
| 6556 | if (!removed_item) removed_item = cur_key->next; |
| 6557 | cur_key = cur_key->next->next; |
| 6558 | pre_key->next->next = cur_key; |
| 6559 | if (i + 1 == obj_len) obj->uni.ptr = pre_key; |
| 6560 | i--; |
| 6561 | obj_len--; |
| 6562 | } else { |
| 6563 | pre_key = cur_key; |
| 6564 | cur_key = cur_key->next->next; |
| 6565 | } |
| 6566 | } |
| 6567 | unsafe_yyjson_set_len(obj, obj_len); |
| 6568 | return removed_item; |
| 6569 | } else { |
| 6570 | return NULL; |
| 6571 | } |
| 6572 | } |
| 6573 | |
| 6574 | yyjson_api_inline bool unsafe_yyjson_mut_obj_replace(yyjson_mut_val *obj, |
| 6575 | yyjson_mut_val *key, |
| 6576 | yyjson_mut_val *val) { |
| 6577 | size_t key_len = unsafe_yyjson_get_len(key); |
| 6578 | size_t obj_len = unsafe_yyjson_get_len(obj); |
| 6579 | if (obj_len) { |
| 6580 | yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; |
| 6581 | yyjson_mut_val *cur_key = pre_key->next->next; |
| 6582 | size_t i; |
| 6583 | for (i = 0; i < obj_len; i++) { |
| 6584 | if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { |
| 6585 | cur_key->next->tag = val->tag; |
| 6586 | cur_key->next->uni.u64 = val->uni.u64; |
| 6587 | return true; |
| 6588 | } else { |
| 6589 | cur_key = cur_key->next->next; |
| 6590 | } |
| 6591 | } |
| 6592 | } |
| 6593 | return false; |
| 6594 | } |
| 6595 | |
| 6596 | yyjson_api_inline void unsafe_yyjson_mut_obj_rotate(yyjson_mut_val *obj, |
| 6597 | size_t idx) { |
| 6598 | yyjson_mut_val *key = (yyjson_mut_val *)obj->uni.ptr; |
| 6599 | while (idx-- > 0) key = key->next->next; |
| 6600 | obj->uni.ptr = (void *)key; |
| 6601 | } |
| 6602 | |
| 6603 | yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, |
| 6604 | yyjson_mut_val *key, |
| 6605 | yyjson_mut_val *val) { |
| 6606 | if (yyjson_likely(yyjson_mut_is_obj(obj) && |
| 6607 | yyjson_mut_is_str(key) && val)) { |
| 6608 | unsafe_yyjson_mut_obj_add(obj, key, val, unsafe_yyjson_get_len(obj)); |
| 6609 | return true; |
| 6610 | } |
| 6611 | return false; |
| 6612 | } |
| 6613 | |
| 6614 | yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, |
| 6615 | yyjson_mut_val *key, |
| 6616 | yyjson_mut_val *val) { |
| 6617 | bool replaced = false; |
| 6618 | size_t key_len; |
| 6619 | yyjson_mut_obj_iter iter; |
| 6620 | yyjson_mut_val *cur_key; |
| 6621 | if (yyjson_unlikely(!yyjson_mut_is_obj(obj) || |
| 6622 | !yyjson_mut_is_str(key))) return false; |
| 6623 | key_len = unsafe_yyjson_get_len(key); |
| 6624 | yyjson_mut_obj_iter_init(obj, &iter); |
| 6625 | while ((cur_key = yyjson_mut_obj_iter_next(&iter))) { |
| 6626 | if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { |
| 6627 | if (!replaced && val) { |
| 6628 | replaced = true; |
| 6629 | val->next = cur_key->next->next; |
| 6630 | cur_key->next = val; |
| 6631 | } else { |
| 6632 | yyjson_mut_obj_iter_remove(&iter); |
| 6633 | } |
| 6634 | } |
| 6635 | } |
| 6636 | if (!replaced && val) unsafe_yyjson_mut_obj_add(obj, key, val, iter.max); |
| 6637 | return true; |
| 6638 | } |
| 6639 | |
| 6640 | yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, |
| 6641 | yyjson_mut_val *key, |
| 6642 | yyjson_mut_val *val, |
| 6643 | size_t idx) { |
| 6644 | if (yyjson_likely(yyjson_mut_is_obj(obj) && |
| 6645 | yyjson_mut_is_str(key) && val)) { |
| 6646 | size_t len = unsafe_yyjson_get_len(obj); |
| 6647 | if (yyjson_likely(len >= idx)) { |
| 6648 | if (len > idx) { |
| 6649 | void *ptr = obj->uni.ptr; |
| 6650 | unsafe_yyjson_mut_obj_rotate(obj, idx); |
| 6651 | unsafe_yyjson_mut_obj_add(obj, key, val, len); |
| 6652 | obj->uni.ptr = ptr; |
| 6653 | } else { |
| 6654 | unsafe_yyjson_mut_obj_add(obj, key, val, len); |
| 6655 | } |
| 6656 | return true; |
| 6657 | } |
| 6658 | } |
| 6659 | return false; |
| 6660 | } |
| 6661 | |
| 6662 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, |
| 6663 | yyjson_mut_val *key) { |
| 6664 | if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key))) { |
| 6665 | return unsafe_yyjson_mut_obj_remove(obj, key->uni.str, |
| 6666 | unsafe_yyjson_get_len(key)); |
| 6667 | } |
| 6668 | return NULL; |
| 6669 | } |
| 6670 | |
| 6671 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( |
| 6672 | yyjson_mut_val *obj, const char *key) { |
| 6673 | if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { |
| 6674 | size_t key_len = strlen(key); |
| 6675 | return unsafe_yyjson_mut_obj_remove(obj, key, key_len); |
| 6676 | } |
| 6677 | return NULL; |
| 6678 | } |
| 6679 | |
| 6680 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( |
| 6681 | yyjson_mut_val *obj, const char *key, size_t key_len) { |
| 6682 | if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { |
| 6683 | return unsafe_yyjson_mut_obj_remove(obj, key, key_len); |
| 6684 | } |
| 6685 | return NULL; |
| 6686 | } |
| 6687 | |
| 6688 | yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj) { |
| 6689 | if (yyjson_likely(yyjson_mut_is_obj(obj))) { |
| 6690 | unsafe_yyjson_set_len(obj, 0); |
| 6691 | return true; |
| 6692 | } |
| 6693 | return false; |
| 6694 | } |
| 6695 | |
| 6696 | yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, |
| 6697 | yyjson_mut_val *key, |
| 6698 | yyjson_mut_val *val) { |
| 6699 | if (yyjson_likely(yyjson_mut_is_obj(obj) && |
| 6700 | yyjson_mut_is_str(key) && val)) { |
| 6701 | return unsafe_yyjson_mut_obj_replace(obj, key, val); |
| 6702 | } |
| 6703 | return false; |
| 6704 | } |
| 6705 | |
| 6706 | yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, |
| 6707 | size_t idx) { |
| 6708 | if (yyjson_likely(yyjson_mut_is_obj(obj) && |
| 6709 | unsafe_yyjson_get_len(obj) > idx)) { |
| 6710 | unsafe_yyjson_mut_obj_rotate(obj, idx); |
| 6711 | return true; |
| 6712 | } |
| 6713 | return false; |
| 6714 | } |
| 6715 | |
| 6716 | |
| 6717 | |
| 6718 | /*============================================================================== |
| 6719 | * Mutable JSON Object Modification Convenience API (Implementation) |
| 6720 | *============================================================================*/ |
| 6721 | |
| 6722 | #define yyjson_mut_obj_add_func(func) \ |
| 6723 | if (yyjson_likely(doc && yyjson_mut_is_obj(obj) && _key)) { \ |
| 6724 | yyjson_mut_val *key = unsafe_yyjson_mut_val(doc, 2); \ |
| 6725 | if (yyjson_likely(key)) { \ |
| 6726 | size_t len = unsafe_yyjson_get_len(obj); \ |
| 6727 | yyjson_mut_val *val = key + 1; \ |
| 6728 | size_t key_len = strlen(_key); \ |
| 6729 | bool noesc = unsafe_yyjson_is_str_noesc(_key, key_len); \ |
| 6730 | key->tag = YYJSON_TYPE_STR; \ |
| 6731 | key->tag |= noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; \ |
| 6732 | key->tag |= (uint64_t)strlen(_key) << YYJSON_TAG_BIT; \ |
| 6733 | key->uni.str = _key; \ |
| 6734 | func \ |
| 6735 | unsafe_yyjson_mut_obj_add(obj, key, val, len); \ |
| 6736 | return true; \ |
| 6737 | } \ |
| 6738 | } \ |
| 6739 | return false |
| 6740 | |
| 6741 | yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, |
| 6742 | yyjson_mut_val *obj, |
| 6743 | const char *_key) { |
| 6744 | yyjson_mut_obj_add_func({ |
| 6745 | val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE; |
| 6746 | }); |
| 6747 | } |
| 6748 | |
| 6749 | yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, |
| 6750 | yyjson_mut_val *obj, |
| 6751 | const char *_key) { |
| 6752 | yyjson_mut_obj_add_func({ |
| 6753 | val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; |
| 6754 | }); |
| 6755 | } |
| 6756 | |
| 6757 | yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, |
| 6758 | yyjson_mut_val *obj, |
| 6759 | const char *_key) { |
| 6760 | yyjson_mut_obj_add_func({ |
| 6761 | val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; |
| 6762 | }); |
| 6763 | } |
| 6764 | |
| 6765 | yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, |
| 6766 | yyjson_mut_val *obj, |
| 6767 | const char *_key, |
| 6768 | bool _val) { |
| 6769 | yyjson_mut_obj_add_func({ |
| 6770 | val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)(_val) << 3); |
| 6771 | }); |
| 6772 | } |
| 6773 | |
| 6774 | yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, |
| 6775 | yyjson_mut_val *obj, |
| 6776 | const char *_key, |
| 6777 | uint64_t _val) { |
| 6778 | yyjson_mut_obj_add_func({ |
| 6779 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; |
| 6780 | val->uni.u64 = _val; |
| 6781 | }); |
| 6782 | } |
| 6783 | |
| 6784 | yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, |
| 6785 | yyjson_mut_val *obj, |
| 6786 | const char *_key, |
| 6787 | int64_t _val) { |
| 6788 | yyjson_mut_obj_add_func({ |
| 6789 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 6790 | val->uni.i64 = _val; |
| 6791 | }); |
| 6792 | } |
| 6793 | |
| 6794 | yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, |
| 6795 | yyjson_mut_val *obj, |
| 6796 | const char *_key, |
| 6797 | int64_t _val) { |
| 6798 | yyjson_mut_obj_add_func({ |
| 6799 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; |
| 6800 | val->uni.i64 = _val; |
| 6801 | }); |
| 6802 | } |
| 6803 | |
| 6804 | yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, |
| 6805 | yyjson_mut_val *obj, |
| 6806 | const char *_key, |
| 6807 | double _val) { |
| 6808 | yyjson_mut_obj_add_func({ |
| 6809 | val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; |
| 6810 | val->uni.f64 = _val; |
| 6811 | }); |
| 6812 | } |
| 6813 | |
| 6814 | yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, |
| 6815 | yyjson_mut_val *obj, |
| 6816 | const char *_key, |
| 6817 | const char *_val) { |
| 6818 | if (yyjson_unlikely(!_val)) return false; |
| 6819 | yyjson_mut_obj_add_func({ |
| 6820 | size_t val_len = strlen(_val); |
| 6821 | bool val_noesc = unsafe_yyjson_is_str_noesc(_val, val_len); |
| 6822 | val->tag = ((uint64_t)strlen(_val) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6823 | val->tag |= val_noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; |
| 6824 | val->uni.str = _val; |
| 6825 | }); |
| 6826 | } |
| 6827 | |
| 6828 | yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, |
| 6829 | yyjson_mut_val *obj, |
| 6830 | const char *_key, |
| 6831 | const char *_val, |
| 6832 | size_t _len) { |
| 6833 | if (yyjson_unlikely(!_val)) return false; |
| 6834 | yyjson_mut_obj_add_func({ |
| 6835 | val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6836 | val->uni.str = _val; |
| 6837 | }); |
| 6838 | } |
| 6839 | |
| 6840 | yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, |
| 6841 | yyjson_mut_val *obj, |
| 6842 | const char *_key, |
| 6843 | const char *_val) { |
| 6844 | if (yyjson_unlikely(!_val)) return false; |
| 6845 | yyjson_mut_obj_add_func({ |
| 6846 | size_t _len = strlen(_val); |
| 6847 | val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); |
| 6848 | if (yyjson_unlikely(!val->uni.str)) return false; |
| 6849 | val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6850 | }); |
| 6851 | } |
| 6852 | |
| 6853 | yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, |
| 6854 | yyjson_mut_val *obj, |
| 6855 | const char *_key, |
| 6856 | const char *_val, |
| 6857 | size_t _len) { |
| 6858 | if (yyjson_unlikely(!_val)) return false; |
| 6859 | yyjson_mut_obj_add_func({ |
| 6860 | val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); |
| 6861 | if (yyjson_unlikely(!val->uni.str)) return false; |
| 6862 | val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; |
| 6863 | }); |
| 6864 | } |
| 6865 | |
| 6866 | yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, |
| 6867 | yyjson_mut_val *obj, |
| 6868 | const char *_key, |
| 6869 | yyjson_mut_val *_val) { |
| 6870 | if (yyjson_unlikely(!_val)) return false; |
| 6871 | yyjson_mut_obj_add_func({ |
| 6872 | val = _val; |
| 6873 | }); |
| 6874 | } |
| 6875 | |
| 6876 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str(yyjson_mut_val *obj, |
| 6877 | const char *key) { |
| 6878 | return yyjson_mut_obj_remove_strn(obj, key, key ? strlen(key) : 0); |
| 6879 | } |
| 6880 | |
| 6881 | yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( |
| 6882 | yyjson_mut_val *obj, const char *_key, size_t _len) { |
| 6883 | if (yyjson_likely(yyjson_mut_is_obj(obj) && _key)) { |
| 6884 | yyjson_mut_val *key; |
| 6885 | yyjson_mut_obj_iter iter; |
| 6886 | yyjson_mut_val *val_removed = NULL; |
| 6887 | yyjson_mut_obj_iter_init(obj, &iter); |
| 6888 | while ((key = yyjson_mut_obj_iter_next(&iter)) != NULL) { |
| 6889 | if (unsafe_yyjson_equals_strn(key, _key, _len)) { |
| 6890 | if (!val_removed) val_removed = key->next; |
| 6891 | yyjson_mut_obj_iter_remove(&iter); |
| 6892 | } |
| 6893 | } |
| 6894 | return val_removed; |
| 6895 | } |
| 6896 | return NULL; |
| 6897 | } |
| 6898 | |
| 6899 | yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, |
| 6900 | yyjson_mut_val *obj, |
| 6901 | const char *key, |
| 6902 | const char *new_key) { |
| 6903 | if (!key || !new_key) return false; |
| 6904 | return yyjson_mut_obj_rename_keyn(doc, obj, key, strlen(key), |
| 6905 | new_key, strlen(new_key)); |
| 6906 | } |
| 6907 | |
| 6908 | yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, |
| 6909 | yyjson_mut_val *obj, |
| 6910 | const char *key, |
| 6911 | size_t len, |
| 6912 | const char *new_key, |
| 6913 | size_t new_len) { |
| 6914 | char *cpy_key = NULL; |
| 6915 | yyjson_mut_val *old_key; |
| 6916 | yyjson_mut_obj_iter iter; |
| 6917 | if (!doc || !obj || !key || !new_key) return false; |
| 6918 | yyjson_mut_obj_iter_init(obj, &iter); |
| 6919 | while ((old_key = yyjson_mut_obj_iter_next(&iter))) { |
| 6920 | if (unsafe_yyjson_equals_strn((void *)old_key, key, len)) { |
| 6921 | if (!cpy_key) { |
| 6922 | cpy_key = unsafe_yyjson_mut_strncpy(doc, new_key, new_len); |
| 6923 | if (!cpy_key) return false; |
| 6924 | } |
| 6925 | yyjson_mut_set_strn(old_key, cpy_key, new_len); |
| 6926 | } |
| 6927 | } |
| 6928 | return cpy_key != NULL; |
| 6929 | } |
| 6930 | |
| 6931 | |
| 6932 | |
| 6933 | /*============================================================================== |
| 6934 | * JSON Pointer API (Implementation) |
| 6935 | *============================================================================*/ |
| 6936 | |
| 6937 | #define yyjson_ptr_set_err(_code, _msg) do { \ |
| 6938 | if (err) { \ |
| 6939 | err->code = YYJSON_PTR_ERR_##_code; \ |
| 6940 | err->msg = _msg; \ |
| 6941 | err->pos = 0; \ |
| 6942 | } \ |
| 6943 | } while(false) |
| 6944 | |
| 6945 | /* require: val != NULL, *ptr == '/', len > 0 */ |
| 6946 | yyjson_api yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, |
| 6947 | const char *ptr, size_t len, |
| 6948 | yyjson_ptr_err *err); |
| 6949 | |
| 6950 | /* require: val != NULL, *ptr == '/', len > 0 */ |
| 6951 | yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(yyjson_mut_val *val, |
| 6952 | const char *ptr, |
| 6953 | size_t len, |
| 6954 | yyjson_ptr_ctx *ctx, |
| 6955 | yyjson_ptr_err *err); |
| 6956 | |
| 6957 | /* require: val/new_val/doc != NULL, *ptr == '/', len > 0 */ |
| 6958 | yyjson_api bool unsafe_yyjson_mut_ptr_putx(yyjson_mut_val *val, |
| 6959 | const char *ptr, size_t len, |
| 6960 | yyjson_mut_val *new_val, |
| 6961 | yyjson_mut_doc *doc, |
| 6962 | bool create_parent, bool insert_new, |
| 6963 | yyjson_ptr_ctx *ctx, |
| 6964 | yyjson_ptr_err *err); |
| 6965 | |
| 6966 | /* require: val/err != NULL, *ptr == '/', len > 0 */ |
| 6967 | yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( |
| 6968 | yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, |
| 6969 | yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); |
| 6970 | |
| 6971 | /* require: val/err != NULL, *ptr == '/', len > 0 */ |
| 6972 | yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val, |
| 6973 | const char *ptr, |
| 6974 | size_t len, |
| 6975 | yyjson_ptr_ctx *ctx, |
| 6976 | yyjson_ptr_err *err); |
| 6977 | |
| 6978 | yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, |
| 6979 | const char *ptr) { |
| 6980 | if (yyjson_unlikely(!ptr)) return NULL; |
| 6981 | return yyjson_doc_ptr_getn(doc, ptr, strlen(ptr)); |
| 6982 | } |
| 6983 | |
| 6984 | yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, |
| 6985 | const char *ptr, size_t len) { |
| 6986 | return yyjson_doc_ptr_getx(doc, ptr, len, NULL); |
| 6987 | } |
| 6988 | |
| 6989 | yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, |
| 6990 | const char *ptr, size_t len, |
| 6991 | yyjson_ptr_err *err) { |
| 6992 | yyjson_ptr_set_err(NONE, NULL); |
| 6993 | if (yyjson_unlikely(!doc || !ptr)) { |
| 6994 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 6995 | return NULL; |
| 6996 | } |
| 6997 | if (yyjson_unlikely(!doc->root)) { |
| 6998 | yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL" ); |
| 6999 | return NULL; |
| 7000 | } |
| 7001 | if (yyjson_unlikely(len == 0)) { |
| 7002 | return doc->root; |
| 7003 | } |
| 7004 | if (yyjson_unlikely(*ptr != '/')) { |
| 7005 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7006 | return NULL; |
| 7007 | } |
| 7008 | return unsafe_yyjson_ptr_getx(doc->root, ptr, len, err); |
| 7009 | } |
| 7010 | |
| 7011 | yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, |
| 7012 | const char *ptr) { |
| 7013 | if (yyjson_unlikely(!ptr)) return NULL; |
| 7014 | return yyjson_ptr_getn(val, ptr, strlen(ptr)); |
| 7015 | } |
| 7016 | |
| 7017 | yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, |
| 7018 | const char *ptr, size_t len) { |
| 7019 | return yyjson_ptr_getx(val, ptr, len, NULL); |
| 7020 | } |
| 7021 | |
| 7022 | yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, |
| 7023 | const char *ptr, size_t len, |
| 7024 | yyjson_ptr_err *err) { |
| 7025 | yyjson_ptr_set_err(NONE, NULL); |
| 7026 | if (yyjson_unlikely(!val || !ptr)) { |
| 7027 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7028 | return NULL; |
| 7029 | } |
| 7030 | if (yyjson_unlikely(len == 0)) { |
| 7031 | return val; |
| 7032 | } |
| 7033 | if (yyjson_unlikely(*ptr != '/')) { |
| 7034 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7035 | return NULL; |
| 7036 | } |
| 7037 | return unsafe_yyjson_ptr_getx(val, ptr, len, err); |
| 7038 | } |
| 7039 | |
| 7040 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, |
| 7041 | const char *ptr) { |
| 7042 | if (!ptr) return NULL; |
| 7043 | return yyjson_mut_doc_ptr_getn(doc, ptr, strlen(ptr)); |
| 7044 | } |
| 7045 | |
| 7046 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, |
| 7047 | const char *ptr, |
| 7048 | size_t len) { |
| 7049 | return yyjson_mut_doc_ptr_getx(doc, ptr, len, NULL, NULL); |
| 7050 | } |
| 7051 | |
| 7052 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, |
| 7053 | const char *ptr, |
| 7054 | size_t len, |
| 7055 | yyjson_ptr_ctx *ctx, |
| 7056 | yyjson_ptr_err *err) { |
| 7057 | yyjson_ptr_set_err(NONE, NULL); |
| 7058 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7059 | |
| 7060 | if (yyjson_unlikely(!doc || !ptr)) { |
| 7061 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7062 | return NULL; |
| 7063 | } |
| 7064 | if (yyjson_unlikely(!doc->root)) { |
| 7065 | yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL" ); |
| 7066 | return NULL; |
| 7067 | } |
| 7068 | if (yyjson_unlikely(len == 0)) { |
| 7069 | return doc->root; |
| 7070 | } |
| 7071 | if (yyjson_unlikely(*ptr != '/')) { |
| 7072 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7073 | return NULL; |
| 7074 | } |
| 7075 | return unsafe_yyjson_mut_ptr_getx(doc->root, ptr, len, ctx, err); |
| 7076 | } |
| 7077 | |
| 7078 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, |
| 7079 | const char *ptr) { |
| 7080 | if (!ptr) return NULL; |
| 7081 | return yyjson_mut_ptr_getn(val, ptr, strlen(ptr)); |
| 7082 | } |
| 7083 | |
| 7084 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, |
| 7085 | const char *ptr, |
| 7086 | size_t len) { |
| 7087 | return yyjson_mut_ptr_getx(val, ptr, len, NULL, NULL); |
| 7088 | } |
| 7089 | |
| 7090 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, |
| 7091 | const char *ptr, |
| 7092 | size_t len, |
| 7093 | yyjson_ptr_ctx *ctx, |
| 7094 | yyjson_ptr_err *err) { |
| 7095 | yyjson_ptr_set_err(NONE, NULL); |
| 7096 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7097 | |
| 7098 | if (yyjson_unlikely(!val || !ptr)) { |
| 7099 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7100 | return NULL; |
| 7101 | } |
| 7102 | if (yyjson_unlikely(len == 0)) { |
| 7103 | return val; |
| 7104 | } |
| 7105 | if (yyjson_unlikely(*ptr != '/')) { |
| 7106 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7107 | return NULL; |
| 7108 | } |
| 7109 | return unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); |
| 7110 | } |
| 7111 | |
| 7112 | yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, |
| 7113 | const char *ptr, |
| 7114 | yyjson_mut_val *new_val) { |
| 7115 | if (yyjson_unlikely(!ptr)) return false; |
| 7116 | return yyjson_mut_doc_ptr_addn(doc, ptr, strlen(ptr), new_val); |
| 7117 | } |
| 7118 | |
| 7119 | yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, |
| 7120 | const char *ptr, |
| 7121 | size_t len, |
| 7122 | yyjson_mut_val *new_val) { |
| 7123 | return yyjson_mut_doc_ptr_addx(doc, ptr, len, new_val, true, NULL, NULL); |
| 7124 | } |
| 7125 | |
| 7126 | yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, |
| 7127 | const char *ptr, size_t len, |
| 7128 | yyjson_mut_val *new_val, |
| 7129 | bool create_parent, |
| 7130 | yyjson_ptr_ctx *ctx, |
| 7131 | yyjson_ptr_err *err) { |
| 7132 | yyjson_ptr_set_err(NONE, NULL); |
| 7133 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7134 | |
| 7135 | if (yyjson_unlikely(!doc || !ptr || !new_val)) { |
| 7136 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7137 | return false; |
| 7138 | } |
| 7139 | if (yyjson_unlikely(len == 0)) { |
| 7140 | if (doc->root) { |
| 7141 | yyjson_ptr_set_err(SET_ROOT, "cannot set document's root" ); |
| 7142 | return false; |
| 7143 | } else { |
| 7144 | doc->root = new_val; |
| 7145 | return true; |
| 7146 | } |
| 7147 | } |
| 7148 | if (yyjson_unlikely(*ptr != '/')) { |
| 7149 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7150 | return false; |
| 7151 | } |
| 7152 | if (yyjson_unlikely(!doc->root && !create_parent)) { |
| 7153 | yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL" ); |
| 7154 | return false; |
| 7155 | } |
| 7156 | if (yyjson_unlikely(!doc->root)) { |
| 7157 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 7158 | if (yyjson_unlikely(!root)) { |
| 7159 | yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value" ); |
| 7160 | return false; |
| 7161 | } |
| 7162 | if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, |
| 7163 | create_parent, true, ctx, err)) { |
| 7164 | doc->root = root; |
| 7165 | return true; |
| 7166 | } |
| 7167 | return false; |
| 7168 | } |
| 7169 | return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, |
| 7170 | create_parent, true, ctx, err); |
| 7171 | } |
| 7172 | |
| 7173 | yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, |
| 7174 | const char *ptr, |
| 7175 | yyjson_mut_val *new_val, |
| 7176 | yyjson_mut_doc *doc) { |
| 7177 | if (yyjson_unlikely(!ptr)) return false; |
| 7178 | return yyjson_mut_ptr_addn(val, ptr, strlen(ptr), new_val, doc); |
| 7179 | } |
| 7180 | |
| 7181 | yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, |
| 7182 | const char *ptr, size_t len, |
| 7183 | yyjson_mut_val *new_val, |
| 7184 | yyjson_mut_doc *doc) { |
| 7185 | return yyjson_mut_ptr_addx(val, ptr, len, new_val, doc, true, NULL, NULL); |
| 7186 | } |
| 7187 | |
| 7188 | yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, |
| 7189 | const char *ptr, size_t len, |
| 7190 | yyjson_mut_val *new_val, |
| 7191 | yyjson_mut_doc *doc, |
| 7192 | bool create_parent, |
| 7193 | yyjson_ptr_ctx *ctx, |
| 7194 | yyjson_ptr_err *err) { |
| 7195 | yyjson_ptr_set_err(NONE, NULL); |
| 7196 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7197 | |
| 7198 | if (yyjson_unlikely(!val || !ptr || !new_val || !doc)) { |
| 7199 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7200 | return false; |
| 7201 | } |
| 7202 | if (yyjson_unlikely(len == 0)) { |
| 7203 | yyjson_ptr_set_err(SET_ROOT, "cannot set root" ); |
| 7204 | return false; |
| 7205 | } |
| 7206 | if (yyjson_unlikely(*ptr != '/')) { |
| 7207 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7208 | return false; |
| 7209 | } |
| 7210 | return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, |
| 7211 | doc, create_parent, true, ctx, err); |
| 7212 | } |
| 7213 | |
| 7214 | yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, |
| 7215 | const char *ptr, |
| 7216 | yyjson_mut_val *new_val) { |
| 7217 | if (yyjson_unlikely(!ptr)) return false; |
| 7218 | return yyjson_mut_doc_ptr_setn(doc, ptr, strlen(ptr), new_val); |
| 7219 | } |
| 7220 | |
| 7221 | yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, |
| 7222 | const char *ptr, size_t len, |
| 7223 | yyjson_mut_val *new_val) { |
| 7224 | return yyjson_mut_doc_ptr_setx(doc, ptr, len, new_val, true, NULL, NULL); |
| 7225 | } |
| 7226 | |
| 7227 | yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, |
| 7228 | const char *ptr, size_t len, |
| 7229 | yyjson_mut_val *new_val, |
| 7230 | bool create_parent, |
| 7231 | yyjson_ptr_ctx *ctx, |
| 7232 | yyjson_ptr_err *err) { |
| 7233 | yyjson_ptr_set_err(NONE, NULL); |
| 7234 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7235 | |
| 7236 | if (yyjson_unlikely(!doc || !ptr)) { |
| 7237 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7238 | return false; |
| 7239 | } |
| 7240 | if (yyjson_unlikely(len == 0)) { |
| 7241 | if (ctx) ctx->old = doc->root; |
| 7242 | doc->root = new_val; |
| 7243 | return true; |
| 7244 | } |
| 7245 | if (yyjson_unlikely(*ptr != '/')) { |
| 7246 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7247 | return false; |
| 7248 | } |
| 7249 | if (!new_val) { |
| 7250 | if (!doc->root) { |
| 7251 | yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved" ); |
| 7252 | return false; |
| 7253 | } |
| 7254 | return !!unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); |
| 7255 | } |
| 7256 | if (yyjson_unlikely(!doc->root && !create_parent)) { |
| 7257 | yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL" ); |
| 7258 | return false; |
| 7259 | } |
| 7260 | if (yyjson_unlikely(!doc->root)) { |
| 7261 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 7262 | if (yyjson_unlikely(!root)) { |
| 7263 | yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value" ); |
| 7264 | return false; |
| 7265 | } |
| 7266 | if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, |
| 7267 | create_parent, false, ctx, err)) { |
| 7268 | doc->root = root; |
| 7269 | return true; |
| 7270 | } |
| 7271 | return false; |
| 7272 | } |
| 7273 | return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, |
| 7274 | create_parent, false, ctx, err); |
| 7275 | } |
| 7276 | |
| 7277 | yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, |
| 7278 | const char *ptr, |
| 7279 | yyjson_mut_val *new_val, |
| 7280 | yyjson_mut_doc *doc) { |
| 7281 | if (yyjson_unlikely(!ptr)) return false; |
| 7282 | return yyjson_mut_ptr_setn(val, ptr, strlen(ptr), new_val, doc); |
| 7283 | } |
| 7284 | |
| 7285 | yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, |
| 7286 | const char *ptr, size_t len, |
| 7287 | yyjson_mut_val *new_val, |
| 7288 | yyjson_mut_doc *doc) { |
| 7289 | return yyjson_mut_ptr_setx(val, ptr, len, new_val, doc, true, NULL, NULL); |
| 7290 | } |
| 7291 | |
| 7292 | yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, |
| 7293 | const char *ptr, size_t len, |
| 7294 | yyjson_mut_val *new_val, |
| 7295 | yyjson_mut_doc *doc, |
| 7296 | bool create_parent, |
| 7297 | yyjson_ptr_ctx *ctx, |
| 7298 | yyjson_ptr_err *err) { |
| 7299 | yyjson_ptr_set_err(NONE, NULL); |
| 7300 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7301 | |
| 7302 | if (yyjson_unlikely(!val || !ptr || !doc)) { |
| 7303 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7304 | return false; |
| 7305 | } |
| 7306 | if (yyjson_unlikely(len == 0)) { |
| 7307 | yyjson_ptr_set_err(SET_ROOT, "cannot set root" ); |
| 7308 | return false; |
| 7309 | } |
| 7310 | if (yyjson_unlikely(*ptr != '/')) { |
| 7311 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7312 | return false; |
| 7313 | } |
| 7314 | if (!new_val) { |
| 7315 | return !!unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); |
| 7316 | } |
| 7317 | return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, doc, |
| 7318 | create_parent, false, ctx, err); |
| 7319 | } |
| 7320 | |
| 7321 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( |
| 7322 | yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val) { |
| 7323 | if (!ptr) return NULL; |
| 7324 | return yyjson_mut_doc_ptr_replacen(doc, ptr, strlen(ptr), new_val); |
| 7325 | } |
| 7326 | |
| 7327 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( |
| 7328 | yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val) { |
| 7329 | return yyjson_mut_doc_ptr_replacex(doc, ptr, len, new_val, NULL, NULL); |
| 7330 | } |
| 7331 | |
| 7332 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( |
| 7333 | yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, |
| 7334 | yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { |
| 7335 | |
| 7336 | yyjson_ptr_set_err(NONE, NULL); |
| 7337 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7338 | |
| 7339 | if (yyjson_unlikely(!doc || !ptr || !new_val)) { |
| 7340 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7341 | return NULL; |
| 7342 | } |
| 7343 | if (yyjson_unlikely(len == 0)) { |
| 7344 | yyjson_mut_val *root = doc->root; |
| 7345 | if (yyjson_unlikely(!root)) { |
| 7346 | yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved" ); |
| 7347 | return NULL; |
| 7348 | } |
| 7349 | if (ctx) ctx->old = root; |
| 7350 | doc->root = new_val; |
| 7351 | return root; |
| 7352 | } |
| 7353 | if (yyjson_unlikely(!doc->root)) { |
| 7354 | yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL" ); |
| 7355 | return NULL; |
| 7356 | } |
| 7357 | if (yyjson_unlikely(*ptr != '/')) { |
| 7358 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7359 | return NULL; |
| 7360 | } |
| 7361 | return unsafe_yyjson_mut_ptr_replacex(doc->root, ptr, len, new_val, |
| 7362 | ctx, err); |
| 7363 | } |
| 7364 | |
| 7365 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( |
| 7366 | yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val) { |
| 7367 | if (!ptr) return NULL; |
| 7368 | return yyjson_mut_ptr_replacen(val, ptr, strlen(ptr), new_val); |
| 7369 | } |
| 7370 | |
| 7371 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( |
| 7372 | yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val) { |
| 7373 | return yyjson_mut_ptr_replacex(val, ptr, len, new_val, NULL, NULL); |
| 7374 | } |
| 7375 | |
| 7376 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( |
| 7377 | yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, |
| 7378 | yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { |
| 7379 | |
| 7380 | yyjson_ptr_set_err(NONE, NULL); |
| 7381 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7382 | |
| 7383 | if (yyjson_unlikely(!val || !ptr || !new_val)) { |
| 7384 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7385 | return NULL; |
| 7386 | } |
| 7387 | if (yyjson_unlikely(len == 0)) { |
| 7388 | yyjson_ptr_set_err(SET_ROOT, "cannot set root" ); |
| 7389 | return NULL; |
| 7390 | } |
| 7391 | if (yyjson_unlikely(*ptr != '/')) { |
| 7392 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7393 | return NULL; |
| 7394 | } |
| 7395 | return unsafe_yyjson_mut_ptr_replacex(val, ptr, len, new_val, ctx, err); |
| 7396 | } |
| 7397 | |
| 7398 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( |
| 7399 | yyjson_mut_doc *doc, const char *ptr) { |
| 7400 | if (!ptr) return NULL; |
| 7401 | return yyjson_mut_doc_ptr_removen(doc, ptr, strlen(ptr)); |
| 7402 | } |
| 7403 | |
| 7404 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( |
| 7405 | yyjson_mut_doc *doc, const char *ptr, size_t len) { |
| 7406 | return yyjson_mut_doc_ptr_removex(doc, ptr, len, NULL, NULL); |
| 7407 | } |
| 7408 | |
| 7409 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( |
| 7410 | yyjson_mut_doc *doc, const char *ptr, size_t len, |
| 7411 | yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { |
| 7412 | |
| 7413 | yyjson_ptr_set_err(NONE, NULL); |
| 7414 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7415 | |
| 7416 | if (yyjson_unlikely(!doc || !ptr)) { |
| 7417 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7418 | return NULL; |
| 7419 | } |
| 7420 | if (yyjson_unlikely(!doc->root)) { |
| 7421 | yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL" ); |
| 7422 | return NULL; |
| 7423 | } |
| 7424 | if (yyjson_unlikely(len == 0)) { |
| 7425 | yyjson_mut_val *root = doc->root; |
| 7426 | if (ctx) ctx->old = root; |
| 7427 | doc->root = NULL; |
| 7428 | return root; |
| 7429 | } |
| 7430 | if (yyjson_unlikely(*ptr != '/')) { |
| 7431 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7432 | return NULL; |
| 7433 | } |
| 7434 | return unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); |
| 7435 | } |
| 7436 | |
| 7437 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, |
| 7438 | const char *ptr) { |
| 7439 | if (!ptr) return NULL; |
| 7440 | return yyjson_mut_ptr_removen(val, ptr, strlen(ptr)); |
| 7441 | } |
| 7442 | |
| 7443 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, |
| 7444 | const char *ptr, |
| 7445 | size_t len) { |
| 7446 | return yyjson_mut_ptr_removex(val, ptr, len, NULL, NULL); |
| 7447 | } |
| 7448 | |
| 7449 | yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, |
| 7450 | const char *ptr, |
| 7451 | size_t len, |
| 7452 | yyjson_ptr_ctx *ctx, |
| 7453 | yyjson_ptr_err *err) { |
| 7454 | yyjson_ptr_set_err(NONE, NULL); |
| 7455 | if (ctx) memset(ctx, 0, sizeof(*ctx)); |
| 7456 | |
| 7457 | if (yyjson_unlikely(!val || !ptr)) { |
| 7458 | yyjson_ptr_set_err(PARAMETER, "input parameter is NULL" ); |
| 7459 | return NULL; |
| 7460 | } |
| 7461 | if (yyjson_unlikely(len == 0)) { |
| 7462 | yyjson_ptr_set_err(SET_ROOT, "cannot set root" ); |
| 7463 | return NULL; |
| 7464 | } |
| 7465 | if (yyjson_unlikely(*ptr != '/')) { |
| 7466 | yyjson_ptr_set_err(SYNTAX, "no prefix '/'" ); |
| 7467 | return NULL; |
| 7468 | } |
| 7469 | return unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); |
| 7470 | } |
| 7471 | |
| 7472 | yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, |
| 7473 | yyjson_mut_val *key, |
| 7474 | yyjson_mut_val *val) { |
| 7475 | yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; |
| 7476 | if (!ctx || !ctx->ctn || !val) return false; |
| 7477 | ctn = ctx->ctn; |
| 7478 | |
| 7479 | if (yyjson_mut_is_obj(ctn)) { |
| 7480 | if (!key) return false; |
| 7481 | key->next = val; |
| 7482 | pre_key = ctx->pre; |
| 7483 | if (unsafe_yyjson_get_len(ctn) == 0) { |
| 7484 | val->next = key; |
| 7485 | ctn->uni.ptr = key; |
| 7486 | ctx->pre = key; |
| 7487 | } else if (!pre_key) { |
| 7488 | pre_key = (yyjson_mut_val *)ctn->uni.ptr; |
| 7489 | pre_val = pre_key->next; |
| 7490 | val->next = pre_val->next; |
| 7491 | pre_val->next = key; |
| 7492 | ctn->uni.ptr = key; |
| 7493 | ctx->pre = pre_key; |
| 7494 | } else { |
| 7495 | cur_key = pre_key->next->next; |
| 7496 | cur_val = cur_key->next; |
| 7497 | val->next = cur_val->next; |
| 7498 | cur_val->next = key; |
| 7499 | if (ctn->uni.ptr == cur_key) ctn->uni.ptr = key; |
| 7500 | ctx->pre = cur_key; |
| 7501 | } |
| 7502 | } else { |
| 7503 | pre_val = ctx->pre; |
| 7504 | if (unsafe_yyjson_get_len(ctn) == 0) { |
| 7505 | val->next = val; |
| 7506 | ctn->uni.ptr = val; |
| 7507 | ctx->pre = val; |
| 7508 | } else if (!pre_val) { |
| 7509 | pre_val = (yyjson_mut_val *)ctn->uni.ptr; |
| 7510 | val->next = pre_val->next; |
| 7511 | pre_val->next = val; |
| 7512 | ctn->uni.ptr = val; |
| 7513 | ctx->pre = pre_val; |
| 7514 | } else { |
| 7515 | cur_val = pre_val->next; |
| 7516 | val->next = cur_val->next; |
| 7517 | cur_val->next = val; |
| 7518 | if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; |
| 7519 | ctx->pre = cur_val; |
| 7520 | } |
| 7521 | } |
| 7522 | unsafe_yyjson_inc_len(ctn); |
| 7523 | return true; |
| 7524 | } |
| 7525 | |
| 7526 | yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, |
| 7527 | yyjson_mut_val *val) { |
| 7528 | yyjson_mut_val *ctn, *pre_key, *cur_key, *pre_val, *cur_val; |
| 7529 | if (!ctx || !ctx->ctn || !ctx->pre || !val) return false; |
| 7530 | ctn = ctx->ctn; |
| 7531 | if (yyjson_mut_is_obj(ctn)) { |
| 7532 | pre_key = ctx->pre; |
| 7533 | pre_val = pre_key->next; |
| 7534 | cur_key = pre_val->next; |
| 7535 | cur_val = cur_key->next; |
| 7536 | /* replace current value */ |
| 7537 | cur_key->next = val; |
| 7538 | val->next = cur_val->next; |
| 7539 | ctx->old = cur_val; |
| 7540 | } else { |
| 7541 | pre_val = ctx->pre; |
| 7542 | cur_val = pre_val->next; |
| 7543 | /* replace current value */ |
| 7544 | if (pre_val != cur_val) { |
| 7545 | val->next = cur_val->next; |
| 7546 | pre_val->next = val; |
| 7547 | if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; |
| 7548 | } else { |
| 7549 | val->next = val; |
| 7550 | ctn->uni.ptr = val; |
| 7551 | ctx->pre = val; |
| 7552 | } |
| 7553 | ctx->old = cur_val; |
| 7554 | } |
| 7555 | return true; |
| 7556 | } |
| 7557 | |
| 7558 | yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx) { |
| 7559 | yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; |
| 7560 | size_t len; |
| 7561 | if (!ctx || !ctx->ctn || !ctx->pre) return false; |
| 7562 | ctn = ctx->ctn; |
| 7563 | if (yyjson_mut_is_obj(ctn)) { |
| 7564 | pre_key = ctx->pre; |
| 7565 | pre_val = pre_key->next; |
| 7566 | cur_key = pre_val->next; |
| 7567 | cur_val = cur_key->next; |
| 7568 | /* remove current key-value */ |
| 7569 | pre_val->next = cur_val->next; |
| 7570 | if (ctn->uni.ptr == cur_key) ctn->uni.ptr = pre_key; |
| 7571 | ctx->pre = NULL; |
| 7572 | ctx->old = cur_val; |
| 7573 | } else { |
| 7574 | pre_val = ctx->pre; |
| 7575 | cur_val = pre_val->next; |
| 7576 | /* remove current key-value */ |
| 7577 | pre_val->next = cur_val->next; |
| 7578 | if (ctn->uni.ptr == cur_val) ctn->uni.ptr = pre_val; |
| 7579 | ctx->pre = NULL; |
| 7580 | ctx->old = cur_val; |
| 7581 | } |
| 7582 | len = unsafe_yyjson_get_len(ctn) - 1; |
| 7583 | if (len == 0) ctn->uni.ptr = NULL; |
| 7584 | unsafe_yyjson_set_len(ctn, len); |
| 7585 | return true; |
| 7586 | } |
| 7587 | |
| 7588 | #undef yyjson_ptr_set_err |
| 7589 | |
| 7590 | |
| 7591 | |
| 7592 | /*============================================================================== |
| 7593 | * JSON Value at Pointer API (Implementation) |
| 7594 | *============================================================================*/ |
| 7595 | |
| 7596 | /** |
| 7597 | Set provided `value` if the JSON Pointer (RFC 6901) exists and is type bool. |
| 7598 | Returns true if value at `ptr` exists and is the correct type, otherwise false. |
| 7599 | */ |
| 7600 | yyjson_api_inline bool yyjson_ptr_get_bool( |
| 7601 | yyjson_val *root, const char *ptr, bool *value) { |
| 7602 | yyjson_val *val = yyjson_ptr_get(root, ptr); |
| 7603 | if (value && yyjson_is_bool(val)) { |
| 7604 | *value = unsafe_yyjson_get_bool(val); |
| 7605 | return true; |
| 7606 | } else { |
| 7607 | return false; |
| 7608 | } |
| 7609 | } |
| 7610 | |
| 7611 | /** |
| 7612 | Set provided `value` if the JSON Pointer (RFC 6901) exists and is type uint. |
| 7613 | Returns true if value at `ptr` exists and is the correct type, otherwise false. |
| 7614 | */ |
| 7615 | yyjson_api_inline bool yyjson_ptr_get_uint( |
| 7616 | yyjson_val *root, const char *ptr, uint64_t *value) { |
| 7617 | yyjson_val *val = yyjson_ptr_get(root, ptr); |
| 7618 | if (value && yyjson_is_uint(val)) { |
| 7619 | *value = unsafe_yyjson_get_uint(val); |
| 7620 | return true; |
| 7621 | } else { |
| 7622 | return false; |
| 7623 | } |
| 7624 | } |
| 7625 | |
| 7626 | /** |
| 7627 | Set provided `value` if the JSON Pointer (RFC 6901) exists and is type sint. |
| 7628 | Returns true if value at `ptr` exists and is the correct type, otherwise false. |
| 7629 | */ |
| 7630 | yyjson_api_inline bool yyjson_ptr_get_sint( |
| 7631 | yyjson_val *root, const char *ptr, int64_t *value) { |
| 7632 | yyjson_val *val = yyjson_ptr_get(root, ptr); |
| 7633 | if (value && yyjson_is_sint(val)) { |
| 7634 | *value = unsafe_yyjson_get_sint(val); |
| 7635 | return true; |
| 7636 | } else { |
| 7637 | return false; |
| 7638 | } |
| 7639 | } |
| 7640 | |
| 7641 | /** |
| 7642 | Set provided `value` if the JSON Pointer (RFC 6901) exists and is type real. |
| 7643 | Returns true if value at `ptr` exists and is the correct type, otherwise false. |
| 7644 | */ |
| 7645 | yyjson_api_inline bool yyjson_ptr_get_real( |
| 7646 | yyjson_val *root, const char *ptr, double *value) { |
| 7647 | yyjson_val *val = yyjson_ptr_get(root, ptr); |
| 7648 | if (value && yyjson_is_real(val)) { |
| 7649 | *value = unsafe_yyjson_get_real(val); |
| 7650 | return true; |
| 7651 | } else { |
| 7652 | return false; |
| 7653 | } |
| 7654 | } |
| 7655 | |
| 7656 | /** |
| 7657 | Set provided `value` if the JSON Pointer (RFC 6901) exists and is type sint, |
| 7658 | uint or real. |
| 7659 | Returns true if value at `ptr` exists and is the correct type, otherwise false. |
| 7660 | */ |
| 7661 | yyjson_api_inline bool yyjson_ptr_get_num( |
| 7662 | yyjson_val *root, const char *ptr, double *value) { |
| 7663 | yyjson_val *val = yyjson_ptr_get(root, ptr); |
| 7664 | if (value && yyjson_is_num(val)) { |
| 7665 | *value = unsafe_yyjson_get_num(val); |
| 7666 | return true; |
| 7667 | } else { |
| 7668 | return false; |
| 7669 | } |
| 7670 | } |
| 7671 | |
| 7672 | /** |
| 7673 | Set provided `value` if the JSON Pointer (RFC 6901) exists and is type string. |
| 7674 | Returns true if value at `ptr` exists and is the correct type, otherwise false. |
| 7675 | */ |
| 7676 | yyjson_api_inline bool yyjson_ptr_get_str( |
| 7677 | yyjson_val *root, const char *ptr, const char **value) { |
| 7678 | yyjson_val *val = yyjson_ptr_get(root, ptr); |
| 7679 | if (value && yyjson_is_str(val)) { |
| 7680 | *value = unsafe_yyjson_get_str(val); |
| 7681 | return true; |
| 7682 | } else { |
| 7683 | return false; |
| 7684 | } |
| 7685 | } |
| 7686 | |
| 7687 | |
| 7688 | |
| 7689 | /*============================================================================== |
| 7690 | * Deprecated |
| 7691 | *============================================================================*/ |
| 7692 | |
| 7693 | /** @deprecated renamed to `yyjson_doc_ptr_get` */ |
| 7694 | yyjson_deprecated("renamed to yyjson_doc_ptr_get" ) |
| 7695 | yyjson_api_inline yyjson_val *yyjson_doc_get_pointer(yyjson_doc *doc, |
| 7696 | const char *ptr) { |
| 7697 | return yyjson_doc_ptr_get(doc, ptr); |
| 7698 | } |
| 7699 | |
| 7700 | /** @deprecated renamed to `yyjson_doc_ptr_getn` */ |
| 7701 | yyjson_deprecated("renamed to yyjson_doc_ptr_getn" ) |
| 7702 | yyjson_api_inline yyjson_val *yyjson_doc_get_pointern(yyjson_doc *doc, |
| 7703 | const char *ptr, |
| 7704 | size_t len) { |
| 7705 | return yyjson_doc_ptr_getn(doc, ptr, len); |
| 7706 | } |
| 7707 | |
| 7708 | /** @deprecated renamed to `yyjson_mut_doc_ptr_get` */ |
| 7709 | yyjson_deprecated("renamed to yyjson_mut_doc_ptr_get" ) |
| 7710 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointer( |
| 7711 | yyjson_mut_doc *doc, const char *ptr) { |
| 7712 | return yyjson_mut_doc_ptr_get(doc, ptr); |
| 7713 | } |
| 7714 | |
| 7715 | /** @deprecated renamed to `yyjson_mut_doc_ptr_getn` */ |
| 7716 | yyjson_deprecated("renamed to yyjson_mut_doc_ptr_getn" ) |
| 7717 | yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointern( |
| 7718 | yyjson_mut_doc *doc, const char *ptr, size_t len) { |
| 7719 | return yyjson_mut_doc_ptr_getn(doc, ptr, len); |
| 7720 | } |
| 7721 | |
| 7722 | /** @deprecated renamed to `yyjson_ptr_get` */ |
| 7723 | yyjson_deprecated("renamed to yyjson_ptr_get" ) |
| 7724 | yyjson_api_inline yyjson_val *yyjson_get_pointer(yyjson_val *val, |
| 7725 | const char *ptr) { |
| 7726 | return yyjson_ptr_get(val, ptr); |
| 7727 | } |
| 7728 | |
| 7729 | /** @deprecated renamed to `yyjson_ptr_getn` */ |
| 7730 | yyjson_deprecated("renamed to yyjson_ptr_getn" ) |
| 7731 | yyjson_api_inline yyjson_val *yyjson_get_pointern(yyjson_val *val, |
| 7732 | const char *ptr, |
| 7733 | size_t len) { |
| 7734 | return yyjson_ptr_getn(val, ptr, len); |
| 7735 | } |
| 7736 | |
| 7737 | /** @deprecated renamed to `yyjson_mut_ptr_get` */ |
| 7738 | yyjson_deprecated("renamed to yyjson_mut_ptr_get" ) |
| 7739 | yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointer(yyjson_mut_val *val, |
| 7740 | const char *ptr) { |
| 7741 | return yyjson_mut_ptr_get(val, ptr); |
| 7742 | } |
| 7743 | |
| 7744 | /** @deprecated renamed to `yyjson_mut_ptr_getn` */ |
| 7745 | yyjson_deprecated("renamed to yyjson_mut_ptr_getn" ) |
| 7746 | yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointern(yyjson_mut_val *val, |
| 7747 | const char *ptr, |
| 7748 | size_t len) { |
| 7749 | return yyjson_mut_ptr_getn(val, ptr, len); |
| 7750 | } |
| 7751 | |
| 7752 | /** @deprecated renamed to `yyjson_mut_ptr_getn` */ |
| 7753 | yyjson_deprecated("renamed to unsafe_yyjson_ptr_getn" ) |
| 7754 | yyjson_api_inline yyjson_val *unsafe_yyjson_get_pointer(yyjson_val *val, |
| 7755 | const char *ptr, |
| 7756 | size_t len) { |
| 7757 | yyjson_ptr_err err; |
| 7758 | return unsafe_yyjson_ptr_getx(val, ptr, len, &err); |
| 7759 | } |
| 7760 | |
| 7761 | /** @deprecated renamed to `unsafe_yyjson_mut_ptr_getx` */ |
| 7762 | yyjson_deprecated("renamed to unsafe_yyjson_mut_ptr_getx" ) |
| 7763 | yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_get_pointer( |
| 7764 | yyjson_mut_val *val, const char *ptr, size_t len) { |
| 7765 | yyjson_ptr_err err; |
| 7766 | return unsafe_yyjson_mut_ptr_getx(val, ptr, len, NULL, &err); |
| 7767 | } |
| 7768 | |
| 7769 | |
| 7770 | |
| 7771 | /*============================================================================== |
| 7772 | * Compiler Hint End |
| 7773 | *============================================================================*/ |
| 7774 | |
| 7775 | #if defined(__clang__) |
| 7776 | # pragma clang diagnostic pop |
| 7777 | #elif defined(__GNUC__) |
| 7778 | # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 7779 | # pragma GCC diagnostic pop |
| 7780 | # endif |
| 7781 | #elif defined(_MSC_VER) |
| 7782 | # pragma warning(pop) |
| 7783 | #endif /* warning suppress end */ |
| 7784 | |
| 7785 | #ifdef __cplusplus |
| 7786 | } |
| 7787 | #endif /* extern "C" end */ |
| 7788 | |
| 7789 | #endif /* YYJSON_H */ |
| 7790 | |