Add Boost.Hana support for binary (de)serialization
This commit is contained in:
parent
9dc7bd371c
commit
ac1fe81497
|
@ -21,10 +21,26 @@
|
|||
#include <boost/hana/intersection.hpp>
|
||||
#include <boost/hana/keys.hpp>
|
||||
|
||||
namespace ReflectiveRapidJSON {
|
||||
namespace BinaryReflector {
|
||||
namespace JsonReflector {
|
||||
|
||||
template <typename Type, Traits::EnableIf<IsCustomType<Type>> *>
|
||||
void readCustomType(BinaryDeserializer &deserializer, Type &customType)
|
||||
{
|
||||
boost::hana::for_each(boost::hana::keys(customType), [&deserializer, &customType](auto key) {
|
||||
deserializer.read(boost::hana::at_key(customType, key));
|
||||
});
|
||||
}
|
||||
|
||||
template <typename Type, Traits::EnableIf<IsCustomType<Type>> *>
|
||||
void writeCustomType(BinarySerializer &serializer, const Type &customType)
|
||||
{
|
||||
boost::hana::for_each(boost::hana::keys(customType), [&serializer, &customType](auto key) {
|
||||
serializer.write(boost::hana::at_key(customType, key));
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace JsonReflector
|
||||
} // namespace BinaryReflector
|
||||
} // namespace ReflectiveRapidJSON
|
||||
|
||||
#endif // REFLECTIVE_RAPIDJSON_BINARY_REFLECTOR_BOOST_HANA_H
|
||||
|
|
|
@ -30,16 +30,12 @@ using namespace ReflectiveRapidJSON;
|
|||
/// \cond
|
||||
|
||||
// define some structs for testing serialization
|
||||
struct TestObjectHana : public BinarySerializable<TestObjectHana> {
|
||||
BOOST_HANA_DEFINE_STRUCT(TestObjectHana, (int, number), (double, number2), (vector<int>, numbers), (string, text), (bool, boolean));
|
||||
struct TestObjectBinaryHana : public BinarySerializable<TestObjectBinaryHana> {
|
||||
BOOST_HANA_DEFINE_STRUCT(TestObjectBinaryHana, (int, number), (double, number2), (vector<int>, numbers), (string, text), (bool, boolean));
|
||||
};
|
||||
|
||||
struct NestingObjectHana : public BinarySerializable<NestingObjectHana> {
|
||||
BOOST_HANA_DEFINE_STRUCT(NestingObjectHana, (string, name), (TestObjectHana, testObj));
|
||||
};
|
||||
|
||||
struct NestingArrayHana : public BinarySerializable<NestingArrayHana> {
|
||||
BOOST_HANA_DEFINE_STRUCT(NestingArrayHana, (string, name), (vector<TestObjectHana>, testObjects));
|
||||
struct NestingArrayBinaryHana : public BinarySerializable<NestingArrayBinaryHana> {
|
||||
BOOST_HANA_DEFINE_STRUCT(NestingArrayBinaryHana, (string, name), (vector<TestObjectBinaryHana>, testObjects));
|
||||
};
|
||||
|
||||
/// \endcond
|
||||
|
@ -50,12 +46,15 @@ struct NestingArrayHana : public BinarySerializable<NestingArrayHana> {
|
|||
*/
|
||||
class BinaryReflectorBoostHanaTests : public TestFixture {
|
||||
CPPUNIT_TEST_SUITE(BinaryReflectorBoostHanaTests);
|
||||
CPPUNIT_TEST(testSerializingAndDeserializing);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
void testSerializingAndDeserializing();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
@ -68,3 +67,32 @@ void BinaryReflectorBoostHanaTests::setUp()
|
|||
void BinaryReflectorBoostHanaTests::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
void BinaryReflectorBoostHanaTests::testSerializingAndDeserializing()
|
||||
{
|
||||
TestObjectBinaryHana testObject;
|
||||
testObject.number = 42;
|
||||
testObject.number2 = 1234.25;
|
||||
testObject.numbers = { 1, 2, 3, 4, 5 };
|
||||
testObject.text = "foo";
|
||||
testObject.boolean = true;
|
||||
|
||||
NestingArrayBinaryHana nestingObject;
|
||||
nestingObject.name = "bar";
|
||||
nestingObject.testObjects.emplace_back(testObject);
|
||||
|
||||
stringstream stream(ios_base::in | ios_base::out | ios_base::binary);
|
||||
stream.exceptions(ios_base::failbit | ios_base::badbit);
|
||||
nestingObject.serialize(stream);
|
||||
|
||||
NestingArrayBinaryHana deserializedObject;
|
||||
deserializedObject.deserialize(stream);
|
||||
const TestObjectBinaryHana &deserializedTestObj(deserializedObject.testObjects.at(0));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(nestingObject.name, deserializedObject.name);
|
||||
CPPUNIT_ASSERT_EQUAL(testObject.number, deserializedTestObj.number);
|
||||
CPPUNIT_ASSERT_EQUAL(testObject.number2, deserializedTestObj.number2);
|
||||
CPPUNIT_ASSERT_EQUAL(testObject.numbers, deserializedTestObj.numbers);
|
||||
CPPUNIT_ASSERT_EQUAL(testObject.text, deserializedTestObj.text);
|
||||
CPPUNIT_ASSERT_EQUAL(testObject.boolean, deserializedTestObj.boolean);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue