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) {}
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));
972 json_value(
const array_t& value)
974 array = create<array_t>(
value);
980 array = create<array_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,
2412 basic_json
get()
const 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>
2550 PointerType
get() noexcept
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 2601 std::remove_const<PointerType>::type>::type>
::type;
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 2629 std::remove_const<PointerType>::type>::type>
::type;
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());
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());
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);
4724 basic_json&& key = init.begin()->moved_or_copied();
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())));
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>
5796 friend std::ostream&
operator<<(std::ostream& o,
const basic_json& j)
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));
5820 friend std::ostream&
operator>>(
const basic_json& j, std::ostream& o)
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);
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>
5979 static basic_json
parse(IteratorType first, IteratorType last,
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 = {};
6209 static std::vector<uint8_t>
to_cbor(
const basic_json& j)
6211 std::vector<uint8_t> result;
6308 std::vector<uint8_t> result;
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)
6419 const bool use_size =
false,
const bool use_type =
false)
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>
6528 static basic_json
from_cbor(A1 && a1, A2 && a2,
const bool strict =
true)
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>
6617 static basic_json
from_msgpack(A1 && a1, A2 && a2,
const bool strict =
true)
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>
6683 static basic_json
from_ubjson(A1 && a1, A2 && a2,
const bool strict =
true)
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);
6969 basic_json
patch(
const basic_json& json_patch)
const 6972 basic_json result = *
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;
7008 const auto operation_add = [&result](
json_pointer & ptr, basic_json val)
7019 if (top_pointer != ptr)
7021 result.
at(top_pointer);
7025 const auto last_path = ptr.pop_back();
7026 basic_json& parent = result[ptr];
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"));
7072 const auto operation_remove = [&result](
json_pointer & ptr)
7075 const auto last_path = ptr.pop_back();
7076 basic_json& parent = result.
at(ptr);
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,
7111 bool string_type) -> basic_json &
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);
7173 basic_json v = result.
at(from_ptr);
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);
7190 basic_json v = result.
at(from_ptr);
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:
7267 static basic_json
diff(
const basic_json& source,
const basic_json& target,
7268 const std::string& path =
"")
7274 if (source == target)
7279 if (source.
type() != target.
type())
7284 {
"op",
"replace"}, {
"path", path}, {
"value", target}
7289 switch (source.
type())
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());
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)
7362 if (source.
find(it.key()) == source.
end())
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}
7451 for (
auto it = patch.
begin(); it != patch.
end(); ++it)
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> ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition: json.hpp:514
reference operator+=(const basic_json &val)
add an object to an array
Definition: json.hpp:4639
basic_json(InputIT first, InputIT last)
construct a JSON container given an iterator range
Definition: json.hpp:1543
const_reference operator[](const typename object_t::key_type &key) const
read-only access specified object element
Definition: json.hpp:3103
void update(const_iterator first, const_iterator last)
updates a JSON object from another object, overwriting existing keys
Definition: json.hpp:5159
static JSON_DEPRECATED iteration_proxy< const_iterator > iterator_wrapper(const_reference ref) noexcept
wrapper to access iterator member functions in range-based for
Definition: json.hpp:4175
typename parser::parser_callback_t parser_callback_t
per-element parser callback type
Definition: json.hpp:1106
void emplace_back(Args &&... args)
add an object to an array
Definition: json.hpp:4766
size_type max_size() const noexcept
returns the maximum possible number of elements
Definition: json.hpp:4438
Definition: primitive_iterator.hpp:19
static JSON_DEPRECATED iteration_proxy< iterator > iterator_wrapper(reference ref) noexcept
wrapper to access iterator member functions in range-based for
Definition: json.hpp:4166
friend bool operator<(const_reference lhs, const_reference rhs) noexcept
comparison: less than
Definition: json.hpp:5528
iteration_proxy< iterator > items() noexcept
helper to access iterator member functions in range-based for
Definition: json.hpp:4232
basic_json(basic_json &&other) noexcept
move constructor
Definition: json.hpp:1750
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:804
value_t
the JSON type enumeration
Definition: value_t.hpp:40
iteration_proxy< const_iterator > items() const noexcept
helper to access iterator member functions in range-based for
Definition: json.hpp:4240
IteratorType erase(IteratorType pos)
remove element given an iterator
Definition: json.hpp:3489
~basic_json() noexcept
destructor
Definition: json.hpp:1820
const_reference operator[](const json_pointer &ptr) const
access specified element via JSON Pointer
Definition: json.hpp:6758
Definition: output_adapters.hpp:92
Definition: serializer.hpp:35
Definition: to_json.hpp:22
array (ordered collection of values)
NumberUnsignedType number_unsigned_t
a type for a number (unsigned)
Definition: json.hpp:736
constexpr value_t type() const noexcept
return the type of the JSON value (explicit)
Definition: json.hpp:1923
friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept
comparison: less than or equal
Definition: json.hpp:5648
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:665
reference operator[](const typename object_t::key_type &key)
access specified object element
Definition: json.hpp:3054
a template for a reverse iterator class
Definition: json_reverse_iterator.hpp:34
constexpr bool is_primitive() const noexcept
return whether type is primitive
Definition: json.hpp:1953
a class to store JSON values
Definition: json.hpp:161
typename std::allocator_traits< allocator_type >::pointer pointer
the type of an element pointer
Definition: json.hpp:258
reference at(const json_pointer &ptr)
access specified element via JSON Pointer
Definition: json.hpp:6801
iterator insert(const_iterator pos, basic_json &&val)
inserts element
Definition: json.hpp:4886
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:1147
void insert(const_iterator first, const_iterator last)
inserts elements
Definition: json.hpp:5066
constexpr bool is_structured() const noexcept
return whether type is structured
Definition: json.hpp:1980
friend std::ostream & operator<<(std::ostream &o, const basic_json &j)
serialize to stream
Definition: json.hpp:5796
static allocator_type get_allocator()
returns the allocator associated with the container
Definition: json.hpp:277
StringType string_t
a type for a string
Definition: json.hpp:567
JSON_DEPRECATED friend std::istream & operator<<(basic_json &j, std::istream &i)
deserialize from stream
Definition: json.hpp:6006
void parse(const bool strict, BasicJsonType &result)
public parser interface
Definition: parser.hpp:75
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:3929
number value (signed integer)
basic_json(size_type cnt, const basic_json &val)
construct an array with count copies of given value
Definition: json.hpp:1478
basic_json flatten() const
return flattened JSON value
Definition: json.hpp:6871
Definition: json.hpp:7478
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:3858
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:250
reference operator+=(const typename object_t::value_type &val)
add an object to an object
Definition: json.hpp:4689
iterator insert(const_iterator pos, const_iterator first, const_iterator last)
inserts elements
Definition: json.hpp:4965
friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
comparison: not equal
Definition: json.hpp:5497
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:77
friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept
comparison: greater than
Definition: json.hpp:5694
const_reverse_iterator rbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:4007
a template for a bidirectional iterator for the basic_json class
Definition: iter_impl.hpp:42
friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
comparison: equal
Definition: json.hpp:5452
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:2161
std::pair< iterator, bool > emplace(Args &&... args)
add an object to an object if key does not exist
Definition: json.hpp:4814
BasicJsonType parse_ubjson(const bool strict)
create a JSON value from UBJSON input
Definition: binary_reader.hpp:104
lexical analysis
Definition: lexer.hpp:30
friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept
comparison: greater than
Definition: json.hpp:5705
syntax analysis
Definition: parser.hpp:30
serialization to CBOR and MessagePack values
Definition: binary_writer.hpp:24
friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:5740
BasicJsonType::object_t::iterator object_iterator
iterator for JSON objects
Definition: internal_iterator.hpp:18
const_reference operator[](T *key) const
read-only access specified object element
Definition: json.hpp:3193
reference & operator=(basic_json other) noexcept(std::is_nothrow_move_constructible< value_t >::value and std::is_nothrow_move_assignable< value_t >::value and std::is_nothrow_move_constructible< json_value >::value and std::is_nothrow_move_assignable< json_value >::value)
copy assignment
Definition: json.hpp:1787
static basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition: json.hpp:1451
exception indicating access out of the defined range
Definition: exceptions.hpp:279
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:2002
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
Definition: json.hpp:2139
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:3767
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:2775
basic_json(std::nullptr_t=nullptr) noexcept
create a null object
Definition: json.hpp:1171
const_reference at(const typename object_t::key_type &key) const
access specified object element with bounds checking
Definition: json.hpp:2924
size_type erase(const typename object_t::key_type &key)
remove element from a JSON object given a key
Definition: json.hpp:3681
friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept
comparison: less than
Definition: json.hpp:5602
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false) const
serialization
Definition: json.hpp:1873
basic_json(const basic_json &other)
copy constructor
Definition: json.hpp:1667
void clear() noexcept
clears the contents
Definition: json.hpp:4508
iterator insert(const_iterator pos, size_type cnt, const basic_json &val)
inserts elements
Definition: json.hpp:4915
reference operator+=(initializer_list_t init)
add an object to an object
Definition: json.hpp:4738
parse_event_t
Definition: parser.hpp:39
constexpr bool is_number() const noexcept
return whether value is a number
Definition: json.hpp:2054
static 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:7267
exception indicating a parse error
Definition: exceptions.hpp:111
reference operator[](T *key)
access specified object element
Definition: json.hpp:3143
static basic_json from_msgpack(detail::input_adapter i, const bool strict=true)
create a JSON value from an input in MessagePack format
Definition: json.hpp:6606
const_iterator find(KeyT &&key) const
find an element in a JSON object
Definition: json.hpp:3784
reference operator[](const json_pointer &ptr)
access specified element via JSON Pointer
Definition: json.hpp:6730
reference at(const typename object_t::key_type &key)
access specified object element with bounds checking
Definition: json.hpp:2873
general exception of the basic_json class
Definition: exceptions.hpp:43
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:4368
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:6403
constexpr const PointerType get_ptr() const noexcept
get a pointer value (implicit)
Definition: json.hpp:2624
static std::vector< uint8_t > to_cbor(const basic_json &j)
create a CBOR serialization of a given JSON value
Definition: json.hpp:6209
reference back()
access the last element
Definition: json.hpp:3422
BasicJsonType::array_t::iterator array_iterator
iterator for JSON arrays
Definition: internal_iterator.hpp:20
friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
comparison: equal
Definition: json.hpp:5441
const char * type_name() const noexcept
return the type as string
Definition: json.hpp:6078
static basic_json from_msgpack(A1 &&a1, A2 &&a2, const bool strict=true)
create a JSON value from an input in MessagePack format
Definition: json.hpp:6617
const_iterator begin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:3868
exception indicating errors with iterators
Definition: exceptions.hpp:183
reverse_iterator rend() noexcept
returns an iterator to the reverse-end
Definition: json.hpp:4036
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:2024
bool operator<(const value_t lhs, const value_t rhs) noexcept
comparison operator for JSON types
Definition: value_t.hpp:63
namespace for Niels Lohmann
Definition: adl_serializer.hpp:8
IteratorType erase(IteratorType first, IteratorType last)
remove elements given an iterator range
Definition: json.hpp:3594
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:2205
object (unordered set of name/value pairs)
ReferenceType get_ref()
get a reference value (implicit)
Definition: json.hpp:2673
std::initializer_list< detail::json_ref< basic_json > > initializer_list_t
helper type for initializer lists of basic_json values
Definition: json.hpp:206
exception indicating executing a member function with a wrong type
Definition: exceptions.hpp:235
friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept
comparison: less than or equal
Definition: json.hpp:5659
PointerType get_ptr() noexcept
get a pointer value (implicit)
Definition: json.hpp:2596
friend std::istream & operator>>(std::istream &i, basic_json &j)
deserialize from stream
Definition: json.hpp:6036
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
comparison: equal
Definition: json.hpp:5370
ObjectType< StringType, basic_json, object_comparator_t, AllocatorType< std::pair< const StringType, basic_json > >> object_t
a type for an object
Definition: json.hpp:468
::nlohmann::json_pointer< basic_json > json_pointer
JSON Pointer.
Definition: json.hpp:202
typename parser::parse_event_t parse_event_t
parser event types
Definition: json.hpp:1055
string_t value(const json_pointer &ptr, const char *default_value) const
overload for a default value of type const char*
Definition: json.hpp:3348
void push_back(const basic_json &val)
add an object to an array
Definition: json.hpp:4615
void push_back(initializer_list_t init)
add an object to an object
Definition: json.hpp:4720
static basic_json parse(IteratorType first, IteratorType last, const parser_callback_t cb=nullptr, const bool allow_exceptions=true)
deserialize from an iterator range with contiguous storage
Definition: json.hpp:5979
void merge_patch(const basic_json &patch)
applies a JSON Merge Patch
Definition: json.hpp:7443
static basic_json from_cbor(A1 &&a1, A2 &&a2, const bool strict=true)
create a JSON value from an input in CBOR format
Definition: json.hpp:6528
std::size_t operator()(const nlohmann::json &j) const
return a hash value for a JSON object
Definition: json.hpp:7504
static int array_index(const std::string &s)
Definition: json_pointer.hpp:85
const_reference back() const
access the last element
Definition: json.hpp:3432
json_reverse_iterator< typename basic_json::const_iterator > const_reverse_iterator
a const reverse iterator for a basic_json container
Definition: json.hpp:269
constexpr bool is_discarded() const noexcept
return whether value is discarded
Definition: json.hpp:2232
void update(const_reference j)
updates a JSON object from another object, overwriting existing keys
Definition: json.hpp:5108
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition: json.hpp:3255
std::shared_ptr< output_adapter_protocol< CharType > > output_adapter_t
a type to simplify interfaces
Definition: output_adapters.hpp:26
BooleanType boolean_t
a type for a boolean
Definition: json.hpp:593
friend bool operator>(const_reference lhs, const_reference rhs) noexcept
comparison: greater than
Definition: json.hpp:5683
bool accept(const bool strict=true)
public accept interface
Definition: parser.hpp:111
static basic_json parse(detail::input_adapter i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true)
deserialize from a compatible input
Definition: json.hpp:5897
reference operator+=(basic_json &&val)
add an object to an array
Definition: json.hpp:4605
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
Definition: json.hpp:2083
exception indicating other library errors
Definition: exceptions.hpp:316
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:3898
friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:5751
void swap(array_t &other)
exchanges the values
Definition: json.hpp:5242
const_reference at(size_type idx) const
access specified array element with bounds checking
Definition: json.hpp:2822
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
the type of an element const pointer
Definition: json.hpp:260
deserialization of CBOR and MessagePack values
Definition: binary_reader.hpp:35
bool operator()(nlohmann::detail::value_t lhs, nlohmann::detail::value_t rhs) const noexcept
compare two value_t enum values
Definition: json.hpp:7522
constexpr bool is_number_unsigned() const noexcept
return whether value is an unsigned integer number
Definition: json.hpp:2111
void swap(string_t &other)
exchanges the values
Definition: json.hpp:5308
iterator insert(const_iterator pos, const basic_json &val)
inserts element
Definition: json.hpp:4862
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:2183
an iterator value
Definition: internal_iterator.hpp:15
const_iterator end() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:3939
const_reverse_iterator crbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:4073
Definition: json_ref.hpp:11
json_reverse_iterator< typename basic_json::iterator > reverse_iterator
a reverse iterator for a basic_json container
Definition: json.hpp:267
iterator insert(const_iterator pos, initializer_list_t ilist)
inserts elements
Definition: json.hpp:5023
static basic_json from_cbor(detail::input_adapter i, const bool strict=true)
create a JSON value from an input in CBOR format
Definition: json.hpp:6517
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:1237
reference operator[](size_type idx)
access specified array element
Definition: json.hpp:2970
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:3277
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:4579
friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept
comparison: less than
Definition: json.hpp:5613
reference front()
access the first element
Definition: json.hpp:3378
JSON Pointer.
Definition: json_pointer.hpp:15
reverse_iterator rbegin() noexcept
returns an iterator to the reverse-beginning
Definition: json.hpp:3999
ReferenceType get_ref() const
get a reference value (implicit)
Definition: json.hpp:2686
BasicJsonType parse_msgpack(const bool strict)
create a JSON value from MessagePack input
Definition: binary_reader.hpp:83
number value (unsigned integer)
std::size_t size_type
a type to represent container sizes
Definition: json.hpp:252
const_reverse_iterator crend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:4102
basic_json patch(const basic_json &json_patch) const
applies a JSON patch
Definition: json.hpp:6969
const_reference operator[](size_type idx) const
access specified array element
Definition: json.hpp:3016
friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
comparison: not equal
Definition: json.hpp:5486
BasicJsonType parse_cbor(const bool strict)
create a JSON value from CBOR input
Definition: binary_reader.hpp:62
size_type count(KeyT &&key) const
returns the number of occurrences of a key in a JSON object
Definition: json.hpp:3818
void swap(object_t &other)
exchanges the values
Definition: json.hpp:5275
static std::vector< uint8_t > to_msgpack(const basic_json &j)
create a MessagePack serialization of a given JSON value
Definition: json.hpp:6306
ValueType value(const json_pointer &ptr, const ValueType &default_value) const
access specified object element via JSON Pointer with default value
Definition: json.hpp:3325
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:3969
proxy class for the items() function
Definition: iter_impl.hpp:19
void push_back(const typename object_t::value_type &val)
add an object to an object
Definition: json.hpp:4665
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:1319
JSON_DEPRECATED friend std::ostream & operator>>(const basic_json &j, std::ostream &o)
serialize to stream
Definition: json.hpp:5820
basic_json unflatten() const
unflatten a previously flattened JSON value
Definition: json.hpp:6908
static basic_json array(initializer_list_t init={})
explicitly create an array from an initializer list
Definition: json.hpp:1408
friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
comparison: less than or equal
Definition: json.hpp:5637
number value (floating-point)
void erase(const size_type idx)
remove element from a JSON array given an index
Definition: json.hpp:3716
friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:5729
friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
comparison: not equal
Definition: json.hpp:5475
static parse_error create(int id_, std::size_t byte_, const std::string &what_arg)
create a parse error exception
Definition: exceptions.hpp:122
AllocatorType< basic_json > allocator_type
the allocator type
Definition: json.hpp:255
void swap(reference other) noexcept(std::is_nothrow_move_constructible< value_t >::value and std::is_nothrow_move_assignable< value_t >::value and std::is_nothrow_move_constructible< json_value >::value and std::is_nothrow_move_assignable< json_value >::value)
exchanges the values
Definition: json.hpp:5210
const_reference front() const
access the first element
Definition: json.hpp:3386
static basic_json from_ubjson(detail::input_adapter i, const bool strict=true)
create a JSON value from an input in UBJSON format
Definition: json.hpp:6675
static basic_json parse(detail::input_adapter &i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true)
create an empty value with a given type parse(detail::input_adapter, const parser_callback_t) ...
Definition: json.hpp:5909
const_reference at(const json_pointer &ptr) const
access specified element via JSON Pointer
Definition: json.hpp:6844
const_reverse_iterator rend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:4044
bool empty() const noexcept
checks whether the container is empty.
Definition: json.hpp:4296
static basic_json meta()
returns version information on the library
Definition: json.hpp:308