This document contains all the API usage and examples for the yyjson library.
API Design
API prefix
All public functions and structs are prefixed with yyjson_, and all constants are prefixed with YYJSON_.
API for immutable/mutable data
The library has 2 types of data structures: immutable and mutable:
When reading a JSON, yyjson returns immutable documents and values.
When building a JSON, yyjson creates mutable documents and values.
The document holds the memory for all its JSON values and strings.
For most immutable APIs, you can just add a mut after yyjson_ to get the mutable version, for example:
yyjson_api_inline bool yyjson_is_str(const yyjson_val *val)
Definition yyjson.h:5402
yyjson_api_inline char * yyjson_mut_write(const yyjson_mut_doc *doc, yyjson_write_flag flg, size_t *len)
Definition yyjson.h:1614
yyjson_api_inline bool yyjson_mut_is_str(const yyjson_mut_val *val)
Definition yyjson.h:5982
yyjson_api_inline char * yyjson_write(const yyjson_doc *doc, yyjson_write_flag flg, size_t *len)
Definition yyjson.h:1481
The library also provides some functions to convert values between immutable and mutable:
yyjson_api yyjson_mut_doc * yyjson_doc_mut_copy(const yyjson_doc *doc, const yyjson_alc *alc)
yyjson_api yyjson_doc * yyjson_mut_doc_imut_copy(const yyjson_mut_doc *doc, const yyjson_alc *alc)
yyjson_api yyjson_doc * yyjson_mut_val_imut_copy(const yyjson_mut_val *val, const yyjson_alc *alc)
yyjson_api yyjson_mut_val * yyjson_val_mut_copy(yyjson_mut_doc *doc, const yyjson_val *val)
API for string
The library supports strings with or without null-terminator (\0).
When you need to use a string without a null-terminator or when you explicitly know the length of the string, you can use the function that ends with n, for example:
yyjson_api_inline bool yyjson_equals_strn(const yyjson_val *val, const char *str, size_t len)
Definition yyjson.h:5498
yyjson_api_inline bool yyjson_equals_str(const yyjson_val *val, const char *str)
Definition yyjson.h:5489
When creating JSON, yyjson treats strings as constants for better performance. However, if your string will be modified, you should use a function with a cpy to copy the string to the document, for example:
yyjson_api_inline yyjson_mut_val * yyjson_mut_strn(yyjson_mut_doc *doc, const char *str, size_t len)
Definition yyjson.h:6288
yyjson_api_inline yyjson_mut_val * yyjson_mut_strncpy(yyjson_mut_doc *doc, const char *str, size_t len)
Definition yyjson.h:6307
yyjson_api_inline yyjson_mut_val * yyjson_mut_strcpy(yyjson_mut_doc *doc, const char *str)
Definition yyjson.h:6294
yyjson_api_inline yyjson_mut_val * yyjson_mut_str(yyjson_mut_doc *doc, const char *str)
Definition yyjson.h:6283
Reading JSON
The library provides 5 functions for reading JSON.
Each function accepts an input of UTF-8 data or a file,
returns a document if successful or NULL if it fails.
Read JSON from string
The dat should be a UTF-8 string, null-terminator is not required.
The len is the byte length of dat.
The flg is reader flag, pass 0 if you don't need it, see reader flag for details.
Returns NULL if dat is NULL or len is 0.
size_t len,
uint32_t yyjson_read_flag
Definition yyjson.h:806
yyjson_api_inline yyjson_doc * yyjson_read(const char *dat, size_t len, yyjson_read_flag flg)
Definition yyjson.h:1073
Sample code:
const char *str = "[1,2,3,4]";
if (doc) {...}
yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc)
Definition yyjson.h:5347
Read JSON from file
The path is the JSON file path. This should be a null-terminated string using the system's native encoding.
The flg is reader flag, pass 0 if you don't need it, see reader flag for details.
The alc is memory allocator, pass NULL if you don't need it, see memory allocator for details.
The err is a pointer to receive error message, pass NULL if you don't need it.
Returns NULL if path is NULL or invalid.
yyjson_api yyjson_doc * yyjson_read_file(const char *path, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err)
Sample code:
Read JSON from file pointer
The fp is file pointer. The data will be read from the current position of the FILE to the end.
The flg is reader flag, pass 0 if you don't need it, see reader flag for details.
The alc is memory allocator, pass NULL if you don't need it, see memory allocator for details.
The err is a pointer to receive error message, pass NULL if you don't need it.
Returns NULL if fp is NULL or invalid.
yyjson_api yyjson_doc * yyjson_read_fp(FILE *fp, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err)
Sample code:
FILE *fp = fdopen(fd, "rb");
if (fp) fclose(fp);
if (doc) {...}
Read JSON with options
The dat should be a UTF-8 string, you can pass a const string if you don't use YYJSON_READ_INSITU flag.
The len is the dat's length in bytes.
The flg is reader flag, pass 0 if you don't need it, see reader flag for details.
The alc is memory allocator, pass NULL if you don't need it, see memory allocator for details.
The err is a pointer to receive error message, pass NULL if you don't need it.
size_t len,
yyjson_api yyjson_doc * yyjson_read_opts(char *dat, size_t len, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err)
Sample code:
const char *dat = your_file.bytes;
size_t len = your_file.size;
if (doc) {...}
static const yyjson_read_flag YYJSON_READ_ALLOW_INF_AND_NAN
Definition yyjson.h:840
static const yyjson_read_flag YYJSON_READ_ALLOW_COMMENTS
Definition yyjson.h:836
Read JSON incrementally
Reading a very large JSON document can freeze the program for a short while. If this is not acceptable, incremental reading can be used.
Incremental reading is recommended only for large documents and only when the program needs to be responsive. Incremental reading is slightly slower than yyjson_read() and yyjson_read_opts().
Note: The incremental JSON reader only supports standard JSON. Flags for non-standard features (e.g. comments, trailing commas) are ignored.
To read a large JSON document incrementally:
- Call yyjson_incr_new() to create the state for incremental reading.
- Call yyjson_incr_read() repeatedly.
- Call yyjson_incr_free() to free the state.
Create the state for incremental reading
The buf should be a UTF-8 string, null-terminator is not required. You can pass a const string if you don't use the YYJSON_READ_INSITU flag.
The buf_len is the length of buf in bytes. The flg is reader flag. Pass 0 if you don't need it. See reader flag for details. The alc is memory allocator, pass NULL if you don't need it. See memory allocator for details.
The function returns a new state, or NULL if memory allocation fails.
struct yyjson_incr_state yyjson_incr_state
Definition yyjson.h:1086
yyjson_api yyjson_incr_state * yyjson_incr_new(char *buf, size_t buf_len, yyjson_read_flag flg, const yyjson_alc *alc)
Perform incremental read
Performs incremental read of up to len bytes.
The state for incremental reading is created using yyjson_incr_new().
The len is the maximum number of bytes to read, counting from the start of the JSON data.
The err is a pointer to receive the error information. Required.
The function returns a document object when the reading is complete and NULL otherwise. If err->code is set to YYJSON_READ_ERROR_MORE, it indicates that parsing is not yet complete. Then, increase len by some kilobytes and call this function again. Continue increasing len until len == buf_len (the total length of the input buffer) or until an error other than YYJSON_READ_ERROR_MORE is returned.
Note: Parsing in very small increments is not efficient. An increment of several kilobytes or megabytes is recommended.
yyjson_api yyjson_doc * yyjson_incr_read(yyjson_incr_state *state, size_t len, yyjson_read_err *err)
Free the state used for incremental reading
Free the state created by yyjson_incr_new().
yyjson_api void yyjson_incr_free(yyjson_incr_state *state)
Sample code
const char *dat = your_file.bytes;
size_t len = your_file.size;
size_t read_so_far = 0;
do {
read_so_far += 100000;
if (read_so_far > len)
read_so_far = len;
break;
} while (read_so_far < len);
if (doc != NULL) { ... }
static const yyjson_read_code YYJSON_READ_ERROR_MORE
Definition yyjson.h:959
yyjson_read_code code
Definition yyjson.h:967
static const yyjson_read_flag YYJSON_READ_NOFLAG
Definition yyjson.h:816
Reader error handling
When reading JSON fails, and you need error information, you can pass a yyjson_read_err pointer to the yyjson_read_xxx() functions to receive the error details.
Sample code:
char *dat = ...;
size_t dat_len = ...;
if (!doc) {
printf("read error: %s, code: %u at byte position: %lu\n",
}
size_t pos
Definition yyjson.h:971
const char * msg
Definition yyjson.h:969
The pos in the error information indicates the byte position where the error occurred. If you need the line and column number of the error, you can use the yyjson_locate_pos() function. Note that line and column start from 1, while character starts from 0. All values are calculated based on Unicode characters to ensure compatibility with various text editors.
Sample code:
char *dat = ...;
size_t dat_len = ...;
size_t line, col, chr;
printf("error at line: %lu, column: %lu, character index: %lu\n",
line, col, chr);
}
yyjson_api bool yyjson_locate_pos(const char *str, size_t len, size_t pos, size_t *line, size_t *col, size_t *chr)
The complete list of error codes (yyjson_read_code):
| Code | Name | Description |
| 0 | YYJSON_READ_SUCCESS | Success, no error. |
| 1 | YYJSON_READ_ERROR_INVALID_PARAMETER | Invalid parameter, such as NULL input string or 0 input length. |
| 2 | YYJSON_READ_ERROR_MEMORY_ALLOCATION | Memory allocation failure. |
| 3 | YYJSON_READ_ERROR_EMPTY_CONTENT | Input JSON string is empty. |
| 4 | YYJSON_READ_ERROR_UNEXPECTED_CONTENT | Unexpected content after document end, such as [123]abc. |
| 5 | YYJSON_READ_ERROR_UNEXPECTED_END | Unexpected end of input; the parsed part is valid, such as [123. |
| 6 | YYJSON_READ_ERROR_UNEXPECTED_CHARACTER | Unexpected character inside the document, such as [abc]. |
| 7 | YYJSON_READ_ERROR_JSON_STRUCTURE | Invalid JSON structure, such as [1,]. |
| 8 | YYJSON_READ_ERROR_INVALID_COMMENT | Invalid comment (deprecated, mapped to UNEXPECTED_END). |
| 9 | YYJSON_READ_ERROR_INVALID_NUMBER | Invalid number, such as 123.e12 or 000. |
| 10 | YYJSON_READ_ERROR_INVALID_STRING | Invalid string, such as an invalid escape sequence. |
| 11 | YYJSON_READ_ERROR_LITERAL | Invalid JSON literal, such as truu. |
| 12 | YYJSON_READ_ERROR_FILE_OPEN | Failed to open a file. |
| 13 | YYJSON_READ_ERROR_FILE_READ | Failed to read a file. |
| 14 | YYJSON_READ_ERROR_MORE | Incomplete input during incremental parsing; state is preserved for continuation. |
| 15 | YYJSON_READ_ERROR_DEPTH | Nesting depth exceeded YYJSON_READER_DEPTH_LIMIT. |
Reader flag
The library provides a set of flags for JSON reader.
You can use a single flag, or combine multiple flags with bitwise | operator.
Non-standard flags (such as YYJSON_READ_JSON5) have no performance impact when reading standard JSON input.
YYJSON_READ_NOFLAG = 0
This is the default flag for JSON reader (RFC-8259 or ECMA-404 compliant):
- Read positive integer as uint64_t.
- Read negative integer as int64_t.
- Read floating-point number as double with correct rounding.
- Read integer which cannot fit in uint64_t or int64_t as double.
- Report error if double number is infinity.
- Report error if string contains invalid UTF-8 character or BOM.
- Report error on trailing commas, comments, Inf and NaN literals.
YYJSON_READ_INSITU
Read the input data in-situ.
This option allows the reader to modify and use the input data to store string values, which can slightly improve reading speed. However, the caller must ensure that the input data is held until the document is freed. The input data must be padded with at least YYJSON_PADDING_SIZE bytes. For example: [1,2] should be [1,2]\0\0\0\0, input length should be 5.
Sample code:
size_t dat_len = ...;
read_from_socket(buf, ...);
if (doc) {...}
free(buf);
static const yyjson_read_flag YYJSON_READ_INSITU
Definition yyjson.h:824
#define YYJSON_PADDING_SIZE
Definition yyjson.h:655
YYJSON_READ_STOP_WHEN_DONE
Stop parsing when reaching the end of a JSON document instead of issuing an error if there's additional content after it.
This option is useful for parsing small pieces of JSON within larger data, such as NDJSON.
Sample code:
size_t file_size = ...;
your_read_file(dat, file);
char *hdr = dat;
char *end = dat + file_size;
while (true) {
if (!doc) break;
your_doc_process(doc);
}
free(dat);
yyjson_api_inline size_t yyjson_doc_get_read_size(const yyjson_doc *doc)
Definition yyjson.h:5339
static const yyjson_read_flag YYJSON_READ_STOP_WHEN_DONE
Definition yyjson.h:829
YYJSON_READ_ALLOW_TRAILING_COMMAS
Allow a single trailing comma at the end of an object or array (non-standard), for example:
{
"a": 1,
"b": 2,
}
[
"a",
"b",
]
YYJSON_READ_ALLOW_COMMENTS
Allow C-style single-line and multi-line comments (non-standard), for example:
{
"name": "Harry", // single-line comment
"id": /* multi-line comment */ 123
}
YYJSON_READ_ALLOW_INF_AND_NAN
Allow nan/inf number or case-insensitive literal (non-standard), for example:
{
"large": 123e999,
"nan1": NaN,
"nan2": nan,
"inf1": Inf,
"inf2": -Infinity
}
YYJSON_READ_NUMBER_AS_RAW
Read all numbers as raw strings without parsing.
This flag is useful if you want to handle number parsing yourself. You can use the following functions to extract raw strings:
yyjson_api_inline size_t yyjson_get_len(const yyjson_val *val)
Definition yyjson.h:5485
yyjson_api_inline const char * yyjson_get_raw(const yyjson_val *val)
Definition yyjson.h:5453
yyjson_api_inline bool yyjson_is_raw(const yyjson_val *val)
Definition yyjson.h:5362
YYJSON_READ_BIGNUM_AS_RAW
Read big numbers as raw strings.
This flag is useful if you want to parse these big numbers yourself. These big numbers include integers that cannot be represented by int64_t and uint64_t, and floating-point numbers that cannot be represented by finite double.
Note that this flag will be overridden by YYJSON_READ_NUMBER_AS_RAW flag.
YYJSON_READ_ALLOW_INVALID_UNICODE
Allow reading invalid unicode when parsing string values (non-standard), for example:
"\x80xyz"
"\xF0\x81\x81\x81"
This flag permits invalid characters to appear in the string values, but it still reports errors for invalid escape sequences. It does not impact the performance of correctly encoded strings.
Warning: when using this option, be aware that strings within JSON values may contain incorrect encoding, so you need to handle these strings carefully to avoid security risks.
YYJSON_READ_ALLOW_BOM
Allow UTF-8 BOM and skip it before parsing if any (non-standard).
YYJSON_READ_ALLOW_EXT_NUMBER
Allow extended number formats (non-standard):
- Hexadecimal numbers, such as 0x7B.
- Numbers with leading or trailing decimal point, such as .123, 123..
- Numbers with a leading plus sign, such as +123.
YYJSON_READ_ALLOW_EXT_ESCAPE
Allow extended escape sequences in strings (non-standard):
- Additional escapes: \a, \e, \v, \', \?, \0.
- Hex escapes: \xNN, such as \x7B.
- Line continuation: backslash followed by line terminator sequences.
- Unknown escape: if backslash is followed by an unsupported character, the backslash will be removed and the character will be kept as-is. However, \1-\9 will still trigger an error.
YYJSON_READ_ALLOW_EXT_WHITESPACE
Allow extended whitespace characters (non-standard):
- Vertical tab \v and form feed \f.
- Line separator \u2028 and paragraph separator \u2029.
- Non-breaking space \xA0.
- Byte order mark: \uFEFF.
- Other Unicode characters in the Zs (Separator, space) category.
YYJSON_READ_ALLOW_SINGLE_QUOTED_STR
Allow strings enclosed in single quotes (non-standard), such as 'ab'.
YYJSON_READ_ALLOW_UNQUOTED_KEY
Allow object keys without quotes (non-standard), such as {a:1,b:2}. This extends the ECMAScript IdentifierName rule by allowing any non-whitespace character with code point above U+007F.
YYJSON_READ_JSON5
Allow JSON5 format, see: https://json5.org.
This flag supports all JSON5 features with some additional extensions:
- Accepts more escape sequences than JSON5 (e.g. \a, \e).
- Unquoted keys are not limited to ECMAScript IdentifierName.
- Allow case-insensitive NaN, Inf and Infinity literals.
For example:
{
/* JSON5 example */
id: 123,
name: 'Harry',
color: 0x66CCFF,
min: .001,
max: Inf,
data: '\x00\xAA\xFF',
}
Writing JSON
The library provides 5 sets of functions for writing JSON.
Each function accepts an input of JSON document or root value, and returns a UTF-8 string or file.
Write JSON to string
The doc/val is the JSON document or root value. If it is NULL, returns NULL.
The flg is writer flag, pass 0 if you don't need it, see writer flag for details.
The len is a pointer to receive output length (not including the null-terminator), pass NULL if you don't need it.
This function returns a new JSON string, or NULL if an error occurs.
The string is encoded as UTF-8 with a null-terminator.
You should use free() or alc->free() to release it when it's no longer needed.
yyjson_api_inline char * yyjson_val_write(const yyjson_val *val, yyjson_write_flag flg, size_t *len)
Definition yyjson.h:1748
yyjson_api_inline char * yyjson_mut_val_write(const yyjson_mut_val *val, yyjson_write_flag flg, size_t *len)
Definition yyjson.h:1879
uint32_t yyjson_write_flag
Definition yyjson.h:1245
Sample code 1:
printf("%s\n", json);
free(json);
static const yyjson_write_flag YYJSON_WRITE_PRETTY
Definition yyjson.h:1255
Sample code 2:
printf("%s\n", json);
free(json);
yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, yyjson_mut_val *root)
Definition yyjson.h:5931
yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, yyjson_mut_val *arr, int64_t num)
Definition yyjson.h:6876
yyjson_api yyjson_mut_doc * yyjson_mut_doc_new(const yyjson_alc *alc)
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr(yyjson_mut_doc *doc)
Definition yyjson.h:6424
Write JSON to file
The path is the output JSON file path. This should be a null-terminated string using the system's native encoding. If path is NULL or invalid, returns false. If the file is not empty, its content is discarded.
The doc/val is the JSON document or root value. If it is NULL, returns false.
The flg is writer flag, pass 0 if you don't need it, see writer flag for details.
The alc is memory allocator, pass NULL if you don't need it, see memory allocator for details.
The err is a pointer to receive error message, pass NULL if you don't need it.
This function returns true on success, or false if an error occurs.
yyjson_api bool yyjson_write_file(const char *path, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
yyjson_api bool yyjson_val_write_file(const char *path, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
yyjson_api bool yyjson_mut_write_file(const char *path, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
yyjson_api bool yyjson_mut_val_write_file(const char *path, const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
Sample code:
Write JSON to file pointer
The fp is the output file pointer. The data will be written to the current position of the file.
If fp is NULL or invalid, returns false.
The doc/val is the JSON document or root value. If it is NULL, returns false.
The flg is writer flag, pass 0 if you don't need it, see writer flag for details.
The alc is memory allocator, pass NULL if you don't need it, see memory allocator for details.
The err is a pointer to receive error message, pass NULL if you don't need it.
This function returns true on success, or false if an error occurs.
yyjson_api bool yyjson_write_fp(FILE *fp, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
yyjson_api bool yyjson_val_write_fp(FILE *fp, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
yyjson_api bool yyjson_mut_write_fp(FILE *fp, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
yyjson_api bool yyjson_mut_val_write_fp(FILE *fp, const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err)
Sample code:
FILE *fp = fdopen(fd, "wb");
if (fp) fclose(fp);
if (suc) printf("OK");
Write JSON to buffer
The buf is the output buffer. If buf is NULL, returns 0.
The buf_len is the buffer length. If buf_len is too small, returns 0.
The doc/val is the JSON document or root value. If it is NULL, returns 0.
The flg is writer flag, pass 0 if you don't need it, see writer flag for details.
The err is a pointer to receive error message, pass NULL if you don't need it.
This function returns the number of bytes written (excluding the null terminator), or 0 on failure.
This function does not allocate memory, but the buffer must be larger than the final JSON size to allow temporary space.
The extra space is needed temporarily for each value while it is written, and is reused for later values:
- Number: 40
- String: 16 + (str_len * 6)
- Other values: 16
- Nesting depth: 16 * max_json_depth
yyjson_api size_t yyjson_write_buf(char *buf, size_t buf_len, const yyjson_doc *doc, yyjson_write_flag flg, yyjson_write_err *err)
yyjson_api size_t yyjson_mut_val_write_buf(char *buf, size_t buf_len, const yyjson_mut_val *val, yyjson_write_flag flg, yyjson_write_err *err)
yyjson_api size_t yyjson_val_write_buf(char *buf, size_t buf_len, const yyjson_val *val, yyjson_write_flag flg, yyjson_write_err *err)
yyjson_api size_t yyjson_mut_write_buf(char *buf, size_t buf_len, const yyjson_mut_doc *doc, yyjson_write_flag flg, yyjson_write_err *err)
Sample code:
char buf[512];
if (len > 0) printf("OK, output:\n%s\n", buf);
Write JSON with options
The doc/val is the JSON document or root value. If it is NULL, returns NULL.
The flg is writer flag, pass 0 if you don't need it, see writer flag for details.
The alc is memory allocator, pass NULL if you don't need it, see memory allocator for details.
The len is a pointer to receive output length (not including the null-terminator), pass NULL if you don't need it.
The err is a pointer to receive error message, pass NULL if you don't need it.
This function returns a new JSON string, or NULL if an error occurs.
The string is encoded as UTF-8 with a null-terminator.
You should use free() or alc->free() to release it when it's no longer needed.
yyjson_api char * yyjson_write_opts(const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err)
yyjson_api char * yyjson_val_write_opts(const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err)
yyjson_api char * yyjson_mut_write_opts(const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err)
yyjson_api char * yyjson_mut_val_write_opts(const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err)
Sample code:
char buf[64 * 1024];
size_t len;
if (json) {
printf("suc: %lu\n%s\n", len, json);
} else {
printf(
"err: %u msg:%s\n", err.
code, err.
msg);
}
void(* free)(void *ctx, void *ptr)
Definition yyjson.h:675
void * ctx
Definition yyjson.h:677
const char * msg
Definition yyjson.h:1348
yyjson_api bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, size_t size)
static const yyjson_write_flag YYJSON_WRITE_ESCAPE_UNICODE
Definition yyjson.h:1258
yyjson_write_code code
Definition yyjson.h:1346
The complete list of error codes (yyjson_write_code):
Writer flag
The library provides a set of flags for JSON writer.
You can use a single flag, or combine multiple flags with bitwise | operator.
YYJSON_WRITE_NOFLAG = 0
This is the default flag for JSON writer:
- Writes JSON in minified format.
- Reports an error on encountering inf or nan number.
- Reports an error on encountering invalid UTF-8 strings.
- Does not escape unicode or slashes.
YYJSON_WRITE_PRETTY
Writes JSON with a pretty format using a 4-space indent.
YYJSON_WRITE_PRETTY_TWO_SPACES
Writes JSON with a pretty format using a 2-space indent. This flag will override YYJSON_WRITE_PRETTY flag.
YYJSON_WRITE_ESCAPE_UNICODE
Escape unicode as \uXXXX, making the output ASCII-only, for example:
["Alizée, 😊"]
["Aliz\\u00E9e, \\uD83D\\uDE0A"]
YYJSON_WRITE_LOWERCASE_HEX
Use lowercase hex digits in \uXXXX escape sequences instead of the default uppercase. Only effective when YYJSON_WRITE_ESCAPE_UNICODE is also set.
YYJSON_WRITE_ESCAPE_SLASHES
Escapes the forward slash character / as \/, for example:
["https://github.com"]
["https:\/\/github.com"]
YYJSON_WRITE_ALLOW_INF_AND_NAN
Writes inf/nan numbers as Infinity and NaN literals instead of reporting errors.
Note that this output is NOT standard JSON and may be rejected by other JSON libraries, for example:
{"not_a_number":NaN,"large_number":Infinity}
YYJSON_WRITE_INF_AND_NAN_AS_NULL
Writes inf/nan numbers as null literals instead of reporting errors.
This flag will override YYJSON_WRITE_ALLOW_INF_AND_NAN flag, for example:
{"not_a_number":null,"large_number":null}
YYJSON_WRITE_ALLOW_INVALID_UNICODE
Allows invalid unicode when encoding string values.
Invalid characters within string values will be copied byte by byte. If YYJSON_WRITE_ESCAPE_UNICODE flag is also set, invalid characters will be escaped as \uFFFD (replacement character).
This flag does not affect the performance of correctly encoded strings.
YYJSON_WRITE_NEWLINE_AT_END
Adds a newline character \n at the end of the JSON. This can be helpful for text editors or NDJSON.
YYJSON_WRITE_FP_TO_FLOAT
Write floating-point numbers using single-precision (float). This casts double to float before serialization. This will produce shorter output, but may lose some precision. This flag is ignored if YYJSON_WRITE_FP_TO_FIXED(prec) is also used.
YYJSON_WRITE_FP_TO_FIXED(prec)
Write floating-point number using fixed-point notation. This is similar to ECMAScript Number.prototype.toFixed(prec), but with trailing zeros removed. The prec ranges from 1 to 15. This will produce shorter output but may lose some precision.
Accessing JSON Document
JSON Document
You can access the content of a document with the following functions:
yyjson_api_inline yyjson_val * yyjson_doc_get_root(const yyjson_doc *doc)
Definition yyjson.h:5335
yyjson_api_inline size_t yyjson_doc_get_val_count(const yyjson_doc *doc)
Definition yyjson.h:5343
A document holds all the memory for its internal values and strings. When you no longer need it, you should release the document and free up all the memory:
JSON Value
Each JSON Value has a type and subtype, as specified in the table:
| Type | Subtype | |
| YYJSON_TYPE_NONE | | Invalid value |
| YYJSON_TYPE_RAW | | Raw string |
| YYJSON_TYPE_NULL | | null literal |
| YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE | false literal |
| YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE | true literal |
| YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT | uint64_t number |
| YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT | int64_t number |
| YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL | double number |
| YYJSON_TYPE_STR | | String value |
| YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC | String value, no-escape |
| YYJSON_TYPE_ARR | | Array value |
| YYJSON_TYPE_OBJ | | Object value |
- YYJSON_TYPE_NONE means invalid value, it does not appear when the JSON is successfully parsed.
- YYJSON_TYPE_RAW only appears when the corresponding flag YYJSON_READ_XXX_AS_RAW is used.
- YYJSON_SUBTYPE_NOESC is used to optimize the writing speed of strings that do not need to be escaped. This subtype is used internally, and the user does not need to handle it.
The following functions can be used to determine the type of JSON value.
yyjson_api_inline bool yyjson_is_ctn(const yyjson_val *val)
Definition yyjson.h:5414
uint8_t yyjson_subtype
Definition yyjson.h:621
yyjson_api_inline bool yyjson_is_obj(const yyjson_val *val)
Definition yyjson.h:5410
yyjson_api_inline const char * yyjson_get_type_desc(const yyjson_val *val)
Definition yyjson.h:5436
yyjson_api_inline bool yyjson_is_sint(const yyjson_val *val)
Definition yyjson.h:5386
uint8_t yyjson_type
Definition yyjson.h:602
yyjson_api_inline bool yyjson_is_true(const yyjson_val *val)
Definition yyjson.h:5370
yyjson_api_inline bool yyjson_is_arr(const yyjson_val *val)
Definition yyjson.h:5406
yyjson_api_inline bool yyjson_is_false(const yyjson_val *val)
Definition yyjson.h:5374
yyjson_api_inline yyjson_type yyjson_get_type(const yyjson_val *val)
Definition yyjson.h:5424
yyjson_api_inline bool yyjson_is_int(const yyjson_val *val)
Definition yyjson.h:5390
yyjson_api_inline bool yyjson_is_real(const yyjson_val *val)
Definition yyjson.h:5394
yyjson_api_inline bool yyjson_is_uint(const yyjson_val *val)
Definition yyjson.h:5382
yyjson_api_inline bool yyjson_is_num(const yyjson_val *val)
Definition yyjson.h:5398
yyjson_api_inline bool yyjson_is_null(const yyjson_val *val)
Definition yyjson.h:5366
yyjson_api_inline uint8_t yyjson_get_tag(const yyjson_val *val)
Definition yyjson.h:5432
yyjson_api_inline yyjson_subtype yyjson_get_subtype(const yyjson_val *val)
Definition yyjson.h:5428
yyjson_api_inline bool yyjson_is_bool(const yyjson_val *val)
Definition yyjson.h:5378
The following functions can be used to get the contents of the JSON value.
yyjson_api_inline int yyjson_get_int(const yyjson_val *val)
Definition yyjson.h:5469
yyjson_api_inline uint64_t yyjson_get_uint(const yyjson_val *val)
Definition yyjson.h:5461
yyjson_api_inline int64_t yyjson_get_sint(const yyjson_val *val)
Definition yyjson.h:5465
yyjson_api_inline bool yyjson_equals(const yyjson_val *lhs, const yyjson_val *rhs)
Definition yyjson.h:5510
yyjson_api_inline bool yyjson_mut_equals(const yyjson_mut_val *lhs, const yyjson_mut_val *rhs)
Definition yyjson.h:6071
yyjson_api_inline const char * yyjson_get_str(const yyjson_val *val)
Definition yyjson.h:5481
yyjson_api_inline double yyjson_get_real(const yyjson_val *val)
Definition yyjson.h:5473
yyjson_api_inline double yyjson_get_num(const yyjson_val *val)
Definition yyjson.h:5477
yyjson_api_inline bool yyjson_get_bool(const yyjson_val *val)
Definition yyjson.h:5457
The following functions can be used to modify the content of a JSON value.
Warning: For immutable documents, these functions will break the immutable convention, you should use this set of APIs with caution (e.g. make sure the document is only accessed in a single thread).
yyjson_api_inline bool yyjson_set_null(yyjson_val *val)
Definition yyjson.h:5524
yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, const char *raw, size_t len)
Definition yyjson.h:5516
yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num)
Definition yyjson.h:5536
yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str)
Definition yyjson.h:5584
yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, const char *str, size_t len)
Definition yyjson.h:5591
yyjson_api_inline bool yyjson_set_float(yyjson_val *val, float num)
Definition yyjson.h:5554
yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int64_t num)
Definition yyjson.h:5548
yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num)
Definition yyjson.h:5566
yyjson_api_inline bool yyjson_set_double(yyjson_val *val, double num)
Definition yyjson.h:5560
yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num)
Definition yyjson.h:5542
yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num)
Definition yyjson.h:5530
JSON Array
The following functions can be used to access a JSON array.
Note that accessing elements by index may take a linear search time. Therefore, if you need to iterate through an array, it is recommended to use the iterator API.
yyjson_api_inline yyjson_val * yyjson_arr_get_first(const yyjson_val *arr)
Definition yyjson.h:5631
yyjson_api_inline yyjson_val * yyjson_arr_get(const yyjson_val *arr, size_t idx)
Definition yyjson.h:5615
yyjson_api_inline size_t yyjson_arr_size(const yyjson_val *arr)
Definition yyjson.h:5611
yyjson_api_inline yyjson_val * yyjson_arr_get_last(const yyjson_val *arr)
Definition yyjson.h:5640
JSON Array Iterator
There are two ways to traverse an array:
Sample code 1 (iterator API):
your_func(val);
}
yyjson_api_inline yyjson_val * yyjson_arr_iter_next(yyjson_arr_iter *iter)
Definition yyjson.h:5684
yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(const yyjson_val *arr)
Definition yyjson.h:5674
Sample code 2 (foreach macro):
size_t idx, max;
your_func(idx, val);
}
#define yyjson_arr_foreach(arr, idx, max, val)
Definition yyjson.h:2258
There's also a mutable version of the API to traverse a mutable array:
Sample code 1 (mutable iterator API):
if (your_val_is_unused(val)) {
}
}
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_iter_remove(yyjson_mut_arr_iter *iter)
Definition yyjson.h:6399
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_iter_next(yyjson_mut_arr_iter *iter)
Definition yyjson.h:6387
yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with(yyjson_mut_val *arr)
Definition yyjson.h:6376
Sample code 2 (mutable foreach macro):
size_t idx, max;
your_func(idx, val);
}
#define yyjson_mut_arr_foreach(arr, idx, max, val)
Definition yyjson.h:2999
JSON Object
The following functions can be used to access a JSON object.
Note that accessing elements by key may take a linear search time. Therefore, if you need to iterate through an object, it is recommended to use the iterator API.
yyjson_api_inline yyjson_val * yyjson_obj_iter_get(yyjson_obj_iter *iter, const char *key)
Definition yyjson.h:5767
yyjson_api_inline size_t yyjson_obj_size(const yyjson_val *obj)
Definition yyjson.h:5701
yyjson_api_inline yyjson_val * yyjson_obj_get(const yyjson_val *obj, const char *key)
Definition yyjson.h:5705
yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(const yyjson_val *obj)
Definition yyjson.h:5743
yyjson_api_inline yyjson_val * yyjson_obj_getn(const yyjson_val *obj, const char *key, size_t key_len)
Definition yyjson.h:5710
JSON Object Iterator
There are two ways to traverse an object:
Sample code 1 (iterator API):
your_func(key, val);
}
yyjson_api_inline yyjson_val * yyjson_obj_iter_get_val(yyjson_val *key)
Definition yyjson.h:5763
yyjson_api_inline yyjson_val * yyjson_obj_iter_next(yyjson_obj_iter *iter)
Definition yyjson.h:5753
Sample code 2 (foreach macro):
size_t idx, max;
your_func(key, val);
}
#define yyjson_obj_foreach(obj, idx, max, key, val)
Definition yyjson.h:2430
There's also a mutable version of the API to traverse a mutable object:
Sample code 1 (mutable iterator API):
if (your_key_is_unused(key)) {
}
}
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_iter_next(yyjson_mut_obj_iter *iter)
Definition yyjson.h:7034
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_iter_remove(yyjson_mut_obj_iter *iter)
Definition yyjson.h:7051
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_iter_get_val(yyjson_mut_val *key)
Definition yyjson.h:7046
yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with(yyjson_mut_val *obj)
Definition yyjson.h:7023
Sample code 2 (mutable foreach macro):
size_t idx, max;
your_func(key, val);
}
#define yyjson_mut_obj_foreach(obj, idx, max, key, val)
Definition yyjson.h:3871
Creating JSON Document
The yyjson_mut_doc and related APIs are used to build JSON documents.
Please note that yyjson_mut_doc uses a memory pool to hold all strings and values. The pool can only be created, grown, or freed in its entirety. Therefore, yyjson_mut_doc is more suitable for write-once than mutation of an existing document.
JSON objects and arrays are composed of linked lists, so each yyjson_mut_val can only be added to one object or array.
Sample code:
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj(yyjson_mut_doc *doc)
Definition yyjson.h:7102
yyjson_api void yyjson_mut_doc_free(yyjson_mut_doc *doc)
yyjson_api_inline yyjson_mut_val * yyjson_mut_int(yyjson_mut_doc *doc, int64_t num)
Definition yyjson.h:6263
yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val)
Definition yyjson.h:7264
yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, yyjson_mut_val *val)
Definition yyjson.h:6635
Mutable Document
The following functions are used to create, modify, copy, and destroy a JSON document.
yyjson_api yyjson_mut_val * yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, const yyjson_mut_val *val)
yyjson_api yyjson_mut_doc * yyjson_mut_doc_mut_copy(const yyjson_mut_doc *doc, const yyjson_alc *alc)
yyjson_api bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, size_t count)
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_get_root(yyjson_mut_doc *doc)
Definition yyjson.h:5927
yyjson_api bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, size_t len)
JSON Value Creation
The following functions are used to create mutable JSON value, the value's memory is held by the document.
yyjson_api_inline yyjson_mut_val * yyjson_mut_true(yyjson_mut_doc *doc)
Definition yyjson.h:6240
yyjson_api_inline yyjson_mut_val * yyjson_mut_real(yyjson_mut_doc *doc, double num)
Definition yyjson.h:6278
yyjson_api_inline yyjson_mut_val * yyjson_mut_false(yyjson_mut_doc *doc)
Definition yyjson.h:6244
yyjson_api_inline yyjson_mut_val * yyjson_mut_float(yyjson_mut_doc *doc, float num)
Definition yyjson.h:6268
yyjson_api_inline yyjson_mut_val * yyjson_mut_bool(yyjson_mut_doc *doc, bool val)
Definition yyjson.h:6248
yyjson_api_inline yyjson_mut_val * yyjson_mut_null(yyjson_mut_doc *doc)
Definition yyjson.h:6236
yyjson_api_inline yyjson_mut_val * yyjson_mut_uint(yyjson_mut_doc *doc, uint64_t num)
Definition yyjson.h:6253
yyjson_api_inline yyjson_mut_val * yyjson_mut_double(yyjson_mut_doc *doc, double num)
Definition yyjson.h:6273
yyjson_api_inline yyjson_mut_val * yyjson_mut_sint(yyjson_mut_doc *doc, int64_t num)
Definition yyjson.h:6258
JSON Array Creation
The following functions are used to create mutable JSON array.
int32_t vals[3] = {-1, 0, 1};
const char *strs[3] = {"Jan", "Feb", "Mar"};
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_sint(yyjson_mut_doc *doc, const int64_t *vals, size_t count)
Definition yyjson.h:6463
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_uint8(yyjson_mut_doc *doc, const uint8_t *vals, size_t count)
Definition yyjson.h:6508
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_sint64(yyjson_mut_doc *doc, const int64_t *vals, size_t count)
Definition yyjson.h:6501
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_strn(yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count)
Definition yyjson.h:6558
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_float(yyjson_mut_doc *doc, const float *vals, size_t count)
Definition yyjson.h:6536
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_sint32(yyjson_mut_doc *doc, const int32_t *vals, size_t count)
Definition yyjson.h:6494
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_real(yyjson_mut_doc *doc, const double *vals, size_t count)
Definition yyjson.h:6473
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_sint16(yyjson_mut_doc *doc, const int16_t *vals, size_t count)
Definition yyjson.h:6487
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_uint16(yyjson_mut_doc *doc, const uint16_t *vals, size_t count)
Definition yyjson.h:6515
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_strcpy(yyjson_mut_doc *doc, const char **vals, size_t count)
Definition yyjson.h:6567
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_uint64(yyjson_mut_doc *doc, const uint64_t *vals, size_t count)
Definition yyjson.h:6529
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_uint(yyjson_mut_doc *doc, const uint64_t *vals, size_t count)
Definition yyjson.h:6468
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_sint8(yyjson_mut_doc *doc, const int8_t *vals, size_t count)
Definition yyjson.h:6480
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_uint32(yyjson_mut_doc *doc, const uint32_t *vals, size_t count)
Definition yyjson.h:6522
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_double(yyjson_mut_doc *doc, const double *vals, size_t count)
Definition yyjson.h:6543
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_strncpy(yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count)
Definition yyjson.h:6581
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_str(yyjson_mut_doc *doc, const char **vals, size_t count)
Definition yyjson.h:6550
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_with_bool(yyjson_mut_doc *doc, const bool *vals, size_t count)
Definition yyjson.h:6456
JSON Array Modification
The following functions are used to modify the contents of a JSON array.
yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str)
Definition yyjson.h:6916
yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, yyjson_mut_val *arr)
Definition yyjson.h:6828
yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, yyjson_mut_val *val)
Definition yyjson.h:6654
yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str, size_t len)
Definition yyjson.h:6946
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_remove(yyjson_mut_val *arr, size_t idx)
Definition yyjson.h:6701
yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr)
Definition yyjson.h:6788
yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str)
Definition yyjson.h:6936
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_replace(yyjson_mut_val *arr, size_t idx, yyjson_mut_val *val)
Definition yyjson.h:6673
yyjson_api_inline bool yyjson_mut_arr_add_double(yyjson_mut_doc *doc, yyjson_mut_val *arr, double num)
Definition yyjson.h:6896
yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, yyjson_mut_val *arr, bool val)
Definition yyjson.h:6846
yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, yyjson_mut_val *arr, uint64_t num)
Definition yyjson.h:6856
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, yyjson_mut_val *arr)
Definition yyjson.h:6956
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_remove_last(yyjson_mut_val *arr)
Definition yyjson.h:6744
yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, yyjson_mut_val *arr)
Definition yyjson.h:6837
yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str, size_t len)
Definition yyjson.h:6926
yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, yyjson_mut_val *arr, double num)
Definition yyjson.h:6906
yyjson_api_inline bool yyjson_mut_arr_add_float(yyjson_mut_doc *doc, yyjson_mut_val *arr, float num)
Definition yyjson.h:6886
yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, yyjson_mut_val *val)
Definition yyjson.h:6814
yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, yyjson_mut_val *arr, int64_t num)
Definition yyjson.h:6866
yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, size_t idx, size_t len)
Definition yyjson.h:6766
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, yyjson_mut_val *arr)
Definition yyjson.h:6965
yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, yyjson_mut_val *val, size_t idx)
Definition yyjson.h:6604
yyjson_api_inline yyjson_mut_val * yyjson_mut_arr_remove_first(yyjson_mut_val *arr)
Definition yyjson.h:6725
yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, yyjson_mut_val *arr)
Definition yyjson.h:6819
JSON Object Creation
The following functions are used to create mutable JSON object.
const char **keys,
const char **vals,
size_t count);
const char *keys[] = {"name", "type", "id"};
const char *vals[] = {"Harry", "student", "123456"};
const char **kv_pairs,
size_t pair_count);
const char *pairs[] = {"name", "Harry", "type", "student", "id", "123456"};
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_with_str(yyjson_mut_doc *doc, const char **keys, const char **vals, size_t count)
Definition yyjson.h:7113
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, const char **kv_pairs, size_t pair_count)
Definition yyjson.h:7148
JSON Object Modification
The following functions are used to modify the contents of a JSON object.
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key)
Definition yyjson.h:7521
yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val, size_t len)
Definition yyjson.h:7508
yyjson_api_inline bool yyjson_mut_obj_add_double(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, double val)
Definition yyjson.h:7455
yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, size_t len, const char *new_key, size_t new_len)
Definition yyjson.h:7579
yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, int64_t val)
Definition yyjson.h:7434
yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val, size_t len)
Definition yyjson.h:7483
yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key)
Definition yyjson.h:7414
yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, int64_t val)
Definition yyjson.h:7441
yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, uint64_t val)
Definition yyjson.h:7427
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_remove_str(yyjson_mut_val *obj, const char *key)
Definition yyjson.h:7547
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_remove(yyjson_mut_val *obj, yyjson_mut_val *key)
Definition yyjson.h:7323
#define yyjson_api_inline
Definition yyjson.h:357
yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key)
Definition yyjson.h:7402
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key)
Definition yyjson.h:7529
yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key)
Definition yyjson.h:7408
yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val)
Definition yyjson.h:7469
yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, double val)
Definition yyjson.h:7462
yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, bool val)
Definition yyjson.h:7420
yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val)
Definition yyjson.h:7495
yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val)
Definition yyjson.h:7275
yyjson_api_inline yyjson_mut_val * yyjson_mut_obj_remove_strn(yyjson_mut_val *obj, const char *key, size_t len)
Definition yyjson.h:7552
yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *new_key)
Definition yyjson.h:7570
yyjson_api_inline bool yyjson_mut_obj_add_float(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, float val)
Definition yyjson.h:7448
yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj)
Definition yyjson.h:7349
JSON Pointer and Patch
JSON Pointer
The library supports querying JSON values using JSON Pointer (RFC 6901).
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_get(const yyjson_mut_val *val, const char *ptr)
Definition yyjson.h:7748
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_getx(const yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:7724
yyjson_api_inline yyjson_val * yyjson_doc_ptr_getn(const yyjson_doc *doc, const char *ptr, size_t len)
Definition yyjson.h:7657
yyjson_api_inline yyjson_val * yyjson_doc_ptr_get(const yyjson_doc *doc, const char *ptr)
Definition yyjson.h:7651
yyjson_api_inline yyjson_val * yyjson_ptr_getx(const yyjson_val *val, const char *ptr, size_t len, yyjson_ptr_err *err)
Definition yyjson.h:7695
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_getn(const yyjson_mut_val *val, const char *ptr, size_t len)
Definition yyjson.h:7754
yyjson_api_inline yyjson_val * yyjson_ptr_get(const yyjson_val *val, const char *ptr)
Definition yyjson.h:7684
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_getn(const yyjson_mut_doc *doc, const char *ptr, size_t len)
Definition yyjson.h:7719
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_get(const yyjson_mut_doc *doc, const char *ptr)
Definition yyjson.h:7713
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_getx(const yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:7760
yyjson_api_inline yyjson_val * yyjson_doc_ptr_getx(const yyjson_doc *doc, const char *ptr, size_t len, yyjson_ptr_err *err)
Definition yyjson.h:7662
yyjson_api_inline yyjson_val * yyjson_ptr_getn(const yyjson_val *val, const char *ptr, size_t len)
Definition yyjson.h:7690
For example, given the JSON document:
{
"size" : 3,
"users" : [
{"id": 1, "name": "Harry"},
{"id": 2, "name": "Ron"},
{"id": 3, "name": "Hermione"}
]
}
The following JSON strings evaluate to the accompanying values:
| Pointer | Matched Value |
| "" | the whole document |
| "/size" | 3 |
| "/users/0" | {"id": 1, "name": "Harry"} |
| "/users/1/name" | "Ron" |
| "/no_match" | NULL |
| "no_slash" | NULL |
| "/" | NULL (match to empty key: root[""]) |
if (!val2) printf(
"err %d: %s\n", err.
code, err.
msg);
const char * msg
Definition yyjson.h:4314
yyjson_ptr_code code
Definition yyjson.h:4312
The library also supports modifying JSON values using JSON Pointer.
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_remove(yyjson_mut_doc *doc, const char *ptr)
Definition yyjson.h:8068
yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:7962
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_replace(yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val)
Definition yyjson.h:7991
yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val)
Definition yyjson.h:7891
yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:7858
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_removex(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:8079
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_removen(yyjson_mut_val *val, const char *ptr, size_t len)
Definition yyjson.h:8113
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_replacex(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:8046
yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:7897
yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val, yyjson_mut_doc *doc)
Definition yyjson.h:7947
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_removen(yyjson_mut_doc *doc, const char *ptr, size_t len)
Definition yyjson.h:8074
yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val)
Definition yyjson.h:7789
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_removex(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:8119
yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val)
Definition yyjson.h:7884
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_replacen(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val)
Definition yyjson.h:7997
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_remove(yyjson_mut_val *val, const char *ptr)
Definition yyjson.h:8107
yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc)
Definition yyjson.h:7851
yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val, yyjson_mut_doc *doc)
Definition yyjson.h:7843
yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc)
Definition yyjson.h:7955
yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:7796
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_replace(yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val)
Definition yyjson.h:8035
yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val)
Definition yyjson.h:7782
yyjson_api_inline yyjson_mut_val * yyjson_mut_doc_ptr_replacex(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err)
Definition yyjson.h:8002
yyjson_api_inline yyjson_mut_val * yyjson_mut_ptr_replacen(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val)
Definition yyjson.h:8041
For example:
All the above functions ending with x can be used to get the result context ctx, and the error message err. For example:
if (err.
code) printf(
"err: %s\n", err.
msg);
yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx)
Definition yyjson.h:8228
yyjson_api_inline bool yyjson_mut_is_null(const yyjson_mut_val *val)
Definition yyjson.h:5946
JSON Patch
The library supports JSON Patch (RFC 6902). Specification and example: https://tools.ietf.org/html/rfc6902
yyjson_api yyjson_mut_val * yyjson_mut_patch(yyjson_mut_doc *doc, const yyjson_mut_val *orig, const yyjson_mut_val *patch, yyjson_patch_err *err)
yyjson_api yyjson_mut_val * yyjson_patch(yyjson_mut_doc *doc, const yyjson_val *orig, const yyjson_val *patch, yyjson_patch_err *err)
JSON Merge Patch
The library supports JSON Merge Patch (RFC 7386). Specification and example: https://tools.ietf.org/html/rfc7386
yyjson_api yyjson_mut_val * yyjson_mut_merge_patch(yyjson_mut_doc *doc, const yyjson_mut_val *orig, const yyjson_mut_val *patch)
yyjson_api yyjson_mut_val * yyjson_merge_patch(yyjson_mut_doc *doc, const yyjson_val *orig, const yyjson_val *patch)
Number Processing
Number reader
The library has a built-in high-performance number reader,
it will read numbers according to these rules by default:
- Positive integers are read as uint64_t. If an overflow occurs, it is converted to double.
- Negative integers are read as int64_t. If an overflow occurs, it is converted to double.
- Floating-point numbers are read as double with correct rounding.
- If a double number overflows (reaches infinity), an error is reported.
- If a number does not conform to the JSON standard, an error is reported.
There are 3 flags that can be used to adjust the number parsing strategy:
See the Reader flag section for more details.
Number writer
The library has a built-in high-performance number writer,
it will write numbers according to these rules by default:
- Positive integers are written without a sign.
- Negative integers are written with a negative sign.
- Floating-point numbers are written using the ECMAScript format, with the following modifications:
- If the number is Infinity or NaN, an error is reported.
- The negative sign of -0.0 is preserved to maintain input information.
- The positive sign in the exponent part is removed.
- The floating-point number writer will generate the shortest correctly rounded decimal representation.
There are several flags that can be used to adjust the number writing strategy:
See the Writer flag section for more details.
There are also some helper functions to control the output format of individual values:
Number conversion function
There are also two utility functions that provide direct access to the library's internal number conversion logic.
They are intended for standalone use and typically do not allocate memory.
yyjson_api char * yyjson_write_number(const yyjson_val *val, char *buf)
yyjson_api const char * yyjson_read_number(const char *dat, yyjson_val *val, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err)
Text Processing
Character Encoding
By default, this library supports UTF-8 encoding without a BOM, as specified in RFC 8259:
JSON text exchanged between systems that are not part of a closed ecosystem MUST be encoded using UTF-8. Implementations MUST NOT add a byte order mark (U+FEFF) to the beginning of a networked-transmitted JSON text.
This library performs strict UTF-8 encoding validation on input strings by default. If an invalid character is encountered, an error will be reported.
To allow a BOM, use the YYJSON_READ_ALLOW_BOM or YYJSON_READ_ALLOW_EXT_WHITESPACE flags.
To allow invalid Unicode encoding, use the YYJSON_READ_ALLOW_INVALID_UNICODE and YYJSON_WRITE_ALLOW_INVALID_UNICODE flags. Note: Enabling these flags may result in yyjson producing values that contain invalid characters, which could be processed by other code and potentially introduce security risks.
To mark a string as not requiring escaping during JSON writing, use yyjson_set_str_noesc(yyjson_val *val, bool noesc) or yyjson_mut_set_str_noesc(yyjson_mut_val *val, bool noesc). This can improve string-writing performance and preserve the original string bytes.
NUL Character
This library supports the NUL character (also known as the null terminator, or Unicode U+0000, ASCII \0) inside strings.
When reading JSON, \u0000 will be unescaped to NUL character. If a string contains the NUL character, the length obtained with strlen() will be inaccurate, and you should use yyjson_get_len() to get the actual length.
When building JSON, the input string is treated as null-terminated by default. If you need to pass in a string that contains the NUL character, you should use the API with the n suffix and provide the actual length of the string.
For example:
Memory Allocator
The library does not directly call libc's memory allocation functions (malloc/realloc/free). Instead, when memory allocation is required, yyjson's API takes a parameter named alc that allows the caller to pass in an allocator. If the alc is NULL, yyjson will use the default memory allocator, which is a simple wrapper of libc's functions.
Using a custom memory allocator allows you to have more control over memory allocation, here are a few examples:
Single allocator for multiple JSON
If you need to parse multiple small JSON one by one, you can use a single allocator to avoid multiple memory allocations.
Sample code:
size_t max_json_size = 64 * 1024;
void *buf = malloc(buf_size);
for (int i = 0; i < your_json_file_count; i++) {
const char *your_json_file_path = ...;
...
}
free(buf);
yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len, yyjson_read_flag flg)
Definition yyjson.h:1180
If you are not sure about the amount of memory required to process JSON, you can use the dynamic allocator.
for (int i = 0; i < your_json_file_count; i++) {
const char *your_json_file_path = ...;
...
}
yyjson_api yyjson_alc * yyjson_alc_dyn_new(void)
yyjson_api void yyjson_alc_dyn_free(yyjson_alc *alc)
Stack memory allocator
If the JSON is small enough, you can use stack memory to read or write it.
Sample code:
char buf[128 * 1024];
...
yyjson_doc_free(doc);
Use a third-party allocator library
You can use a third-party high-performance memory allocator for yyjson, such as jemalloc, tcmalloc, mimalloc. You can also refer to the following code to implement your own allocator.
Sample code:
#include <mimalloc.h>
static void *priv_malloc(void *ctx, size_t size) {
return mi_malloc(size);
}
static void *priv_realloc(void *ctx, void *ptr, size_t old_size, size_t size) {
return mi_realloc(ptr, size);
}
static void priv_free(void *ctx, void *ptr) {
mi_free(ptr);
}
priv_malloc,
priv_realloc,
priv_free,
NULL
};
...
yyjson_doc_free(doc);
...
alc->free(alc->
ctx, json);
Stack Memory Usage
Most functions in the library use fixed-size stack memory. This includes functions for JSON reading and writing, as well as JSON Pointer handling.
However, a few functions use recursion and may cause a stack overflow if the nesting level is too deep. These functions are marked with the following warning in the header file:
- Warning
- This function is recursive and may cause a stack overflow if the object level is too deep.
Null Check
The library's public APIs perform a null check for every input parameter to prevent crashes.
For example, when reading a JSON, you don't need to perform null checks or type checks on each value:
if (!str) printf("err!");
However, if you are certain that a value is non-null and matches the expected type, you can use the unsafe prefix API to avoid the null check.
For example, when iterating over an array or object, the value and key must be non-null:
size_t idx, max;
if (unsafe_yyjson_equals_str(key, "id") &&
unsafe_yyjson_is_uint(val) &&
unsafe_yyjson_get_uint(val) == 1234) {
...
}
}
Thread Safety
The library does not use global variables. Therefore, if you can ensure that the input parameters of a function are thread-safe, then the function calls are also thread-safe.
In general, yyjson_doc and yyjson_val are immutable and thread-safe, while yyjson_mut_doc and yyjson_mut_val are mutable and not thread-safe.
Locale Independence
The library is designed to be locale-independent.
However, there are certain conditions that you should be aware of:
- You use libc's setlocale() function to change the locale.
- Your environment does not adhere to the IEEE 754 floating-point standard (e.g. some IBM mainframes), or you explicitly set YYJSON_DISABLE_FAST_FP_CONV during build, in which case yyjson will use strtod() to parse floating-point numbers.
If both of these conditions are met, it is recommended to avoid calling setlocale() while another thread is parsing JSON. Otherwise, an error may be returned during JSON floating-point number parsing.