SAVED EN CANONICAL
---
title: "Writing JSON"
documentId: "yyjson:01-guide/04-writing-json.md"
order: 4
licenseSource: "yyjson-fixed"
toc: {"maxLevel": 6}
documentContext: [{"kind": "source", "html": "<p>yyjson 0.13.0, fixed commit 6447536015f3d600f3d65323b10976103b337ca7. By YaoYuan and yyjson contributors. <a href=\"https://github.com/ibireme/yyjson/blob/6447536015f3d600f3d65323b10976103b337ca7/doc/API.md#L515-L796\">Fixed official original</a>. <a href=\"/docs/yyjson/notices/LICENSE.txt\">Original MIT notice</a>.</p><p>Libxによる非公式編集・日本語訳です。固定Markdown7資料とSVG7図を保持し、利用ガイド16ページが日本語訳の対象です。更新履歴・原著Performance TODO・原MIT通知は英語原文参照です。Libxの運用方針に基づき、意味のまとまった節分割・見出し・内部参照・静的な図の配置を調整しています。</p>"}, {"kind": "editorial", "html": "<p>原著のPerformance.mdはTODOのみで未執筆です。READMEの性能図表と2020年のレポートは固定原文のまま提供し、原著の制限・注意書きも保持しています。生成Doxygenの全シンボル一覧、第三者テーマ、対話型ベンチマークや構造図の編集ファイルは収録せず、<a href=\"https://ibireme.github.io/yyjson/doc/doxygen/html/\">公式文書</a>と原稿中の参照リンクで補います。原文の技術監査やサンプルプログラムの実行は行っていません。</p>"}]
---
# Writing JSON
The library provides 5 sets of functions for writing JSON.<br/>
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.<br/>
The `flg` is writer flag, pass 0 if you don't need it, see `writer flag` for details.<br/>
The `len` is a pointer to receive output length (not including the
    null-terminator), pass NULL if you don't need it.<br/>
This function returns a new JSON string, or NULL if an error occurs.<br/>
The string is encoded as UTF-8 with a null-terminator. <br/>
You should use `free()` or `alc->free()` to release it when it's no longer needed.

```c
// doc -> str
char *yyjson_write(const yyjson_doc *doc, yyjson_write_flag flg, size_t *len);
// mut_doc -> str
char *yyjson_mut_write(const yyjson_mut_doc *doc, yyjson_write_flag flg, size_t *len);
// val -> str
char *yyjson_val_write(const yyjson_val *val, yyjson_write_flag flg, size_t *len);
// mut_val -> str
char *yyjson_mut_val_write(const yyjson_mut_val *val, yyjson_write_flag flg, size_t *len);
```

Sample code 1:

```c
yyjson_doc *doc = yyjson_read("[1,2,3]", 7, 0);
char *json = yyjson_write(doc, YYJSON_WRITE_PRETTY, NULL);
printf("%s\n", json);
free(json);
```

Sample code 2:
```c
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
yyjson_mut_val *arr = yyjson_mut_arr(doc);
yyjson_mut_doc_set_root(doc, arr);
yyjson_mut_arr_add_int(doc, arr, 1);
yyjson_mut_arr_add_int(doc, arr, 2);
yyjson_mut_arr_add_int(doc, arr, 3);
    
char *json = yyjson_mut_write(doc, YYJSON_WRITE_PRETTY, NULL);
printf("%s\n", json);
free(json);
```

## 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.<br/>
The `doc/val` is the JSON document or root value. If it is NULL, returns false.<br/>
The `flg` is writer flag, pass 0 if you don't need it, see `writer flag` for details.<br/>
The `alc` is memory allocator, pass NULL if you don't need it, see `memory allocator` for details.<br/>
The `err` is a pointer to receive error message, pass NULL if you don't need it.<br/>
This function returns true on success, or false if an error occurs.<br/>

