Reflection for RapidJSON  0.0.15
Reflection for serializing/deserializing with RapidJSON
jsonreflector-chronoutilities.cpp
Go to the documentation of this file.
1 #include "../json/reflector-chronoutilities.h"
2 #include "../json/serializable.h"
3 
4 #include <c++utilities/tests/testutils.h>
5 
6 #include <cppunit/TestFixture.h>
7 #include <cppunit/extensions/HelperMacros.h>
8 
9 #include <rapidjson/document.h>
10 #include <rapidjson/stringbuffer.h>
11 #include <rapidjson/writer.h>
12 
13 #include <iostream>
14 #include <string>
15 #include <vector>
16 
17 using namespace std;
18 using namespace CPPUNIT_NS;
19 using namespace RAPIDJSON_NAMESPACE;
20 using namespace CppUtilities;
21 using namespace CppUtilities::Literals;
22 using namespace ReflectiveRapidJSON;
23 
29 class JsonReflectorChronoUtilitiesTests : public TestFixture {
30  CPPUNIT_TEST_SUITE(JsonReflectorChronoUtilitiesTests);
31  CPPUNIT_TEST(testSerializeing);
32  CPPUNIT_TEST(testDeserializeing);
33  CPPUNIT_TEST(testErroHandling);
34  CPPUNIT_TEST_SUITE_END();
35 
36 public:
38 
39  void testSerializeing();
40  void testDeserializeing();
41  void testErroHandling();
42 
43 private:
44  const DateTime m_dateTime;
45  const TimeSpan m_timeSpan;
46  const string m_string;
47 };
48 
50 
52  : m_dateTime(DateTime::fromDateAndTime(2017, 4, 2, 15, 31, 21, 165.125))
53  , m_timeSpan(TimeSpan::fromHours(3.25) + TimeSpan::fromSeconds(19.125))
54  , m_string("[\"2017-04-02T15:31:21.165125\",\"03:15:19.125\"]")
55 {
56 }
57 
62 {
63  Document doc(kArrayType);
64  Document::AllocatorType &alloc = doc.GetAllocator();
65  doc.SetArray();
66  Document::Array array(doc.GetArray());
67 
68  JsonReflector::push(m_dateTime, array, alloc);
69  JsonReflector::push(m_timeSpan, array, alloc);
70 
71  StringBuffer strbuf;
72  Writer<StringBuffer> jsonWriter(strbuf);
73  doc.Accept(jsonWriter);
74  CPPUNIT_ASSERT_EQUAL(m_string, string(strbuf.GetString()));
75 }
76 
81 {
82  Document doc(kArrayType);
83 
84  doc.Parse(m_string.data(), m_string.size());
85  auto array = doc.GetArray().begin();
86 
87  DateTime dateTime;
88  TimeSpan timeSpan;
90  JsonReflector::pull(dateTime, array, &errors);
91  JsonReflector::pull(timeSpan, array, &errors);
92 
93  CPPUNIT_ASSERT_EQUAL(0_st, errors.size());
94  CPPUNIT_ASSERT_EQUAL(m_dateTime.toIsoString(), dateTime.toIsoString());
95  CPPUNIT_ASSERT_EQUAL(m_timeSpan.toString(), timeSpan.toString());
96 }
97 
102 {
103  Document doc(kArrayType);
104 
105  doc.Parse("[\"2017-04-31T15:31:21.165125\",\"03:15:19.125\"]");
106  auto array = doc.GetArray().begin();
107 
108  DateTime dateTime;
109  TimeSpan timeSpan;
111  JsonReflector::pull(dateTime, array, &errors);
112  JsonReflector::pull(timeSpan, array, &errors);
113 
114  CPPUNIT_ASSERT_EQUAL(1_st, errors.size());
115  CPPUNIT_ASSERT(dateTime.isNull());
116  CPPUNIT_ASSERT_EQUAL(m_timeSpan.toString(), timeSpan.toString());
117 }
ReflectiveRapidJSON::JsonDeserializationErrors
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
Definition: errorhandling.h:154
JsonReflectorChronoUtilitiesTests
The JsonReflectorChronoUtilitiesTests class tests the custom (de)serialization of the chrono utilitie...
Definition: jsonreflector-chronoutilities.cpp:29
ReflectiveRapidJSON::JsonReflector::pull
void pull(Type &reflectable, const RAPIDJSON_NAMESPACE::GenericValue< RAPIDJSON_NAMESPACE::UTF8< char >>::ConstObject &value, JsonDeserializationErrors *errors)
Pulls the reflectable which has a custom type from the specified object.
Definition: reflector-boosthana.h:40
ReflectiveRapidJSON
Definition: traits.h:13
JsonReflectorChronoUtilitiesTests::testDeserializeing
void testDeserializeing()
Tests deserializing DateTime and TimeSpan objects.
Definition: jsonreflector-chronoutilities.cpp:80
JsonReflectorChronoUtilitiesTests::testSerializeing
void testSerializeing()
Tests serializing DateTime and TimeSpan objects.
Definition: jsonreflector-chronoutilities.cpp:61
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(JsonReflectorChronoUtilitiesTests)
JsonReflectorChronoUtilitiesTests::JsonReflectorChronoUtilitiesTests
JsonReflectorChronoUtilitiesTests()
Definition: jsonreflector-chronoutilities.cpp:51
ReflectiveRapidJSON::JsonReflector::push
void push(const Type &reflectable, RAPIDJSON_NAMESPACE::Value &value, RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
Pushes the specified reflectable to the specified value.
Definition: reflector.h:140
JsonReflectorChronoUtilitiesTests::testErroHandling
void testErroHandling()
Tests deserializing DateTime and TimeSpan objects (error case).
Definition: jsonreflector-chronoutilities.cpp:101