JSON for Modern C++  3.7.0

◆ basic_json() [7/9]

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 InputIT , typename std::enable_if< std::is_same< InputIT, typename basic_json_t::iterator >::value or std::is_same< InputIT, typename basic_json_t::const_iterator >::value, int >::type = 0>
nlohmann::basic_json::basic_json ( InputIT  first,
InputIT  last 

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)
[in]firstbegin of the range to copy from (included)
[in]lastend of the range to copy from (excluded)
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.
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.
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.
Linear in distance between first and last.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
The example below shows several ways to create JSON values by specifying a subrange with iterators.
1 #include <iostream>
2 #include <nlohmann/json.hpp>
4 using json = nlohmann::json;
6 int 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"}};
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"));
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';
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 }
basic_json<> json
default JSON class
Definition: json.hpp:2355
detail::invalid_iterator invalid_iterator
exception indicating errors with iterators
Definition: json.hpp:14641

Output (play with this example online):
[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 
version 1.0.0

Definition at line 16041 of file json.hpp.