Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

**WIP DO NOT MERGE IT**: Add automatic serialization support for boost::hana::Struct #55

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions include/trial/protocol/json/serialization/boost/hana.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021 Vinícius dos Santos Oliveira
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
///////////////////////////////////////////////////////////////////////////////

#ifndef TRIAL_PROTOCOL_JSON_SERIALIZATION_BOOST_HANA_HPP
#define TRIAL_PROTOCOL_JSON_SERIALIZATION_BOOST_HANA_HPP

#include <trial/protocol/json/serialization/serialization.hpp>
#include <boost/hana.hpp>
#include <trial/protocol/json/token.hpp>
#include <trial/protocol/json/partial/skip.hpp>

namespace trial
{
namespace protocol
{
namespace serialization
{

template <typename CharT, typename Value>
struct save_overloader<json::basic_oarchive<CharT>,
Value,
std::enable_if_t<boost::hana::Struct<Value>::value>>
{
static void save(json::basic_oarchive<CharT>& ar,
const Value& data,
const unsigned int /*protocol_version*/)
{
using string_view = core::detail::basic_string_view<CharT>;

ar.template save<json::token::begin_object>();
boost::hana::for_each(data, [&ar](auto member) {
auto key = boost::hana::first(member);
ar.save(string_view(key.c_str(), boost::hana::size(key)));
// TODO: what if subvalue uses old reflection infra? then it
// requires us to write the necessary framing (begin_array and
// end_array)
ar << boost::hana::second(member);
});
ar.template save<json::token::end_object>();
}
};

template <typename CharT, typename Value>
struct load_overloader<json::basic_iarchive<CharT>,
Value,
std::enable_if_t<boost::hana::Struct<Value>::value>>
{
static void load(json::basic_iarchive<CharT>& ar,
Value& data,
const unsigned int /*protocol_version*/)
{
ar.template load<json::token::begin_object>();
for (;;) {
if (ar.symbol() == json::token::symbol::end_object) {
ar.template load<json::token::end_object>();
break;
}

// skip key
{
auto r = ar.reader();
json::partial::skip(r);
ar.reader(std::move(r));
}
// skip value
{
auto r = ar.reader();
json::partial::skip(r);
ar.reader(std::move(r));
}

// TODO: actually decode stuff
//ar >> x;
}
ar.template load<json::token::end_object>();
}
};

} // namespace serialization
} // namespace protocol
} // namespace trial

#endif // TRIAL_PROTOCOL_JSON_SERIALIZATION_BOOST_HANA_HPP
1 change: 1 addition & 0 deletions test/json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ trial_add_test(json_writer_suite writer_suite.cpp)
trial_add_test(json_iarchive_suite iarchive_suite.cpp)
trial_add_test(json_oarchive_suite oarchive_suite.cpp)
trial_add_test(json_partial_skip_suite skip_suite.cpp)
trial_add_test(hana_reflection_suite hana_reflection_suite.cpp)

# Tree processing
trial_add_test(json_parse_suite parse_suite.cpp)
Expand Down
70 changes: 70 additions & 0 deletions test/json/hana_reflection_suite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021 Vinícius dos Santos Oliveira
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
///////////////////////////////////////////////////////////////////////////////

#include <sstream>
#include <trial/protocol/buffer/ostream.hpp>
#include <trial/protocol/json/serialization/boost/hana.hpp>
#include <trial/protocol/core/detail/lightweight_test.hpp>

using namespace trial::protocol;

//-----------------------------------------------------------------------------
// Basic types
//-----------------------------------------------------------------------------

struct Foobar {
BOOST_HANA_DEFINE_STRUCT(Foobar,
(int, foo),
(int, bar),
(int, baz)
);
};

namespace basic_suite
{

void test_oarchive()
{
std::ostringstream result;
json::oarchive ar(result);
Foobar value{5, 10, 0};
ar << value;
TRIAL_PROTOCOL_TEST_EQUAL(result.str(), "{\"foo\":5,\"bar\":10,\"baz\":0}");
}

void test_iarchive()
{
const char input[] = "{\"foo\":5,\"bar\":10,\"baz\":0}";
json::iarchive in(input);
Foobar value{1, 2, 3};
TRIAL_PROTOCOL_TEST_NO_THROW(in >> value);
TRIAL_PROTOCOL_TEST_EQUAL(value.foo, 5);
TRIAL_PROTOCOL_TEST_EQUAL(value.bar, 10);
TRIAL_PROTOCOL_TEST_EQUAL(value.baz, 0);
}

void run()
{
test_oarchive();
test_iarchive();
}

} // namespace basic_suite

//-----------------------------------------------------------------------------
// main
//-----------------------------------------------------------------------------

int main()
{
basic_suite::run();

return boost::report_errors();
}