Skip to content

Commit 0eada75

Browse files
committed
Added a set of write_buf() functions for writing JSON to a buffer without allocation
1 parent b142f53 commit 0eada75

File tree

5 files changed

+425
-108
lines changed

5 files changed

+425
-108
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33

44

55

6+
## Unreleased
7+
### Added a set of `write_buf()` functions for writing JSON to a buffer without allocation.
8+
9+
610
## 0.12.0 (2025-08-18)
711
#### Added
812
- Add `yyjson_write_number()` and `yyjson_mut_write_number()` to write a number value to a string buffer.

doc/API.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ For example:
492492

493493
---------------
494494
# Writing JSON
495-
The library provides 4 sets of functions for writing JSON.<br/>
495+
The library provides 5 sets of functions for writing JSON.<br/>
496496
Each function accepts an input of JSON document or root value, and returns a UTF-8 string or file.
497497

498498
## Write JSON to string
@@ -593,6 +593,42 @@ if (fp) fclose(fp);
593593
if (suc) printf("OK");
594594
```
595595

596+
## Write JSON to buffer
597+
The `buf` is output buffer, if you pass NULL, the function will return 0.<br/>
598+
The `buf_len` is the length of output buffer. If the length is too small, the function will fail and return 0.<br/>
599+
The `doc/val` is JSON document or root value, if you pass NULL, you will get an error.<br/>
600+
The `flg` is writer flag, pass 0 if you don't need it, see `writer flag` for details.<br/>
601+
The `err` is a pointer to receive error message, pass NULL if you don't need it.<br/>
602+
This function returns the number of bytes written (excluding the null terminator), or 0 on failure.<br/>
603+
604+
This function does not allocate memory, but the buffer must be larger than the final JSON size to allow temporary space.
605+
606+
The extra space is needed temporarily for each value while it is written, and is reused for later values:
607+
- Number: `40`
608+
- String: `16 + (str_len * 6)`
609+
- Other values: `16`
610+
- Nesting depth: `16 * max_json_depth`
611+
612+
```c
613+
// doc -> buffer
614+
bool yyjson_write_fp(char *buf, size_t buf_len, const yyjson_doc *doc, yyjson_write_flag flg, yyjson_write_err *err);
615+
// mut_doc -> buffer
616+
bool yyjson_mut_write_fp(char *buf, size_t buf_len, const yyjson_mut_doc *doc, yyjson_write_flag flg, yyjson_write_err *err);
617+
// val -> buffer
618+
bool yyjson_val_write_fp(char *buf, size_t buf_len, const yyjson_val *val, yyjson_write_flag flg, yyjson_write_err *err);
619+
// mut_val -> buffer
620+
bool yyjson_mut_val_write_buf(char *buf, size_t buf_len, const yyjson_mut_val *val, yyjson_write_flag flg, yyjson_write_err *err);
621+
```
622+
623+
Sample code:
624+
625+
```c
626+
char buf[512];
627+
size_t len = yyjson_write_buf(buf, sizeof(buf), doc, YYJSON_WRITE_PRETTY, NULL);
628+
if (len > 0) printf("OK, output:\n%s\n", buf);
629+
```
630+
631+
596632
## Write JSON with options
597633
The `doc/val` is JSON document or root value, if you pass NULL, you will get NULL result.<br/>
598634
The `flg` is writer flag, pass 0 if you don't need it, see `writer flag` for details.<br/>

0 commit comments

Comments
 (0)