-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
162 lines (138 loc) · 3.8 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
A sample using map<uint32_t, int> with several custom reader_writers.
*/
#include <cstdio>
#include <iostream>
#include <map>
#include <json_dto/pub.hpp>
// Basic functionality for (de)serializing of values.
struct basic_reader_writer
{
void read(
int & value,
const rapidjson::Value & from ) const
{
// Just use json_dto functionality.
json_dto::read_json_value( value, from );
}
void write(
const int & value,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator ) const
{
// Just use json_dto functionality.
json_dto::write_json_value( value, to, allocator );
}
};
// Reader_Writer for the case of simple representation of keys.
struct simple_reader_writer : basic_reader_writer
{
using basic_reader_writer::read;
using basic_reader_writer::write;
void read(
json_dto::mutable_map_key_t<std::uint32_t> & key,
const rapidjson::Value & from ) const
{
if( !from.IsString() )
throw std::runtime_error( "string value expected" );
if( 1 != std::sscanf( from.GetString(), "%u", &key.v ) )
throw std::runtime_error( "unable to parse key value" );
}
void write(
const json_dto::const_map_key_t<std::uint32_t> & key,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator ) const
{
char buf[32];
std::sprintf( buf, "%u", key.v );
to.SetString( buf, allocator );
}
};
// Reader_Writer for the case of representation of keys in hexadecimal form.
struct color_hex_reader_writer : basic_reader_writer
{
using basic_reader_writer::read;
using basic_reader_writer::write;
void read(
json_dto::mutable_map_key_t<std::uint32_t> & key,
const rapidjson::Value & from ) const
{
if( !from.IsString() )
throw std::runtime_error( "string value expected" );
const char * str_v = from.GetString();
if( std::strlen(str_v) < 2u )
throw std::runtime_error( "invalid value length" );
if( '#' != *str_v )
throw std::runtime_error( "invalid value format" );
if( 1 != std::sscanf( str_v+1, "%x", &key.v ) )
throw std::runtime_error( "unable to parse key value" );
}
void write(
const json_dto::const_map_key_t<std::uint32_t> & key,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator ) const
{
char buf[16];
std::sprintf( buf, "#%06x", key.v );
to.SetString( buf, allocator );
}
};
// Data type to be (de)serialized.
struct data_t
{
std::map< std::uint32_t, int > m_weights;
std::map< std::uint32_t, int > m_colors;
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::mandatory(
// Use apply_to_content_t from json_dto to apply
// simple_reader_writer to every member of m_weights.
json_dto::apply_to_content_t< simple_reader_writer >{},
"weights", m_weights )
& json_dto::mandatory(
json_dto::apply_to_content_t< color_hex_reader_writer >{},
"colors", m_colors )
;
}
};
const std::string json_data{
R"JSON({
"weights" : {"1": 1, "2": 2, "3": 3},
"colors" : {"#000000":1, "#FF0000": 2, "#00FF00": 3, "#0000FF": 4}
})JSON" };
int
main( int , char *[] )
{
try
{
{
auto data = json_dto::from_json< data_t >( json_data );
std::cout << "Deserialized from JSON:\n"
<< "weights: ";
for( const auto & kv : data.m_weights )
std::cout << kv.first << "->" << kv.second << ", ";
std::cout << "\n" "colors: ";
for( const auto & kv : data.m_colors )
std::cout << std::hex << kv.first << std::dec << "->" << kv.second << ", ";
std::cout << std::endl;
}
{
data_t data;
data.m_weights[ 3 ] = 33;
data.m_weights[ 2 ] = 22;
data.m_weights[ 1 ] = 11;
data.m_colors[ 0x00FF00 ] = 2;
data.m_colors[ 0xFF00FF ] = 3;
std::cout
<< "\nSerialized to JSON:\n"
<< json_dto::to_json( data ) << std::endl;
}
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << "." << std::endl;
return 1;
}
return 0;
}