Add JSON support (refs #28)

This commit is contained in:
Peter Dimov
2022-10-18 06:08:18 +03:00
parent 457147d1b0
commit 67a12199e6
4 changed files with 175 additions and 2 deletions

View File

@ -15,8 +15,9 @@
#include <boost/mp11.hpp>
#include <boost/assert.hpp>
#include <boost/assert/source_location.hpp>
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/config/workaround.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
#include <type_traits>
@ -24,8 +25,9 @@
#include <initializer_list>
#include <utility>
#include <functional> // std::hash
#include <cstdint>
#include <iosfwd>
#include <cstdint>
#include <cerrno>
//
@ -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<class T>
void value_from( T&& t, value& jv );
template<class T>
struct value_to_tag;
template<class T>
T value_to( value const & v );
template<class T>
struct try_value_to_tag;
template<class T1, class T2>
struct result_for;
template<class T>
typename result_for<T, value>::type
try_value_to( value const & jv );
template<class T>
typename result_for<T, value>::type
result_from_errno( int e, boost::source_location const* loc ) noexcept;
template<class T, class E> struct is_null_like;
template<> struct is_null_like< variant2::monostate, void >: std::true_type
{
};
} // namespace json
namespace variant2
{
template<class... T>
void tag_invoke( boost::json::value_from_tag const&, boost::json::value& v, variant<T...> const & w )
{
visit( [&](auto const& t){
boost::json::value_from( t, v );
}, w );
}
template<class... T>
typename boost::json::result_for<variant<T...>, boost::json::value>::type
tag_invoke( boost::json::try_value_to_tag<variant<T...>> const&, boost::json::value const& v )
{
static constexpr boost::source_location loc = BOOST_CURRENT_LOCATION;
auto r = boost::json::result_from_errno< variant<T...> >( EINVAL, &loc );
mp11::mp_for_each<mp11::mp_iota_c<sizeof...(T)>>( [&](auto I){
if( !r )
{
using Ti = mp11::mp_at_c<variant<T...>, I>;
auto r2 = boost::json::try_value_to<Ti>( v );
if( r2 )
{
r.emplace( in_place_index<I>, *r2 );
}
}
});
return r;
}
} // namespace variant2
} // namespace boost
#undef BOOST_VARIANT2_CX14_ASSERT
#if defined(_MSC_VER) && _MSC_VER < 1910

View File

@ -126,3 +126,8 @@ run variant_visit_by_index.cpp ;
run variant_ostream_insert.cpp ;
run is_output_streamable.cpp ;
local JSON = <library>/boost//json/<warnings>off "<toolset>msvc-14.0:<build>no" "<toolset>msvc-14.2:<cxxflags>-wd5104" ;
run variant_json_value_from.cpp : : : $(JSON) ;
run variant_json_value_to.cpp : : : $(JSON) ;

View File

@ -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 <boost/variant2/variant.hpp>
#include <boost/json/value_from.hpp>
#include <boost/json/serialize.hpp>
#include <boost/core/lightweight_test.hpp>
#include <string>
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<monostate, int, std::string> v;
json::value w = json::value_from( v );
BOOST_TEST_EQ( w, json::value( nullptr ) );
}
{
variant<monostate, int, std::string> v( 17 );
json::value w = json::value_from( v );
BOOST_TEST_EQ( w, json::value( 17 ) );
}
{
variant<monostate, int, std::string> v( "test" );
json::value w = json::value_from( v );
BOOST_TEST_EQ( w, json::value( "test" ) );
}
return boost::report_errors();
}

View File

@ -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 <boost/variant2/variant.hpp>
#include <boost/json/value_to.hpp>
#include <boost/json/serialize.hpp>
#include <boost/core/lightweight_test.hpp>
using namespace boost::variant2;
namespace json = boost::json;
int main()
{
{
json::value v;
auto r = json::try_value_to<monostate>( v );
BOOST_TEST( r.has_value() );
}
using V = variant<monostate, int, std::string>;
{
json::value v;
auto r = json::try_value_to<V>( v );
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, V() );
}
{
json::value v( 12 );
auto r = json::try_value_to<V>( v );
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, V(12) );
}
{
json::value v( "test" );
auto r = json::try_value_to<V>( v );
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, V("test") );
}
return boost::report_errors();
}