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