Add ignore_trailing_commas option (#4609)

Added examples and modified the corresponding documents and unit tests.

Signed-off-by: chirsz-ever <chirsz-ever@outlook.com>
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
This commit is contained in:
chirsz
2025-05-22 14:01:46 +08:00
committed by GitHub
parent 2d9a251266
commit 4b17f90f65
15 changed files with 386 additions and 148 deletions

View File

@@ -0,0 +1,39 @@
# Trailing Commas
Like [comments](comments.md), this library does not support trailing commas in arrays and objects *by default*.
You can set parameter `ignore_trailing_commas` to `#!cpp true` in the [`parse`](../api/basic_json/parse.md) function to allow trailing commas in arrays and objects. Note that a single comma as the only content of the array or object (`[,]` or `{,}`) is not allowed, and multiple trailing commas (`[1,,]`) are not allowed either.
This library does not add trailing commas when serializing JSON data.
For more information, see [JSON With Commas and Comments (JWCC)](https://nigeltao.github.io/blog/2021/json-with-commas-comments.html).
!!! example
Consider the following JSON with trailing commas.
```json
{
"planets": [
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Uranus",
"Neptune",
]
}
```
When calling `parse` without additional argument, a parse error exception is thrown. If `ignore_trailing_commas` is set to `#! true`, the trailing commas are ignored during parsing:
```cpp
--8<-- "examples/trailing_commas.cpp"
```
Output:
```
--8<-- "examples/trailing_commas.output"
```