Skip to content

Commit

Permalink
📝 add examples for parsing from iterator pair
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Oct 20, 2021
1 parent 67c8ead commit 91f9fd6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
15 changes: 15 additions & 0 deletions doc/examples/parse__iterator_pair.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
// a JSON text given an input with other values
std::vector<std::uint8_t> input = {'[', '1', ',', '2', ',', '3', ']', 'o', 't', 'h', 'e', 'r'};

// parse and serialize JSON
json j_complete = json::parse(input.begin(), input.begin() + 7);
std::cout << std::setw(4) << j_complete << "\n\n";
}
1 change: 1 addition & 0 deletions doc/examples/parse__iterator_pair.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/hUJYo1HWmfTBLMGn"><b>online</b></a>
6 changes: 6 additions & 0 deletions doc/examples/parse__iterator_pair.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
1,
2,
3
]

15 changes: 15 additions & 0 deletions doc/examples/parse__pointers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
// a JSON text given as string that is not null-terminated
const char* ptr = "[1,2,3]another value";

// parse and serialize JSON
json j_complete = json::parse(ptr, ptr + 7);
std::cout << std::setw(4) << j_complete << "\n\n";
}
1 change: 1 addition & 0 deletions doc/examples/parse__pointers.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/AWbpa8e1xRV3y4MM"><b>online</b></a>
6 changes: 6 additions & 0 deletions doc/examples/parse__pointers.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
1,
2,
3
]

35 changes: 33 additions & 2 deletions doc/mkdocs/docs/api/basic_json/parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static basic_json parse(IteratorType first, IteratorType last,
1. Deserialize from a compatible input.
2. Deserialize from a pair of character iterators
The value_type of the iterator must be a integral type with size of 1, 2 or 4 bytes, which will be interpreted
The `value_type` of the iterator must be an integral type with size of 1, 2 or 4 bytes, which will be interpreted
respectively as UTF-8, UTF-16 and UTF-32.
## Template parameters
Expand All @@ -34,7 +34,10 @@ static basic_json parse(IteratorType first, IteratorType last,
- an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators.
`IteratorType`
: a compatible iterator type
: a compatible iterator type, for instance.
- a pair of `std::string::iterator` or `std::vector<std::uint8_t>::iterator`
- a pair of pointers such as `ptr` and `ptr + len`
## Parameters
Expand Down Expand Up @@ -135,6 +138,34 @@ super-linear complexity.
--8<-- "examples/parse__contiguouscontainer__parser_callback_t.output"
```

??? example "Parsing from a non null-terminated string"

The example below demonstrates the `parse()` function reading from a string that is not null-terminated.

```cpp
--8<-- "examples/parse__pointers.cpp"
```

Output:

```json
--8<-- "examples/parse__pointers.output"
```

??? example "Parsing from an iterator pair"

The example below demonstrates the `parse()` function reading from an iterator pair.

```cpp
--8<-- "examples/parse__iterator_pair.cpp"
```

Output:

```json
--8<-- "examples/parse__iterator_pair.output"
```

??? example "Effect of `allow_exceptions` parameter"

The example below demonstrates the effect of the `allow_exceptions` parameter in the ´parse()` function.
Expand Down

0 comments on commit 91f9fd6

Please sign in to comment.