Skip to content

Commit

Permalink
Move example code to docs/examples
Browse files Browse the repository at this point in the history
Signed-off-by: chirsz-ever <chirsz@foxmail.com>
  • Loading branch information
chirsz-ever committed Jan 23, 2025
1 parent b40faaf commit e83580b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 101 deletions.
31 changes: 31 additions & 0 deletions docs/mkdocs/docs/examples/comments.cpp
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';
}
12 changes: 12 additions & 0 deletions docs/mkdocs/docs/examples/comments.output
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"
]
}
37 changes: 37 additions & 0 deletions docs/mkdocs/docs/examples/trailing_commas.cpp
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';
}
12 changes: 12 additions & 0 deletions docs/mkdocs/docs/examples/trailing_commas.output
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"
]
}
51 changes: 3 additions & 48 deletions docs/mkdocs/docs/features/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This library does not support comments *by default*. It does so for three reason
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.

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.
However, you can pass parameter `ignore_comments` to `#!cpp true` in the parse function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace.

!!! example

Expand All @@ -28,56 +28,11 @@ However, you can pass set parameter `ignore_comments` to `#!c true` in the parse
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:

```cpp
#include <iostream>
#include "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';
}
--8<-- "examples/comments.cpp"
```

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"
]
}
--8<-- "examples/comments.output"
```
55 changes: 2 additions & 53 deletions docs/mkdocs/docs/features/trailing_commas.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,62 +27,11 @@ This library does not add trailing commas when serializing JSON data.
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
#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,
/* ignore_trailing_commas */ true);
std::cout << j.dump(2) << '\n';
}
--8<-- "examples/trailing_commas.cpp"
```

Output:

```
[json.exception.parse_error.101] parse error at line 11, column 13:
syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal
```

```json
{
"planets": [
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Uranus",
"Neptune"
]
}
--8<-- "examples/trailing_commas.output"
```

0 comments on commit e83580b

Please sign in to comment.