Reflection for RapidJSON 0.0.16
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
jsonreflector-chronoutilities.cpp
Go to the documentation of this file.
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
17using namespace std;
18using namespace CPPUNIT_NS;
19using namespace RAPIDJSON_NAMESPACE;
20using namespace CppUtilities;
21using namespace CppUtilities::Literals;
22using namespace ReflectiveRapidJSON;
23
29class 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
36public:
38
39 void testSerializeing();
40 void testDeserializeing();
41 void testErroHandling();
42
43private:
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}
The JsonReflectorChronoUtilitiesTests class tests the custom (de)serialization of the chrono utilitie...
void testSerializeing()
Tests serializing DateTime and TimeSpan objects.
void testErroHandling()
Tests deserializing DateTime and TimeSpan objects (error case).
void testDeserializeing()
Tests deserializing DateTime and TimeSpan objects.
Contains functions for (de)serializing objects from the chrono utilities provided by the C++ utilitie...
Contains only the definition of the JsonSerializable template class which makes the reflection access...
CPPUNIT_TEST_SUITE_REGISTRATION(JsonReflectorChronoUtilitiesTests)
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.
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
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.