forked from Stiffstream/json_dto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
124 lines (107 loc) · 2.54 KB
/
main.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <cstdint>
#include <json_dto/pub.hpp>
struct supported_types_t
{
supported_types_t() {}
supported_types_t(
bool val_bool,
std::int16_t val_int16,
std::uint16_t val_uint16,
std::int32_t val_int32,
std::uint32_t val_uint32,
std::int64_t val_int64,
std::uint64_t val_uint64,
double val_double,
std::string val_string )
: m_bool{ val_bool }
, m_int16{ val_int16 }
, m_uint16{ val_uint16 }
, m_int32{ val_int32 }
, m_uint32{ val_uint32 }
, m_int64{ val_int64 }
, m_uint64{ val_uint64 }
, m_double{ val_double }
, m_string{ std::move( val_string ) }
{}
bool m_bool{ false };
std::int16_t m_int16{};
std::uint16_t m_uint16{};
std::int32_t m_int32{};
std::uint32_t m_uint32{};
std::int64_t m_int64{};
std::uint64_t m_uint64{};
double m_double{};
std::string m_string{};
};
namespace json_dto
{
template< typename Json_Io >
void json_io( Json_Io & io, supported_types_t & obj )
{
io & json_dto::mandatory( "bool", obj.m_bool )
& json_dto::mandatory( "int16", obj.m_int16 )
& json_dto::mandatory( "uint16", obj.m_uint16 )
& json_dto::mandatory( "int32", obj.m_int32 )
& json_dto::mandatory( "uint32", obj.m_uint32 )
& json_dto::mandatory( "int64", obj.m_int64 )
& json_dto::mandatory( "uint64", obj.m_uint64 )
& json_dto::mandatory( "double", obj.m_double )
& json_dto::mandatory( "string", obj.m_string );
}
} /* namespace json_dto */
const std::string json_data{
R"JSON({
"bool" : true,
"int16" : -1,
"uint16" : 2,
"int32" : -4,
"uint32" : 8,
"int64" : -16,
"uint64" : 32,
"double" : 2.718281828,
"string" : "Sample string"
})JSON" };
int
main( int , char *[] )
{
try
{
{
auto obj = json_dto::from_json< supported_types_t >( json_data );
std::cout
<< "Deserialized from JSON:\n"
<< "\tbool: " << obj.m_bool << "\n"
<< "\tint16: " << obj.m_int16 << "\n"
<< "\tuint16: " << obj.m_uint16 << "\n"
<< "\tint32: " << obj.m_int32 << "\n"
<< "\tuint32: " << obj.m_uint32 << "\n"
<< "\tint64: " << obj.m_int64 << "\n"
<< "\tuint64: " << obj.m_uint64 << "\n"
<< "\tdouble: " << obj.m_double << "\n"
<< "\tstring: " << obj.m_string << "\n"
<< std::endl;
}
{
const supported_types_t obj{
false,
-10,
0xFFFF,
-100,
0xFFFFFFFF,
~10000,
0xFFFFFFFFFFFFFFFFul,
3.14,
"Another string" };
std::cout
<< "\nSerialized to JSON:\n"
<< json_dto::to_json( obj ) << std::endl;
}
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << "." << std::endl;
return 1;
}
return 0;
}