30 #ifndef INCLUDE_NLOHMANN_JSON_HPP_
31 #define INCLUDE_NLOHMANN_JSON_HPP_
33 #define NLOHMANN_JSON_VERSION_MAJOR 3
34 #define NLOHMANN_JSON_VERSION_MINOR 9
35 #define NLOHMANN_JSON_VERSION_PATCH 1
40 #include <initializer_list>
49 #include <nlohmann/adl_serializer.hpp>
50 #include <nlohmann/byte_container_with_subtype.hpp>
51 #include <nlohmann/detail/conversions/from_json.hpp>
52 #include <nlohmann/detail/conversions/to_json.hpp>
53 #include <nlohmann/detail/exceptions.hpp>
54 #include <nlohmann/detail/hash.hpp>
55 #include <nlohmann/detail/input/binary_reader.hpp>
56 #include <nlohmann/detail/input/input_adapters.hpp>
57 #include <nlohmann/detail/input/lexer.hpp>
58 #include <nlohmann/detail/input/parser.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/iterators/primitive_iterator.hpp>
64 #include <nlohmann/detail/json_pointer.hpp>
65 #include <nlohmann/detail/json_ref.hpp>
66 #include <nlohmann/detail/macro_scope.hpp>
67 #include <nlohmann/detail/meta/cpp_future.hpp>
68 #include <nlohmann/detail/meta/type_traits.hpp>
69 #include <nlohmann/detail/output/binary_writer.hpp>
70 #include <nlohmann/detail/output/output_adapters.hpp>
71 #include <nlohmann/detail/output/serializer.hpp>
72 #include <nlohmann/detail/value_t.hpp>
73 #include <nlohmann/json_fwd.hpp>
74 #include <nlohmann/ordered_map.hpp>
168 NLOHMANN_BASIC_JSON_TPL_DECLARATION
173 friend ::nlohmann::json_pointer<basic_json>;
175 template<
typename BasicJsonType,
typename InputType>
176 friend class ::nlohmann::detail::parser;
177 friend ::nlohmann::detail::serializer<basic_json>;
178 template<
typename BasicJsonType>
179 friend class ::nlohmann::detail::iter_impl;
180 template<
typename BasicJsonType,
typename CharType>
181 friend class ::nlohmann::detail::binary_writer;
182 template<
typename BasicJsonType,
typename InputType,
typename SAX>
183 friend class ::nlohmann::detail::binary_reader;
184 template<
typename BasicJsonType>
185 friend class ::nlohmann::detail::json_sax_dom_parser;
186 template<
typename BasicJsonType>
187 friend class ::nlohmann::detail::json_sax_dom_callback_parser;
190 using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
195 template<
typename InputAdapterType>
196 static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
197 InputAdapterType adapter,
198 detail::parser_callback_t<basic_json>cb =
nullptr,
199 const bool allow_exceptions =
true,
200 const bool ignore_comments =
false
203 return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
204 std::move(cb), allow_exceptions, ignore_comments);
208 template<
typename BasicJsonType>
210 template<
typename BasicJsonType>
212 template<
typename Iterator>
216 template<
typename CharType>
219 template<
typename InputType>
229 template<
typename T,
typename SFINAE>
230 using json_serializer = JSONSerializer<T, SFINAE>;
292 using pointer =
typename std::allocator_traits<allocator_type>::pointer;
294 using const_pointer =
typename std::allocator_traits<allocator_type>::const_pointer;
342 JSON_HEDLEY_WARN_UNUSED_RESULT
347 result[
"copyright"] =
"(C) 2013-2020 Niels Lohmann";
348 result[
"name"] =
"JSON for Modern C++";
349 result[
"url"] =
"https://github.com/nlohmann/json";
350 result[
"version"][
"string"] =
351 std::to_string(NLOHMANN_JSON_VERSION_MAJOR) +
"." +
352 std::to_string(NLOHMANN_JSON_VERSION_MINOR) +
"." +
353 std::to_string(NLOHMANN_JSON_VERSION_PATCH);
354 result[
"version"][
"major"] = NLOHMANN_JSON_VERSION_MAJOR;
355 result[
"version"][
"minor"] = NLOHMANN_JSON_VERSION_MINOR;
356 result[
"version"][
"patch"] = NLOHMANN_JSON_VERSION_PATCH;
359 result[
"platform"] =
"win32";
360 #elif defined __linux__
361 result[
"platform"] =
"linux";
362 #elif defined __APPLE__
363 result[
"platform"] =
"apple";
364 #elif defined __unix__
365 result[
"platform"] =
"unix";
367 result[
"platform"] =
"unknown";
370 #if defined(__ICC) || defined(__INTEL_COMPILER)
371 result[
"compiler"] = {{
"family",
"icc"}, {
"version", __INTEL_COMPILER}};
372 #elif defined(__clang__)
373 result[
"compiler"] = {{
"family",
"clang"}, {
"version", __clang_version__}};
374 #elif defined(__GNUC__) || defined(__GNUG__)
375 result[
"compiler"] = {{
"family",
"gcc"}, {
"version", std::to_string(__GNUC__) +
"." + std::to_string(__GNUC_MINOR__) +
"." + std::to_string(__GNUC_PATCHLEVEL__)}};
376 #elif defined(__HP_cc) || defined(__HP_aCC)
377 result[
"compiler"] =
"hp"
378 #elif defined(__IBMCPP__)
379 result[
"compiler"] = {{
"family",
"ilecpp"}, {
"version", __IBMCPP__}};
380 #elif defined(_MSC_VER)
381 result[
"compiler"] = {{
"family",
"msvc"}, {
"version", _MSC_VER}};
383 result[
"compiler"] = {{
"family",
"pgcpp"}, {
"version", __PGI}};
384 #elif defined(__SUNPRO_CC)
385 result[
"compiler"] = {{
"family",
"sunpro"}, {
"version", __SUNPRO_CC}};
387 result[
"compiler"] = {{
"family",
"unknown"}, {
"version",
"unknown"}};
391 result[
"compiler"][
"c++"] = std::to_string(__cplusplus);
393 result[
"compiler"][
"c++"] =
"unknown";
408 #if defined(JSON_HAS_CPP_14)
411 using object_comparator_t = std::less<>;
413 using object_comparator_t = std::less<StringType>;
502 AllocatorType<std::pair<
const StringType,
549 using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
916 template<
typename T,
typename... Args>
917 JSON_HEDLEY_RETURNS_NON_NULL
918 static T* create(Args&& ... args)
920 AllocatorType<T> alloc;
921 using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
923 auto deleter = [&](T *
object)
925 AllocatorTraits::deallocate(alloc,
object, 1);
927 std::unique_ptr<T, decltype(deleter)>
object(AllocatorTraits::allocate(alloc, 1), deleter);
928 AllocatorTraits::construct(alloc,
object.
get(), std::forward<Args>(args)...);
929 JSON_ASSERT(
object !=
nullptr);
930 return object.release();
982 json_value() =
default;
984 json_value(
boolean_t v) noexcept : boolean(v) {}
992 json_value(value_t t)
998 object = create<object_t>();
1004 array = create<array_t>();
1010 string = create<string_t>(
"");
1016 binary = create<binary_t>();
1055 JSON_THROW(other_error::create(500,
"961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1"));
1065 string = create<string_t>(
value);
1071 string = create<string_t>(std::move(
value));
1077 object = create<object_t>(
value);
1083 object = create<object_t>(std::move(
value));
1122 void destroy(value_t t) noexcept
1125 std::vector<basic_json> stack;
1136 for (
auto&& it : *
object)
1138 stack.push_back(std::move(it.second));
1142 while (!stack.empty())
1145 basic_json current_item(std::move(stack.back()));
1150 if (current_item.is_array())
1152 std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
1153 std::back_inserter(stack));
1155 current_item.m_value.array->clear();
1157 else if (current_item.is_object())
1159 for (
auto&& it : *current_item.m_value.object)
1161 stack.push_back(std::move(it.second));
1164 current_item.m_value.object->clear();
1175 AllocatorType<object_t> alloc;
1176 std::allocator_traits<decltype(alloc)>::destroy(alloc,
object);
1177 std::allocator_traits<decltype(alloc)>::deallocate(alloc,
object, 1);
1183 AllocatorType<array_t> alloc;
1184 std::allocator_traits<decltype(alloc)>::destroy(alloc,
array);
1185 std::allocator_traits<decltype(alloc)>::deallocate(alloc,
array, 1);
1191 AllocatorType<string_t> alloc;
1192 std::allocator_traits<decltype(alloc)>::destroy(alloc,
string);
1193 std::allocator_traits<decltype(alloc)>::deallocate(alloc,
string, 1);
1199 AllocatorType<binary_t> alloc;
1200 std::allocator_traits<decltype(alloc)>::destroy(alloc,
binary);
1201 std::allocator_traits<decltype(alloc)>::deallocate(alloc,
binary, 1);
1222 void assert_invariant() const noexcept
1225 JSON_ASSERT(m_type !=
value_t::array || m_value.array !=
nullptr);
1343 : m_type(v), m_value(v)
1435 template <
typename CompatibleType,
1436 typename U = detail::uncvref_t<CompatibleType>,
1437 detail::enable_if_t <
1440 JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
1441 std::forward<CompatibleType>(val))))
1443 JSONSerializer<U>::to_json(*
this, std::forward<CompatibleType>(val));
1473 template <
typename BasicJsonType,
1474 detail::enable_if_t <
1478 using other_boolean_t =
typename BasicJsonType::boolean_t;
1479 using other_number_float_t =
typename BasicJsonType::number_float_t;
1480 using other_number_integer_t =
typename BasicJsonType::number_integer_t;
1481 using other_number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
1482 using other_string_t =
typename BasicJsonType::string_t;
1483 using other_object_t =
typename BasicJsonType::object_t;
1484 using other_array_t =
typename BasicJsonType::array_t;
1485 using other_binary_t =
typename BasicJsonType::binary_t;
1490 JSONSerializer<other_boolean_t>::to_json(*
this, val.template get<other_boolean_t>());
1493 JSONSerializer<other_number_float_t>::to_json(*
this, val.template get<other_number_float_t>());
1496 JSONSerializer<other_number_integer_t>::to_json(*
this, val.template get<other_number_integer_t>());
1499 JSONSerializer<other_number_unsigned_t>::to_json(*
this, val.template get<other_number_unsigned_t>());
1502 JSONSerializer<other_string_t>::to_json(*
this, val.template get_ref<const other_string_t&>());
1505 JSONSerializer<other_object_t>::to_json(*
this, val.template get_ref<const other_object_t&>());
1508 JSONSerializer<other_array_t>::to_json(*
this, val.template get_ref<const other_array_t&>());
1511 JSONSerializer<other_binary_t>::to_json(*
this, val.template get_ref<const other_binary_t&>());
1600 bool type_deduction =
true,
1605 bool is_an_object = std::all_of(init.begin(), init.end(),
1608 return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
1612 if (!type_deduction)
1617 is_an_object =
false;
1621 if (JSON_HEDLEY_UNLIKELY(manual_type ==
value_t::object && !is_an_object))
1623 JSON_THROW(type_error::create(301,
"cannot create object from initializer list"));
1635 auto element = element_ref.moved_or_copied();
1636 m_value.object->emplace(
1637 std::move(*((*element.m_value.array)[0].m_value.string)),
1638 std::move((*element.m_value.array)[1]));
1645 m_value.array = create<array_t>(init.begin(), init.end());
1678 JSON_HEDLEY_WARN_UNUSED_RESULT
1715 JSON_HEDLEY_WARN_UNUSED_RESULT
1720 res.m_value =
binary_t(init, subtype);
1725 JSON_HEDLEY_WARN_UNUSED_RESULT
1730 res.m_value = std::move(init);
1735 JSON_HEDLEY_WARN_UNUSED_RESULT
1740 res.m_value =
binary_t(std::move(init), subtype);
1781 JSON_HEDLEY_WARN_UNUSED_RESULT
1825 JSON_HEDLEY_WARN_UNUSED_RESULT
1856 m_value.array = create<array_t>(cnt, val);
1915 template <
class InputIT,
typename std::enable_if <
1916 std::is_same<InputIT, typename basic_json_t::iterator>::value ||
1917 std::is_same<InputIT, typename basic_json_t::const_iterator>::value,
int >
::type = 0 >
1920 JSON_ASSERT(first.m_object !=
nullptr);
1921 JSON_ASSERT(last.m_object !=
nullptr);
1924 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
1926 JSON_THROW(invalid_iterator::create(201,
"iterators are not compatible"));
1930 m_type = first.m_object->m_type;
1941 if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
1942 || !last.m_it.primitive_iterator.is_end()))
1944 JSON_THROW(invalid_iterator::create(204,
"iterators out of range"));
1957 m_value.number_integer = first.m_object->m_value.number_integer;
1963 m_value.number_unsigned = first.m_object->m_value.number_unsigned;
1969 m_value.number_float = first.m_object->m_value.number_float;
1975 m_value.boolean = first.m_object->m_value.boolean;
1981 m_value = *first.m_object->m_value.string;
1987 m_value.object = create<object_t>(first.m_it.object_iterator,
1988 last.m_it.object_iterator);
1994 m_value.array = create<array_t>(first.m_it.array_iterator,
1995 last.m_it.array_iterator);
2001 m_value = *first.m_object->m_value.binary;
2006 JSON_THROW(invalid_iterator::create(206,
"cannot construct with iterators from " +
2007 std::string(first.m_object->type_name())));
2018 template<
typename JsonRef,
2019 detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
2020 std::is_same<typename JsonRef::value_type, basic_json>>
::value,
int> = 0 >
2049 : m_type(other.m_type)
2052 other.assert_invariant();
2058 m_value = *other.m_value.object;
2064 m_value = *other.m_value.array;
2070 m_value = *other.m_value.string;
2076 m_value = other.m_value.boolean;
2082 m_value = other.m_value.number_integer;
2088 m_value = other.m_value.number_unsigned;
2094 m_value = other.m_value.number_float;
2100 m_value = *other.m_value.binary;
2138 : m_type(std::move(other.m_type)),
2139 m_value(std::move(other.m_value))
2142 other.assert_invariant();
2175 std::is_nothrow_move_constructible<value_t>::value&&
2176 std::is_nothrow_move_assignable<value_t>::value&&
2177 std::is_nothrow_move_constructible<json_value>::value&&
2178 std::is_nothrow_move_assignable<json_value>::value
2182 other.assert_invariant();
2185 swap(m_type, other.m_type);
2186 swap(m_value, other.m_value);
2210 m_value.destroy(m_type);
2272 const char indent_char =
' ',
2273 const bool ensure_ascii =
false,
2281 s.
dump(*
this,
true, ensure_ascii,
static_cast<unsigned int>(indent));
2285 s.
dump(*
this,
false, ensure_ascii, 0);
2699 return m_value.boolean;
2702 JSON_THROW(type_error::create(302,
"type must be boolean, but is " + std::string(
type_name())));
2708 return is_object() ? m_value.object :
nullptr;
2714 return is_object() ? m_value.object :
nullptr;
2720 return is_array() ? m_value.array :
nullptr;
2724 constexpr
const array_t* get_impl_ptr(
const array_t* )
const noexcept
2726 return is_array() ? m_value.array :
nullptr;
2732 return is_string() ? m_value.string :
nullptr;
2738 return is_string() ? m_value.string :
nullptr;
2744 return is_boolean() ? &m_value.boolean :
nullptr;
2750 return is_boolean() ? &m_value.boolean :
nullptr;
2792 return is_binary() ? m_value.binary :
nullptr;
2798 return is_binary() ? m_value.binary :
nullptr;
2812 template<
typename ReferenceType,
typename ThisType>
2813 static ReferenceType get_ref_impl(ThisType& obj)
2816 auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
2818 if (JSON_HEDLEY_LIKELY(ptr !=
nullptr))
2823 JSON_THROW(type_error::create(303,
"incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
2845 template<
typename BasicJsonType, detail::enable_if_t<
2846 std::is_same<typename std::remove_const<BasicJsonType>::type, basic_json_t>
::value,
2868 template <
typename BasicJsonType, detail::enable_if_t <
2869 !std::is_same<BasicJsonType, basic_json>::value&&
2915 template <
typename ValueTypeCV,
typename ValueType = detail::uncvref_t<ValueTypeCV>,
2916 detail::enable_if_t <
2917 !detail::is_basic_json<ValueType>::value &&
2918 detail::has_from_json<basic_json_t, ValueType>::value &&
2919 !detail::has_non_default_from_json<basic_json_t, ValueType>::value,
2921 ValueType
get() const noexcept(noexcept(
2922 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
2927 static_assert(!std::is_reference<ValueTypeCV>::value,
2928 "get() cannot be used with reference types, you might want to use get_ref()");
2929 static_assert(std::is_default_constructible<ValueType>::value,
2930 "types must be DefaultConstructible when used with get()");
2933 JSONSerializer<ValueType>::from_json(*
this, ret);
2968 template <
typename ValueTypeCV,
typename ValueType = detail::uncvref_t<ValueTypeCV>,
2969 detail::enable_if_t < !std::is_same<basic_json_t, ValueType>::value &&
2970 detail::has_non_default_from_json<basic_json_t, ValueType>::value,
2972 ValueType
get() const noexcept(noexcept(
2973 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
2975 static_assert(!std::is_reference<ValueTypeCV>::value,
2976 "get() cannot be used with reference types, you might want to use get_ref()");
2977 return JSONSerializer<ValueType>::from_json(*
this);
3013 template <
typename ValueType,
3014 detail::enable_if_t <
3018 ValueType &
get_to(ValueType& v)
const noexcept(noexcept(
3019 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
3021 JSONSerializer<ValueType>::from_json(*
this, v);
3027 template<
typename ValueType,
3028 detail::enable_if_t <
3031 ValueType &
get_to(ValueType& v)
const
3038 typename T, std::size_t N,
3039 typename Array = T (&)[N],
3040 detail::enable_if_t <
3041 detail::has_from_json<basic_json_t, Array>::value,
int > = 0 >
3042 Array
get_to(T (&v)[N])
const
3043 noexcept(noexcept(JSONSerializer<Array>::from_json(
3044 std::declval<const basic_json_t&>(), v)))
3046 JSONSerializer<Array>::from_json(*
this, v);
3077 template<
typename PointerType,
typename std::enable_if<
3078 std::is_pointer<PointerType>::value,
int>
::type = 0>
3079 auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
3082 return get_impl_ptr(
static_cast<PointerType
>(
nullptr));
3089 template <
typename PointerType,
typename std::enable_if <
3090 std::is_pointer<PointerType>::value&&
3091 std::is_const<typename std::remove_pointer<PointerType>::type>
::value,
int >
::type = 0 >
3092 constexpr
auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
3095 return get_impl_ptr(
static_cast<PointerType
>(
nullptr));
3125 template<
typename PointerType,
typename std::enable_if<
3126 std::is_pointer<PointerType>::value,
int>
::type = 0>
3127 auto get() noexcept -> decltype(std::declval<basic_json_t&>().template
get_ptr<PointerType>())
3130 return get_ptr<PointerType>();
3137 template<
typename PointerType,
typename std::enable_if<
3138 std::is_pointer<PointerType>::value,
int>
::type = 0>
3139 constexpr
auto get() const noexcept -> decltype(std::declval<const basic_json_t&>().template
get_ptr<PointerType>())
3142 return get_ptr<PointerType>();
3171 template<
typename ReferenceType,
typename std::enable_if<
3172 std::is_reference<ReferenceType>::value,
int>
::type = 0>
3176 return get_ref_impl<ReferenceType>(*
this);
3183 template <
typename ReferenceType,
typename std::enable_if <
3184 std::is_reference<ReferenceType>::value&&
3185 std::is_const<typename std::remove_reference<ReferenceType>::type>
::value,
int >
::type = 0 >
3189 return get_ref_impl<ReferenceType>(*
this);
3221 template <
typename ValueType,
typename std::enable_if <
3222 !std::is_pointer<ValueType>::value&&
3223 !std::is_same<ValueType, detail::json_ref<basic_json>>
::value&&
3224 !std::is_same<ValueType, typename string_t::value_type>::value&&
3226 && !std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>
::value
3227 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
3228 && !std::is_same<ValueType, typename std::string_view>::value
3230 && detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value
3232 JSON_EXPLICIT
operator ValueType()
const
3235 return get<ValueType>();
3251 JSON_THROW(type_error::create(302,
"type must be binary, but is " + std::string(
type_name())));
3254 return *get_ptr<binary_t*>();
3262 JSON_THROW(type_error::create(302,
"type must be binary, but is " + std::string(
type_name())));
3265 return *get_ptr<const binary_t*>();
3308 if (JSON_HEDLEY_LIKELY(
is_array()))
3312 return m_value.array->at(idx);
3314 JSON_CATCH (std::out_of_range&)
3317 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
3322 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
3355 if (JSON_HEDLEY_LIKELY(
is_array()))
3359 return m_value.array->at(idx);
3361 JSON_CATCH (std::out_of_range&)
3364 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
3369 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
3410 return m_value.object->at(key);
3412 JSON_CATCH (std::out_of_range&)
3415 JSON_THROW(out_of_range::create(403,
"key '" + key +
"' not found"));
3420 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
3461 return m_value.object->at(key);
3463 JSON_CATCH (std::out_of_range&)
3466 JSON_THROW(out_of_range::create(403,
"key '" + key +
"' not found"));
3471 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name())));
3506 m_value.array = create<array_t>();
3511 if (JSON_HEDLEY_LIKELY(
is_array()))
3514 if (idx >= m_value.array->size())
3516 m_value.array->insert(m_value.array->end(),
3517 idx - m_value.array->size() + 1,
3521 return m_value.array->operator[](idx);
3524 JSON_THROW(type_error::create(305,
"cannot use operator[] with a numeric argument with " + std::string(
type_name())));
3549 if (JSON_HEDLEY_LIKELY(
is_array()))
3551 return m_value.array->operator[](idx);
3554 JSON_THROW(type_error::create(305,
"cannot use operator[] with a numeric argument with " + std::string(
type_name())));
3590 m_value.object = create<object_t>();
3597 return m_value.object->operator[](key);
3600 JSON_THROW(type_error::create(305,
"cannot use operator[] with a string argument with " + std::string(
type_name())));
3638 JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
3639 return m_value.object->find(key)->second;
3642 JSON_THROW(type_error::create(305,
"cannot use operator[] with a string argument with " + std::string(
type_name())));
3672 template<
typename T>
3673 JSON_HEDLEY_NON_NULL(2)
3687 return m_value.object->operator[](key);
3690 JSON_THROW(type_error::create(305,
"cannot use operator[] with a string argument with " + std::string(
type_name())));
3723 template<
typename T>
3724 JSON_HEDLEY_NON_NULL(2)
3730 JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
3731 return m_value.object->find(key)->second;
3734 JSON_THROW(type_error::create(305,
"cannot use operator[] with a string argument with " + std::string(
type_name())));
3788 template <
class ValueType,
typename std::enable_if <
3790 && !std::is_same<value_t, ValueType>::value,
int >
::type = 0 >
3791 ValueType
value(
const typename object_t::key_type& key,
const ValueType& default_value)
const
3797 const auto it =
find(key);
3800 return it->template get<ValueType>();
3803 return default_value;
3806 JSON_THROW(type_error::create(306,
"cannot use value() with " + std::string(
type_name())));
3813 string_t value(
const typename object_t::key_type& key,
const char* default_value)
const
3861 template<
class ValueType,
typename std::enable_if<
3871 return ptr.get_checked(
this).template get<ValueType>();
3875 return default_value;
3879 JSON_THROW(type_error::create(306,
"cannot use value() with " + std::string(
type_name())));
3886 JSON_HEDLEY_NON_NULL(3)
4024 template <
class IteratorType,
typename std::enable_if <
4025 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
4026 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value,
int >
::type
4031 if (JSON_HEDLEY_UNLIKELY(
this != pos.m_object))
4033 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
4036 IteratorType result =
end();
4047 if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
4049 JSON_THROW(invalid_iterator::create(205,
"iterator out of range"));
4054 AllocatorType<string_t> alloc;
4055 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
4056 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
4057 m_value.string =
nullptr;
4061 AllocatorType<binary_t> alloc;
4062 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
4063 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
4064 m_value.binary =
nullptr;
4074 result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
4080 result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
4085 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
4137 template <
class IteratorType,
typename std::enable_if <
4138 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
4139 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value,
int >
::type
4141 IteratorType
erase(IteratorType first, IteratorType last)
4144 if (JSON_HEDLEY_UNLIKELY(
this != first.m_object ||
this != last.m_object))
4146 JSON_THROW(invalid_iterator::create(203,
"iterators do not fit current value"));
4149 IteratorType result =
end();
4160 if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
4161 || !last.m_it.primitive_iterator.is_end()))
4163 JSON_THROW(invalid_iterator::create(204,
"iterators out of range"));
4168 AllocatorType<string_t> alloc;
4169 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
4170 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
4171 m_value.string =
nullptr;
4175 AllocatorType<binary_t> alloc;
4176 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
4177 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
4178 m_value.binary =
nullptr;
4188 result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
4189 last.m_it.object_iterator);
4195 result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
4196 last.m_it.array_iterator);
4201 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
4241 return m_value.object->erase(key);
4244 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
4274 if (JSON_HEDLEY_LIKELY(
is_array()))
4276 if (JSON_HEDLEY_UNLIKELY(idx >=
size()))
4278 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
4281 m_value.array->erase(m_value.array->begin() +
static_cast<difference_type>(idx));
4285 JSON_THROW(type_error::create(307,
"cannot use erase() with " + std::string(
type_name())));
4323 template<
typename KeyT>
4326 auto result =
end();
4330 result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
4340 template<
typename KeyT>
4343 auto result =
cend();
4347 result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
4374 template<
typename KeyT>
4378 return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
4406 template <
typename KeyT,
typename std::enable_if <
4410 return is_object() && m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
4441 return ptr.contains(
this);
4785 JSON_HEDLEY_DEPRECATED_FOR(3.1.0,
items())
4794 JSON_HEDLEY_DEPRECATED_FOR(3.1.0,
items())
4946 return m_value.array->empty();
4952 return m_value.object->empty();
5019 return m_value.array->size();
5025 return m_value.object->size();
5084 return m_value.array->max_size();
5090 return m_value.object->max_size();
5154 m_value.number_integer = 0;
5160 m_value.number_unsigned = 0;
5166 m_value.number_float = 0.0;
5172 m_value.boolean =
false;
5178 m_value.string->clear();
5184 m_value.binary->clear();
5190 m_value.array->clear();
5196 m_value.object->clear();
5230 JSON_THROW(type_error::create(308,
"cannot use push_back() with " + std::string(
type_name())));
5242 m_value.array->push_back(std::move(val));
5265 JSON_THROW(type_error::create(308,
"cannot use push_back() with " + std::string(
type_name())));
5277 m_value.array->push_back(val);
5315 JSON_THROW(type_error::create(308,
"cannot use push_back() with " + std::string(
type_name())));
5327 m_value.object->insert(val);
5367 if (
is_object() && init.size() == 2 && (*init.begin())->is_string())
5369 basic_json&& key = init.begin()->moved_or_copied();
5370 push_back(
typename object_t::value_type(
5371 std::move(key.get_ref<
string_t&>()), (init.begin() + 1)->moved_or_copied()));
5412 template<
class... Args>
5418 JSON_THROW(type_error::create(311,
"cannot use emplace_back() with " + std::string(
type_name())));
5430 #ifdef JSON_HAS_CPP_17
5431 return m_value.array->emplace_back(std::forward<Args>(args)...);
5433 m_value.array->emplace_back(std::forward<Args>(args)...);
5434 return m_value.array->back();
5465 template<
class... Args>
5466 std::pair<iterator, bool>
emplace(Args&& ... args)
5471 JSON_THROW(type_error::create(311,
"cannot use emplace() with " + std::string(
type_name())));
5483 auto res = m_value.object->emplace(std::forward<Args>(args)...);
5486 it.m_it.object_iterator = res.first;
5489 return {it, res.second};
5495 template<
typename... Args>
5499 JSON_ASSERT(m_value.array !=
nullptr);
5501 auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.
array_iterator);
5502 m_value.array->insert(pos.m_it.
array_iterator, std::forward<Args>(args)...);
5503 result.m_it.
array_iterator = m_value.array->begin() + insert_pos;
5537 if (JSON_HEDLEY_LIKELY(
is_array()))
5540 if (JSON_HEDLEY_UNLIKELY(pos.m_object !=
this))
5542 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
5549 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
5588 if (JSON_HEDLEY_LIKELY(
is_array()))
5591 if (JSON_HEDLEY_UNLIKELY(pos.m_object !=
this))
5593 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
5600 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
5636 if (JSON_HEDLEY_UNLIKELY(!
is_array()))
5638 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
5642 if (JSON_HEDLEY_UNLIKELY(pos.m_object !=
this))
5644 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
5648 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
5650 JSON_THROW(invalid_iterator::create(210,
"iterators do not fit"));
5653 if (JSON_HEDLEY_UNLIKELY(first.m_object ==
this))
5655 JSON_THROW(invalid_iterator::create(211,
"passed iterators may not belong to container"));
5689 if (JSON_HEDLEY_UNLIKELY(!
is_array()))
5691 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
5695 if (JSON_HEDLEY_UNLIKELY(pos.m_object !=
this))
5697 JSON_THROW(invalid_iterator::create(202,
"iterator does not fit current value"));
5732 JSON_THROW(type_error::create(309,
"cannot use insert() with " + std::string(
type_name())));
5736 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
5738 JSON_THROW(invalid_iterator::create(210,
"iterators do not fit"));
5742 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
5744 JSON_THROW(invalid_iterator::create(202,
"iterators first and last must point to objects"));
5775 m_value.object = create<object_t>();
5781 JSON_THROW(type_error::create(312,
"cannot use update() with " + std::string(
type_name())));
5783 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
5785 JSON_THROW(type_error::create(312,
"cannot use update() with " + std::string(j.type_name())));
5788 for (
auto it = j.cbegin(); it != j.cend(); ++it)
5790 m_value.object->operator[](it.key()) = it.value();
5826 m_value.object = create<object_t>();
5832 JSON_THROW(type_error::create(312,
"cannot use update() with " + std::string(
type_name())));
5836 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
5838 JSON_THROW(invalid_iterator::create(210,
"iterators do not fit"));
5842 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()
5843 || !last.m_object->is_object()))
5845 JSON_THROW(invalid_iterator::create(202,
"iterators first and last must point to objects"));
5848 for (
auto it = first; it != last; ++it)
5850 m_value.object->operator[](it.key()) = it.value();
5872 std::is_nothrow_move_constructible<value_t>::value&&
5873 std::is_nothrow_move_assignable<value_t>::value&&
5874 std::is_nothrow_move_constructible<json_value>::value&&
5875 std::is_nothrow_move_assignable<json_value>::value
5878 std::swap(m_type, other.m_type);
5879 std::swap(m_value, other.m_value);
5902 std::is_nothrow_move_constructible<value_t>::value&&
5903 std::is_nothrow_move_assignable<value_t>::value&&
5904 std::is_nothrow_move_constructible<json_value>::value&&
5905 std::is_nothrow_move_assignable<json_value>::value
5934 if (JSON_HEDLEY_LIKELY(
is_array()))
5936 std::swap(*(m_value.array), other);
5940 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
5969 std::swap(*(m_value.object), other);
5973 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
6002 std::swap(*(m_value.string), other);
6006 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
6035 std::swap(*(m_value.binary), other);
6039 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
6049 std::swap(*(m_value.binary), other);
6053 JSON_THROW(type_error::create(310,
"cannot use swap() with " + std::string(
type_name())));
6124 const auto lhs_type = lhs.type();
6125 const auto rhs_type = rhs.type();
6127 if (lhs_type == rhs_type)
6132 return *lhs.m_value.array == *rhs.m_value.array;
6135 return *lhs.m_value.object == *rhs.m_value.object;
6141 return *lhs.m_value.string == *rhs.m_value.string;
6144 return lhs.m_value.boolean == rhs.m_value.boolean;
6147 return lhs.m_value.number_integer == rhs.m_value.number_integer;
6150 return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned;
6153 return lhs.m_value.number_float == rhs.m_value.number_float;
6156 return *lhs.m_value.binary == *rhs.m_value.binary;
6164 return static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float;
6168 return lhs.m_value.number_float ==
static_cast<number_float_t>(rhs.m_value.number_integer);
6172 return static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float;
6176 return lhs.m_value.number_float ==
static_cast<number_float_t>(rhs.m_value.number_unsigned);
6180 return static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer;
6184 return lhs.m_value.number_integer ==
static_cast<number_integer_t>(rhs.m_value.number_unsigned);
6194 template<
typename ScalarType,
typename std::enable_if<
6195 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6205 template<
typename ScalarType,
typename std::enable_if<
6206 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6232 return !(lhs == rhs);
6239 template<
typename ScalarType,
typename std::enable_if<
6240 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6250 template<
typename ScalarType,
typename std::enable_if<
6251 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6285 const auto lhs_type = lhs.type();
6286 const auto rhs_type = rhs.type();
6288 if (lhs_type == rhs_type)
6295 return (*lhs.m_value.array) < (*rhs.m_value.array);
6298 return (*lhs.m_value.object) < (*rhs.m_value.object);
6304 return (*lhs.m_value.string) < (*rhs.m_value.string);
6307 return (lhs.m_value.boolean) < (rhs.m_value.boolean);
6310 return (lhs.m_value.number_integer) < (rhs.m_value.number_integer);
6313 return (lhs.m_value.number_unsigned) < (rhs.m_value.number_unsigned);
6316 return (lhs.m_value.number_float) < (rhs.m_value.number_float);
6319 return (*lhs.m_value.binary) < (*rhs.m_value.binary);
6327 return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
6331 return lhs.m_value.number_float <
static_cast<number_float_t>(rhs.m_value.number_integer);
6335 return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
6339 return lhs.m_value.number_float <
static_cast<number_float_t>(rhs.m_value.number_unsigned);
6343 return lhs.m_value.number_integer <
static_cast<number_integer_t>(rhs.m_value.number_unsigned);
6347 return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
6360 template<
typename ScalarType,
typename std::enable_if<
6361 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6371 template<
typename ScalarType,
typename std::enable_if<
6372 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6399 return !(rhs < lhs);
6406 template<
typename ScalarType,
typename std::enable_if<
6407 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6417 template<
typename ScalarType,
typename std::enable_if<
6418 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6445 return !(lhs <= rhs);
6452 template<
typename ScalarType,
typename std::enable_if<
6453 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6463 template<
typename ScalarType,
typename std::enable_if<
6464 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6491 return !(lhs < rhs);
6498 template<
typename ScalarType,
typename std::enable_if<
6499 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6509 template<
typename ScalarType,
typename std::enable_if<
6510 std::is_scalar<ScalarType>::value,
int>
::type = 0>
6559 const bool pretty_print = o.width() > 0;
6560 const auto indentation = pretty_print ? o.width() : 0;
6567 s.
dump(j, pretty_print,
false,
static_cast<unsigned int>(indentation));
6579 JSON_HEDLEY_DEPRECATED_FOR(3.0.0,
operator<<(std::ostream&,
const basic_json&))
6580 friend std::ostream& operator>>(const
basic_json& j, std::ostream& o)
6646 template<
typename InputType>
6647 JSON_HEDLEY_WARN_UNUSED_RESULT
6650 const bool allow_exceptions =
true,
6651 const bool ignore_comments =
false)
6654 parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(
true, result);
6684 template<
typename IteratorType>
6685 JSON_HEDLEY_WARN_UNUSED_RESULT
6689 const bool allow_exceptions =
true,
6690 const bool ignore_comments =
false)
6693 parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(
true, result);
6697 JSON_HEDLEY_WARN_UNUSED_RESULT
6698 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
parse(ptr, ptr + len))
6701 const bool allow_exceptions =
true,
6702 const bool ignore_comments =
false)
6705 parser(i.get(), cb, allow_exceptions, ignore_comments).parse(
true, result);
6739 template<
typename InputType>
6741 const bool ignore_comments =
false)
6743 return parser(detail::input_adapter(std::forward<InputType>(i)),
nullptr,
false, ignore_comments).accept(
true);
6746 template<
typename IteratorType>
6747 static bool accept(IteratorType first, IteratorType last,
6748 const bool ignore_comments =
false)
6750 return parser(detail::input_adapter(std::move(first), std::move(last)),
nullptr,
false, ignore_comments).accept(
true);
6753 JSON_HEDLEY_WARN_UNUSED_RESULT
6754 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
accept(ptr, ptr + len))
6755 static bool accept(detail::span_input_adapter&& i,
6756 const bool ignore_comments =
false)
6758 return parser(i.get(),
nullptr,
false, ignore_comments).accept(
true);
6801 template <
typename InputType,
typename SAX>
6802 JSON_HEDLEY_NON_NULL(2)
6805 const
bool strict = true,
6806 const
bool ignore_comments = false)
6808 auto ia = detail::input_adapter(std::forward<InputType>(i));
6809 return format == input_format_t::json
6810 ? parser(std::move(ia),
nullptr,
true, ignore_comments).sax_parse(sax, strict)
6814 template<
class IteratorType,
class SAX>
6815 JSON_HEDLEY_NON_NULL(3)
6816 static
bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
6817 input_format_t format = input_format_t::
json,
6818 const
bool strict = true,
6819 const
bool ignore_comments = false)
6821 auto ia = detail::input_adapter(std::move(first), std::move(last));
6822 return format == input_format_t::json
6823 ? parser(std::move(ia),
nullptr,
true, ignore_comments).sax_parse(sax, strict)
6827 template <
typename SAX>
6828 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
sax_parse(ptr, ptr + len, ...))
6829 JSON_HEDLEY_NON_NULL(2)
6830 static
bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
6831 input_format_t format = input_format_t::
json,
6832 const
bool strict = true,
6833 const
bool ignore_comments = false)
6836 return format == input_format_t::json
6837 ? parser(std::move(ia),
nullptr,
true, ignore_comments).sax_parse(sax, strict)
6838 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
6849 JSON_HEDLEY_DEPRECATED_FOR(3.0.0,
operator>>(std::istream&,
basic_json&))
6882 parser(detail::input_adapter(i)).parse(
false, j);
6923 JSON_HEDLEY_RETURNS_NON_NULL
6959 json_value m_value = {};
7065 std::vector<uint8_t> result;
7072 binary_writer<uint8_t>(o).write_cbor(j);
7077 binary_writer<char>(o).write_cbor(j);
7160 std::vector<uint8_t> result;
7167 binary_writer<uint8_t>(o).write_msgpack(j);
7172 binary_writer<char>(o).write_msgpack(j);
7262 const bool use_size =
false,
7263 const bool use_type =
false)
7265 std::vector<uint8_t> result;
7266 to_ubjson(j, result, use_size, use_type);
7271 const bool use_size =
false,
const bool use_type =
false)
7273 binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
7277 const bool use_size =
false,
const bool use_type =
false)
7279 binary_writer<char>(o).write_ubjson(j, use_size, use_type);
7341 std::vector<uint8_t> result;
7470 template<
typename InputType>
7471 JSON_HEDLEY_WARN_UNUSED_RESULT
7473 const bool strict =
true,
7474 const bool allow_exceptions =
true,
7479 auto ia = detail::input_adapter(std::forward<InputType>(i));
7487 template<
typename IteratorType>
7488 JSON_HEDLEY_WARN_UNUSED_RESULT
7490 const bool strict =
true,
7491 const bool allow_exceptions =
true,
7496 auto ia = detail::input_adapter(std::move(first), std::move(last));
7501 template<
typename T>
7502 JSON_HEDLEY_WARN_UNUSED_RESULT
7503 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_cbor(ptr, ptr + len))
7505 const bool strict =
true,
7506 const bool allow_exceptions =
true,
7509 return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
7513 JSON_HEDLEY_WARN_UNUSED_RESULT
7514 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_cbor(ptr, ptr + len))
7516 const bool strict =
true,
7517 const bool allow_exceptions =
true,
7521 detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
7523 const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
7613 template<
typename InputType>
7614 JSON_HEDLEY_WARN_UNUSED_RESULT
7616 const bool strict =
true,
7617 const bool allow_exceptions =
true)
7621 auto ia = detail::input_adapter(std::forward<InputType>(i));
7629 template<
typename IteratorType>
7630 JSON_HEDLEY_WARN_UNUSED_RESULT
7632 const bool strict =
true,
7633 const bool allow_exceptions =
true)
7637 auto ia = detail::input_adapter(std::move(first), std::move(last));
7643 template<
typename T>
7644 JSON_HEDLEY_WARN_UNUSED_RESULT
7645 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_msgpack(ptr, ptr + len))
7647 const bool strict =
true,
7648 const bool allow_exceptions =
true)
7650 return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
7653 JSON_HEDLEY_WARN_UNUSED_RESULT
7654 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_msgpack(ptr, ptr + len))
7656 const bool strict =
true,
7657 const bool allow_exceptions =
true)
7660 detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
7662 const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
7729 template<
typename InputType>
7730 JSON_HEDLEY_WARN_UNUSED_RESULT
7732 const bool strict =
true,
7733 const bool allow_exceptions =
true)
7737 auto ia = detail::input_adapter(std::forward<InputType>(i));
7745 template<
typename IteratorType>
7746 JSON_HEDLEY_WARN_UNUSED_RESULT
7748 const bool strict =
true,
7749 const bool allow_exceptions =
true)
7753 auto ia = detail::input_adapter(std::move(first), std::move(last));
7758 template<
typename T>
7759 JSON_HEDLEY_WARN_UNUSED_RESULT
7760 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_ubjson(ptr, ptr + len))
7762 const bool strict =
true,
7763 const bool allow_exceptions =
true)
7765 return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
7768 JSON_HEDLEY_WARN_UNUSED_RESULT
7769 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_ubjson(ptr, ptr + len))
7771 const bool strict =
true,
7772 const bool allow_exceptions =
true)
7775 detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
7777 const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
7842 template<
typename InputType>
7843 JSON_HEDLEY_WARN_UNUSED_RESULT
7845 const bool strict =
true,
7846 const bool allow_exceptions =
true)
7850 auto ia = detail::input_adapter(std::forward<InputType>(i));
7858 template<
typename IteratorType>
7859 JSON_HEDLEY_WARN_UNUSED_RESULT
7861 const bool strict =
true,
7862 const bool allow_exceptions =
true)
7866 auto ia = detail::input_adapter(std::move(first), std::move(last));
7871 template<
typename T>
7872 JSON_HEDLEY_WARN_UNUSED_RESULT
7873 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_bson(ptr, ptr + len))
7875 const bool strict =
true,
7876 const bool allow_exceptions =
true)
7878 return from_bson(ptr, ptr + len, strict, allow_exceptions);
7881 JSON_HEDLEY_WARN_UNUSED_RESULT
7882 JSON_HEDLEY_DEPRECATED_FOR(3.8.0,
from_bson(ptr, ptr + len))
7884 const bool strict =
true,
7885 const bool allow_exceptions =
true)
7888 detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
7890 const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
7937 return ptr.get_unchecked(
this);
7965 return ptr.get_unchecked(
this);
8008 return ptr.get_checked(
this);
8051 return ptr.get_checked(
this);
8079 json_pointer::flatten(
"", *
this, result);
8115 return json_pointer::unflatten(*
this);
8180 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
8182 const auto get_op = [](
const std::string & op)
8186 return patch_operations::add;
8190 return patch_operations::remove;
8192 if (op ==
"replace")
8194 return patch_operations::replace;
8198 return patch_operations::move;
8202 return patch_operations::copy;
8206 return patch_operations::test;
8209 return patch_operations::invalid;
8224 if (top_pointer != ptr)
8226 result.
at(top_pointer);
8230 const auto last_path = ptr.
back();
8234 switch (parent.m_type)
8240 parent[last_path] = val;
8246 if (last_path ==
"-")
8253 const auto idx = json_pointer::array_index(last_path);
8254 if (JSON_HEDLEY_UNLIKELY(idx > parent.
size()))
8257 JSON_THROW(out_of_range::create(401,
"array index " + std::to_string(idx) +
" is out of range"));
8273 const auto operation_remove = [&result](
json_pointer & ptr)
8276 const auto last_path = ptr.
back();
8284 auto it = parent.
find(last_path);
8285 if (JSON_HEDLEY_LIKELY(it != parent.
end()))
8291 JSON_THROW(out_of_range::create(403,
"key '" + last_path +
"' not found"));
8297 parent.
erase(json_pointer::array_index(last_path));
8302 if (JSON_HEDLEY_UNLIKELY(!json_patch.
is_array()))
8308 for (
const auto& val : json_patch)
8311 const auto get_value = [&val](
const std::string & op,
8312 const std::string & member,
8316 auto it = val.m_value.object->find(member);
8319 const auto error_msg = (op ==
"op") ?
"operation" :
"operation '" + op +
"'";
8322 if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
8324 JSON_THROW(
parse_error::create(105, 0, error_msg +
" must have member '" + member +
"'"));
8328 if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
8330 JSON_THROW(
parse_error::create(105, 0, error_msg +
" must have string member '" + member +
"'"));
8338 if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
8344 const auto op = get_value(
"op",
"op",
true).template get<std::string>();
8345 const auto path = get_value(op,
"path",
true).template get<std::string>();
8350 case patch_operations::add:
8352 operation_add(ptr, get_value(
"add",
"value",
false));
8356 case patch_operations::remove:
8358 operation_remove(ptr);
8362 case patch_operations::replace:
8365 result.
at(ptr) = get_value(
"replace",
"value",
false);
8369 case patch_operations::move:
8371 const auto from_path = get_value(
"move",
"from",
true).template get<std::string>();
8381 operation_remove(from_ptr);
8382 operation_add(ptr, v);
8386 case patch_operations::copy:
8388 const auto from_path = get_value(
"copy",
"from",
true).template get<std::string>();
8397 operation_add(ptr, v);
8401 case patch_operations::test:
8403 bool success =
false;
8408 success = (result.
at(ptr) == get_value(
"test",
"value",
false));
8416 if (JSON_HEDLEY_UNLIKELY(!success))
8418 JSON_THROW(other_error::create(501,
"unsuccessful: " + val.dump()));
8469 JSON_HEDLEY_WARN_UNUSED_RESULT
8471 const std::string& path =
"")
8487 {
"op",
"replace"}, {
"path", path}, {
"value", target}
8498 while (i <
source.size() && i < target.
size())
8501 auto temp_diff =
diff(
source[i], target[i], path +
"/" + std::to_string(i));
8502 result.
insert(result.
end(), temp_diff.begin(), temp_diff.end());
8511 while (i <
source.size())
8518 {
"path", path +
"/" + std::to_string(i)}
8524 while (i < target.
size())
8529 {
"path", path +
"/-"},
8530 {
"value", target[i]}
8541 for (
auto it =
source.cbegin(); it !=
source.cend(); ++it)
8544 const auto key = json_pointer::escape(it.key());
8546 if (target.
find(it.key()) != target.
end())
8549 auto temp_diff =
diff(it.value(), target[it.key()], path +
"/" + key);
8550 result.
insert(result.
end(), temp_diff.begin(), temp_diff.end());
8557 {
"op",
"remove"}, {
"path", path +
"/" + key}
8563 for (
auto it = target.
cbegin(); it != target.
cend(); ++it)
8568 const auto key = json_pointer::escape(it.key());
8571 {
"op",
"add"}, {
"path", path +
"/" + key},
8572 {
"value", it.value()}
8585 {
"op",
"replace"}, {
"path", path}, {
"value", target}
8653 for (
auto it = apply_patch.
begin(); it != apply_patch.
end(); ++it)
8655 if (it.value().is_null())
8667 *
this = apply_patch;
8683 NLOHMANN_BASIC_JSON_TPL_DECLARATION
8731 #ifndef JSON_HAS_CPP_20
8740 is_nothrow_move_constructible<nlohmann::json>::value&&
8741 is_nothrow_move_assignable<nlohmann::json>::value
8764 JSON_HEDLEY_NON_NULL(1)
8765 inline
nlohmann::
json operator "" _json(const
char* s, std::
size_t n)
8783 JSON_HEDLEY_NON_NULL(1)
8784 inline
nlohmann::
json::json_pointer operator "" _json_pointer(const
char* s, std::
size_t n)
8789 #include <nlohmann/detail/macro_unscope.hpp>
a class to store JSON values
Definition: json.hpp:170
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json binary(typename binary_t::container_type &&init, std::uint8_t subtype)
explicitly create a binary array (with subtype)
Definition: json.hpp:1736
constexpr auto get() const noexcept -> decltype(std::declval< const basic_json_t & >().template get_ptr< PointerType >())
get a pointer value (explicit)
Definition: json.hpp:3139
void insert(const_iterator first, const_iterator last)
inserts elements
Definition: json.hpp:5727
detail::parser_callback_t< basic_json > parser_callback_t
per-element parser callback type
Definition: json.hpp:1301
bool contains(KeyT &&key) const
check the existence of an element in a JSON object
Definition: json.hpp:4408
const_reverse_iterator crbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:4693
const_reference operator[](const json_pointer &ptr) const
access specified element via JSON Pointer
Definition: json.hpp:7963
basic_json get() const
get special-case overload
Definition: json.hpp:2848
reference operator[](const json_pointer &ptr)
access specified element via JSON Pointer
Definition: json.hpp:7935
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition: json.hpp:3791
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
Definition: json.hpp:2541
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:700
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
comparison: equal
Definition: json.hpp:6122
friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept
comparison: greater than
Definition: json.hpp:6465
static bool sax_parse(InputType &&i, SAX *sax, input_format_t format=input_format_t::json, const bool strict=true, const bool ignore_comments=false)
generate SAX events
Definition: json.hpp:6803
ReferenceType get_ref()
get a reference value (implicit)
Definition: json.hpp:3173
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition: json.hpp:6648
reference emplace_back(Args &&... args)
add an object to an array
Definition: json.hpp:5413
const_iterator find(KeyT &&key) const
find an element in a JSON object
Definition: json.hpp:4341
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:1342
basic_json & operator=(basic_json other) noexcept(std::is_nothrow_move_constructible< value_t >::value &&std::is_nothrow_move_assignable< value_t >::value &&std::is_nothrow_move_constructible< json_value >::value &&std::is_nothrow_move_assignable< json_value >::value)
copy assignment
Definition: json.hpp:2174
size_type max_size() const noexcept
returns the maximum possible number of elements
Definition: json.hpp:5077
basic_json(CompatibleType &&val) noexcept(noexcept(JSONSerializer< U >::to_json(std::declval< basic_json_t & >(), std::forward< CompatibleType >(val))))
create a JSON value
Definition: json.hpp:1439
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json diff(const basic_json &source, const basic_json &target, const std::string &path="")
creates a diff as a JSON patch
Definition: json.hpp:8470
void erase(const size_type idx)
remove element from a JSON array given an index
Definition: json.hpp:4271
const_reverse_iterator crend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:4722
const_reference at(const typename object_t::key_type &key) const
access specified object element with bounds checking
Definition: json.hpp:3454
static iteration_proxy< iterator > iterator_wrapper(reference ref) noexcept
wrapper to access iterator member functions in range-based for
Definition: json.hpp:4786
reference at(const typename object_t::key_type &key)
access specified object element with bounds checking
Definition: json.hpp:3403
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:4478
basic_json(InputIT first, InputIT last)
construct a JSON container given an iterator range
Definition: json.hpp:1918
static std::vector< uint8_t > to_ubjson(const basic_json &j, const bool use_size=false, const bool use_type=false)
create a UBJSON serialization of a given JSON value
Definition: json.hpp:7261
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json array(initializer_list_t init={})
explicitly create an array from an initializer list
Definition: json.hpp:1782
const_reverse_iterator rend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:4664
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:4589
reference back()
access the last element
Definition: json.hpp:3961
static bool accept(InputType &&i, const bool ignore_comments=false)
check if the input is valid JSON
Definition: json.hpp:6740
StringType string_t
a type for a string
Definition: json.hpp:602
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:5006
void push_back(const basic_json &val)
add an object to an array
Definition: json.hpp:5260
friend std::ostream & operator>>(const basic_json &j, std::ostream &o)
serialize to stream
Definition: json.hpp:6580
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json meta()
returns version information on the library
Definition: json.hpp:343
ValueType value(const json_pointer &ptr, const ValueType &default_value) const
access specified object element via JSON Pointer with default value
Definition: json.hpp:3863
void update(const_reference j)
updates a JSON object from another object, overwriting existing keys
Definition: json.hpp:5769
std::size_t size_type
a type to represent container sizes
Definition: json.hpp:286
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:284
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json binary(const typename binary_t::container_type &init)
explicitly create a binary array (without subtype)
Definition: json.hpp:1679
reference operator[](const typename object_t::key_type &key)
access specified object element
Definition: json.hpp:3584
reference operator+=(basic_json &&val)
add an object to an array
Definition: json.hpp:5250
basic_json(const BasicJsonType &val)
create a JSON value from an existing one
Definition: json.hpp:1476
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
the type of an element const pointer
Definition: json.hpp:294
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_bson(IteratorType first, IteratorType last, const bool strict=true, const bool allow_exceptions=true)
Definition: json.hpp:7860
friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept
comparison: greater than
Definition: json.hpp:6454
typename std::allocator_traits< allocator_type >::pointer pointer
the type of an element pointer
Definition: json.hpp:292
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_cbor(InputType &&i, const bool strict=true, const bool allow_exceptions=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error)
create a JSON value from an input in CBOR format
Definition: json.hpp:7472
BooleanType boolean_t
a type for a boolean
Definition: json.hpp:628
void push_back(initializer_list_t init)
add an object to an object
Definition: json.hpp:5365
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:6924
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition: json.hpp:2271
IteratorType erase(IteratorType pos)
remove element given an iterator
Definition: json.hpp:4028
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_bson(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
Create a JSON value from an input in BSON format.
Definition: json.hpp:7844
constexpr bool is_structured() const noexcept
return whether type is structured
Definition: json.hpp:2382
const_iterator begin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:4488
iteration_proxy< const_iterator > items() const noexcept
helper to access iterator member functions in range-based for
Definition: json.hpp:4876
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:3305
reference front()
access the first element
Definition: json.hpp:3917
constexpr bool is_primitive() const noexcept
return whether type is primitive
Definition: json.hpp:2355
constexpr bool is_number_unsigned() const noexcept
return whether value is an unsigned integer number
Definition: json.hpp:2513
detail::cbor_tag_handler_t cbor_tag_handler_t
how to treat CBOR tags
Definition: json.hpp:234
void swap(object_t &other)
exchanges the values
Definition: json.hpp:5964
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:2563
const_reference front() const
access the first element
Definition: json.hpp:3925
constexpr value_t type() const noexcept
return the type of the JSON value (explicit)
Definition: json.hpp:2324
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:839
json_reverse_iterator< typename basic_json::iterator > reverse_iterator
a reverse iterator for a basic_json container
Definition: json.hpp:301
friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
comparison: less than or equal
Definition: json.hpp:6397
bool empty() const noexcept
checks whether the container is empty.
Definition: json.hpp:4933
friend std::ostream & operator<<(std::ostream &o, const basic_json &j)
serialize to stream
Definition: json.hpp:6556
friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:6511
basic_json(const basic_json &other)
copy constructor
Definition: json.hpp:2048
~basic_json() noexcept
destructor
Definition: json.hpp:2207
BasicJsonType get() const
get special-case overload
Definition: json.hpp:2871
basic_json(basic_json &&other) noexcept
move constructor
Definition: json.hpp:2137
static void to_bson(const basic_json &j, detail::output_adapter< uint8_t > o)
Serializes the given JSON object j to BSON and forwards the corresponding BSON-representation to the ...
Definition: json.hpp:7354
friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:6500
friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
comparison: not equal
Definition: json.hpp:6230
iterator insert(const_iterator pos, size_type cnt, const basic_json &val)
inserts elements
Definition: json.hpp:5585
void swap(typename binary_t::container_type &other)
Definition: json.hpp:6044
friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:6489
void swap(array_t &other)
exchanges the values
Definition: json.hpp:5931
friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept
comparison: less than
Definition: json.hpp:6362
reverse_iterator rend() noexcept
returns an iterator to the reverse-end
Definition: json.hpp:4656
friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept
comparison: less than or equal
Definition: json.hpp:6408
ReferenceType get_ref() const
get a reference value (implicit)
Definition: json.hpp:3186
iterator insert(const_iterator pos, const_iterator first, const_iterator last)
inserts elements
Definition: json.hpp:5633
auto get() noexcept -> decltype(std::declval< basic_json_t & >().template get_ptr< PointerType >())
get a pointer value (explicit)
Definition: json.hpp:3127
const_reference at(const json_pointer &ptr) const
access specified element via JSON Pointer
Definition: json.hpp:8049
const_iterator end() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:4559
void merge_patch(const basic_json &apply_patch)
applies a JSON Merge Patch
Definition: json.hpp:8645
auto get_ptr() noexcept -> decltype(std::declval< basic_json_t & >().get_impl_ptr(std::declval< PointerType >()))
get a pointer value (implicit)
Definition: json.hpp:3079
iterator insert(const_iterator pos, initializer_list_t ilist)
inserts elements
Definition: json.hpp:5686
ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition: json.hpp:549
friend bool operator>(const_reference lhs, const_reference rhs) noexcept
comparison: greater than
Definition: json.hpp:6443
IteratorType erase(IteratorType first, IteratorType last)
remove elements given an iterator range
Definition: json.hpp:4141
ValueType get() const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >(), std::declval< ValueType & >())))
get a value (explicit)
Definition: json.hpp:2921
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:2426
iteration_proxy< iterator > items() noexcept
helper to access iterator member functions in range-based for
Definition: json.hpp:4868
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:4549
void swap(reference other) noexcept(std::is_nothrow_move_constructible< value_t >::value &&std::is_nothrow_move_assignable< value_t >::value &&std::is_nothrow_move_constructible< json_value >::value &&std::is_nothrow_move_assignable< json_value >::value)
exchanges the values
Definition: json.hpp:5871
void clear() noexcept
clears the contents
Definition: json.hpp:5148
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_msgpack(IteratorType first, IteratorType last, const bool strict=true, const bool allow_exceptions=true)
Definition: json.hpp:7631
constexpr bool is_binary() const noexcept
return whether value is a binary array
Definition: json.hpp:2629
static std::vector< uint8_t > to_msgpack(const basic_json &j)
create a MessagePack serialization of a given JSON value
Definition: json.hpp:7158
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition: json.hpp:1826
iterator insert(const_iterator pos, basic_json &&val)
inserts element
Definition: json.hpp:5556
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_ubjson(IteratorType first, IteratorType last, const bool strict=true, const bool allow_exceptions=true)
Definition: json.hpp:7747
reference operator[](size_type idx)
access specified array element
Definition: json.hpp:3500
static void to_bson(const basic_json &j, detail::output_adapter< char > o)
Serializes the given JSON object j to BSON and forwards the corresponding BSON-representation to the ...
Definition: json.hpp:7362
void update(const_iterator first, const_iterator last)
updates a JSON object from another object, overwriting existing keys
Definition: json.hpp:5820
reference at(const json_pointer &ptr)
access specified element via JSON Pointer
Definition: json.hpp:8006
ValueType & get_to(ValueType &v) const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >(), v)))
get a value (explicit)
Definition: json.hpp:3018
void swap(binary_t &other)
exchanges the values
Definition: json.hpp:6030
static std::vector< uint8_t > to_bson(const basic_json &j)
Serializes the given JSON object j to BSON and returns a vector containing the corresponding BSON-rep...
Definition: json.hpp:7339
json_reverse_iterator< typename basic_json::const_iterator > const_reverse_iterator
a const reverse iterator for a basic_json container
Definition: json.hpp:303
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_ubjson(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in UBJSON format
Definition: json.hpp:7731
::nlohmann::json_pointer< basic_json > json_pointer
JSON Pointer, see nlohmann::json_pointer.
Definition: json.hpp:228
const_reverse_iterator rbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:4627
binary_t & get_binary()
Definition: json.hpp:3247
void swap(string_t &other)
exchanges the values
Definition: json.hpp:5997
const_reference back() const
access the last element
Definition: json.hpp:3971
friend bool operator<(const_reference lhs, const_reference rhs) noexcept
comparison: less than
Definition: json.hpp:6283
friend std::istream & operator>>(std::istream &i, basic_json &j)
deserialize from stream
Definition: json.hpp:6880
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json binary(typename binary_t::container_type &&init)
explicitly create a binary array (without subtype)
Definition: json.hpp:1726
friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
comparison: not equal
Definition: json.hpp:6252
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:2607
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:2585
iterator insert_iterator(const_iterator pos, Args &&... args)
Helper for insertion of an iterator.
Definition: json.hpp:5496
basic_json flatten() const
return flattened JSON value
Definition: json.hpp:8076
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:5225
friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
comparison: equal
Definition: json.hpp:6196
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_cbor(IteratorType first, IteratorType last, const bool strict=true, const bool allow_exceptions=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error)
Definition: json.hpp:7489
size_type count(KeyT &&key) const
returns the number of occurrences of a key in a JSON object
Definition: json.hpp:4375
const binary_t & get_binary() const
Definition: json.hpp:3258
constexpr bool is_number() const noexcept
return whether value is a number
Definition: json.hpp:2456
friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept
comparison: less than
Definition: json.hpp:6373
std::pair< iterator, bool > emplace(Args &&... args)
add an object to an object if key does not exist
Definition: json.hpp:5466
reference operator+=(initializer_list_t init)
add an object to an object
Definition: json.hpp:5383
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
Definition: json.hpp:2485
std::initializer_list< detail::json_ref< basic_json > > initializer_list_t
helper type for initializer lists of basic_json values
Definition: json.hpp:236
const_reference operator[](const typename object_t::key_type &key) const
read-only access specified object element
Definition: json.hpp:3633
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json binary(const typename binary_t::container_type &init, std::uint8_t subtype)
explicitly create a binary array (with subtype)
Definition: json.hpp:1716
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:4324
basic_json(std::nullptr_t=nullptr) noexcept
create a null object
Definition: json.hpp:1366
const_reference operator[](size_type idx) const
access specified array element
Definition: json.hpp:3546
AllocatorType< basic_json > allocator_type
the allocator type
Definition: json.hpp:289
nlohmann::byte_container_with_subtype< BinaryType > binary_t
a type for a packed binary type
Definition: json.hpp:910
void push_back(const typename object_t::value_type &val)
add an object to an object
Definition: json.hpp:5310
friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept
comparison: less than or equal
Definition: json.hpp:6419
ValueType get() const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >())))
get a value (explicit); special case
Definition: json.hpp:2972
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json parse(IteratorType first, IteratorType last, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a pair of character iterators
Definition: json.hpp:6686
static std::vector< uint8_t > to_cbor(const basic_json &j)
create a CBOR serialization of a given JSON value
Definition: json.hpp:7063
bool contains(const json_pointer &ptr) const
check the existence of an element in a JSON object given a JSON pointer
Definition: json.hpp:4439
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_msgpack(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in MessagePack format
Definition: json.hpp:7615
basic_json patch(const basic_json &json_patch) const
applies a JSON patch
Definition: json.hpp:8174
string_t value(const typename object_t::key_type &key, const char *default_value) const
overload for a default value of type const char*
Definition: json.hpp:3813
basic_json unflatten() const
unflatten a previously flattened JSON value
Definition: json.hpp:8113
NumberUnsignedType number_unsigned_t
a type for a number (unsigned)
Definition: json.hpp:771
reference operator+=(const typename object_t::value_type &val)
add an object to an object
Definition: json.hpp:5334
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:4518
basic_json(initializer_list_t init, bool type_deduction=true, value_t manual_type=value_t::array)
create a container (array or object) from an initializer list
Definition: json.hpp:1599
const_reference at(size_type idx) const
access specified array element with bounds checking
Definition: json.hpp:3352
iterator insert(const_iterator pos, const basic_json &val)
inserts element
Definition: json.hpp:5534
constexpr bool is_discarded() const noexcept
return whether value is discarded
Definition: json.hpp:2656
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:2404
friend void swap(reference left, reference right) noexcept(std::is_nothrow_move_constructible< value_t >::value &&std::is_nothrow_move_assignable< value_t >::value &&std::is_nothrow_move_constructible< json_value >::value &&std::is_nothrow_move_assignable< json_value >::value)
exchanges the values
Definition: json.hpp:5901
friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
comparison: equal
Definition: json.hpp:6207
ObjectType< StringType, basic_json, object_comparator_t, AllocatorType< std::pair< const StringType, basic_json > >> object_t
a type for an object
Definition: json.hpp:503
reference operator+=(const basic_json &val)
add an object to an array
Definition: json.hpp:5284
size_type erase(const typename object_t::key_type &key)
remove element from a JSON object given a key
Definition: json.hpp:4236
basic_json(size_type cnt, const basic_json &val)
construct an array with count copies of given value
Definition: json.hpp:1853
static allocator_type get_allocator()
returns the allocator associated with the container
Definition: json.hpp:311
constexpr auto get_ptr() const noexcept -> decltype(std::declval< const basic_json_t & >().get_impl_ptr(std::declval< PointerType >()))
get a pointer value (implicit)
Definition: json.hpp:3092
friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
comparison: not equal
Definition: json.hpp:6241
reverse_iterator rbegin() noexcept
returns an iterator to the reverse-beginning
Definition: json.hpp:4619
an internal type for a backed binary type
Definition: byte_container_with_subtype.hpp:25
BinaryType container_type
the type of the underlying container
Definition: byte_container_with_subtype.hpp:28
deserialization of CBOR, MessagePack, and UBJSON values
Definition: binary_reader.hpp:57
bool sax_parse(const input_format_t format, json_sax_t *sax_, const bool strict=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error)
Definition: binary_reader.hpp:94
serialization to CBOR and MessagePack values
Definition: binary_writer.hpp:28
void write_bson(const BasicJsonType &j)
Definition: binary_writer.hpp:48
general exception of the basic_json class
Definition: exceptions.hpp:47
exception indicating errors with iterators
Definition: exceptions.hpp:205
a template for a bidirectional iterator for the basic_json class This class implements a both iterato...
Definition: iter_impl.hpp:40
proxy class for the items() function
Definition: iteration_proxy.hpp:111
Definition: json_ref.hpp:14
a template for a reverse iterator class
Definition: json_reverse_iterator.hpp:35
SAX implementation to create a JSON value from SAX events.
Definition: json_sax.hpp:150
exception indicating other library errors
Definition: exceptions.hpp:344
exception indicating access out of the defined range
Definition: exceptions.hpp:306
Definition: output_adapters.hpp:103
exception indicating a parse error
Definition: exceptions.hpp:119
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: exceptions.hpp:130
Definition: primitive_iterator.hpp:20
Definition: serializer.hpp:41
void dump(const BasicJsonType &val, const bool pretty_print, const bool ensure_ascii, const unsigned int indent_step, const unsigned int current_indent=0)
internal implementation of the serialization function
Definition: serializer.hpp:96
exception indicating executing a member function with a wrong type
Definition: exceptions.hpp:259
JSON Pointer.
Definition: json_pointer.hpp:19
const std::string & back() const
return last reference token
Definition: json_pointer.hpp:270
void pop_back()
remove last reference token
Definition: json_pointer.hpp:246
bool empty() const noexcept
return whether pointer points to the root document
Definition: json_pointer.hpp:317
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
std::function< struct zip_source *(struct zip *)> source
Source creation for adding files.
Definition: zip.hpp:122
bool operator<(const value_t lhs, const value_t rhs) noexcept
comparison operator for JSON types
Definition: value_t.hpp:67
value_t
the JSON type enumeration
Definition: value_t.hpp:41
@ 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)
parse_event_t
Definition: parser.hpp:27
cbor_tag_handler_t
how to treat CBOR tags
Definition: binary_reader.hpp:30
@ error
throw a parse_error exception in case of a tag
error_handler_t
how to treat decoding errors
Definition: serializer.hpp:33
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: hash.hpp:30
std::shared_ptr< output_adapter_protocol< CharType > > output_adapter_t
a type to simplify interfaces
Definition: output_adapters.hpp:27
input_format_t
the supported input formats
Definition: input_adapters.hpp:23
namespace for Niels Lohmann
Definition: adl_serializer.hpp:9
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:8684
Definition: to_json.hpp:25
Definition: type_traits.hpp:95
an iterator value
Definition: internal_iterator.hpp:16
BasicJsonType::array_t::iterator array_iterator
iterator for JSON arrays
Definition: internal_iterator.hpp:20
BasicJsonType::object_t::iterator object_iterator
iterator for JSON objects
Definition: internal_iterator.hpp:18
Definition: type_traits.hpp:38
Definition: type_traits.hpp:381
Definition: type_traits.hpp:103
SAX interface.
Definition: json_sax.hpp:24
std::size_t operator()(const nlohmann::json &j) const
return a hash value for a JSON object
Definition: json.hpp:8707
bool operator()(nlohmann::detail::value_t lhs, nlohmann::detail::value_t rhs) const noexcept
compare two value_t enum values
Definition: json.hpp:8723