29 #ifndef NLOHMANN_JSON_HPP
30 #define NLOHMANN_JSON_HPP
32 #define NLOHMANN_JSON_VERSION_MAJOR 3
33 #define NLOHMANN_JSON_VERSION_MINOR 1
34 #define NLOHMANN_JSON_VERSION_PATCH 0
41 #include <initializer_list>
48 #include <nlohmann/json_fwd.hpp>
49 #include <nlohmann/detail/macro_scope.hpp>
50 #include <nlohmann/detail/meta.hpp>
51 #include <nlohmann/detail/exceptions.hpp>
52 #include <nlohmann/detail/value_t.hpp>
53 #include <nlohmann/detail/conversions/from_json.hpp>
54 #include <nlohmann/detail/conversions/to_json.hpp>
55 #include <nlohmann/detail/input/input_adapters.hpp>
56 #include <nlohmann/detail/input/lexer.hpp>
57 #include <nlohmann/detail/input/parser.hpp>
58 #include <nlohmann/detail/iterators/primitive_iterator.hpp>
59 #include <nlohmann/detail/iterators/internal_iterator.hpp>
60 #include <nlohmann/detail/iterators/iter_impl.hpp>
61 #include <nlohmann/detail/iterators/iteration_proxy.hpp>
62 #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
63 #include <nlohmann/detail/output/output_adapters.hpp>
64 #include <nlohmann/detail/input/binary_reader.hpp>
65 #include <nlohmann/detail/output/binary_writer.hpp>
66 #include <nlohmann/detail/output/serializer.hpp>
67 #include <nlohmann/detail/json_ref.hpp>
68 #include <nlohmann/detail/json_pointer.hpp>
69 #include <nlohmann/adl_serializer.hpp>
160 NLOHMANN_BASIC_JSON_TPL_DECLARATION
165 friend ::nlohmann::json_pointer<basic_json>;
166 friend ::nlohmann::detail::parser<basic_json>;
167 friend ::nlohmann::detail::serializer<basic_json>;
168 template<
typename BasicJsonType>
169 friend class ::nlohmann::detail::iter_impl;
170 template<
typename BasicJsonType,
typename CharType>
171 friend class ::nlohmann::detail::binary_writer;
172 template<
typename BasicJsonType>
173 friend class ::nlohmann::detail::binary_reader;
176 using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
183 template<
typename BasicJsonType>
185 template<
typename BasicJsonType>
187 template<
typename Iterator>
191 template<
typename CharType>
203 template<
typename T,
typename SFINAE>
204 using json_serializer = JSONSerializer<T, SFINAE>;
258 using pointer =
typename std::allocator_traits<allocator_type>::pointer;
260 using const_pointer =
typename std::allocator_traits<allocator_type>::const_pointer;
312 result[
"copyright"] =
"(C) 2013-2017 Niels Lohmann";
313 result[
"name"] =
"JSON for Modern C++";
314 result[
"url"] =
"https://github.com/nlohmann/json";
315 result[
"version"][
"string"] =
316 std::to_string(NLOHMANN_JSON_VERSION_MAJOR) +
"." +
317 std::to_string(NLOHMANN_JSON_VERSION_MINOR) +
"." +
318 std::to_string(NLOHMANN_JSON_VERSION_PATCH);
319 result[
"version"][
"major"] = NLOHMANN_JSON_VERSION_MAJOR;
320 result[
"version"][
"minor"] = NLOHMANN_JSON_VERSION_MINOR;
321 result[
"version"][
"patch"] = NLOHMANN_JSON_VERSION_PATCH;
324 result[
"platform"] =
"win32";
325 #elif defined __linux__
326 result[
"platform"] =
"linux";
327 #elif defined __APPLE__
328 result[
"platform"] =
"apple";
329 #elif defined __unix__
330 result[
"platform"] =
"unix";
332 result[
"platform"] =
"unknown";
335 #if defined(__ICC) || defined(__INTEL_COMPILER)
336 result[
"compiler"] = {{
"family",
"icc"}, {
"version", __INTEL_COMPILER}};
337 #elif defined(__clang__)
338 result[
"compiler"] = {{
"family",
"clang"}, {
"version", __clang_version__}};
339 #elif defined(__GNUC__) || defined(__GNUG__)
340 result[
"compiler"] = {{
"family",
"gcc"}, {
"version", std::to_string(__GNUC__) +
"." + std::to_string(__GNUC_MINOR__) +
"." + std::to_string(__GNUC_PATCHLEVEL__)}};
341 #elif defined(__HP_cc) || defined(__HP_aCC)
342 result[
"compiler"] =
"hp"
343 #elif defined(__IBMCPP__)
344 result[
"compiler"] = {{
"family",
"ilecpp"}, {
"version", __IBMCPP__}};
345 #elif defined(_MSC_VER)
346 result[
"compiler"] = {{
"family",
"msvc"}, {
"version", _MSC_VER}};
348 result[
"compiler"] = {{
"family",
"pgcpp"}, {
"version", __PGI}};
349 #elif defined(__SUNPRO_CC)
350 result[
"compiler"] = {{
"family",
"sunpro"}, {
"version", __SUNPRO_CC}};
352 result[
"compiler"] = {{
"family",
"unknown"}, {
"version",
"unknown"}};
356 result[
"compiler"][
"c++"] = std::to_string(__cplusplus);
358 result[
"compiler"][
"c++"] =
"unknown";
373 #if defined(JSON_HAS_CPP_14)
376 using object_comparator_t = std::less<>;
378 using object_comparator_t = std::less<StringType>;
464 using object_t = ObjectType<StringType,
467 AllocatorType<std::pair<
const StringType,
514 using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
811 template<
typename T,
typename... Args>
812 static T* create(Args&& ... args)
814 AllocatorType<T> alloc;
815 using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
817 auto deleter = [&](T *
object)
819 AllocatorTraits::deallocate(alloc,
object, 1);
821 std::unique_ptr<T, decltype(deleter)>
object(AllocatorTraits::allocate(alloc, 1), deleter);
822 AllocatorTraits::construct(alloc,
object.
get(), std::forward<Args>(args)...);
823 assert(
object !=
nullptr);
824 return object.release();
873 json_value() =
default;
875 json_value(
boolean_t v) noexcept : boolean(v) {}
883 json_value(value_t t)
889 object = create<object_t>();
895 array = create<array_t>();
901 string = create<string_t>(
"");
938 if (JSON_UNLIKELY(t == value_t::null))
940 JSON_THROW(other_error::create(500,
"961c151d2e87f2686a955a9be24d316f1362bf21 3.1.0"));
950 string = create<string_t>(
value);
956 string = create<string_t>(std::move(
value));
962 object = create<object_t>(
value);
968 object = create<object_t>(std::move(
value));
983 void destroy(value_t t) noexcept
989 AllocatorType<object_t> alloc;
990 std::allocator_traits<decltype(alloc)>::destroy(alloc,
object);
991 std::allocator_traits<decltype(alloc)>::deallocate(alloc,
object, 1);
997 AllocatorType<array_t> alloc;
998 std::allocator_traits<decltype(alloc)>::destroy(alloc,
array);
999 std::allocator_traits<decltype(alloc)>::deallocate(alloc,
array, 1);
1005 AllocatorType<string_t> alloc;
1006 std::allocator_traits<decltype(alloc)>::destroy(alloc,
string);
1007 std::allocator_traits<decltype(alloc)>::deallocate(alloc,
string, 1);
1028 void assert_invariant() const noexcept
1148 : m_type(v), m_value(v)
1233 template <
typename CompatibleType,
1234 typename U = detail::uncvref_t<CompatibleType>,
1235 detail::enable_if_t<
1238 JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
1239 std::forward<CompatibleType>(val))))
1241 JSONSerializer<U>::to_json(*
this, std::forward<CompatibleType>(val));
1320 bool type_deduction =
true,
1325 bool is_an_object = std::all_of(init.begin(), init.end(),
1328 return (element_ref->is_array() and element_ref->size() == 2 and (*element_ref)[0].is_string());
1332 if (not type_deduction)
1337 is_an_object =
false;
1341 if (JSON_UNLIKELY(manual_type ==
value_t::object and not is_an_object))
1343 JSON_THROW(type_error::create(301,
"cannot create object from initializer list"));
1355 auto element = element_ref.moved_or_copied();
1356 m_value.object->emplace(
1357 std::move(*((*element.m_value.array)[0].m_value.string)),
1358 std::move((*element.m_value.array)[1]));
1365 m_value.array = create<array_t>(init.begin(), init.end());
1481 m_value.array = create<array_t>(cnt, val);
1540 template<
class InputIT,
typename std::enable_if<
1541 std::is_same<InputIT, typename basic_json_t::iterator>::value or
1542 std::is_same<InputIT, typename basic_json_t::const_iterator>::value,
int>
::type = 0>
1545 assert(first.m_object !=
nullptr);
1546 assert(last.m_object !=
nullptr);
1549 if (JSON_UNLIKELY(first.m_object != last.m_object))
1551 JSON_THROW(invalid_iterator::create(201,
"iterators are not compatible"));
1555 m_type = first.m_object->m_type;
1566 if (JSON_UNLIKELY(not first.m_it.primitive_iterator.is_begin()
1567 or not last.m_it.primitive_iterator.is_end()))
1569 JSON_THROW(invalid_iterator::create(204,
"iterators out of range"));
1582 m_value.number_integer = first.m_object->m_value.number_integer;
1588 m_value.number_unsigned = first.m_object->m_value.number_unsigned;
1594 m_value.number_float = first.m_object->m_value.number_float;
1600 m_value.boolean = first.m_object->m_value.boolean;
1606 m_value = *first.m_object->m_value.string;
1612 m_value.object = create<object_t>(first.m_it.object_iterator,
1613 last.m_it.object_iterator);
1619 m_value.array = create<array_t>(first.m_it.array_iterator,
1620 last.m_it.array_iterator);
1625 JSON_THROW(invalid_iterator::create(206,
"cannot construct with iterators from " +
1626 std::string(first.m_object->type_name())));
1668 : m_type(other.m_type)
1671 other.assert_invariant();
1677 m_value = *other.m_value.object;
1683 m_value = *other.m_value.array;
1689 m_value = *other.m_value.string;
1695 m_value = other.m_value.boolean;
1701 m_value = other.m_value.number_integer;
1707 m_value = other.m_value.number_unsigned;
1713 m_value = other.m_value.number_float;
1751 : m_type(std::move(other.m_type)),
1752 m_value(std::move(other.m_value))
1755 other.assert_invariant();
1758 other.m_type = value_t::null;
1788 std::is_nothrow_move_constructible<value_t>::value and
1789 std::is_nothrow_move_assignable<value_t>::value and
1790 std::is_nothrow_move_constructible<json_value>::value and
1791 std::is_nothrow_move_assignable<json_value>::value
1795 other.assert_invariant();
1798 swap(m_type, other.m_type);
1799 swap(m_value, other.m_value);
1823 m_value.destroy(m_type);
1874 const bool ensure_ascii =
false)
const
1881 s.
dump(*
this,
true, ensure_ascii, static_cast<unsigned int>(indent));
1885 s.
dump(*
this,
false, ensure_ascii, 0);
2004 return (m_type == value_t::null);
2234 return (m_type == value_t::discarded);
2275 return m_value.boolean;
2278 JSON_THROW(type_error::create(302,
"type must be boolean, but is " + std::string(
type_name())));
2284 return is_object() ? m_value.object :
nullptr;
2290 return is_object() ? m_value.object :
nullptr;
2296 return is_array() ? m_value.array :
nullptr;
2300 constexpr
const array_t* get_impl_ptr(
const array_t* )
const noexcept
2302 return is_array() ? m_value.array :
nullptr;
2308 return is_string() ? m_value.string :
nullptr;
2314 return is_string() ? m_value.string :
nullptr;
2320 return is_boolean() ? &m_value.boolean :
nullptr;
2326 return is_boolean() ? &m_value.boolean :
nullptr;
2376 template<
typename ReferenceType,
typename ThisType>
2377 static ReferenceType get_ref_impl(ThisType& obj)
2380 auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
2382 if (JSON_LIKELY(ptr !=
nullptr))
2387 JSON_THROW(type_error::create(303,
"incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
2409 template<
typename BasicJsonType, detail::enable_if_t<
2410 std::is_same<typename std::remove_const<BasicJsonType>::type, basic_json_t>
::value,
2456 template<
typename ValueTypeCV,
typename ValueType = detail::uncvref_t<ValueTypeCV>,
2457 detail::enable_if_t <
2458 not std::is_same<basic_json_t, ValueType>::value and
2459 detail::has_from_json<basic_json_t, ValueType>::value and
2460 not detail::has_non_default_from_json<basic_json_t, ValueType>::value,
2462 ValueType
get() const noexcept(noexcept(
2463 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
2468 static_assert(not std::is_reference<ValueTypeCV>::value,
2469 "get() cannot be used with reference types, you might want to use get_ref()");
2470 static_assert(std::is_default_constructible<ValueType>::value,
2471 "types must be DefaultConstructible when used with get()");
2474 JSONSerializer<ValueType>::from_json(*
this, ret);
2509 template<
typename ValueTypeCV,
typename ValueType = detail::uncvref_t<ValueTypeCV>,
2510 detail::enable_if_t<not std::is_same<basic_json_t, ValueType>::value and
2511 detail::has_non_default_from_json<basic_json_t, ValueType>::value,
2513 ValueType
get() const noexcept(noexcept(
2514 JSONSerializer<ValueTypeCV>::from_json(std::declval<const basic_json_t&>())))
2516 static_assert(not std::is_reference<ValueTypeCV>::value,
2517 "get() cannot be used with reference types, you might want to use get_ref()");
2518 return JSONSerializer<ValueTypeCV>::from_json(*
this);
2548 template<
typename PointerType,
typename std::enable_if<
2549 std::is_pointer<PointerType>::value,
int>
::type = 0>
2553 return get_ptr<PointerType>();
2560 template<
typename PointerType,
typename std::enable_if<
2561 std::is_pointer<PointerType>::value,
int>
::type = 0>
2562 constexpr
const PointerType
get() const noexcept
2565 return get_ptr<PointerType>();
2594 template<
typename PointerType,
typename std::enable_if<
2595 std::is_pointer<PointerType>::value,
int>
::type = 0>
2599 using pointee_t =
typename std::remove_const<
typename
2600 std::remove_pointer<
typename
2604 std::is_same<object_t, pointee_t>::value
2605 or std::is_same<array_t, pointee_t>::value
2606 or std::is_same<string_t, pointee_t>::value
2607 or std::is_same<boolean_t, pointee_t>::value
2608 or std::is_same<number_integer_t, pointee_t>::value
2609 or std::is_same<number_unsigned_t, pointee_t>::value
2610 or std::is_same<number_float_t, pointee_t>::value
2611 ,
"incompatible pointer type");
2614 return get_impl_ptr(static_cast<PointerType>(
nullptr));
2621 template<
typename PointerType,
typename std::enable_if<
2622 std::is_pointer<PointerType>::value and
2623 std::is_const<typename std::remove_pointer<PointerType>::type>
::value,
int>
::type = 0>
2624 constexpr
const PointerType
get_ptr() const noexcept
2627 using pointee_t =
typename std::remove_const<
typename
2628 std::remove_pointer<
typename
2632 std::is_same<object_t, pointee_t>::value
2633 or std::is_same<array_t, pointee_t>::value
2634 or std::is_same<string_t, pointee_t>::value
2635 or std::is_same<boolean_t, pointee_t>::value
2636 or std::is_same<number_integer_t, pointee_t>::value
2637 or std::is_same<number_unsigned_t, pointee_t>::value
2638 or std::is_same<number_float_t, pointee_t>::value
2639 ,
"incompatible pointer type");
2642 return get_impl_ptr(static_cast<PointerType>(
nullptr));
2671 template<
typename ReferenceType,
typename std::enable_if<
2672 std::is_reference<ReferenceType>::value,
int>
::type = 0>
2676 return get_ref_impl<ReferenceType>(*
this);
2683 template<
typename ReferenceType,
typename std::enable_if<
2684 std::is_reference<ReferenceType>::value and
2685 std::is_const<typename std::remove_reference<ReferenceType>::type>
::value,
int>
::type = 0>
2689 return get_ref_impl<ReferenceType>(*
this);
2721 template <
typename ValueType,
typename std::enable_if <
2722 not std::is_pointer<ValueType>::value and
2723 not std::is_same<ValueType, detail::json_ref<basic_json>>
::value and
2724 not std::is_same<ValueType, typename string_t::value_type>::value
2725 #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015
2726 and not std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>
::value
2728 #if defined(JSON_HAS_CPP_17)
2729 and not std::is_same<ValueType, typename std::string_view>::value
2732 operator ValueType()
const
2735 return get<ValueType>();
2782 return m_value.array->at(idx);
2784 JSON_CATCH (std::out_of_range&)
2787 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
2792 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
2829 return m_value.array->at(idx);
2831 JSON_CATCH (std::out_of_range&)
2834 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
2839 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
2880 return m_value.object->at(key);
2882 JSON_CATCH (std::out_of_range&)
2885 JSON_THROW(out_of_range::create(403,
"key '" + key +
"' not found"));
2890 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
2931 return m_value.object->at(key);
2933 JSON_CATCH (std::out_of_range&)
2936 JSON_THROW(out_of_range::create(403,
"key '" + key +
"' not found"));
2941 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
2976 m_value.array = create<array_t>();
2984 if (idx >= m_value.array->size())
2986 m_value.array->insert(m_value.array->end(),
2987 idx - m_value.array->size() + 1,
2991 return m_value.array->operator[](idx);
2994 JSON_THROW(type_error::create(305,
"cannot use operator[] with " + std::string(
type_name())));
3021 return m_value.array->operator[](idx);
3024 JSON_THROW(type_error::create(305,
"cannot use operator[] with " + std::string(
type_name())));
3060 m_value.object = create<object_t>();
3067 return m_value.object->operator[](key);
3070 JSON_THROW(type_error::create(305,
"cannot use operator[] with " + std::string(
type_name())));
3108 assert(m_value.object->find(key) != m_value.object->end());
3109 return m_value.object->find(key)->second;
3112 JSON_THROW(type_error::create(305,
"cannot use operator[] with " + std::string(
type_name())));
3142 template<
typename T>
3156 return m_value.object->operator[](key);
3159 JSON_THROW(type_error::create(305,
"cannot use operator[] with " + std::string(
type_name())));
3192 template<
typename T>
3198 assert(m_value.object->find(key) != m_value.object->end());
3199 return m_value.object->find(key)->second;
3202 JSON_THROW(type_error::create(305,
"cannot use operator[] with " + std::string(
type_name())));
3253 template<
class ValueType,
typename std::enable_if<
3254 std::is_convertible<basic_json_t, ValueType>::value,
int>
::type = 0>
3255 ValueType
value(
const typename object_t::key_type& key,
const ValueType& default_value)
const
3261 const auto it =
find(key);
3267 return default_value;
3270 JSON_THROW(type_error::create(306,
"cannot use value() with " + std::string(
type_name())));
3277 string_t value(
const typename object_t::key_type& key,
const char* default_value)
const
3323 template<
class ValueType,
typename std::enable_if<
3324 std::is_convertible<basic_json_t, ValueType>::value,
int>
::type = 0>
3333 return ptr.get_checked(
this);
3337 return default_value;
3341 JSON_THROW(type_error::create(306,
"cannot use value() with " + std::string(
type_name())));
3485 template<
class IteratorType,
typename std::enable_if<
3486 std::is_same<IteratorType, typename basic_json_t::iterator>::value or
3487 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value,
int>
::type
3492 if (JSON_UNLIKELY(
this != pos.m_object))
3494 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
3497 IteratorType result =
end();
3507 if (JSON_UNLIKELY(not pos.m_it.primitive_iterator.is_begin()))
3509 JSON_THROW(invalid_iterator::create(205,
"iterator out of range"));
3514 AllocatorType<string_t> alloc;
3515 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
3516 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
3517 m_value.string =
nullptr;
3520 m_type = value_t::null;
3527 result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
3533 result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
3538 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
3590 template<
class IteratorType,
typename std::enable_if<
3591 std::is_same<IteratorType, typename basic_json_t::iterator>::value or
3592 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value,
int>
::type
3594 IteratorType
erase(IteratorType first, IteratorType last)
3597 if (JSON_UNLIKELY(
this != first.m_object or
this != last.m_object))
3599 JSON_THROW(invalid_iterator::create(203,
"iterators do not fit current value"));
3602 IteratorType result =
end();
3612 if (JSON_LIKELY(not first.m_it.primitive_iterator.is_begin()
3613 or not last.m_it.primitive_iterator.is_end()))
3615 JSON_THROW(invalid_iterator::create(204,
"iterators out of range"));
3620 AllocatorType<string_t> alloc;
3621 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
3622 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
3623 m_value.string =
nullptr;
3626 m_type = value_t::null;
3633 result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
3634 last.m_it.object_iterator);
3640 result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
3641 last.m_it.array_iterator);
3646 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
3686 return m_value.object->erase(key);
3689 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
3721 if (JSON_UNLIKELY(idx >=
size()))
3723 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
3726 m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
3730 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
3766 template<
typename KeyT>
3769 auto result =
end();
3773 result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
3783 template<
typename KeyT>
3786 auto result =
cend();
3790 result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
3817 template<
typename KeyT>
3821 return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
4309 return m_value.array->empty();
4315 return m_value.object->empty();
4381 return m_value.array->size();
4387 return m_value.object->size();
4445 return m_value.array->max_size();
4451 return m_value.object->max_size();
4514 m_value.number_integer = 0;
4520 m_value.number_unsigned = 0;
4526 m_value.number_float = 0.0;
4532 m_value.boolean =
false;
4538 m_value.string->clear();
4544 m_value.array->clear();
4550 m_value.object->clear();
4584 JSON_THROW(type_error::create(308,
"cannot use push_back() with " + std::string(
type_name())));
4596 m_value.array->push_back(std::move(val));
4598 val.m_type = value_t::null;
4620 JSON_THROW(type_error::create(308,
"cannot use push_back() with " + std::string(
type_name())));
4632 m_value.array->push_back(val);
4670 JSON_THROW(type_error::create(308,
"cannot use push_back() with " + std::string(
type_name())));
4682 m_value.object->insert(val);
4725 push_back(
typename object_t::value_type(
4726 std::move(key.
get_ref<
string_t&>()), (init.begin() + 1)->moved_or_copied()));
4765 template<
class... Args>
4771 JSON_THROW(type_error::create(311,
"cannot use emplace_back() with " + std::string(
type_name())));
4783 m_value.array->emplace_back(std::forward<Args>(args)...);
4813 template<
class... Args>
4814 std::pair<iterator, bool>
emplace(Args&& ... args)
4819 JSON_THROW(type_error::create(311,
"cannot use emplace() with " + std::string(
type_name())));
4831 auto res = m_value.object->emplace(std::forward<Args>(args)...);
4834 it.m_it.object_iterator = res.first;
4837 return {it, res.second};
4868 if (JSON_UNLIKELY(pos.m_object !=
this))
4870 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
4879 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
4921 if (JSON_UNLIKELY(pos.m_object !=
this))
4923 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
4932 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
4970 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
4974 if (JSON_UNLIKELY(pos.m_object !=
this))
4976 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
4980 if (JSON_UNLIKELY(first.m_object != last.m_object))
4982 JSON_THROW(invalid_iterator::create(210,
"iterators do not fit"));
4985 if (JSON_UNLIKELY(first.m_object ==
this))
4987 JSON_THROW(invalid_iterator::create(211,
"passed iterators may not belong to container"));
5028 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
5032 if (JSON_UNLIKELY(pos.m_object !=
this))
5034 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
5071 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
5075 if (JSON_UNLIKELY(first.m_object != last.m_object))
5077 JSON_THROW(invalid_iterator::create(210,
"iterators do not fit"));
5081 if (JSON_UNLIKELY(not first.m_object->is_object()))
5083 JSON_THROW(invalid_iterator::create(202,
"iterators first and last must point to objects"));
5114 m_value.object = create<object_t>();
5120 JSON_THROW(type_error::create(312,
"cannot use update() with " + std::string(
type_name())));
5122 if (JSON_UNLIKELY(not j.is_object()))
5124 JSON_THROW(type_error::create(312,
"cannot use update() with " + std::string(j.type_name())));
5127 for (
auto it = j.cbegin(); it != j.cend(); ++it)
5129 m_value.object->operator[](it.key()) = it.value();
5165 m_value.object = create<object_t>();
5171 JSON_THROW(type_error::create(312,
"cannot use update() with " + std::string(
type_name())));
5175 if (JSON_UNLIKELY(first.m_object != last.m_object))
5177 JSON_THROW(invalid_iterator::create(210,
"iterators do not fit"));
5181 if (JSON_UNLIKELY(not first.m_object->is_object()
5182 or not first.m_object->is_object()))
5184 JSON_THROW(invalid_iterator::create(202,
"iterators first and last must point to objects"));
5187 for (
auto it = first; it != last; ++it)
5189 m_value.object->operator[](it.key()) = it.value();
5211 std::is_nothrow_move_constructible<value_t>::value and
5212 std::is_nothrow_move_assignable<value_t>::value and
5213 std::is_nothrow_move_constructible<json_value>::value and
5214 std::is_nothrow_move_assignable<json_value>::value
5217 std::swap(m_type, other.m_type);
5218 std::swap(m_value, other.m_value);
5247 std::swap(*(m_value.array), other);
5251 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
5280 std::swap(*(m_value.object), other);
5284 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
5313 std::swap(*(m_value.string), other);
5317 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
5372 const auto lhs_type = lhs.type();
5373 const auto rhs_type = rhs.type();
5375 if (lhs_type == rhs_type)
5380 return (*lhs.m_value.array == *rhs.m_value.array);
5383 return (*lhs.m_value.object == *rhs.m_value.object);
5389 return (*lhs.m_value.string == *rhs.m_value.string);
5392 return (lhs.m_value.boolean == rhs.m_value.boolean);
5395 return (lhs.m_value.number_integer == rhs.m_value.number_integer);
5398 return (lhs.m_value.number_unsigned == rhs.m_value.number_unsigned);
5401 return (lhs.m_value.number_float == rhs.m_value.number_float);
5409 return (static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float);
5413 return (lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer));
5417 return (static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float);
5421 return (lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned));
5425 return (static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer);
5429 return (lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_unsigned));
5439 template<
typename ScalarType,
typename std::enable_if<
5440 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5450 template<
typename ScalarType,
typename std::enable_if<
5451 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5477 return not (lhs == rhs);
5484 template<
typename ScalarType,
typename std::enable_if<
5485 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5495 template<
typename ScalarType,
typename std::enable_if<
5496 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5530 const auto lhs_type = lhs.type();
5531 const auto rhs_type = rhs.type();
5533 if (lhs_type == rhs_type)
5538 return (*lhs.m_value.array) < (*rhs.m_value.array);
5541 return *lhs.m_value.object < *rhs.m_value.object;
5547 return *lhs.m_value.string < *rhs.m_value.string;
5550 return lhs.m_value.boolean < rhs.m_value.boolean;
5553 return lhs.m_value.number_integer < rhs.m_value.number_integer;
5556 return lhs.m_value.number_unsigned < rhs.m_value.number_unsigned;
5559 return lhs.m_value.number_float < rhs.m_value.number_float;
5567 return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
5571 return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
5575 return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
5579 return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_unsigned);
5583 return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_unsigned);
5587 return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
5600 template<
typename ScalarType,
typename std::enable_if<
5601 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5611 template<
typename ScalarType,
typename std::enable_if<
5612 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5639 return not (rhs < lhs);
5646 template<
typename ScalarType,
typename std::enable_if<
5647 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5657 template<
typename ScalarType,
typename std::enable_if<
5658 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5685 return not (lhs <= rhs);
5692 template<
typename ScalarType,
typename std::enable_if<
5693 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5703 template<
typename ScalarType,
typename std::enable_if<
5704 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5731 return not (lhs < rhs);
5738 template<
typename ScalarType,
typename std::enable_if<
5739 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5749 template<
typename ScalarType,
typename std::enable_if<
5750 std::is_scalar<ScalarType>::value,
int>
::type = 0>
5799 const bool pretty_print = (o.width() > 0);
5800 const auto indentation = (pretty_print ? o.width() : 0);
5807 s.
dump(j, pretty_print,
false, static_cast<unsigned int>(indentation));
5899 const bool allow_exceptions =
true)
5902 parser(i, cb, allow_exceptions).
parse(
true, result);
5911 const bool allow_exceptions =
true)
5914 parser(i, cb, allow_exceptions).
parse(
true, result);
5920 return parser(i).accept(
true);
5923 static bool accept(detail::input_adapter& i)
5925 return parser(i).accept(
true);
5975 template<
class IteratorType,
typename std::enable_if<
5977 std::random_access_iterator_tag,
5978 typename std::iterator_traits<IteratorType>::iterator_category>
::value,
int>
::type = 0>
5981 const bool allow_exceptions =
true)
5988 template<
class IteratorType,
typename std::enable_if<
5990 std::random_access_iterator_tag,
5991 typename std::iterator_traits<IteratorType>::iterator_category>
::value,
int>
::type = 0>
5992 static bool accept(IteratorType first, IteratorType last)
6093 case value_t::discarded:
6108 value_t m_type = value_t::null;
6111 json_value m_value = {};
6211 std::vector<uint8_t> result;
6218 binary_writer<uint8_t>(o).write_cbor(j);
6223 binary_writer<char>(o).write_cbor(j);
6308 std::vector<uint8_t> result;
6315 binary_writer<uint8_t>(o).write_msgpack(j);
6320 binary_writer<char>(o).write_msgpack(j);
6404 const bool use_size =
false,
6405 const bool use_type =
false)
6407 std::vector<uint8_t> result;
6408 to_ubjson(j, result, use_size, use_type);
6413 const bool use_size =
false,
const bool use_type =
false)
6415 binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
6419 const bool use_size =
false,
const bool use_type =
false)
6421 binary_writer<char>(o).write_ubjson(j, use_size, use_type);
6518 const bool strict =
true)
6526 template<
typename A1,
typename A2,
6527 detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value,
int> = 0>
6607 const bool strict =
true)
6615 template<
typename A1,
typename A2,
6616 detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value,
int> = 0>
6676 const bool strict =
true)
6681 template<
typename A1,
typename A2,
6682 detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value,
int> = 0>
6685 return binary_reader(
detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).parse_ubjson(strict);
6732 return ptr.get_unchecked(
this);
6760 return ptr.get_unchecked(
this);
6803 return ptr.get_checked(
this);
6846 return ptr.get_checked(
this);
6874 json_pointer::flatten(
"", *
this, result);
6910 return json_pointer::unflatten(*
this);
6975 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
6977 const auto get_op = [](
const std::string & op)
6981 return patch_operations::add;
6985 return patch_operations::remove;
6987 if (op ==
"replace")
6989 return patch_operations::replace;
6993 return patch_operations::move;
6997 return patch_operations::copy;
7001 return patch_operations::test;
7004 return patch_operations::invalid;
7019 if (top_pointer != ptr)
7021 result.
at(top_pointer);
7025 const auto last_path = ptr.pop_back();
7028 switch (parent.m_type)
7034 parent[last_path] = val;
7040 if (last_path ==
"-")
7048 if (JSON_UNLIKELY(static_cast<size_type>(idx) > parent.
size()))
7051 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
7056 parent.
insert(parent.
begin() + static_cast<difference_type>(idx), val);
7072 const auto operation_remove = [&result](
json_pointer & ptr)
7075 const auto last_path = ptr.pop_back();
7082 auto it = parent.
find(last_path);
7083 if (JSON_LIKELY(it != parent.
end()))
7089 JSON_THROW(out_of_range::create(403,
"key '" + last_path +
"' not found"));
7100 if (JSON_UNLIKELY(not json_patch.
is_array()))
7106 for (
const auto& val : json_patch)
7109 const auto get_value = [&val](
const std::string & op,
7110 const std::string & member,
7114 auto it = val.m_value.object->find(member);
7117 const auto error_msg = (op ==
"op") ?
"operation" :
"operation '" + op +
"'";
7120 if (JSON_UNLIKELY(it == val.m_value.object->end()))
7122 JSON_THROW(
parse_error::create(105, 0, error_msg +
" must have member '" + member +
"'"));
7126 if (JSON_UNLIKELY(string_type and not it->second.is_string()))
7128 JSON_THROW(
parse_error::create(105, 0, error_msg +
" must have string member '" + member +
"'"));
7136 if (JSON_UNLIKELY(not val.is_object()))
7142 const std::string op = get_value(
"op",
"op",
true);
7143 const std::string path = get_value(op,
"path",
true);
7148 case patch_operations::add:
7150 operation_add(ptr, get_value(
"add",
"value",
false));
7154 case patch_operations::remove:
7156 operation_remove(ptr);
7160 case patch_operations::replace:
7163 result.
at(ptr) = get_value(
"replace",
"value",
false);
7167 case patch_operations::move:
7169 const std::string from_path = get_value(
"move",
"from",
true);
7179 operation_remove(from_ptr);
7180 operation_add(ptr, v);
7184 case patch_operations::copy:
7186 const std::string from_path = get_value(
"copy",
"from",
true);
7195 operation_add(ptr, v);
7199 case patch_operations::test:
7201 bool success =
false;
7206 success = (result.
at(ptr) == get_value(
"test",
"value",
false));
7214 if (JSON_UNLIKELY(not success))
7216 JSON_THROW(other_error::create(501,
"unsuccessful: " + val.dump()));
7222 case patch_operations::invalid:
7268 const std::string& path =
"")
7284 {
"op",
"replace"}, {
"path", path}, {
"value", target}
7295 while (i <
source.size() and i < target.
size())
7298 auto temp_diff =
diff(
source[i], target[i], path +
"/" + std::to_string(i));
7299 result.
insert(result.
end(), temp_diff.begin(), temp_diff.end());
7307 const auto end_index = static_cast<difference_type>(result.
size());
7308 while (i <
source.size())
7315 {
"path", path +
"/" + std::to_string(i)}
7321 while (i < target.
size())
7326 {
"path", path +
"/" + std::to_string(i)},
7327 {
"value", target[i]}
7338 for (
auto it =
source.cbegin(); it !=
source.cend(); ++it)
7341 const auto key = json_pointer::escape(it.key());
7343 if (target.
find(it.key()) != target.
end())
7346 auto temp_diff =
diff(it.value(), target[it.key()], path +
"/" + key);
7347 result.
insert(result.
end(), temp_diff.begin(), temp_diff.end());
7354 {
"op",
"remove"}, {
"path", path +
"/" + key}
7360 for (
auto it = target.
cbegin(); it != target.
cend(); ++it)
7365 const auto key = json_pointer::escape(it.key());
7368 {
"op",
"add"}, {
"path", path +
"/" + key},
7369 {
"value", it.value()}
7382 {
"op",
"replace"}, {
"path", path}, {
"value", target}
7453 if (it.value().is_null())
7488 is_nothrow_move_constructible<nlohmann::json>::value and
7489 is_nothrow_move_assignable<nlohmann::json>::value
7507 const auto& h = hash<nlohmann::json::string_t>();
7544 inline nlohmann::json operator "" _json(
const char* s, std::size_t n)
7567 #include <nlohmann/detail/macro_unscope.hpp>