forked from AustinBrunkhorst/CPP-Reflection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialization.cpp
More file actions
65 lines (49 loc) · 1.79 KB
/
Copy pathSerialization.cpp
File metadata and controls
65 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "TestReflectionModule.h"
#include "TestTypes.h"
#include "TestProperties.h"
using namespace ursine;
using namespace meta;
int main(void)
{
MetaInitialize( UsingModule( TestModule ) );
///////////////////////////////////////////////////////////////////////////
// Serialization
///////////////////////////////////////////////////////////////////////////
ComplexType complexType;
complexType.stringValue = "Testing";
complexType.intValue = 23;
complexType.floatValue = 77.0f;
complexType.doubleValue = 86.35f;
complexType.soundEffect.volume = 50.0f;
complexType.arrayValue = { 1, 2, 3, 4, 5 };
complexType.enumValue = Eighty;
std::cout << "Serialized: "
<< Variant( complexType ).SerializeJson( ).dump( )
<< std::endl;
///////////////////////////////////////////////////////////////////////////
// Deserialization
///////////////////////////////////////////////////////////////////////////
std::string complexJson = R"(
{
"arrayValue": [ 5, 6, 7, 8 ],
"doubleValue": 100.0,
"enumValue": "Two",
"floatValue": 98.5,
"intValue": -25,
"soundEffect":{
"volume": 100.0
},
"stringValue": "Deserialization!"
}
)";
std::string jsonError;
// This is a static helper method. It is essentially doing this -
// typeof( ComplexType ).DeserializeJson( ).GetValue<ComplexType>( )
ComplexType deserializedComplexType = Type::DeserializeJson<ComplexType>(
Json::parse( complexJson, jsonError )
);
std::cout << "Deserialized: " << std::endl
<< Variant( deserializedComplexType ).SerializeJson( ).dump( )
<< std::endl;
return 0;
}