-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: chirsz-ever <[email protected]>
- Loading branch information
1 parent
e72046e
commit ed1d330
Showing
9 changed files
with
300 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Trailing Commas | ||
|
||
Like comments, this library does not support trailing commas in arrays and objects *by default*. | ||
|
||
You can pass set parameter `allow_trailing_commas` to `#!c true` in the parse function to allow trailing commas in arrays and objects. Trailing commas are ignored during parsing. | ||
|
||
Note that this library does not add trailing commas when serializing JSON data. Even when you set `allow_trailing_commas` to true, `{,}` and `[,]` are not valid JSON. | ||
|
||
!!! 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 `allow_trailing_commas` is set to `#! true`, the trailing commas are ignored during parsing: | ||
|
||
```cpp | ||
#include <iostream> | ||
#include "json.hpp" | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
std::string s = R"( | ||
{ | ||
"planets": [ | ||
"Mercury", | ||
"Venus", | ||
"Earth", | ||
"Mars", | ||
"Jupiter", | ||
"Uranus", | ||
"Neptune", | ||
] | ||
} | ||
)"; | ||
try | ||
{ | ||
json j = json::parse(s); | ||
} | ||
catch (json::exception &e) | ||
{ | ||
std::cout << e.what() << std::endl; | ||
} | ||
json j = json::parse(s, | ||
/* callback */ nullptr, | ||
/* allow exceptions */ true, | ||
/* ignore_comments */ false, | ||
/* allow_trailing_commas */ true); | ||
std::cout << j.dump(2) << '\n'; | ||
} | ||
``` | ||
|
||
Output: | ||
|
||
``` | ||
[json.exception.parse_error.101] parse error at line 3, column 9: | ||
syntax error while parsing object key - invalid literal; | ||
last read: '<U+000A> {<U+000A> /'; expected string literal | ||
``` | ||
|
||
```json | ||
{ | ||
"planets": [ | ||
"Mercury", | ||
"Venus", | ||
"Earth", | ||
"Mars", | ||
"Jupiter", | ||
"Uranus", | ||
"Neptune" | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.