You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added examples and modified the corresponding documents and unit tests.
Signed-off-by: chirsz-ever <[email protected]>
Co-authored-by: Niels Lohmann <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+11-1Lines changed: 11 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1801,7 +1801,17 @@ This library does not support comments by default. It does so for three reasons:
1801
1801
1802
1802
3. It is dangerous for interoperability if some libraries would add comment support while others don't. Please check [The Harmful Consequences of the Robustness Principle](https://tools.ietf.org/html/draft-iab-protocol-maintenance-01) on this.
1803
1803
1804
-
However, you can pass set parameter `ignore_comments` to true in the `parse` function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace.
1804
+
However, you can set set parameter `ignore_comments` to true in the `parse` function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace.
1805
+
1806
+
### Trailing commas
1807
+
1808
+
The JSON specification does not allow trailing commas in arrays and objects, and hence this library is treating them as parsing errors by default.
1809
+
1810
+
Like comments, you can set parameter `ignore_trailing_commas` to true in the `parse` function to ignore 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.
1811
+
1812
+
This library does not add trailing commas when serializing JSON data.
1813
+
1814
+
For more information, see [JSON With Commas and Comments (JWCC)](https://nigeltao.github.io/blog/2021/json-with-commas-comments.html).
Copy file name to clipboardExpand all lines: docs/mkdocs/docs/features/comments.md
+5-48Lines changed: 5 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,9 @@ This library does not support comments *by default*. It does so for three reason
11
11
12
12
3. It is dangerous for interoperability if some libraries add comment support while others do not. Please check [The Harmful Consequences of the Robustness Principle](https://tools.ietf.org/html/draft-iab-protocol-maintenance-01) on this.
13
13
14
-
However, you can pass set parameter `ignore_comments` to `#!c true` in the parse function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace.
14
+
However, you can set parameter `ignore_comments` to `#!cpp true` in the [`parse`](../api/basic_json/parse.md) function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace.
15
+
16
+
For more information, see [JSON With Commas and Comments (JWCC)](https://nigeltao.github.io/blog/2021/json-with-commas-comments.html).
15
17
16
18
!!! example
17
19
@@ -28,56 +30,11 @@ However, you can pass set parameter `ignore_comments` to `#!c true` in the parse
28
30
When calling `parse` without additional argument, a parse error exception is thrown. If `ignore_comments` is set to `#! true`, the comments are ignored during parsing:
29
31
30
32
```cpp
31
-
#include <iostream>
32
-
#include "json.hpp"
33
-
34
-
using json = nlohmann::json;
35
-
36
-
int main()
37
-
{
38
-
std::string s = R"(
39
-
{
40
-
// update in 2006: removed Pluto
41
-
"planets": ["Mercury", "Venus", "Earth", "Mars",
42
-
"Jupiter", "Uranus", "Neptune" /*, "Pluto" */]
43
-
}
44
-
)";
45
-
46
-
try
47
-
{
48
-
json j = json::parse(s);
49
-
}
50
-
catch (json::exception &e)
51
-
{
52
-
std::cout << e.what() << std::endl;
53
-
}
54
-
55
-
json j = json::parse(s,
56
-
/* callback */ nullptr,
57
-
/* allow exceptions */ true,
58
-
/* ignore_comments */ true);
59
-
std::cout << j.dump(2) << '\n';
60
-
}
33
+
--8<-- "examples/comments.cpp"
61
34
```
62
35
63
36
Output:
64
37
65
38
```
66
-
[json.exception.parse_error.101] parse error at line 3, column 9:
67
-
syntax error while parsing object key - invalid literal;
68
-
last read: '<U+000A> {<U+000A> /'; expected string literal
Like [comments](comments.md), this library does not support trailing commas in arrays and objects *by default*.
4
+
5
+
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.
6
+
7
+
This library does not add trailing commas when serializing JSON data.
8
+
9
+
For more information, see [JSON With Commas and Comments (JWCC)](https://nigeltao.github.io/blog/2021/json-with-commas-comments.html).
10
+
11
+
!!! example
12
+
13
+
Consider the following JSON with trailing commas.
14
+
15
+
```json
16
+
{
17
+
"planets": [
18
+
"Mercury",
19
+
"Venus",
20
+
"Earth",
21
+
"Mars",
22
+
"Jupiter",
23
+
"Uranus",
24
+
"Neptune",
25
+
]
26
+
}
27
+
```
28
+
29
+
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:
0 commit comments