1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-28 12:02:00 +03:00

Merge branch 'develop' of https://github.com/nlohmann/json into feature/bson

Conflicts:
	include/nlohmann/detail/input/binary_reader.hpp
	single_include/nlohmann/json.hpp
	src/unit-bson.cpp
This commit is contained in:
Julian Becker
2018-10-17 19:06:22 +02:00
110 changed files with 2497 additions and 1418 deletions

View File

@ -1,7 +1,7 @@
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++
| | |__ | | | | | | version 3.2.0
| | |__ | | | | | | version 3.3.0
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
@ -31,7 +31,7 @@ SOFTWARE.
#define NLOHMANN_JSON_HPP
#define NLOHMANN_JSON_VERSION_MAJOR 3
#define NLOHMANN_JSON_VERSION_MINOR 2
#define NLOHMANN_JSON_VERSION_MINOR 3
#define NLOHMANN_JSON_VERSION_PATCH 0
#include <algorithm> // all_of, find, for_each
@ -947,7 +947,7 @@ class basic_json
object = nullptr; // silence warning, see #821
if (JSON_UNLIKELY(t == value_t::null))
{
JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.2.0")); // LCOV_EXCL_LINE
JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.3.0")); // LCOV_EXCL_LINE
}
break;
}
@ -1866,7 +1866,7 @@ class basic_json
@since version 1.0.0
*/
reference& operator=(basic_json other) noexcept (
basic_json& 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
@ -2624,51 +2624,50 @@ class basic_json
}
/*!
@brief get a pointer value (explicit)
@brief get a value (explicit)
Explicit pointer access to the internally stored JSON value. No copies are
made.
Explicit type conversion between the JSON value and a compatible value.
The value is filled into the input parameter by calling the @ref json_serializer<ValueType>
`from_json()` method.
@warning The pointer becomes invalid if the underlying JSON object
changes.
The function is equivalent to executing
@code {.cpp}
ValueType v;
JSONSerializer<ValueType>::from_json(*this, v);
@endcode
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
@ref number_unsigned_t, or @ref number_float_t.
This overloads is chosen if:
- @a ValueType is not @ref basic_json,
- @ref json_serializer<ValueType> has a `from_json()` method of the form
`void from_json(const basic_json&, ValueType&)`, and
@return pointer to the internally stored JSON value if the requested
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
@tparam ValueType the input parameter type.
@complexity Constant.
@return the input parameter, allowing chaining calls.
@liveexample{The example below shows how pointers to internal values of a
JSON value can be requested. Note that no type conversions are made and a
`nullptr` is returned if the value and the requested pointer type does not
match.,get__PointerType}
@throw what @ref json_serializer<ValueType> `from_json()` method throws
@sa @ref get_ptr() for explicit pointer-member access
@liveexample{The example below shows several conversions from JSON values
to other types. There a few things to note: (1) Floating-point numbers can
be converted to integers\, (2) A JSON array can be converted to a standard
`std::vector<short>`\, (3) A JSON object can be converted to C++
associative containers such as `std::unordered_map<std::string\,
json>`.,get_to}
@since version 1.0.0
@since version 3.3.0
*/
template<typename PointerType, typename std::enable_if<
std::is_pointer<PointerType>::value, int>::type = 0>
PointerType get() noexcept
template<typename ValueType,
detail::enable_if_t <
not detail::is_basic_json<ValueType>::value and
detail::has_from_json<basic_json_t, ValueType>::value,
int> = 0>
ValueType & get_to(ValueType& v) const noexcept(noexcept(
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
{
// delegate the call to get_ptr
return get_ptr<PointerType>();
JSONSerializer<ValueType>::from_json(*this, v);
return v;
}
/*!
@brief get a pointer value (explicit)
@copydoc get()
*/
template<typename PointerType, typename std::enable_if<
std::is_pointer<PointerType>::value, int>::type = 0>
constexpr const PointerType get() const noexcept
{
// delegate the call to get_ptr
return get_ptr<PointerType>();
}
/*!
@brief get a pointer value (implicit)
@ -2698,23 +2697,8 @@ class basic_json
*/
template<typename PointerType, typename std::enable_if<
std::is_pointer<PointerType>::value, int>::type = 0>
PointerType get_ptr() noexcept
auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
{
// get the type of the PointerType (remove pointer and const)
using pointee_t = typename std::remove_const<typename
std::remove_pointer<typename
std::remove_const<PointerType>::type>::type>::type;
// make sure the type matches the allowed types
static_assert(
std::is_same<object_t, pointee_t>::value
or std::is_same<array_t, pointee_t>::value
or std::is_same<string_t, pointee_t>::value
or std::is_same<boolean_t, pointee_t>::value
or std::is_same<number_integer_t, pointee_t>::value
or std::is_same<number_unsigned_t, pointee_t>::value
or std::is_same<number_float_t, pointee_t>::value
, "incompatible pointer type");
// delegate the call to get_impl_ptr<>()
return get_impl_ptr(static_cast<PointerType>(nullptr));
}
@ -2726,27 +2710,59 @@ class basic_json
template<typename PointerType, typename std::enable_if<
std::is_pointer<PointerType>::value and
std::is_const<typename std::remove_pointer<PointerType>::type>::value, int>::type = 0>
constexpr const PointerType get_ptr() const noexcept
constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
{
// get the type of the PointerType (remove pointer and const)
using pointee_t = typename std::remove_const<typename
std::remove_pointer<typename
std::remove_const<PointerType>::type>::type>::type;
// make sure the type matches the allowed types
static_assert(
std::is_same<object_t, pointee_t>::value
or std::is_same<array_t, pointee_t>::value
or std::is_same<string_t, pointee_t>::value
or std::is_same<boolean_t, pointee_t>::value
or std::is_same<number_integer_t, pointee_t>::value
or std::is_same<number_unsigned_t, pointee_t>::value
or std::is_same<number_float_t, pointee_t>::value
, "incompatible pointer type");
// delegate the call to get_impl_ptr<>() const
return get_impl_ptr(static_cast<PointerType>(nullptr));
}
/*!
@brief get a pointer value (explicit)
Explicit pointer access to the internally stored JSON value. No copies are
made.
@warning The pointer becomes invalid if the underlying JSON object
changes.
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
@ref number_unsigned_t, or @ref number_float_t.
@return pointer to the internally stored JSON value if the requested
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
@complexity Constant.
@liveexample{The example below shows how pointers to internal values of a
JSON value can be requested. Note that no type conversions are made and a
`nullptr` is returned if the value and the requested pointer type does not
match.,get__PointerType}
@sa @ref get_ptr() for explicit pointer-member access
@since version 1.0.0
*/
template<typename PointerType, typename std::enable_if<
std::is_pointer<PointerType>::value, int>::type = 0>
auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
{
// delegate the call to get_ptr
return get_ptr<PointerType>();
}
/*!
@brief get a pointer value (explicit)
@copydoc get()
*/
template<typename PointerType, typename std::enable_if<
std::is_pointer<PointerType>::value, int>::type = 0>
constexpr auto get() const noexcept -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
{
// delegate the call to get_ptr
return get_ptr<PointerType>();
}
/*!
@brief get a reference value (implicit)
@ -2828,12 +2844,14 @@ class basic_json
not std::is_same<ValueType, detail::json_ref<basic_json>>::value and
not std::is_same<ValueType, typename string_t::value_type>::value and
not detail::is_basic_json<ValueType>::value
#ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015
and not std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
#if defined(JSON_HAS_CPP_17) && defined(_MSC_VER) and _MSC_VER <= 1914
and not std::is_same<ValueType, typename std::string_view>::value
#endif
#endif
and detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value
, int >::type = 0 >
operator ValueType() const
{
@ -3097,7 +3115,7 @@ class basic_json
return m_value.array->operator[](idx);
}
JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
}
/*!
@ -3127,7 +3145,7 @@ class basic_json
return m_value.array->operator[](idx);
}
JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
}
/*!
@ -3173,7 +3191,7 @@ class basic_json
return m_value.object->operator[](key);
}
JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
}
/*!
@ -3215,7 +3233,7 @@ class basic_json
return m_value.object->find(key)->second;
}
JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
}
/*!
@ -3262,7 +3280,7 @@ class basic_json
return m_value.object->operator[](key);
}
JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
}
/*!
@ -3305,7 +3323,7 @@ class basic_json
return m_value.object->find(key)->second;
}
JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name())));
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
}
/*!
@ -4943,6 +4961,26 @@ class basic_json
return {it, res.second};
}
/// Helper for insertion of an iterator
/// @note: This uses std::distance to support GCC 4.8,
/// see https://github.com/nlohmann/json/pull/1257
template<typename... Args>
iterator insert_iterator(const_iterator pos, Args&& ... args)
{
iterator result(this);
assert(m_value.array != nullptr);
auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
result.m_it.array_iterator = m_value.array->begin() + insert_pos;
// This could have been written as:
// result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
// but the return value of insert is missing in GCC 4.8, so it is written this way instead.
return result;
}
/*!
@brief inserts element
@ -4977,9 +5015,7 @@ class basic_json
}
// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, val);
return result;
return insert_iterator(pos, val);
}
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
@ -5030,9 +5066,7 @@ class basic_json
}
// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
return result;
return insert_iterator(pos, cnt, val);
}
JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
@ -5094,12 +5128,7 @@ class basic_json
}
// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(
pos.m_it.array_iterator,
first.m_it.array_iterator,
last.m_it.array_iterator);
return result;
return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
}
/*!
@ -5141,9 +5170,7 @@ class basic_json
}
// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist.begin(), ilist.end());
return result;
return insert_iterator(pos, ilist.begin(), ilist.end());
}
/*!
@ -5972,6 +5999,8 @@ class basic_json
@param[in] cb a parser callback function of type @ref parser_callback_t
which is used to control the deserialization by filtering unwanted values
(optional)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@return result of the deserialization
@ -6367,7 +6396,7 @@ class basic_json
vector in CBOR format.,to_cbor}
@sa http://cbor.io
@sa @ref from_cbor(detail::input_adapter, const bool strict) for the
@sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
analogous deserialization
@sa @ref to_msgpack(const basic_json&) for the related MessagePack format
@sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
@ -6464,8 +6493,7 @@ class basic_json
vector in MessagePack format.,to_msgpack}
@sa http://msgpack.org
@sa @ref from_msgpack(const std::vector<uint8_t>&, const size_t) for the
analogous deserialization
@sa @ref from_msgpack for the analogous deserialization
@sa @ref to_cbor(const basic_json& for the related CBOR format
@sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
related UBJSON format
@ -6562,7 +6590,7 @@ class basic_json
vector in UBJSON format.,to_ubjson}
@sa http://ubjson.org
@sa @ref from_ubjson(detail::input_adapter, const bool strict) for the
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
analogous deserialization
@sa @ref to_cbor(const basic_json& for the related CBOR format
@sa @ref to_msgpack(const basic_json&) for the related MessagePack format
@ -6758,14 +6786,14 @@ class basic_json
@sa http://cbor.io
@sa @ref to_cbor(const basic_json&) for the analogous serialization
@sa @ref from_msgpack(detail::input_adapter, const bool, const bool) for the
@sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for the
related MessagePack format
@sa @ref from_ubjson(detail::input_adapter, const bool, const bool) for the
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
related UBJSON format
@since version 2.0.9; parameter @a start_index since 2.1.1; changed to
consume input adapters, removed start_index parameter, and added
@a strict parameter since 3.0.0; added @allow_exceptions parameter
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
*/
static basic_json from_cbor(detail::input_adapter&& i,
@ -6779,7 +6807,7 @@ class basic_json
}
/*!
@copydoc from_cbor(detail::input_adapter, const bool, const bool)
@copydoc from_cbor(detail::input_adapter&&, const bool, const bool)
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
@ -6861,16 +6889,16 @@ class basic_json
@sa http://msgpack.org
@sa @ref to_msgpack(const basic_json&) for the analogous serialization
@sa @ref from_cbor(detail::input_adapter, const bool, const bool) for the
@sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
related CBOR format
@sa @ref from_ubjson(detail::input_adapter, const bool, const bool) for
@sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for
the related UBJSON format
@sa @ref from_bson(detail::input_adapter, const bool, const bool) for
the related BSON format
@since version 2.0.9; parameter @a start_index since 2.1.1; changed to
consume input adapters, removed start_index parameter, and added
@a strict parameter since 3.0.0; added @allow_exceptions parameter
@a strict parameter since 3.0.0; added @a allow_exceptions parameter
since 3.2.0
*/
static basic_json from_msgpack(detail::input_adapter&& i,
@ -6884,7 +6912,7 @@ class basic_json
}
/*!
@copydoc from_msgpack(detail::input_adapter, const bool, const bool)
@copydoc from_msgpack(detail::input_adapter&&, const bool, const bool)
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
@ -6948,14 +6976,14 @@ class basic_json
@sa http://ubjson.org
@sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
analogous serialization
@sa @ref from_cbor(detail::input_adapter, const bool, const bool) for the
@sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
related CBOR format
@sa @ref from_msgpack(detail::input_adapter, const bool, const bool) for
@sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for
the related MessagePack format
@sa @ref from_bson(detail::input_adapter, const bool, const bool) for
the related BSON format
@since version 3.1.0; added @allow_exceptions parameter since 3.2.0
@since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
*/
static basic_json from_ubjson(detail::input_adapter&& i,
const bool strict = true,
@ -6968,7 +6996,7 @@ class basic_json
}
/*!
@copydoc from_ubjson(detail::input_adapter, const bool, const bool)
@copydoc from_ubjson(detail::input_adapter&&, const bool, const bool)
*/
template<typename A1, typename A2,
detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
@ -7432,11 +7460,9 @@ class basic_json
// avoid undefined behavior
JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
}
else
{
// default case: insert add offset
parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
}
// default case: insert add offset
parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
}
break;
}
@ -7861,19 +7887,6 @@ class basic_json
// specialization of std::swap, and std::hash
namespace std
{
/*!
@brief exchanges the values of two JSON objects
@since version 1.0.0
*/
template<>
inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept(
is_nothrow_move_constructible<nlohmann::json>::value and
is_nothrow_move_assignable<nlohmann::json>::value
)
{
j1.swap(j2);
}
/// hash value for JSON objects
template<>
@ -7909,6 +7922,20 @@ struct less< ::nlohmann::detail::value_t>
}
};
/*!
@brief exchanges the values of two JSON objects
@since version 1.0.0
*/
template<>
inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept(
is_nothrow_move_constructible<nlohmann::json>::value and
is_nothrow_move_assignable<nlohmann::json>::value
)
{
j1.swap(j2);
}
} // namespace std
/*!