Reflection for RapidJSON  0.0.15
Reflection for serializing/deserializing with RapidJSON
binaryreflector-boosthana.cpp
Go to the documentation of this file.
1 #include "../binary/reflector-boosthana.h"
2 #include "../binary/serializable.h"
3 
4 #include <c++utilities/conversion/stringbuilder.h>
5 #include <c++utilities/conversion/stringconversion.h>
6 #include <c++utilities/io/misc.h>
7 #include <c++utilities/tests/testutils.h>
8 
9 using CppUtilities::operator<<; // must be visible prior to the call site
10 #include <cppunit/TestFixture.h>
11 #include <cppunit/extensions/HelperMacros.h>
12 
13 #include <rapidjson/document.h>
14 #include <rapidjson/stringbuffer.h>
15 #include <rapidjson/writer.h>
16 
17 #include <iostream>
18 #include <string>
19 #include <vector>
20 
21 using namespace std;
22 using namespace CPPUNIT_NS;
23 using namespace RAPIDJSON_NAMESPACE;
24 using namespace CppUtilities;
25 using namespace CppUtilities::Literals;
26 using namespace ReflectiveRapidJSON;
27 
29 
30 // define some structs for testing serialization
31 struct TestObjectBinaryHana : public BinarySerializable<TestObjectBinaryHana> {
32  BOOST_HANA_DEFINE_STRUCT(TestObjectBinaryHana, (int, number), (double, number2), (vector<int>, numbers), (string, text), (bool, boolean));
33 };
34 
35 struct NestingArrayBinaryHana : public BinarySerializable<NestingArrayBinaryHana> {
36  BOOST_HANA_DEFINE_STRUCT(NestingArrayBinaryHana, (string, name), (vector<TestObjectBinaryHana>, testObjects));
37 };
38 
40 
45 class BinaryReflectorBoostHanaTests : public TestFixture {
46  CPPUNIT_TEST_SUITE(BinaryReflectorBoostHanaTests);
47  CPPUNIT_TEST(testSerializingAndDeserializing);
48  CPPUNIT_TEST_SUITE_END();
49 
50 public:
51  void setUp() override;
52  void tearDown() override;
53 
54  void testSerializingAndDeserializing();
55 
56 private:
57 };
58 
60 
62 {
63 }
64 
66 {
67 }
68 
70 {
71  TestObjectBinaryHana testObject;
72  testObject.number = 42;
73  testObject.number2 = 1234.25;
74  testObject.numbers = { 1, 2, 3, 4, 5 };
75  testObject.text = "foo";
76  testObject.boolean = true;
77 
78  NestingArrayBinaryHana nestingObject;
79  nestingObject.name = "bar";
80  nestingObject.testObjects.emplace_back(testObject);
81 
82  stringstream stream(ios_base::in | ios_base::out | ios_base::binary);
83  stream.exceptions(ios_base::failbit | ios_base::badbit);
84  nestingObject.toBinary(stream);
85 
86  const auto deserializedObject(NestingArrayBinaryHana::fromBinary(stream));
87  const auto &deserializedTestObj(deserializedObject.testObjects.at(0));
88  CPPUNIT_ASSERT_EQUAL(nestingObject.name, deserializedObject.name);
89  CPPUNIT_ASSERT_EQUAL(testObject.number, deserializedTestObj.number);
90  CPPUNIT_ASSERT_EQUAL(testObject.number2, deserializedTestObj.number2);
91  CPPUNIT_ASSERT_EQUAL(testObject.numbers, deserializedTestObj.numbers);
92  CPPUNIT_ASSERT_EQUAL(testObject.text, deserializedTestObj.text);
93  CPPUNIT_ASSERT_EQUAL(testObject.boolean, deserializedTestObj.boolean);
94 }
BinaryReflectorBoostHanaTests
The BinaryReflectorBoostHanaTests class tests the integration of Boost.Hana with the (de)serializer.
Definition: binaryreflector-boosthana.cpp:45
ReflectiveRapidJSON
Definition: traits.h:13
BinaryReflectorBoostHanaTests::testSerializingAndDeserializing
void testSerializingAndDeserializing()
Definition: binaryreflector-boosthana.cpp:69
BinaryReflectorBoostHanaTests::tearDown
void tearDown() override
Definition: binaryreflector-boosthana.cpp:65
ReflectiveRapidJSON::BinarySerializable
The BinarySerializable class provides the CRTP-base for (de)serializable objects.
Definition: reflector.h:36
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(BinaryReflectorBoostHanaTests)
BinaryReflectorBoostHanaTests::setUp
void setUp() override
Definition: binaryreflector-boosthana.cpp:61