TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ 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

construct a JSON container given an iterator range

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.

@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

@liveexample{The example below shows several ways to create JSON values by specifying a subrange with iterators.,basic_json__InputIt_InputIt}

Since
version 1.0.0

Definition at line 19755 of file json.hpp.

19756 {
19757 JSON_ASSERT(first.m_object != nullptr);
19758 JSON_ASSERT(last.m_object != nullptr);
19759
19760 // make sure iterator fits the current value
19761 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
19762 {
19763 JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", basic_json()));
19764 }
19765
19766 // copy type from first iterator
19767 m_type = first.m_object->m_type;
19768
19769 // check if iterator range is complete for primitive values
19770 switch (m_type)
19771 {
19772 case value_t::boolean:
19776 case value_t::string:
19777 {
19778 if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
19779 || !last.m_it.primitive_iterator.is_end()))
19780 {
19781 JSON_THROW(invalid_iterator::create(204, "iterators out of range", *first.m_object));
19782 }
19783
19784 break;
19785 }
19786
19787 case value_t::null:
19788 case value_t::object:
19789 case value_t::array:
19790 case value_t::binary:
19791 case value_t::discarded:
19792 default:
19793 break;
19794 }
19795
19796 switch (m_type)
19797 {
19799 {
19800 m_value.number_integer = first.m_object->m_value.number_integer;
19801 break;
19802 }
19803
19805 {
19806 m_value.number_unsigned = first.m_object->m_value.number_unsigned;
19807 break;
19808 }
19809
19811 {
19812 m_value.number_float = first.m_object->m_value.number_float;
19813 break;
19814 }
19815
19816 case value_t::boolean:
19817 {
19818 m_value.boolean = first.m_object->m_value.boolean;
19819 break;
19820 }
19821
19822 case value_t::string:
19823 {
19824 m_value = *first.m_object->m_value.string;
19825 break;
19826 }
19827
19828 case value_t::object:
19829 {
19830 m_value.object = create<object_t>(first.m_it.object_iterator,
19831 last.m_it.object_iterator);
19832 break;
19833 }
19834
19835 case value_t::array:
19836 {
19837 m_value.array = create<array_t>(first.m_it.array_iterator,
19838 last.m_it.array_iterator);
19839 break;
19840 }
19841
19842 case value_t::binary:
19843 {
19844 m_value = *first.m_object->m_value.binary;
19845 break;
19846 }
19847
19848 case value_t::null:
19849 case value_t::discarded:
19850 default:
19851 JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), *first.m_object));
19852 }
19853
19854 set_parents();
19856 }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:19164
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
Definition: json.hpp:18935
json_value m_value
the value of the current element
Definition: json.hpp:24928
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)