|
◆ basic_json() [7/9]
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 |
|
) |
| |
|
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
-
- Parameters
-
[in] | first | begin of the range to copy from (included) |
[in] | last | end 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.201 | if 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.204 | if 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.206 | if 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.
2 #include <nlohmann/json.hpp>
9 json j_array = { "alpha", "bravo", "charly", "delta", "easy"};
11 json j_object = {{ "one", "eins"}, { "two", "zwei"}};
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"));
19 std::cout << j_array_range << '\n';
20 std::cout << j_number_range << '\n';
21 std::cout << j_object_range << '\n';
26 json j_invalid(j_number.begin() + 1, j_number.end());
30 std::cout << e.what() << '\n';
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 withg++ -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 16225 of file json.hpp.
|