Reflection for RapidJSON  0.0.6
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 TestUtilities::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 IoUtilities;
25 using namespace ConversionUtilities;
26 using namespace TestUtilities;
27 using namespace TestUtilities::Literals;
28 using namespace ReflectiveRapidJSON;
29 
31 
32 // define some structs for testing serialization
33 struct TestObjectBinaryHana : public BinarySerializable<TestObjectBinaryHana> {
34  BOOST_HANA_DEFINE_STRUCT(TestObjectBinaryHana, (int, number), (double, number2), (vector<int>, numbers), (string, text), (bool, boolean));
35 };
36 
37 struct NestingArrayBinaryHana : public BinarySerializable<NestingArrayBinaryHana> {
38  BOOST_HANA_DEFINE_STRUCT(NestingArrayBinaryHana, (string, name), (vector<TestObjectBinaryHana>, testObjects));
39 };
40 
42 
47 class BinaryReflectorBoostHanaTests : public TestFixture {
48  CPPUNIT_TEST_SUITE(BinaryReflectorBoostHanaTests);
49  CPPUNIT_TEST(testSerializingAndDeserializing);
50  CPPUNIT_TEST_SUITE_END();
51 
52 public:
53  void setUp();
54  void tearDown();
55 
56  void testSerializingAndDeserializing();
57 
58 private:
59 };
60 
62 
64 {
65 }
66 
68 {
69 }
70 
72 {
73  TestObjectBinaryHana testObject;
74  testObject.number = 42;
75  testObject.number2 = 1234.25;
76  testObject.numbers = { 1, 2, 3, 4, 5 };
77  testObject.text = "foo";
78  testObject.boolean = true;
79 
80  NestingArrayBinaryHana nestingObject;
81  nestingObject.name = "bar";
82  nestingObject.testObjects.emplace_back(testObject);
83 
84  stringstream stream(ios_base::in | ios_base::out | ios_base::binary);
85  stream.exceptions(ios_base::failbit | ios_base::badbit);
86  nestingObject.toBinary(stream);
87 
88  const auto deserializedObject(NestingArrayBinaryHana::fromBinary(stream));
89  const auto &deserializedTestObj(deserializedObject.testObjects.at(0));
90  CPPUNIT_ASSERT_EQUAL(nestingObject.name, deserializedObject.name);
91  CPPUNIT_ASSERT_EQUAL(testObject.number, deserializedTestObj.number);
92  CPPUNIT_ASSERT_EQUAL(testObject.number2, deserializedTestObj.number2);
93  CPPUNIT_ASSERT_EQUAL(testObject.numbers, deserializedTestObj.numbers);
94  CPPUNIT_ASSERT_EQUAL(testObject.text, deserializedTestObj.text);
95  CPPUNIT_ASSERT_EQUAL(testObject.boolean, deserializedTestObj.boolean);
96 }
STL namespace.
CPPUNIT_TEST_SUITE_REGISTRATION(BinaryReflectorBoostHanaTests)
The BinaryReflectorBoostHanaTests class tests the integration of Boost.Hana with the (de)serializer...
The BinarySerializable class provides the CRTP-base for (de)serializable objects. ...
Definition: reflector.h:33