JSON for Modern C++ 2.1.1
Loading...
Searching...
No Matches

◆ parse() [3/6]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class IteratorType , typename std::enable_if< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< IteratorType >::iterator_category >::value, int >::type = 0>
static basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parse ( IteratorType  first,
IteratorType  last,
const parser_callback_t  cb = nullptr 
)
inlinestatic

This function reads from an iterator range of a container with contiguous storage of 1-byte values. Compatible container types include std::vector, std::string, std::array, std::valarray, and std::initializer_list. Furthermore, C-style arrays can be used with std::begin()/std::end(). User-defined containers can be used as long as they implement random-access iterators and a contiguous storage.

Precondition
The iterator range is contiguous. Violating this precondition yields undefined behavior. This precondition is enforced with an assertion.
Each element in the range has a size of 1 byte. Violating this precondition yields undefined behavior. This precondition is enforced with a static assertion.
Warning
There is no way to enforce all preconditions at compile-time. If the function is called with noncompliant iterators and with assertions switched off, the behavior is undefined and will most likely yield segmentation violation.
Template Parameters
IteratorTypeiterator of container with contiguous storage
Parameters
[in]firstbegin of the range to parse (included)
[in]lastend of the range to parse (excluded)
[in]cba parser callback function of type parser_callback_t which is used to control the deserialization by filtering unwanted values (optional)
Returns
result of the deserialization
Complexity\n Linear in the length of the input. The parser is a predictive
LL(1) parser. The complexity can be higher if the parser callback function cb has a super-linear complexity.
Note
A UTF-8 byte order mark is silently ignored.
Example\n The example below demonstrates the parse() function reading
from an iterator range.
1#include <json.hpp>
2
3using json = nlohmann::json;
4
5int main()
6{
7 // a JSON text given as std::vector
8 std::vector<uint8_t> text = {'[', '1', ',', '2', ',', '3', ']', '\0'};
9
10 // parse and serialize JSON
12 std::cout << std::setw(4) << j_complete << "\n\n";
13}
iterator begin() noexcept
returns an iterator to the first element
Definition json.hpp:4670
iterator end() noexcept
returns an iterator to one past the last element
Definition json.hpp:4741
static basic_json array(std::initializer_list< basic_json > init=std::initializer_list< basic_json >())
explicitly create an array from an initializer list
Definition json.hpp:2165
static basic_json parse(T(&array)[N], const parser_callback_t cb=nullptr)
deserialize from an array
Definition json.hpp:6284
a class to store JSON values
Definition json.hpp:1040
basic_json<> json
default JSON class
Definition json.hpp:12369

Output (play with this example online):
[
    1,
    2,
    3
]


The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/parse__iteratortype__parser_callback_t.cpp -o parse__iteratortype__parser_callback_t 
Since
version 2.0.3

Definition at line 6412 of file json.hpp.