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

◆ basic_json() [6/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>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::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 primitive types (number, boolean, or string), first must be begin() and last must be end(). In this case, the value is copied. Otherwise, std::out_of_range is thrown.
  • In case of structured types (array, object), the constructor behaves as similar versions for std::vector.
  • In case of a null type, std::domain_error is thrown.
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.
Exceptions
std::domain_errorif iterators are not compatible; that is, do not belong to the same JSON value; example: "iterators are not compatible"
std::out_of_rangeif iterators are for a primitive type (number, boolean, or string) where an out of range error can be detected easily; example: "iterators out of range"
std::bad_allocif allocation for object, array, or string fails
std::domain_errorif called with a null value; example: "cannot use construct with iterators from null"
Complexity\n Linear in distance between first and last.
Example\n The example below shows several ways to create JSON values by
specifying a subrange with iterators.
1#include <json.hpp>
2
3using json = nlohmann::json;
4
5int main()
6{
7 // create JSON values
8 json j_array = {"alpha", "bravo", "charly", "delta", "easy"};
9 json j_number = 42;
10 json j_object = {{"one", "eins"}, {"two", "zwei"}};
11
12 // create copies using iterators
16
17 // serialize the values
18 std::cout << j_array_range << '\n';
19 std::cout << j_number_range << '\n';
20 std::cout << j_object_range << '\n';
21}
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
iterator find(typename object_t::key_type key)
find an element in a JSON object
Definition json.hpp:4581
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):
["bravo","charly"]
42
{"one":"eins"}

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

Definition at line 2276 of file json.hpp.