```c
// doc -> file
bool yyjson_write_file(const char *path, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_doc -> file
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);
// val -> file
bool yyjson_val_write_file(const char *path, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_val -> file
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:

```c
yyjson_doc *doc = yyjson_read_file("/tmp/test.json", 0, NULL, NULL);
bool suc = yyjson_write_file("tmp/test.json", doc, YYJSON_WRITE_PRETTY, NULL, NULL);
if (suc) printf("OK");
```

## Write JSON to file pointer
The `fp` is the output file pointer. The data will be written to the current position of the file.<br/>
If `fp` is NULL or invalid, returns false.<br/>
The `doc/val` is the JSON document or root value. If it is NULL, returns false.<br/>
The `flg` is writer flag, pass 0 if you don't need it, see `writer flag` for details.<br/>
The `alc` is memory allocator, pass NULL if you don't need it, see `memory allocator` for details.<br/>
The `err` is a pointer to receive error message, pass NULL if you don't need it.<br/>
This function returns true on success, or false if an error occurs.<br/>

```c
// doc -> file
bool yyjson_write_fp(FILE *fp, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_doc -> file
bool yyjson_mut_write_fp(FILE *fp, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// val -> file
bool yyjson_val_write_fp(FILE *fp, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_val -> file
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:

```c
FILE *fp = fdopen(fd, "wb"); // POSIX file descriptor (fd)
bool suc = yyjson_write_fp(fp, doc, YYJSON_WRITE_PRETTY, NULL, NULL);
if (fp) fclose(fp);
if (suc) printf("OK");
```

## Write JSON to buffer
The `buf` is the output buffer. If `buf` is NULL, returns 0.<br/>
The `buf_len` is the buffer length. If `buf_len` is too small, returns 0.<br/>
The `doc/val` is the JSON document or root value. If it is NULL, returns 0.<br/>
The `flg` is writer flag, pass 0 if you don't need it, see `writer flag` for details.<br/>
The `err` is a pointer to receive error message, pass NULL if you don't need it.<br/>
This function returns the number of bytes written (excluding the null terminator), or 0 on failure.<br/>

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`

```c
// doc -> buffer
size_t yyjson_write_buf(char *buf, size_t buf_len, const yyjson_doc *doc, yyjson_write_flag flg, yyjson_write_err *err);
// mut_doc -> buffer
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);
// val -> buffer
size_t yyjson_val_write_buf(char *buf, size_t buf_len, const yyjson_val *val, yyjson_write_flag flg, yyjson_write_err *err);
// mut_val -> buffer
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);
```

Sample code:

```c
char buf[512];
size_t len = yyjson_write_buf(buf, sizeof(buf), doc, YYJSON_WRITE_PRETTY, NULL);
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.<br/>
The `flg` is writer flag, pass 0 if you don't need it, see `writer flag` for details.<br/>
The `alc` is memory allocator, pass NULL if you don't need it, see `memory allocator` for details.<br/>
The `len` is a pointer to receive output length (not including the
    null-terminator), pass NULL if you don't need it.<br/>
The `err` is a pointer to receive error message, pass NULL if you don't need it.<br/>

This function returns a new JSON string, or NULL if an error occurs.<br/>
The string is encoded as UTF-8 with a null-terminator. <br/>
You should use free() or alc->free() to release it when it's no longer needed.

```c
char *yyjson_write_opts(const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err);

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);

char *yyjson_val_write_opts(const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err);

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:

```c
yyjson_doc *doc = ...;

// init an allocator with stack memory
char buf[64 * 1024];
yyjson_alc alc;
yyjson_alc_pool_init(&alc, buf, sizeof(buf));

// write
size_t len;
yyjson_write_err err;
char *json = yyjson_write_opts(doc, YYJSON_WRITE_PRETTY | YYJSON_WRITE_ESCAPE_UNICODE, &alc, &len, &err);

// get result
if (json) {
    printf("suc: %lu\n%s\n", len, json);
} else {
    printf("err: %u msg:%s\n", err.code, err.msg);
}
alc.free(alc.ctx, json);
```

The complete list of error codes (`yyjson_write_code`):

| Code | Name | Description |
|------|------|-------------|
| 0 | `YYJSON_WRITE_SUCCESS` | Success, no error. |
| 1 | `YYJSON_WRITE_ERROR_INVALID_PARAMETER` | Invalid parameter, such as NULL document. |
| 2 | `YYJSON_WRITE_ERROR_MEMORY_ALLOCATION` | Memory allocation failure. |
| 3 | `YYJSON_WRITE_ERROR_INVALID_VALUE_TYPE` | Invalid value type in JSON document. |
| 4 | `YYJSON_WRITE_ERROR_NAN_OR_INF` | NaN or Infinity number occurs. |
| 5 | `YYJSON_WRITE_ERROR_FILE_OPEN` | Failed to open a file. |
| 6 | `YYJSON_WRITE_ERROR_FILE_WRITE` | Failed to write a file. |
| 7 | `YYJSON_WRITE_ERROR_INVALID_STRING` | Invalid unicode in string. |
| 8 | `YYJSON_WRITE_ERROR_DEPTH` | Nesting depth exceeded `YYJSON_WRITER_DEPTH_LIMIT`. |


## Writer flag
The library provides a set of flags for JSON writer.<br/>
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:

```json
["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:

```json
["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.<br/>

Note that this output is **NOT** standard JSON and may be rejected by other JSON libraries, for example:

```js
{"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.<br/>
This flag will override `YYJSON_WRITE_ALLOW_INF_AND_NAN` flag, for example:

```js
{"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.



---------------

SAVED JA BODY
<a id="writing-json"></a>
# JSONの書き出し

ライブラリは、JSONを書き出す5組の関数を提供しています。<br/>
各関数はJSONドキュメントまたはルート値を入力として受け取り、UTF-8文字列またはファイルを返します。

<a id="write-json-to-string"></a>
## JSONを文字列に書き出す

`doc/val`はJSONドキュメントまたはルート値です。NULLの場合はNULLを返します。<br/>
`flg`は書き出しフラグです。不要なら0を渡します。詳細は書き出しフラグを参照してください。<br/>
`len`は出力長（NUL終端文字を含まない）を受け取るためのポインターです。不要ならNULLを渡します。<br/>
この関数は新しいJSON文字列を返します。エラーが起きた場合はNULLを返します。<br/>
文字列はNUL終端されたUTF-8でエンコードされます。<br/>
不要になったら、`free()`または`alc->free()`で解放する必要があります。

```c
// doc -> str
char *yyjson_write(const yyjson_doc *doc, yyjson_write_flag flg, size_t *len);
// mut_doc -> str
char *yyjson_mut_write(const yyjson_mut_doc *doc, yyjson_write_flag flg, size_t *len);
// val -> str
char *yyjson_val_write(const yyjson_val *val, yyjson_write_flag flg, size_t *len);
// mut_val -> str
char *yyjson_mut_val_write(const yyjson_mut_val *val, yyjson_write_flag flg, size_t *len);
```


サンプルコード1：

```c
yyjson_doc *doc = yyjson_read("[1,2,3]", 7, 0);
char *json = yyjson_write(doc, YYJSON_WRITE_PRETTY, NULL);
printf("%s\n", json);
free(json);
```


サンプルコード2：

```c
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
yyjson_mut_val *arr = yyjson_mut_arr(doc);
yyjson_mut_doc_set_root(doc, arr);
yyjson_mut_arr_add_int(doc, arr, 1);
yyjson_mut_arr_add_int(doc, arr, 2);
yyjson_mut_arr_add_int(doc, arr, 3);
    
char *json = yyjson_mut_write(doc, YYJSON_WRITE_PRETTY, NULL);
printf("%s\n", json);
free(json);
```


<a id="write-json-to-file"></a>
## JSONをファイルに書き出す

`path`は出力JSONファイルのパスです。システム固有の文字エンコーディングによるNUL終端文字列である必要があります。`path`がNULLまたは無効な場合はfalseを返します。ファイルが空でない場合、その内容は破棄されます。<br/>
`doc/val`はJSONドキュメントまたはルート値です。NULLの場合はfalseを返します。<br/>
`flg`は書き出しフラグです。不要なら0を渡します。詳細は書き出しフラグを参照してください。<br/>
`alc`はメモリアロケーターです。不要ならNULLを渡します。詳細はメモリアロケーターを参照してください。<br/>
`err`はエラーメッセージを受け取るためのポインターです。不要ならNULLを渡します。<br/>
この関数は成功するとtrueを返し、エラーが起きた場合はfalseを返します。<br/>

```c
// doc -> file
bool yyjson_write_file(const char *path, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_doc -> file
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);
// val -> file
bool yyjson_val_write_file(const char *path, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_val -> file
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);
```


サンプルコード：

```c
yyjson_doc *doc = yyjson_read_file("/tmp/test.json", 0, NULL, NULL);
bool suc = yyjson_write_file("tmp/test.json", doc, YYJSON_WRITE_PRETTY, NULL, NULL);
if (suc) printf("OK");
```


<a id="write-json-to-file-pointer"></a>
## JSONをファイルポインターに書き出す

`fp`は出力ファイルポインターです。ファイルの現在位置にデータを書き込みます。<br/>
`fp`がNULLまたは無効な場合はfalseを返します。<br/>
`doc/val`はJSONドキュメントまたはルート値です。NULLの場合はfalseを返します。<br/>
`flg`は書き出しフラグです。不要なら0を渡します。詳細は書き出しフラグを参照してください。<br/>
`alc`はメモリアロケーターです。不要ならNULLを渡します。詳細はメモリアロケーターを参照してください。<br/>
`err`はエラーメッセージを受け取るためのポインターです。不要ならNULLを渡します。<br/>
この関数は成功するとtrueを返し、エラーが起きた場合はfalseを返します。<br/>

```c
// doc -> file
bool yyjson_write_fp(FILE *fp, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_doc -> file
bool yyjson_mut_write_fp(FILE *fp, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// val -> file
bool yyjson_val_write_fp(FILE *fp, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err);
// mut_val -> file
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);
```


サンプルコード：

```c
FILE *fp = fdopen(fd, "wb"); // POSIX file descriptor (fd)
bool suc = yyjson_write_fp(fp, doc, YYJSON_WRITE_PRETTY, NULL, NULL);
if (fp) fclose(fp);
if (suc) printf("OK");
```


<a id="write-json-to-buffer"></a>
## JSONをバッファーに書き出す

`buf`は出力バッファーです。NULLの場合は0を返します。<br/>
`buf_len`はバッファーの長さです。小さすぎる場合は0を返します。<br/>
`doc/val`はJSONドキュメントまたはルート値です。NULLの場合は0を返します。<br/>
`flg`は書き出しフラグです。不要なら0を渡します。詳細は書き出しフラグを参照してください。<br/>
`err`はエラーメッセージを受け取るためのポインターです。不要ならNULLを渡します。<br/>
この関数は書き込んだバイト数（NUL終端文字を含まない）を返し、失敗した場合は0を返します。<br/>

この関数はメモリを確保しません。ただし、一時的な作業領域を確保するため、バッファーは最終的なJSONのサイズより大きくする必要があります。

追加の領域は、各値を書き出す間だけ必要になり、後の値に再利用されます。

- 数値：`40`
- 文字列：`16 + (str_len * 6)`
- その他の値：`16`
- ネストの深さ：`16 * max_json_depth`

```c
// doc -> buffer
size_t yyjson_write_buf(char *buf, size_t buf_len, const yyjson_doc *doc, yyjson_write_flag flg, yyjson_write_err *err);
// mut_doc -> buffer
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);
// val -> buffer
size_t yyjson_val_write_buf(char *buf, size_t buf_len, const yyjson_val *val, yyjson_write_flag flg, yyjson_write_err *err);
// mut_val -> buffer
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);
```


サンプルコード：

```c
char buf[512];
size_t len = yyjson_write_buf(buf, sizeof(buf), doc, YYJSON_WRITE_PRETTY, NULL);
if (len > 0) printf("OK, output:\n%s\n", buf);
```



<a id="write-json-with-options"></a>
## オプションを指定してJSONを書き出す

`doc/val`はJSONドキュメントまたはルート値です。NULLの場合はNULLを返します。<br/>
`flg`は書き出しフラグです。不要なら0を渡します。詳細は書き出しフラグを参照してください。<br/>
`alc`はメモリアロケーターです。不要ならNULLを渡します。詳細はメモリアロケーターを参照してください。<br/>
`len`は出力長（NUL終端文字を含まない）を受け取るためのポインターです。不要ならNULLを渡します。<br/>
`err`はエラーメッセージを受け取るためのポインターです。不要ならNULLを渡します。<br/>

この関数は新しいJSON文字列を返します。エラーが起きた場合はNULLを返します。<br/>
文字列はNUL終端されたUTF-8でエンコードされます。<br/>
不要になったら、free()またはalc->free()で解放する必要があります。

```c
char *yyjson_write_opts(const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err);

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);

char *yyjson_val_write_opts(const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err);

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);
```


サンプルコード：

```c
yyjson_doc *doc = ...;

