TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ basic_json() [5/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>>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::basic_json ( initializer_list_t  init,
bool  type_deduction = true,
value_t  manual_type = value_t::array 
)
inline

create a container (array or object) from an initializer list

Creates a JSON value of type array or object from the passed initializer list init. In case type_deduction is true (default), the type of the JSON value to be created is deducted from the initializer list init according to the following rules:

  1. If the list is empty, an empty JSON object value {} is created.
  2. If the list consists of pairs whose first element is a string, a JSON object value is created where the first elements of the pairs are treated as keys and the second elements are as values.
  3. In all other cases, an array is created.

The rules aim to create the best fit between a C++ initializer list and JSON values. The rationale is as follows:

  1. The empty initializer list is written as {} which is exactly an empty JSON object.
  2. C++ has no way of describing mapped types other than to list a list of pairs. As JSON requires that keys must be of type string, rule 2 is the weakest constraint one can pose on initializer lists to interpret them as an object.
  3. In all other cases, the initializer list could not be interpreted as JSON object type, so interpreting it as JSON array type is safe.

With the rules described above, the following JSON values cannot be expressed by an initializer list:

Note
When used without parentheses around an empty initializer list, basic_json() is called instead of this function, yielding the JSON null value.
Parameters
[in]initinitializer list with JSON values
[in]type_deductioninternal parameter; when set to true, the type of the JSON value is deducted from the initializer list init; when set to false, the type provided via manual_type is forced. This mode is used by the functions array(initializer_list_t) and object(initializer_list_t).
[in]manual_typeinternal parameter; when type_deduction is set to false, the created JSON value will use the provided type (only value_t::array and value_t::object are valid); when type_deduction is set to true, this parameter has no effect
Exceptions
type_error.301if type_deduction is false, manual_type is value_t::object, but init contains an element which is not a pair whose first element is a string. In this case, the constructor could not create an object. If type_deduction would have be true, an array would have been created. See object(initializer_list_t) for an example.

@complexity Linear in the size of the initializer list init.

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

@liveexample{The example below shows how JSON values are created from initializer lists.,basic_json__list_init_t}

See also
see array(initializer_list_t)create a JSON array value from an initializer list
see object(initializer_list_t)create a JSON object value from an initializer list
Since
version 1.0.0

Definition at line 19433 of file json.hpp.

19436 {
19437 // check if each element is an array with two elements whose first
19438 // element is a string
19439 bool is_an_object = std::all_of(init.begin(), init.end(),
19440 [](const detail::json_ref<basic_json> &element_ref)
19441 {
19442 return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
19443 });
19444
19445 // adjust type if type deduction is not wanted
19446 if (!type_deduction)
19447 {
19448 // if array is wanted, do not create an object though possible
19449 if (manual_type == value_t::array)
19450 {
19451 is_an_object = false;
19452 }
19453
19454 // if object is wanted but impossible, throw an exception
19455 if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
19456 {
19457 JSON_THROW(type_error::create(301, "cannot create object from initializer list", basic_json()));
19458 }
19459 }
19460
19461 if (is_an_object)
19462 {
19463 // the initializer list is a list of pairs -> create object
19464 m_type = value_t::object;
19466
19467 for (auto &element_ref : init)
19468 {
19469 auto element = element_ref.moved_or_copied();
19470 m_value.object->emplace(
19471 std::move(*((*element.m_value.array)[0].m_value.string)),
19472 std::move((*element.m_value.array)[1]));
19473 }
19474 }
19475
19476 else
19477 {
19478 // the initializer list describes an array -> create array
19479 m_type = value_t::array;
19480 m_value.array = create<array_t>(init.begin(), init.end());
19481 }
19482
19483 set_parents();
19485 }
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
@ object
object (unordered set of name/value pairs)
@ array
array (ordered collection of values)