JSON for Modern C++ 3.10.4

◆ basic_json() [7/10]

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, class BinaryType = std::vector<std::uint8_t>>
template<class InputIT , typename std::enable_if< std::is_same< InputIT, typename basic_json_t::iterator >::value||std::is_same< InputIT, typename basic_json_t::const_iterator >::value, int >::type = 0>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::basic_json ( InputIT  first,
InputIT  last 
)
inline

Constructs the JSON value with the contents of the range [first, last). The semantics depends on the different types a JSON value can have:

  • In case of a null type, invalid_iterator.206 is thrown.
  • In case of other primitive types (number, boolean, or string), first must be begin() and last must be end(). In this case, the value is copied. Otherwise, invalid_iterator.204 is thrown.
  • In case of structured types (array, object), the constructor behaves as similar versions for std::vector or std::map; that is, a JSON array or object is constructed from the values in the range.
Template Parameters
InputITan input iterator type (iterator or const_iterator)
Parameters
[in]firstbegin of the range to copy from (included)
[in]lastend of the range to copy from (excluded)
Precondition
Iterators first and last must be initialized. This precondition is enforced with an assertion (see warning). If assertions are switched off, a violation of this precondition yields undefined behavior.
Range [first, last) is valid. Usually, this precondition cannot be checked efficiently. Only certain edge cases are detected; see the description of the exceptions below. A violation of this precondition yields undefined behavior.
Warning
A precondition is enforced with a runtime assertion that will result in calling std::abort if this precondition is not met. Assertions can be disabled by defining NDEBUG at compile time. See https://en.cppreference.com/w/cpp/error/assert for more information.
Exceptions
invalid_iterator.201if iterators first and last are not compatible (i.e., do not belong to the same JSON value). In this case, the range [first, last) is undefined.
invalid_iterator.204if iterators first and last belong to a primitive type (number, boolean, or string), but first does not point to the first element any more. In this case, the range [first, last) is undefined. See example code below.
invalid_iterator.206if iterators first and last belong to a null value. In this case, the range [first, last) is undefined.
Complexity
Linear in distance between first and last.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
Example
The example below shows several ways to create JSON values by specifying a subrange with iterators.
1#include <iostream>
2#include <nlohmann/json.hpp>
3
4using json = nlohmann::json;
5
6int main()
7{
8 // create JSON values
9 json j_array = {"alpha", "bravo", "charly", "delta", "easy"};
10 json j_number = 42;
11 json j_object = {{"one", "eins"}, {"two", "zwei"}};
12
13 // create copies using iterators
14 json j_array_range(j_array.begin() + 1, j_array.end() - 2);
15 json j_number_range(j_number.begin(), j_number.end());
16 json j_object_range(j_object.begin(), j_object.find("two"));
17
18 // serialize the values
19 std::cout << j_array_range << '\n';
20 std::cout << j_number_range << '\n';
21 std::cout << j_object_range << '\n';
22
23 // example for an exception
24 try
25 {
26 json j_invalid(j_number.begin() + 1, j_number.end());
27 }
28 catch (json::invalid_iterator& e)
29 {
30 std::cout << e.what() << '\n';
31 }
32}
detail::invalid_iterator invalid_iterator
exception indicating errors with iterators
Definition: json.hpp:17767
basic_json<> json
default JSON class
Definition: json.hpp:3472

Output (play with this example online):
["bravo","charly"]
42
{"one":"eins"}
[json.exception.invalid_iterator.204] iterators out of range
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/basic_json__InputIt_InputIt.cpp -o basic_json__InputIt_InputIt 
Since
version 1.0.0

Definition at line 19554 of file json.hpp.