// init an allocator with stack memory
char buf[64 * 1024];
yyjson_alc alc;
yyjson_alc_pool_init(&alc, buf, sizeof(buf));

// write
size_t len;
yyjson_write_err err;
char *json = yyjson_write_opts(doc, YYJSON_WRITE_PRETTY | YYJSON_WRITE_ESCAPE_UNICODE, &alc, &len, &err);

// get result
if (json) {
    printf("suc: %lu\n%s\n", len, json);
} else {
    printf("err: %u msg:%s\n", err.code, err.msg);
}
alc.free(alc.ctx, json);
```


エラーコード（`yyjson_write_code`）の完全な一覧：

|コード|名前|説明|
|---|---|---|
|0|`YYJSON_WRITE_SUCCESS`|成功。エラーなし。|
|1|`YYJSON_WRITE_ERROR_INVALID_PARAMETER`|NULLのドキュメントなど、無効な引数。|
|2|`YYJSON_WRITE_ERROR_MEMORY_ALLOCATION`|メモリ確保の失敗。|
|3|`YYJSON_WRITE_ERROR_INVALID_VALUE_TYPE`|JSONドキュメント内の値の型が無効。|
|4|`YYJSON_WRITE_ERROR_NAN_OR_INF`|NaNまたはInfinityの数値が現れた。|
|5|`YYJSON_WRITE_ERROR_FILE_OPEN`|ファイルを開けなかった。|
|6|`YYJSON_WRITE_ERROR_FILE_WRITE`|ファイルに書き込めなかった。|
|7|`YYJSON_WRITE_ERROR_INVALID_STRING`|文字列に無効なUnicodeがある。|
|8|`YYJSON_WRITE_ERROR_DEPTH`|ネストの深さが`YYJSON_WRITER_DEPTH_LIMIT`を超えた。|

<a id="writer-flag"></a>
## 書き出しフラグ

ライブラリは、JSONライター用のフラグを用意しています。<br/>
フラグを単独で使うことも、ビット単位の`|`演算子で複数のフラグを組み合わせることもできます。

### **YYJSON_WRITE_NOFLAG = 0**

JSONライターの既定のフラグです。

- JSONを最小化した形式で書き出します。
- `inf`または`nan`の数値が現れるとエラーを報告します。
- 無効なUTF-8文字列が現れるとエラーを報告します。
- Unicodeやスラッシュをエスケープしません。

### **YYJSON_WRITE_PRETTY**

空白4つのインデントで、読みやすく整形してJSONを書き出します。

### **YYJSON_WRITE_PRETTY_TWO_SPACES**

空白2つのインデントで、読みやすく整形してJSONを書き出します。
このフラグは`YYJSON_WRITE_PRETTY`フラグを上書きします。

### **YYJSON_WRITE_ESCAPE_UNICODE**

Unicodeを`\uXXXX`としてエスケープし、出力をASCIIのみにします。例：

```json
["Alizée, 😊"]
["Aliz\\u00E9e, \\uD83D\\uDE0A"]
```


### **YYJSON_WRITE_LOWERCASE_HEX**

`\uXXXX`エスケープシーケンスの16進数字に、既定の大文字ではなく小文字を使います。
`YYJSON_WRITE_ESCAPE_UNICODE`も設定されている場合にのみ有効です。

### **YYJSON_WRITE_ESCAPE_SLASHES**

スラッシュ`/`を`\/`としてエスケープします。例：

```json
["https://github.com"]
["https:\/\/github.com"]
```


### **YYJSON_WRITE_ALLOW_INF_AND_NAN**

inf/nanの数値についてエラーを報告する代わりに、`Infinity`および`NaN`リテラルとして書き出します。<br/>

この出力は標準JSONでは**ない**ため、ほかのJSONライブラリが受け付けない場合があることに注意してください。例：

```js
{"not_a_number":NaN,"large_number":Infinity}
```


### **YYJSON_WRITE_INF_AND_NAN_AS_NULL**

inf/nanの数値についてエラーを報告する代わりに、`null`リテラルとして書き出します。<br/>
このフラグは`YYJSON_WRITE_ALLOW_INF_AND_NAN`フラグを上書きします。例：

```js
{"not_a_number":null,"large_number":null}
```


### **YYJSON_WRITE_ALLOW_INVALID_UNICODE**

文字列の値をエンコードする際に、無効なUnicodeを許可します。

文字列の値に含まれる無効な文字は、バイトごとにコピーされます。`YYJSON_WRITE_ESCAPE_UNICODE`フラグも設定されている場合、無効な文字は`\uFFFD`（置換文字）としてエスケープされます。

このフラグは、正しくエンコードされた文字列の処理性能には影響しません。

### **YYJSON_WRITE_NEWLINE_AT_END**

JSONの末尾に改行文字`\n`を追加します。
テキストエディターやNDJSONで役立つことがあります。

### **YYJSON_WRITE_FP_TO_FLOAT**

浮動小数点数を単精度（float）で書き出します。
シリアライズ前に`double`を`float`へキャストします。
出力は短くなりますが、精度が多少失われる可能性があります。
`YYJSON_WRITE_FP_TO_FIXED(prec)`も使っている場合、このフラグは無視されます。

### **YYJSON_WRITE_FP_TO_FIXED(prec)**

浮動小数点数を固定小数点表記で書き出します。
ECMAScriptの`Number.prototype.toFixed(prec)`に似ていますが、末尾の0は除去されます。`prec`の範囲は1から15です。
出力は短くなりますが、精度が多少失われる可能性があります。

---------------
