Reflection for RapidJSON 0.0.17
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
binaryreflector-boosthana.cpp
Go to the documentation of this file.
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
9using 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
21using namespace std;
22using namespace CPPUNIT_NS;
23using namespace RAPIDJSON_NAMESPACE;
24using namespace CppUtilities;
25using namespace CppUtilities::Literals;
26using namespace ReflectiveRapidJSON;
27
29
30// define some structs for testing serialization
31struct TestObjectBinaryHana : public BinarySerializable<TestObjectBinaryHana> {
32 BOOST_HANA_DEFINE_STRUCT(TestObjectBinaryHana, (int, number), (double, number2), (vector<int>, numbers), (string, text), (bool, boolean));
33};
34
35struct NestingArrayBinaryHana : public BinarySerializable<NestingArrayBinaryHana> {
36 BOOST_HANA_DEFINE_STRUCT(NestingArrayBinaryHana, (string, name), (vector<TestObjectBinaryHana>, testObjects));
37};
38
40
45class BinaryReflectorBoostHanaTests : public TestFixture {
46 CPPUNIT_TEST_SUITE(BinaryReflectorBoostHanaTests);
48 CPPUNIT_TEST_SUITE_END();
49
50public:
51 void setUp() override;
52 void tearDown() override;
53
55
56private:
57};
58
60
64
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}
Contains generic functions relying on Boost.Hana which can replace the code which would otherwise had...
Contains only the definition of the BinarySerializable template class which makes the reflection acce...
The BinaryReflectorBoostHanaTests class tests the integration of Boost.Hana with the (de)serializer.
CPPUNIT_TEST_SUITE_REGISTRATION(JsonReflectorTests)
The BinarySerializable class provides the CRTP-base for (de)serializable objects.