mirror of
https://github.com/boostorg/json.git
synced 2025-05-12 14:11:40 +00:00
UDT serialization of optionals
This commit is contained in:
parent
4a5e6bbccf
commit
8f7b1edef9
@ -46,6 +46,10 @@ suspend(state st, U u, T const* pt)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T, bool StackEmpty>
|
||||||
|
bool
|
||||||
|
write_impl(writer& w, stream& ss);
|
||||||
|
|
||||||
template<class T, bool StackEmpty>
|
template<class T, bool StackEmpty>
|
||||||
BOOST_FORCEINLINE
|
BOOST_FORCEINLINE
|
||||||
bool
|
bool
|
||||||
@ -714,6 +718,55 @@ write_impl(variant_conversion_tag, writer& w, stream& ss)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T, bool StackEmpty>
|
||||||
|
BOOST_FORCEINLINE
|
||||||
|
bool
|
||||||
|
write_impl(optional_conversion_tag, writer& w, stream& ss)
|
||||||
|
{
|
||||||
|
using Elem = value_result_type<T>;
|
||||||
|
|
||||||
|
bool done;
|
||||||
|
bool has_value;
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
# pragma warning( push )
|
||||||
|
# pragma warning( disable : 4127 )
|
||||||
|
#endif
|
||||||
|
if(StackEmpty || w.st_.empty())
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
# pragma warning( pop )
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( w.p_ );
|
||||||
|
T const* pt = reinterpret_cast<T const*>(w.p_);
|
||||||
|
has_value = static_cast<bool>(*pt);
|
||||||
|
if( has_value )
|
||||||
|
{
|
||||||
|
w.p_ = std::addressof( *(*pt) );
|
||||||
|
done = write_impl<Elem, true>(w, ss);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
w.p_ = nullptr;
|
||||||
|
done = write_impl<std::nullptr_t, true>(w, ss);;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
w.st_.pop(has_value);
|
||||||
|
|
||||||
|
if( has_value )
|
||||||
|
done = write_impl<Elem, false>(w, ss);
|
||||||
|
else
|
||||||
|
done = write_impl<std::nullptr_t, false>(w, ss);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(BOOST_JSON_UNLIKELY( !done ))
|
||||||
|
w.st_.push(has_value);
|
||||||
|
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
template<class T, bool StackEmpty>
|
template<class T, bool StackEmpty>
|
||||||
bool
|
bool
|
||||||
write_impl(writer& w, stream& ss)
|
write_impl(writer& w, stream& ss)
|
||||||
|
@ -857,6 +857,15 @@ public:
|
|||||||
check_udt(std::monostate(), "null");
|
check_udt(std::monostate(), "null");
|
||||||
}
|
}
|
||||||
#endif // BOOST_NO_CXX17_HDR_VARIANT
|
#endif // BOOST_NO_CXX17_HDR_VARIANT
|
||||||
|
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
|
||||||
|
{
|
||||||
|
std::optional<int> o;
|
||||||
|
check_udt( o, "null" );
|
||||||
|
|
||||||
|
o = 2315;
|
||||||
|
check_udt( o, "2315" );
|
||||||
|
}
|
||||||
|
#endif // BOOST_NO_CXX17_HDR_OPTIONAL
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user