From 67a12199e65e37efcd96b69ab403eee58b559218 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 18 Oct 2022 06:08:18 +0300 Subject: [PATCH] Add JSON support (refs #28) --- include/boost/variant2/variant.hpp | 89 +++++++++++++++++++++++++++++- test/Jamfile | 5 ++ test/variant_json_value_from.cpp | 41 ++++++++++++++ test/variant_json_value_to.cpp | 42 ++++++++++++++ 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 test/variant_json_value_from.cpp create mode 100644 test/variant_json_value_to.cpp diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index e2bbc3a..b474643 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -15,8 +15,9 @@ #include #include +#include #include -#include +#include #include #include #include @@ -24,8 +25,9 @@ #include #include #include // std::hash -#include #include +#include +#include // @@ -2402,6 +2404,89 @@ template<> struct hash< ::boost::variant2::monostate > } // namespace std +// JSON support + +namespace boost +{ +namespace json +{ + +class value; + +struct value_from_tag; + +template +void value_from( T&& t, value& jv ); + +template +struct value_to_tag; + +template +T value_to( value const & v ); + +template +struct try_value_to_tag; + +template +struct result_for; + +template +typename result_for::type +try_value_to( value const & jv ); + +template +typename result_for::type +result_from_errno( int e, boost::source_location const* loc ) noexcept; + +template struct is_null_like; + +template<> struct is_null_like< variant2::monostate, void >: std::true_type +{ +}; + +} // namespace json + +namespace variant2 +{ + +template + void tag_invoke( boost::json::value_from_tag const&, boost::json::value& v, variant const & w ) +{ + visit( [&](auto const& t){ + + boost::json::value_from( t, v ); + + }, w ); +} + +template + typename boost::json::result_for, boost::json::value>::type + tag_invoke( boost::json::try_value_to_tag> const&, boost::json::value const& v ) +{ + static constexpr boost::source_location loc = BOOST_CURRENT_LOCATION; + auto r = boost::json::result_from_errno< variant >( EINVAL, &loc ); + + mp11::mp_for_each>( [&](auto I){ + + if( !r ) + { + using Ti = mp11::mp_at_c, I>; + auto r2 = boost::json::try_value_to( v ); + + if( r2 ) + { + r.emplace( in_place_index, *r2 ); + } + } + + }); + + return r; +} + +} // namespace variant2 +} // namespace boost + #undef BOOST_VARIANT2_CX14_ASSERT #if defined(_MSC_VER) && _MSC_VER < 1910 diff --git a/test/Jamfile b/test/Jamfile index 2fbb975..3c67277 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -126,3 +126,8 @@ run variant_visit_by_index.cpp ; run variant_ostream_insert.cpp ; run is_output_streamable.cpp ; + +local JSON = /boost//json/off "msvc-14.0:no" "msvc-14.2:-wd5104" ; + +run variant_json_value_from.cpp : : : $(JSON) ; +run variant_json_value_to.cpp : : : $(JSON) ; diff --git a/test/variant_json_value_from.cpp b/test/variant_json_value_from.cpp new file mode 100644 index 0000000..9734c74 --- /dev/null +++ b/test/variant_json_value_from.cpp @@ -0,0 +1,41 @@ +// Copyright 2022 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +using namespace boost::variant2; +namespace json = boost::json; + +int main() +{ + { + monostate m; + json::value w = json::value_from( m ); + BOOST_TEST_EQ( w, json::value( nullptr ) ); + } + + { + variant v; + json::value w = json::value_from( v ); + BOOST_TEST_EQ( w, json::value( nullptr ) ); + } + + { + variant v( 17 ); + json::value w = json::value_from( v ); + BOOST_TEST_EQ( w, json::value( 17 ) ); + } + + { + variant v( "test" ); + json::value w = json::value_from( v ); + BOOST_TEST_EQ( w, json::value( "test" ) ); + } + + return boost::report_errors(); +} diff --git a/test/variant_json_value_to.cpp b/test/variant_json_value_to.cpp new file mode 100644 index 0000000..1263931 --- /dev/null +++ b/test/variant_json_value_to.cpp @@ -0,0 +1,42 @@ +// Copyright 2022 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +using namespace boost::variant2; +namespace json = boost::json; + +int main() +{ + { + json::value v; + auto r = json::try_value_to( v ); + BOOST_TEST( r.has_value() ); + } + + using V = variant; + + { + json::value v; + auto r = json::try_value_to( v ); + BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, V() ); + } + + { + json::value v( 12 ); + auto r = json::try_value_to( v ); + BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, V(12) ); + } + + { + json::value v( "test" ); + auto r = json::try_value_to( v ); + BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, V("test") ); + } + + return boost::report_errors(); +}