Reflection for RapidJSON
0.0.17
Reflection for serializing/deserializing with RapidJSON
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
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
:
37
JsonReflectorChronoUtilitiesTests
();
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
49
CPPUNIT_TEST_SUITE_REGISTRATION
(
JsonReflectorChronoUtilitiesTests
);
50
51
JsonReflectorChronoUtilitiesTests::JsonReflectorChronoUtilitiesTests
()
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
61
void
JsonReflectorChronoUtilitiesTests::testSerializeing
()
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
80
void
JsonReflectorChronoUtilitiesTests::testDeserializeing
()
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;
89
JsonDeserializationErrors
errors;
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
101
void
JsonReflectorChronoUtilitiesTests::testErroHandling
()
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;
110
JsonDeserializationErrors
errors;
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
}
JsonReflectorChronoUtilitiesTests
The JsonReflectorChronoUtilitiesTests class tests the custom (de)serialization of the chrono utilitie...
Definition
jsonreflector-chronoutilities.cpp:29
JsonReflectorChronoUtilitiesTests::testSerializeing
void testSerializeing()
Tests serializing DateTime and TimeSpan objects.
Definition
jsonreflector-chronoutilities.cpp:61
JsonReflectorChronoUtilitiesTests::JsonReflectorChronoUtilitiesTests
JsonReflectorChronoUtilitiesTests()
Definition
jsonreflector-chronoutilities.cpp:51
JsonReflectorChronoUtilitiesTests::testErroHandling
void testErroHandling()
Tests deserializing DateTime and TimeSpan objects (error case).
Definition
jsonreflector-chronoutilities.cpp:101
JsonReflectorChronoUtilitiesTests::testDeserializeing
void testDeserializeing()
Tests deserializing DateTime and TimeSpan objects.
Definition
jsonreflector-chronoutilities.cpp:80
reflector-chronoutilities.h
Contains functions for (de)serializing objects from the chrono utilities provided by the C++ utilitie...
serializable.h
Contains only the definition of the JsonSerializable template class which makes the reflection access...
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(JsonReflectorTests)
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::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
ReflectiveRapidJSON
Definition
traits.h:13
ReflectiveRapidJSON::JsonDeserializationErrors
The JsonDeserializationErrors struct can be passed to fromJson() for error handling.
Definition
errorhandling.h:154
Generated on
for Reflection for RapidJSON by
1.18.0