1
0
mirror of https://github.com/nlohmann/json.git synced 2025-08-09 05:22:48 +03:00

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

 Conflicts:
	single_include/nlohmann/json.hpp
This commit is contained in:
Niels Lohmann
2020-07-23 12:30:07 +02:00
21 changed files with 1886 additions and 1852 deletions

View File

@@ -128,7 +128,7 @@ void from_json(const BasicJsonType& j, EnumType& e)
// forward_list doesn't have an insert method
template<typename BasicJsonType, typename T, typename Allocator,
enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
@@ -145,7 +145,7 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
// valarray doesn't have an insert method
template<typename BasicJsonType, typename T,
enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
void from_json(const BasicJsonType& j, std::valarray<T>& l)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
@@ -153,7 +153,11 @@ void from_json(const BasicJsonType& j, std::valarray<T>& l)
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
}
l.resize(j.size());
std::copy(j.begin(), j.end(), std::begin(l));
std::transform(j.begin(), j.end(), std::begin(l),
[](const BasicJsonType & elem)
{
return elem.template get<T>();
});
}
template<typename BasicJsonType, typename T, std::size_t N>

View File

@@ -2334,7 +2334,7 @@ class binary_reader
success = false;
break;
}
result.push_back(std::char_traits<char_type>::to_char_type(current));
result.push_back(static_cast<typename string_t::value_type>(current));
};
return success;
}

View File

@@ -284,3 +284,13 @@
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
void to_json(nlohmann::json& j, const Type& t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
void from_json(const nlohmann::json& j, Type& t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
#ifndef JSON_USE_IMPLICIT_CONVERSIONS
#define JSON_USE_IMPLICIT_CONVERSIONS 1
#endif
#if JSON_USE_IMPLICIT_CONVERSIONS
#define JSON_EXPLICIT
#else
#define JSON_EXPLICIT explicit
#endif

View File

@@ -18,5 +18,6 @@
#undef JSON_HAS_CPP_17
#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
#undef NLOHMANN_BASIC_JSON_TPL
#undef JSON_EXPLICIT
#include <nlohmann/thirdparty/hedley/hedley_undef.hpp>

View File

@@ -94,6 +94,16 @@ using get_template_function = decltype(std::declval<T>().template get<U>());
template<typename BasicJsonType, typename T, typename = void>
struct has_from_json : std::false_type {};
// trait checking if j.get<T> is valid
// use this trait instead of std::is_constructible or std::is_convertible,
// both rely on, or make use of implicit conversions, and thus fail when T
// has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
template <typename BasicJsonType, typename T>
struct is_getable
{
static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
};
template<typename BasicJsonType, typename T>
struct has_from_json < BasicJsonType, T,
enable_if_t < !is_basic_json<T>::value >>

View File

@@ -3229,7 +3229,7 @@ class basic_json
#endif
&& detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value
, int >::type = 0 >
operator ValueType() const
JSON_EXPLICIT operator ValueType() const
{
// delegate the call to get<>() const
return get<ValueType>();
@@ -3784,8 +3784,9 @@ class basic_json
@since version 1.0.0
*/
// using std::is_convertible in a std::enable_if will fail when using explicit conversions
template < class ValueType, typename std::enable_if <
std::is_convertible<basic_json_t, ValueType>::value
detail::is_getable<basic_json_t, ValueType>::value
&& !std::is_same<value_t, ValueType>::value, int >::type = 0 >
ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
{
@@ -3796,7 +3797,7 @@ class basic_json
const auto it = find(key);
if (it != end())
{
return *it;
return it->template get<ValueType>();
}
return default_value;
@@ -3858,7 +3859,7 @@ class basic_json
@since version 2.0.2
*/
template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
detail::is_getable<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, const ValueType& default_value) const
{
// at only works for objects
@@ -3867,7 +3868,7 @@ class basic_json
// if pointer resolves a value, return it or use default value
JSON_TRY
{
return ptr.get_checked(this);
return ptr.get_checked(this).template get<ValueType>();
}
JSON_INTERNAL_CATCH (out_of_range&)
{
@@ -8342,8 +8343,8 @@ class basic_json
}
// collect mandatory members
const std::string op = get_value("op", "op", true);
const std::string path = get_value(op, "path", true);
const auto op = get_value("op", "op", true).template get<std::string>();
const auto path = get_value(op, "path", true).template get<std::string>();
json_pointer ptr(path);
switch (get_op(op))
@@ -8369,7 +8370,7 @@ class basic_json
case patch_operations::move:
{
const std::string from_path = get_value("move", "from", true);
const auto from_path = get_value("move", "from", true).template get<std::string>();
json_pointer from_ptr(from_path);
// the "from" location must exist - use at()
@@ -8386,7 +8387,7 @@ class basic_json
case patch_operations::copy:
{
const std::string from_path = get_value("copy", "from", true);
const auto from_path = get_value("copy", "from", true).template get<std::string>();
const json_pointer from_ptr(from_path);
// the "from" location must exist - use at()