29 #ifndef NLOHMANN_JSON_HPP 30 #define NLOHMANN_JSON_HPP 42 #include <forward_list> 44 #include <initializer_list> 56 #include <type_traits> 61 #if defined(__clang__) 62 #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 63 #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" 65 #elif defined(__GNUC__) 66 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900 72 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) 73 #pragma GCC diagnostic push 74 #pragma GCC diagnostic ignored "-Wfloat-equal" 78 #if defined(__clang__) 79 #pragma GCC diagnostic push 80 #pragma GCC diagnostic ignored "-Wdocumentation" 84 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) 85 #define JSON_DEPRECATED __attribute__((deprecated)) 86 #elif defined(_MSC_VER) 87 #define JSON_DEPRECATED __declspec(deprecated) 89 #define JSON_DEPRECATED 93 #if not defined(JSON_NOEXCEPTION) || defined(__EXCEPTIONS) 94 #define JSON_THROW(exception) throw exception 96 #define JSON_CATCH(exception) catch(exception) 98 #define JSON_THROW(exception) std::abort() 99 #define JSON_TRY if(true) 100 #define JSON_CATCH(exception) if(false) 173 static constexpr std::array<uint8_t, 8> order = {{
191 return order[
static_cast<std::size_t
>(lhs)] <
192 order[static_cast<std::size_t>(rhs)];
201 template<
bool B,
typename T =
void>
210 std::integral_constant<bool, std::is_convertible<T, int>::value and
211 std::is_enum<T>::value>;
228 template<
class B1,
class... Bn>
229 struct conjunction<B1, Bn...> : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
231 template<
class B>
struct negation : std::integral_constant < bool, !B::value > {};
247 template<
typename BasicJsonType>
248 static void construct(BasicJsonType& j,
typename BasicJsonType::boolean_t
b) noexcept
252 j.assert_invariant();
259 template<
typename BasicJsonType>
260 static void construct(BasicJsonType& j,
const typename BasicJsonType::string_t&
s)
264 j.assert_invariant();
271 template<
typename BasicJsonType>
272 static void construct(BasicJsonType& j,
typename BasicJsonType::number_float_t val) noexcept
275 if (not std::isfinite(val))
284 j.assert_invariant();
291 template<
typename BasicJsonType>
292 static void construct(BasicJsonType& j,
typename BasicJsonType::number_unsigned_t val) noexcept
296 j.assert_invariant();
303 template<
typename BasicJsonType>
304 static void construct(BasicJsonType& j,
typename BasicJsonType::number_integer_t val) noexcept
308 j.assert_invariant();
315 template<
typename BasicJsonType>
316 static void construct(BasicJsonType& j,
const typename BasicJsonType::array_t& arr)
320 j.assert_invariant();
323 template<
typename BasicJsonType,
typename CompatibleArrayType,
325 typename BasicJsonType::array_t>::value,
327 static void construct(BasicJsonType& j,
const CompatibleArrayType& arr)
332 j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
333 j.assert_invariant();
340 template<
typename BasicJsonType>
341 static void construct(BasicJsonType& j,
const typename BasicJsonType::object_t& obj)
345 j.assert_invariant();
348 template<
typename BasicJsonType,
typename CompatibleObjectType,
350 typename BasicJsonType::object_t>::value,
352 static void construct(BasicJsonType& j,
const CompatibleObjectType& obj)
358 j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
359 j.assert_invariant();
378 #define NLOHMANN_JSON_HAS_HELPER(type) \ 379 template<typename T> struct has_##type { \ 381 template<typename U, typename = typename U::type> \ 382 static int detect(U &&); \ 383 static void detect(...); \ 385 static constexpr bool value = \ 386 std::is_integral<decltype(detect(std::declval<T>()))>::value; \ 394 #undef NLOHMANN_JSON_HAS_HELPER 397 template<
bool B,
class RealType,
class CompatibleObjectType>
400 template<
class RealType,
class CompatibleObjectType>
403 static constexpr
auto value =
404 std::is_constructible<
typename RealType::key_type,
405 typename CompatibleObjectType::key_type>::value and
406 std::is_constructible<
typename RealType::mapped_type,
407 typename CompatibleObjectType::mapped_type>::value;
410 template<
class BasicJsonType,
class CompatibleObjectType>
415 has_mapped_type<CompatibleObjectType>,
416 has_key_type<CompatibleObjectType>>::value,
417 typename BasicJsonType::object_t, CompatibleObjectType >::value;
420 template<
typename BasicJsonType,
typename T>
423 static auto constexpr value = std::is_same<T, typename BasicJsonType::iterator>::value or
424 std::is_same<T, typename BasicJsonType::const_iterator>::value or
425 std::is_same<T, typename BasicJsonType::reverse_iterator>::value or
426 std::is_same<T, typename BasicJsonType::const_reverse_iterator>::value or
427 std::is_same<T, typename BasicJsonType::json_pointer>::value;
430 template<
class BasicJsonType,
class CompatibleArrayType>
433 static auto constexpr value =
436 BasicJsonType, CompatibleArrayType>>,
437 negation<std::is_constructible<
typename BasicJsonType::string_t,
438 CompatibleArrayType>>,
440 has_value_type<CompatibleArrayType>,
441 has_iterator<CompatibleArrayType>>::value;
444 template<
bool,
typename,
typename>
447 template<
typename RealIntegerType,
typename CompatibleNumberIntegerType>
454 static constexpr
auto value =
455 std::is_constructible<RealIntegerType,
456 CompatibleNumberIntegerType>::value and
457 CompatibleLimits::is_integer and
458 RealLimits::is_signed == CompatibleLimits::is_signed;
461 template<
typename RealIntegerType,
typename CompatibleNumberIntegerType>
464 static constexpr
auto value =
466 std::is_integral<CompatibleNumberIntegerType>::value and
467 not std::is_same<bool, CompatibleNumberIntegerType>::value,
468 RealIntegerType, CompatibleNumberIntegerType > ::value;
473 template<
typename BasicJsonType,
typename T>
479 std::declval<BasicJsonType>(), std::declval<T&>()))>::value>>
480 static int detect(U&&);
481 static void detect(...);
484 static constexpr
bool value = std::is_integral<decltype(
485 detect(std::declval<
typename BasicJsonType::template json_serializer<T, void>>()))>::value;
490 template<
typename BasicJsonType,
typename T>
498 static int detect(U&&);
499 static void detect(...);
502 static constexpr
bool value = std::is_integral<decltype(detect(
503 std::declval<
typename BasicJsonType::template json_serializer<T, void>>()))>::value;
507 template<
typename BasicJsonType,
typename T>
512 std::declval<BasicJsonType&>(), std::declval<T>()))>
513 static int detect(U&&);
514 static void detect(...);
517 static constexpr
bool value = std::is_integral<decltype(detect(
518 std::declval<
typename BasicJsonType::template json_serializer<T, void>>()))>::value;
526 template<
typename BasicJsonType,
typename T,
enable_if_t<
527 std::is_same<T, typename BasicJsonType::boolean_t>::value,
int> = 0>
533 template<
typename BasicJsonType,
typename CompatibleString,
534 enable_if_t<std::is_constructible<
typename BasicJsonType::string_t,
535 CompatibleString>::value,
int> = 0>
536 void to_json(BasicJsonType& j,
const CompatibleString&
s)
541 template<
typename BasicJsonType,
typename FloatType,
543 void to_json(BasicJsonType& j, FloatType val) noexcept
549 typename BasicJsonType,
typename CompatibleNumberUnsignedType,
551 CompatibleNumberUnsignedType>::value,
int> = 0 >
552 void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
558 typename BasicJsonType,
typename CompatibleNumberIntegerType,
560 CompatibleNumberIntegerType>::value,
int> = 0 >
561 void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
566 template<
typename BasicJsonType,
typename UnscopedEnumType,
568 void to_json(BasicJsonType& j, UnscopedEnumType
e) noexcept
574 typename BasicJsonType,
typename CompatibleArrayType,
577 std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value,
579 void to_json(BasicJsonType& j,
const CompatibleArrayType& arr)
585 typename BasicJsonType,
typename CompatibleObjectType,
588 void to_json(BasicJsonType& j,
const CompatibleObjectType& arr)
599 template<
typename BasicJsonType,
typename ArithmeticType,
601 not std::is_same<ArithmeticType,
602 typename BasicJsonType::boolean_t>::value,
606 switch (static_cast<value_t>(j))
610 val =
static_cast<ArithmeticType
>(
611 *j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
616 val =
static_cast<ArithmeticType
>(
617 *j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
622 val =
static_cast<ArithmeticType
>(
623 *j.template get_ptr<const typename BasicJsonType::number_float_t*>());
629 std::domain_error(
"type must be number, but is " + j.type_name()));
634 template<
typename BasicJsonType>
635 void from_json(
const BasicJsonType& j,
typename BasicJsonType::boolean_t&
b)
637 if (not j.is_boolean())
639 JSON_THROW(std::domain_error(
"type must be boolean, but is " + j.type_name()));
641 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
644 template<
typename BasicJsonType>
645 void from_json(
const BasicJsonType& j,
typename BasicJsonType::string_t&
s)
647 if (not j.is_string())
649 JSON_THROW(std::domain_error(
"type must be string, but is " + j.type_name()));
651 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
654 template<
typename BasicJsonType>
655 void from_json(
const BasicJsonType& j,
typename BasicJsonType::number_float_t& val)
660 template<
typename BasicJsonType>
661 void from_json(
const BasicJsonType& j,
typename BasicJsonType::number_unsigned_t& val)
666 template<
typename BasicJsonType>
667 void from_json(
const BasicJsonType& j,
typename BasicJsonType::number_integer_t& val)
672 template<
typename BasicJsonType,
typename UnscopedEnumType,
673 enable_if_t<is_unscoped_enum<UnscopedEnumType>::value,
int> = 0>
678 e =
static_cast<UnscopedEnumType
>(val);
681 template<
typename BasicJsonType>
682 void from_json(
const BasicJsonType& j,
typename BasicJsonType::array_t& arr)
684 if (not j.is_array())
686 JSON_THROW(std::domain_error(
"type must be array, but is " + j.type_name()));
688 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
692 template<
typename BasicJsonType,
typename T,
typename Allocator>
693 void from_json(
const BasicJsonType& j, std::forward_list<T, Allocator>&
l)
699 JSON_THROW(std::domain_error(
"type must be array, but is " + j.type_name()));
701 if (not std::is_same<T, BasicJsonType>::value)
703 if (not j.is_array())
705 JSON_THROW(std::domain_error(
"type must be array, but is " + j.type_name()));
708 for (
auto it = j.rbegin(), end = j.rend(); it != end; ++it)
710 l.push_front(it->template get<T>());
714 template<
typename BasicJsonType,
typename CompatibleArrayType>
720 std::transform(j.begin(), j.end(),
721 std::inserter(arr, end(arr)), [](
const BasicJsonType & i)
725 return i.template get<typename CompatibleArrayType::value_type>();
729 template<
typename BasicJsonType,
typename CompatibleArrayType>
732 arr.reserve(std::declval<typename CompatibleArrayType::size_type>()),
738 arr.reserve(j.size());
740 j.begin(), j.end(), std::inserter(arr, end(arr)), [](
const BasicJsonType & i)
744 return i.template get<typename CompatibleArrayType::value_type>();
748 template<
typename BasicJsonType,
typename CompatibleArrayType,
750 not std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value,
int> = 0>
751 void from_json(
const BasicJsonType& j, CompatibleArrayType& arr)
755 JSON_THROW(std::domain_error(
"type must be array, but is " + j.type_name()));
759 if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
761 if (not j.is_array())
763 JSON_THROW(std::domain_error(
"type must be array, but is " + j.type_name()));
769 template<
typename BasicJsonType,
typename CompatibleObjectType,
770 enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value,
int> = 0>
771 void from_json(
const BasicJsonType& j, CompatibleObjectType& obj)
773 if (not j.is_object())
775 JSON_THROW(std::domain_error(
"type must be object, but is " + j.type_name()));
778 auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
784 obj = CompatibleObjectType(begin(*inner_object), end(*inner_object));
791 template<
typename BasicJsonType,
typename ArithmeticType,
793 std::is_arithmetic<ArithmeticType>::value and
794 not std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value and
795 not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
796 not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
797 not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
799 void from_json(
const BasicJsonType& j, ArithmeticType& val)
801 switch (static_cast<value_t>(j))
805 val =
static_cast<ArithmeticType
>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
810 val =
static_cast<ArithmeticType
>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
815 val =
static_cast<ArithmeticType
>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
820 val =
static_cast<ArithmeticType
>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
825 JSON_THROW(std::domain_error(
"type must be number, but is " + j.type_name()));
833 template<
typename BasicJsonType,
typename T>
835 -> decltype(
to_json(j, std::forward<T>(val)),
void())
837 return to_json(j, std::forward<T>(val));
840 template<
typename BasicJsonType,
typename T>
843 static_assert(
sizeof(BasicJsonType) == 0,
844 "could not find to_json() method in T's namespace");
848 template<
typename BasicJsonType,
typename T>
850 noexcept(noexcept(std::declval<to_json_fn>().call(j, std::forward<T>(val),
priority_tag<1> {})))
859 template<
typename BasicJsonType,
typename T>
867 template<
typename BasicJsonType,
typename T>
870 static_assert(
sizeof(BasicJsonType) == 0,
871 "could not find from_json() method in T's namespace");
875 template<
typename BasicJsonType,
typename T>
877 noexcept(noexcept(std::declval<from_json_fn>().call(j, val,
priority_tag<1> {})))
887 static constexpr T value{};
910 template<
typename =
void,
typename =
void>
922 template<
typename BasicJsonType,
typename ValueType>
923 static void from_json(BasicJsonType&& j, ValueType& val) noexcept(
938 template<
typename BasicJsonType,
typename ValueType>
939 static void to_json(BasicJsonType& j, ValueType&& val) noexcept(
1029 template<
typename U,
typename V,
typename... Args>
class ObjectType = std::map,
1030 template<
typename U,
typename... Args>
class ArrayType = std::vector,
1032 class BooleanType = bool,
1033 class NumberIntegerType = std::int64_t,
1034 class NumberUnsignedType = std::uint64_t,
1035 class NumberFloatType = double,
1036 template<
typename U>
class AllocatorType = std::allocator,
1037 template<
typename T,
typename SFINAE =
void>
class JSONSerializer =
adl_serializer 1045 BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType,
1046 AllocatorType, JSONSerializer>;
1054 template<
typename T,
typename SFINAE>
1083 using pointer =
typename std::allocator_traits<allocator_type>::pointer;
1085 using const_pointer =
typename std::allocator_traits<allocator_type>::const_pointer;
1134 result[
"copyright"] =
"(C) 2013-2017 Niels Lohmann";
1135 result[
"name"] =
"JSON for Modern C++";
1136 result[
"url"] =
"https://github.com/nlohmann/json";
1139 {
"string",
"2.1.1"},
1146 result[
"platform"] =
"win32";
1147 #elif defined __linux__ 1148 result[
"platform"] =
"linux";
1149 #elif defined __APPLE__ 1150 result[
"platform"] =
"apple";
1151 #elif defined __unix__ 1152 result[
"platform"] =
"unix";
1154 result[
"platform"] =
"unknown";
1157 #if defined(__clang__) 1158 result[
"compiler"] = {{
"family",
"clang"}, {
"version", __clang_version__}};
1159 #elif defined(__ICC) || defined(__INTEL_COMPILER) 1160 result[
"compiler"] = {{
"family",
"icc"}, {
"version", __INTEL_COMPILER}};
1161 #elif defined(__GNUC__) || defined(__GNUG__) 1162 result[
"compiler"] = {{
"family",
"gcc"}, {
"version", std::to_string(__GNUC__) +
"." + std::to_string(__GNUC_MINOR__) +
"." + std::to_string(__GNUC_PATCHLEVEL__)}};
1163 #elif defined(__HP_cc) || defined(__HP_aCC) 1164 result[
"compiler"] =
"hp" 1165 #elif defined(__IBMCPP__) 1166 result[
"compiler"] = {{
"family",
"ilecpp"}, {
"version", __IBMCPP__}};
1167 #elif defined(_MSC_VER) 1168 result[
"compiler"] = {{
"family",
"msvc"}, {
"version", _MSC_VER}};
1169 #elif defined(__PGI) 1170 result[
"compiler"] = {{
"family",
"pgcpp"}, {
"version", __PGI}};
1171 #elif defined(__SUNPRO_CC) 1172 result[
"compiler"] = {{
"family",
"sunpro"}, {
"version", __SUNPRO_CC}};
1174 result[
"compiler"] = {{
"family",
"unknown"}, {
"version",
"unknown"}};
1178 result[
"compiler"][
"c++"] = std::to_string(__cplusplus);
1180 result[
"compiler"][
"c++"] =
"unknown";
1278 using object_t = ObjectType<StringType,
1280 std::less<StringType>,
1281 AllocatorType<std::pair<
const StringType,
1328 using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
1625 template<
typename T,
typename... Args>
1628 AllocatorType<T> alloc;
1629 auto deleter = [&](T *
object)
1631 alloc.deallocate(
object, 1);
1633 std::unique_ptr<T, decltype(deleter)>
object(alloc.allocate(1), deleter);
1634 alloc.construct(
object.
get(), std::forward<Args>(args)...);
1635 assert(
object !=
nullptr);
1636 return object.release();
1701 object = create<object_t>();
1707 array = create<array_t>();
1713 string = create<string_t>(
"");
1750 JSON_THROW(std::domain_error(
"961c151d2e87f2686a955a9be24d316f1362bf21 2.1.1"));
1760 string = create<string_t>(value);
1766 object = create<object_t>(value);
1772 array = create<array_t>(value);
1877 basic_json& parsed)>;
1917 : m_type(value_type), m_value(value_type)
1999 template<
typename CompatibleType,
typename U = detail::uncvref_t<CompatibleType>,
2000 detail::enable_if_t<not std::is_base_of<std::istream, U>::value and
2001 not std::is_same<U, basic_json_t>::value and
2002 not detail::is_basic_json_nested_type<
2003 basic_json_t, U>::value and
2004 detail::has_to_json<basic_json, U>::value,
2007 std::declval<basic_json_t&>(), std::forward<CompatibleType>(val))))
2083 bool type_deduction =
true,
2088 bool is_an_object = std::all_of(init.begin(), init.end(),
2091 return element.is_array() and element.size() == 2 and element[0].is_string();
2095 if (not type_deduction)
2100 is_an_object =
false;
2106 JSON_THROW(std::domain_error(
"cannot create object from initializer list"));
2116 std::for_each(init.begin(), init.end(), [
this](
const basic_json & element)
2118 m_value.object->emplace(*(element[0].m_value.string), element[1]);
2125 m_value.array = create<array_t>(
init);
2166 std::initializer_list<basic_json>())
2206 std::initializer_list<basic_json>())
2232 m_value.array = create<array_t>(
cnt, val);
2273 template<
class InputIT,
typename std::enable_if<
2274 std::is_same<InputIT, typename basic_json_t::iterator>::value or
2275 std::is_same<InputIT, typename basic_json_t::const_iterator>::value,
int>::type = 0>
2278 assert(first.m_object !=
nullptr);
2279 assert(last.m_object !=
nullptr);
2282 if (first.m_object != last.m_object)
2284 JSON_THROW(std::domain_error(
"iterators are not compatible"));
2288 m_type = first.m_object->m_type;
2299 if (not first.m_it.primitive_iterator.is_begin() or not last.m_it.primitive_iterator.is_end())
2301 JSON_THROW(std::out_of_range(
"iterators out of range"));
2316 m_value.number_integer = first.m_object->m_value.number_integer;
2322 m_value.number_unsigned = first.m_object->m_value.number_unsigned;
2328 m_value.number_float = first.m_object->m_value.number_float;
2334 m_value.boolean = first.m_object->m_value.boolean;
2340 m_value = *first.m_object->m_value.string;
2346 m_value.object = create<object_t>(first.m_it.object_iterator,
2347 last.m_it.object_iterator);
2353 m_value.array = create<array_t>(first.m_it.array_iterator,
2354 last.m_it.array_iterator);
2360 JSON_THROW(std::domain_error(
"cannot use construct with iterators from " + first.m_object->type_name()));
2429 : m_type(other.m_type)
2506 : m_type(std::move(other.m_type)),
2507 m_value(std::move(other.m_value))
2510 other.assert_invariant();
2543 std::is_nothrow_move_constructible<value_t>::value and
2544 std::is_nothrow_move_assignable<value_t>::value and
2545 std::is_nothrow_move_constructible<json_value>::value and
2546 std::is_nothrow_move_assignable<json_value>::value
2553 swap(m_type, other.m_type);
2554 swap(m_value, other.m_value);
2583 AllocatorType<object_t> alloc;
2584 alloc.destroy(m_value.object);
2585 alloc.deallocate(m_value.object, 1);
2591 AllocatorType<array_t> alloc;
2592 alloc.destroy(m_value.array);
2593 alloc.deallocate(m_value.array, 1);
2599 AllocatorType<string_t> alloc;
2600 alloc.destroy(m_value.string);
2601 alloc.deallocate(m_value.string, 1);
2649 std::stringstream ss;
2653 dump(ss,
true, static_cast<unsigned int>(indent));
2713 return is_null() or is_string() or is_boolean() or is_number();
2740 return is_array() or is_object();
2814 return is_number_integer() or is_number_float();
3030 return m_value.boolean;
3033 JSON_THROW(std::domain_error(
"type must be boolean, but is " + type_name()));
3039 return is_object() ? m_value.object :
nullptr;
3045 return is_object() ? m_value.object :
nullptr;
3051 return is_array() ? m_value.array :
nullptr;
3057 return is_array() ? m_value.array :
nullptr;
3063 return is_string() ? m_value.string :
nullptr;
3069 return is_string() ? m_value.string :
nullptr;
3075 return is_boolean() ? &m_value.boolean :
nullptr;
3081 return is_boolean() ? &m_value.boolean :
nullptr;
3087 return is_number_integer() ? &m_value.number_integer :
nullptr;
3093 return is_number_integer() ? &m_value.number_integer :
nullptr;
3099 return is_number_unsigned() ? &m_value.number_unsigned :
nullptr;
3105 return is_number_unsigned() ? &m_value.number_unsigned :
nullptr;
3111 return is_number_float() ? &m_value.number_float :
nullptr;
3117 return is_number_float() ? &m_value.number_float :
nullptr;
3131 template<
typename ReferenceType,
typename ThisType>
3138 auto ptr = obj.template get_ptr<PointerType>();
3145 JSON_THROW(std::domain_error(
"incompatible ReferenceType for get_ref, actual type is " +
3169 typename BasicJsonType,
3218 typename ValueTypeCV,
3221 not std::is_same<basic_json_t, ValueType>::value and
3225 ValueType
get()
const noexcept(noexcept(
3231 static_assert(not std::is_reference<ValueTypeCV>::value,
3232 "get() cannot be used with reference types, you might want to use get_ref()");
3233 static_assert(std::is_default_constructible<ValueType>::value,
3234 "types must be DefaultConstructible when used with get()");
3273 typename ValueTypeCV,
3277 ValueType>::value,
int> = 0 >
3278 ValueType
get()
const noexcept(noexcept(
3281 static_assert(not std::is_reference<ValueTypeCV>::value,
3282 "get() cannot be used with reference types, you might want to use get_ref()");
3313 template<
typename PointerType,
typename std::enable_if<
3314 std::is_pointer<PointerType>::value,
int>::type = 0>
3315 PointerType
get() noexcept
3318 return get_ptr<PointerType>();
3325 template<
typename PointerType,
typename std::enable_if<
3326 std::is_pointer<PointerType>::value,
int>::type = 0>
3327 constexpr
const PointerType
get()
const noexcept
3330 return get_ptr<PointerType>();
3359 template<
typename PointerType,
typename std::enable_if<
3360 std::is_pointer<PointerType>::value,
int>::type = 0>
3364 using pointee_t =
typename std::remove_const<
typename 3365 std::remove_pointer<
typename 3369 std::is_same<object_t, pointee_t>::value
3370 or std::is_same<array_t, pointee_t>::value
3371 or std::is_same<string_t, pointee_t>::value
3372 or std::is_same<boolean_t, pointee_t>::value
3373 or std::is_same<number_integer_t, pointee_t>::value
3374 or std::is_same<number_unsigned_t, pointee_t>::value
3375 or std::is_same<number_float_t, pointee_t>::value
3376 ,
"incompatible pointer type");
3379 return get_impl_ptr(static_cast<PointerType>(
nullptr));
3386 template<
typename PointerType,
typename std::enable_if<
3387 std::is_pointer<PointerType>::value and
3389 constexpr
const PointerType
get_ptr() const noexcept
3392 using pointee_t =
typename std::remove_const<
typename 3393 std::remove_pointer<
typename 3397 std::is_same<object_t, pointee_t>::value
3398 or std::is_same<array_t, pointee_t>::value
3399 or std::is_same<string_t, pointee_t>::value
3400 or std::is_same<boolean_t, pointee_t>::value
3401 or std::is_same<number_integer_t, pointee_t>::value
3402 or std::is_same<number_unsigned_t, pointee_t>::value
3403 or std::is_same<number_float_t, pointee_t>::value
3404 ,
"incompatible pointer type");
3407 return get_impl_ptr(static_cast<const PointerType>(
nullptr));
3436 template<
typename ReferenceType,
typename std::enable_if<
3437 std::is_reference<ReferenceType>::value,
int>::type = 0>
3441 return get_ref_impl<ReferenceType>(*this);
3448 template<
typename ReferenceType,
typename std::enable_if<
3449 std::is_reference<ReferenceType>::value and
3454 return get_ref_impl<ReferenceType>(*this);
3485 template <
typename ValueType,
typename std::enable_if <
3486 not std::is_pointer<ValueType>::value and
3487 not std::is_same<ValueType, typename string_t::value_type>::value
3488 #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015 3489 and not std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
3492 operator ValueType()
const 3495 return get<ValueType>();
3538 return m_value.array->at(idx);
3543 JSON_THROW(std::out_of_range(
"array index " + std::to_string(idx) +
" is out of range"));
3548 JSON_THROW(std::domain_error(
"cannot use at() with " + type_name()));
3581 return m_value.array->at(idx);
3586 JSON_THROW(std::out_of_range(
"array index " + std::to_string(idx) +
" is out of range"));
3591 JSON_THROW(std::domain_error(
"cannot use at() with " + type_name()));
3628 return m_value.object->at(key);
3633 JSON_THROW(std::out_of_range(
"key '" + key +
"' not found"));
3638 JSON_THROW(std::domain_error(
"cannot use at() with " + type_name()));
3675 return m_value.object->at(key);
3680 JSON_THROW(std::out_of_range(
"key '" + key +
"' not found"));
3685 JSON_THROW(std::domain_error(
"cannot use at() with " + type_name()));
3720 m_value.array = create<array_t>();
3728 if (idx >= m_value.array->size())
3730 m_value.array->insert(m_value.array->end(),
3731 idx - m_value.array->size() + 1,
3735 return m_value.array->operator[](idx);
3738 JSON_THROW(std::domain_error(
"cannot use operator[] with " + type_name()));
3765 return m_value.
array->operator[](idx);
3768 JSON_THROW(std::domain_error(
"cannot use operator[] with " + type_name()));
3804 m_value.object = create<object_t>();
3811 return m_value.object->operator[](key);
3814 JSON_THROW(std::domain_error(
"cannot use operator[] with " + type_name()));
3852 assert(m_value.object->find(key) != m_value.object->end());
3856 JSON_THROW(std::domain_error(
"cannot use operator[] with " + type_name()));
3886 template<
typename T, std::
size_t n>
3889 return operator[](static_cast<const T>(key));
3921 template<
typename T, std::
size_t n>
3924 return operator[](static_cast<const T>(key));
3954 template<
typename T>
3968 return m_value.object->operator[](key);
3971 JSON_THROW(std::domain_error(
"cannot use operator[] with " + type_name()));
4004 template<
typename T>
4010 assert(m_value.object->find(key) != m_value.object->end());
4014 JSON_THROW(std::domain_error(
"cannot use operator[] with " + type_name()));
4065 template<
class ValueType,
typename std::enable_if<
4066 std::is_convertible<basic_json_t, ValueType>::value,
int>::type = 0>
4067 ValueType
value(
const typename object_t::key_type& key, ValueType default_value)
const 4073 const auto it = find(key);
4079 return default_value;
4083 JSON_THROW(std::domain_error(
"cannot use value() with " + type_name()));
4091 string_t value(
const typename object_t::key_type& key,
const char* default_value)
const 4093 return value(key,
string_t(default_value));
4137 template<
class ValueType,
typename std::enable_if<
4138 std::is_convertible<basic_json_t, ValueType>::value,
int>::type = 0>
4151 return default_value;
4155 JSON_THROW(std::domain_error(
"cannot use value() with " + type_name()));
4164 return value(ptr,
string_t(default_value));
4297 template<
class IteratorType,
typename std::enable_if<
4298 std::is_same<IteratorType, typename basic_json_t::iterator>::value or
4299 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value,
int>::type
4304 if (
this != pos.m_object)
4306 JSON_THROW(std::domain_error(
"iterator does not fit current value"));
4309 IteratorType result = end();
4319 if (not pos.m_it.primitive_iterator.is_begin())
4321 JSON_THROW(std::out_of_range(
"iterator out of range"));
4326 AllocatorType<string_t> alloc;
4327 alloc.destroy(m_value.string);
4328 alloc.deallocate(m_value.string, 1);
4329 m_value.string =
nullptr;
4339 result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
4345 result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
4351 JSON_THROW(std::domain_error(
"cannot use erase() with " + type_name()));
4404 template<
class IteratorType,
typename std::enable_if<
4405 std::is_same<IteratorType, typename basic_json_t::iterator>::value or
4406 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value,
int>::type
4411 if (
this != first.m_object or
this != last.m_object)
4413 JSON_THROW(std::domain_error(
"iterators do not fit current value"));
4416 IteratorType result = end();
4426 if (not first.m_it.primitive_iterator.is_begin() or not last.m_it.primitive_iterator.is_end())
4428 JSON_THROW(std::out_of_range(
"iterators out of range"));
4433 AllocatorType<string_t> alloc;
4434 alloc.destroy(m_value.string);
4435 alloc.deallocate(m_value.string, 1);
4436 m_value.string =
nullptr;
4446 result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
4447 last.m_it.object_iterator);
4453 result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
4454 last.m_it.array_iterator);
4460 JSON_THROW(std::domain_error(
"cannot use erase() with " + type_name()));
4501 return m_value.object->erase(key);
4504 JSON_THROW(std::domain_error(
"cannot use erase() with " + type_name()));
4538 JSON_THROW(std::out_of_range(
"array index " + std::to_string(idx) +
" is out of range"));
4541 m_value.array->erase(m_value.array->begin() +
static_cast<difference_type>(idx));
4545 JSON_THROW(std::domain_error(
"cannot use erase() with " + type_name()));
4583 auto result = end();
4587 result.m_it.object_iterator = m_value.object->find(key);
4599 auto result = cend();
4603 result.m_it.object_iterator = m_value.object->find(key);
4633 return is_object() ? m_value.object->count(key) : 0;
5008 return m_value.array->empty();
5014 return m_value.object->empty();
5076 return m_value.array->size();
5082 return m_value.object->size();
5136 return m_value.array->max_size();
5142 return m_value.object->max_size();
5191 m_value.number_integer = 0;
5197 m_value.number_unsigned = 0;
5203 m_value.number_float = 0.0;
5209 m_value.boolean =
false;
5215 m_value.string->clear();
5221 m_value.array->clear();
5227 m_value.object->clear();
5261 if (not(is_null() or is_array()))
5263 JSON_THROW(std::domain_error(
"cannot use push_back() with " + type_name()));
5275 m_value.array->push_back(std::move(val));
5286 push_back(std::move(val));
5297 if (not(is_null() or is_array()))
5299 JSON_THROW(std::domain_error(
"cannot use push_back() with " + type_name()));
5311 m_value.array->push_back(val);
5347 if (not(is_null() or is_object()))
5349 JSON_THROW(std::domain_error(
"cannot use push_back() with " + type_name()));
5361 m_value.object->insert(val);
5401 if (is_object() and init.size() == 2 and init.begin()->is_string())
5403 const string_t key = *init.begin();
5404 push_back(
typename object_t::value_type(key, *(init.begin() + 1)));
5443 template<
class... Args>
5447 if (not(is_null() or is_array()))
5449 JSON_THROW(std::domain_error(
"cannot use emplace_back() with " + type_name()));
5461 m_value.array->emplace_back(std::forward<Args>(args)...);
5491 template<
class... Args>
5492 std::pair<iterator, bool>
emplace(Args&& ... args)
5495 if (not(is_null() or is_object()))
5497 JSON_THROW(std::domain_error(
"cannot use emplace() with " + type_name()));
5509 auto res = m_value.object->emplace(std::forward<Args>(args)...);
5512 it.m_it.object_iterator = res.first;
5515 return {it, res.second};
5548 JSON_THROW(std::domain_error(
"iterator does not fit current value"));
5557 JSON_THROW(std::domain_error(
"cannot use insert() with " + type_name()));
5566 return insert(pos, val);
5601 JSON_THROW(std::domain_error(
"iterator does not fit current value"));
5610 JSON_THROW(std::domain_error(
"cannot use insert() with " + type_name()));
5648 JSON_THROW(std::domain_error(
"cannot use insert() with " + type_name()));
5654 JSON_THROW(std::domain_error(
"iterator does not fit current value"));
5660 JSON_THROW(std::domain_error(
"iterators do not fit"));
5665 JSON_THROW(std::domain_error(
"passed iterators may not belong to container"));
5706 JSON_THROW(std::domain_error(
"cannot use insert() with " + type_name()));
5712 JSON_THROW(std::domain_error(
"iterator does not fit current value"));
5739 std::is_nothrow_move_constructible<value_t>::value and
5740 std::is_nothrow_move_assignable<value_t>::value and
5741 std::is_nothrow_move_constructible<json_value>::value and
5742 std::is_nothrow_move_assignable<json_value>::value
5745 std::swap(m_type, other.m_type);
5746 std::swap(m_value, other.m_value);
5775 std::swap(*(m_value.array), other);
5779 JSON_THROW(std::domain_error(
"cannot use swap() with " + type_name()));
5808 std::swap(*(m_value.object), other);
5812 JSON_THROW(std::domain_error(
"cannot use swap() with " + type_name()));
5841 std::swap(*(m_value.string), other);
5845 JSON_THROW(std::domain_error(
"cannot use swap() with " + type_name()));
5884 const auto lhs_type = lhs.type();
5885 const auto rhs_type = rhs.type();
5887 if (lhs_type == rhs_type)
5893 return *lhs.m_value.array == *rhs.m_value.array;
5897 return *lhs.m_value.object == *rhs.m_value.object;
5905 return *lhs.m_value.string == *rhs.m_value.string;
5909 return lhs.m_value.boolean == rhs.m_value.boolean;
5913 return lhs.m_value.number_integer == rhs.m_value.number_integer;
5917 return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned;
5921 return lhs.m_value.number_float == rhs.m_value.number_float;
5931 return static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float;
5935 return lhs.m_value.number_float ==
static_cast<number_float_t>(rhs.m_value.number_integer);
5939 return static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float;
5943 return lhs.m_value.number_float ==
static_cast<number_float_t>(rhs.m_value.number_unsigned);
5947 return static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer;
5951 return lhs.m_value.number_integer ==
static_cast<number_integer_t>(rhs.m_value.number_unsigned);
5961 template<
typename ScalarType,
typename std::enable_if<
5962 std::is_scalar<ScalarType>::value,
int>::type = 0>
5972 template<
typename ScalarType,
typename std::enable_if<
5973 std::is_scalar<ScalarType>::value,
int>::type = 0>
5997 return not (lhs == rhs);
6004 template<
typename ScalarType,
typename std::enable_if<
6005 std::is_scalar<ScalarType>::value,
int>::type = 0>
6015 template<
typename ScalarType,
typename std::enable_if<
6016 std::is_scalar<ScalarType>::value,
int>::type = 0>
6048 const auto lhs_type = lhs.type();
6049 const auto rhs_type = rhs.type();
6051 if (lhs_type == rhs_type)
6057 return *lhs.m_value.array < *rhs.m_value.array;
6061 return *lhs.m_value.object < *rhs.m_value.object;
6069 return *lhs.m_value.string < *rhs.m_value.string;
6073 return lhs.m_value.boolean < rhs.m_value.boolean;
6077 return lhs.m_value.number_integer < rhs.m_value.number_integer;
6081 return lhs.m_value.number_unsigned < rhs.m_value.number_unsigned;
6085 return lhs.m_value.number_float < rhs.m_value.number_float;
6095 return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
6099 return lhs.m_value.number_float <
static_cast<number_float_t>(rhs.m_value.number_integer);
6103 return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
6107 return lhs.m_value.number_float <
static_cast<number_float_t>(rhs.m_value.number_unsigned);
6111 return lhs.m_value.number_integer <
static_cast<number_integer_t>(rhs.m_value.number_unsigned);
6115 return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
6143 return not (rhs < lhs);
6165 return not (lhs <= rhs);
6187 return not (lhs < rhs);
6225 const bool pretty_print = (o.width() > 0);
6226 const auto indentation = (pretty_print ? o.width() : 0);
6232 j.
dump(o, pretty_print, static_cast<unsigned int>(indentation));
6283 template<
class T, std::
size_t N>
6288 return parse(std::begin(
array), std::end(
array), cb);
6318 template<
typename CharT,
typename std::enable_if<
6319 std::is_pointer<CharT>::value and
6325 return parser(reinterpret_cast<const char*>(s), cb).
parse();
6408 template<
class IteratorType,
typename std::enable_if<
6410 std::random_access_iterator_tag,
6411 typename std::iterator_traits<IteratorType>::iterator_category>::value,
int>::type = 0>
6417 assert(std::accumulate(first, last, std::pair<bool, int>(
true, 0),
6418 [&first](std::pair<bool, int> res, decltype(*first) val)
6420 res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
6425 static_assert(
sizeof(
typename std::iterator_traits<IteratorType>::value_type) == 1,