Skip to content

Commit 61d7451

Browse files
committed
reflect supports other option types
1 parent fb44982 commit 61d7451

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

include/jsoncons/reflect/json_conv_traits.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,34 +1243,34 @@ has_can_convert = ext_traits::is_detected<traits_can_convert_t, Json, T>;
12431243
};
12441244

12451245
template <typename Json,typename T>
1246-
struct json_conv_traits<Json, jsoncons::optional<T>,
1247-
typename std::enable_if<!is_json_conv_traits_declared<jsoncons::optional<T>>::value>::type>
1246+
struct json_conv_traits<Json, T,
1247+
typename std::enable_if<jsoncons::ext_traits::is_optional<T>::value && !is_json_conv_traits_declared<T>::value>::type>
12481248
{
12491249
public:
1250-
using result_type = conversion_result<jsoncons::optional<T>>;
1250+
using result_type = conversion_result<T>;
12511251

12521252
static bool is(const Json& j) noexcept
12531253
{
1254-
return j.is_null() || j.template is<T>();
1254+
return j.is_null() || j.template is<typename T::value_type>();
12551255
}
12561256

12571257
template <typename Alloc, typename TempAlloc>
12581258
static result_type try_as(const allocator_set<Alloc,TempAlloc>& aset, const Json& j)
12591259
{
12601260
if (j.is_null())
12611261
{
1262-
return result_type(jsoncons::optional<T>());
1262+
return result_type(T());
12631263
}
1264-
auto r = j.template try_as<T>(aset);
1264+
auto r = j.template try_as<typename T::value_type>(aset);
12651265
if (!r)
12661266
{
12671267
return result_type(jsoncons::unexpect, r.error());
12681268
}
1269-
return result_type(jsoncons::optional<T>(std::move(r.value())));
1269+
return result_type(T(std::move(r.value())));
12701270
}
12711271

12721272
template <typename Alloc, typename TempAlloc>
1273-
static Json to_json(const allocator_set<Alloc,TempAlloc>& aset, const jsoncons::optional<T>& val)
1273+
static Json to_json(const allocator_set<Alloc,TempAlloc>& aset, const T& val)
12741274
{
12751275
return val.has_value() ? jsoncons::make_obj_using_allocator<Json>(aset.get_allocator(), *val) : Json::null();
12761276
}

include/jsoncons/utility/more_type_traits.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ namespace ext_traits {
463463
using
464464
optional_has_value_t = decltype(std::declval<Optional>().has_value());
465465

466+
template <typename T>
467+
using
468+
value_type_t = typename T::value_type;
469+
466470
template <typename Optional>
467471
using
468472
optional_value_t = decltype(std::declval<Optional>().value());
@@ -587,6 +591,10 @@ namespace ext_traits {
587591
using
588592
has_has_value = is_detected_exact<bool, optional_has_value_t, T>;
589593

594+
template <typename T>
595+
using
596+
has_value_type = is_detected<value_type_t, T>;
597+
590598
template <typename T>
591599
using
592600
has_value = is_detected<optional_value_t, T>;
@@ -602,7 +610,7 @@ namespace ext_traits {
602610
template <typename T>
603611
struct is_optional
604612
{
605-
static constexpr bool value = has_has_value<T>::value && has_value<T>::value && has_operator_bool<T>::value && has_operator_star<T>::value;
613+
static constexpr bool value = has_value_type<T>::value && has_has_value<T>::value && has_value<T>::value && has_operator_bool<T>::value && has_operator_star<T>::value;
606614
};
607615

608616
// is_front_insertable

0 commit comments

Comments
 (0)