-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Add ignore_trailing_commas option #4609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nlohmann
merged 1 commit into
nlohmann:develop
from
chirsz-ever:chirsz/feat-allow-trailing-commas-2501
May 22, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
|
|
||
| #include <iostream> | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| int main() | ||
| { | ||
| std::string s = R"( | ||
| { | ||
| // update in 2006: removed Pluto | ||
| "planets": ["Mercury", "Venus", "Earth", "Mars", | ||
| "Jupiter", "Uranus", "Neptune" /*, "Pluto" */] | ||
| } | ||
| )"; | ||
|
|
||
| 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 */ true); | ||
| std::cout << j.dump(2) << '\n'; | ||
| } | ||
This file contains hidden or 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,12 @@ | ||
| [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 | ||
| { | ||
| "planets": [ | ||
| "Mercury", | ||
| "Venus", | ||
| "Earth", | ||
| "Mars", | ||
| "Jupiter", | ||
| "Uranus", | ||
| "Neptune" | ||
| ] | ||
| } |
This file contains hidden or 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,37 @@ | ||
| #include <iostream> | ||
| #include <nlohmann/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, | ||
| /* ignore_trailing_commas */ true); | ||
| std::cout << j.dump(2) << '\n'; | ||
| } |
This file contains hidden or 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,12 @@ | ||
| [json.exception.parse_error.101] parse error at line 11, column 9: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal | ||
| { | ||
| "planets": [ | ||
| "Mercury", | ||
| "Venus", | ||
| "Earth", | ||
| "Mars", | ||
| "Jupiter", | ||
| "Uranus", | ||
| "Neptune" | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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,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 | ||
chirsz-ever marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| --8<-- "examples/trailing_commas.cpp" | ||
| ``` | ||
|
|
||
| Output: | ||
|
|
||
| ``` | ||
| --8<-- "examples/trailing_commas.output" | ||
| ``` | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.