diff --git a/lib/binary/reflector-boosthana.h b/lib/binary/reflector-boosthana.h index 611a480..b13e43c 100644 --- a/lib/binary/reflector-boosthana.h +++ b/lib/binary/reflector-boosthana.h @@ -21,10 +21,26 @@ #include #include +namespace ReflectiveRapidJSON { namespace BinaryReflector { -namespace JsonReflector { + +template > *> +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 > *> +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 diff --git a/lib/tests/binaryreflector-boosthana.cpp b/lib/tests/binaryreflector-boosthana.cpp index 7e071ce..4dc68fd 100644 --- a/lib/tests/binaryreflector-boosthana.cpp +++ b/lib/tests/binaryreflector-boosthana.cpp @@ -30,16 +30,12 @@ using namespace ReflectiveRapidJSON; /// \cond // define some structs for testing serialization -struct TestObjectHana : public BinarySerializable { - BOOST_HANA_DEFINE_STRUCT(TestObjectHana, (int, number), (double, number2), (vector, numbers), (string, text), (bool, boolean)); +struct TestObjectBinaryHana : public BinarySerializable { + BOOST_HANA_DEFINE_STRUCT(TestObjectBinaryHana, (int, number), (double, number2), (vector, numbers), (string, text), (bool, boolean)); }; -struct NestingObjectHana : public BinarySerializable { - BOOST_HANA_DEFINE_STRUCT(NestingObjectHana, (string, name), (TestObjectHana, testObj)); -}; - -struct NestingArrayHana : public BinarySerializable { - BOOST_HANA_DEFINE_STRUCT(NestingArrayHana, (string, name), (vector, testObjects)); +struct NestingArrayBinaryHana : public BinarySerializable { + BOOST_HANA_DEFINE_STRUCT(NestingArrayBinaryHana, (string, name), (vector, testObjects)); }; /// \endcond @@ -50,12 +46,15 @@ struct NestingArrayHana : public BinarySerializable { */ 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); +}