-
-
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.
Merge pull request #2312 from nlohmann/docs
Update documentation
- Loading branch information
Showing
21 changed files
with
547 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# basic_json::dump | ||
|
||
```cpp | ||
string_t dump(const int indent = -1, | ||
const char indent_char = ' ', | ||
const bool ensure_ascii = false, | ||
const error_handler_t error_handler = error_handler_t::strict) const | ||
``` | ||
Serialization function for JSON values. The function tries to mimic | ||
Python's `json.dumps()` function, and currently supports its `indent` | ||
and `ensure_ascii` parameters. | ||
## Parameters | ||
`indent` (in) | ||
: If `indent` is nonnegative, then array elements and object | ||
members will be pretty-printed with that indent level. An indent level of | ||
`0` will only insert newlines. `-1` (the default) selects the most compact | ||
representation. | ||
`indent_char` (in) | ||
: The character to use for indentation if `indent` is | ||
greater than `0`. The default is ` ` (space). | ||
`ensure_ascii` (in) | ||
: If `ensure_ascii` is true, all non-ASCII characters | ||
in the output are escaped with `\uXXXX` sequences, and the result consists | ||
of ASCII characters only. | ||
`error_handler` (in) | ||
: how to react on decoding errors; there are three | ||
possible values: `strict` (throws and exception in case a decoding error | ||
occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD), | ||
and `ignore` (ignore invalid UTF-8 sequences during serialization; all | ||
bytes are copied to the output unchanged). | ||
## Return value | ||
string containing the serialization of the JSON value | ||
## Exception safety | ||
Strong guarantee: if an exception is thrown, there are no | ||
changes to any JSON value. | ||
## Complexity | ||
Linear. | ||
## Notes | ||
Binary values are serialized as object containing two keys: | ||
- "bytes": an array of bytes as integers | ||
- "subtype": the subtype as integer or `#!json null` if the binary has no subtype | ||
## Example | ||
The following example shows the effect of different `indent`, | ||
`indent_char`, and `ensure_ascii` parameters to the result of the | ||
serialization. | ||
```cpp | ||
--8<-- "examples/dump.cpp" | ||
``` | ||
|
||
Output: | ||
|
||
```json | ||
--8<-- "examples/dump.output" | ||
``` |
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,15 @@ | ||
# Overview | ||
|
||
!!! note | ||
|
||
This page is under construction. | ||
|
||
## Member functions | ||
|
||
### Object inspection | ||
|
||
- [dump](dump.md) - serialization | ||
|
||
## Static functions | ||
|
||
- [meta](meta.md) - returns version information on the library |
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,45 @@ | ||
# basic_json::meta | ||
|
||
```cpp | ||
static basic_json meta(); | ||
``` | ||
|
||
This function returns a JSON object with information about the library, | ||
including the version number and information on the platform and compiler. | ||
|
||
## Return value | ||
|
||
JSON object holding version information | ||
|
||
key | description | ||
----------- | --------------- | ||
`compiler` | Information on the used compiler. It is an object with the following keys: `c++` (the used C++ standard), `family` (the compiler family; possible values are `clang`, `icc`, `gcc`, `ilecpp`, `msvc`, `pgcpp`, `sunpro`, and `unknown`), and `version` (the compiler version). | ||
`copyright` | The copyright line for the library as string. | ||
`name` | The name of the library as string. | ||
`platform` | The used platform as string. Possible values are `win32`, `linux`, `apple`, `unix`, and `unknown`. | ||
`url` | The URL of the project as string. | ||
`version` | The version of the library. It is an object with the following keys: `major`, `minor`, and `patch` as defined by [Semantic Versioning](http://semver.org), and `string` (the version string). | ||
|
||
## Exception safety | ||
|
||
Strong guarantee: if an exception is thrown, there are no | ||
changes to any JSON value. | ||
|
||
## Complexity | ||
|
||
Constant. | ||
|
||
## Example | ||
|
||
The following code shows an example output of the `meta()` | ||
function. | ||
|
||
```cpp | ||
--8<-- "examples/meta.cpp" | ||
``` | ||
|
||
Output: | ||
|
||
```json | ||
--8<-- "examples/meta.output" | ||
``` |
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,83 @@ | ||
# Comments | ||
|
||
This library does not support comments *by default*. It does so for three reasons: | ||
|
||
1. Comments are not part of the [JSON specification](https://tools.ietf.org/html/rfc8259). You may argue that `//` or `/* */` are allowed in JavaScript, but JSON is not JavaScript. | ||
2. This was not an oversight: Douglas Crockford [wrote on this](https://plus.google.com/118095276221607585885/posts/RK8qyGVaGSr) in May 2012: | ||
|
||
> I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability. I know that the lack of comments makes some people sad, but it shouldn't. | ||
|
||
> Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser. | ||
|
||
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. | ||
|
||
!!! example | ||
|
||
Consider the following JSON with comments. | ||
|
||
```json | ||
{ | ||
// update in 2006: removed Pluto | ||
"planets": ["Mercury", "Venus", "Earth", "Mars", | ||
"Jupiter", "Uranus", "Neptune" /*, "Pluto" */] | ||
} | ||
``` | ||
|
||
When calling `parse` without additional argument, a parse error exception is thrown. If `skip_comments` is set to `#! true`, the comments are skipped 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, | ||
/* skip_comments */ 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" | ||
] | ||
} | ||
``` |
Oops, something went wrong.