-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
87 lines (71 loc) · 1.64 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
/*
A sample using map<int, int> with overloads for read/write_json_value.
*/
#include <cstdio>
#include <iostream>
#include <map>
#include <json_dto/pub.hpp>
// New overloads of read/write_json_value for integer keys.
namespace json_dto
{
inline void read_json_value(
mutable_map_key_t<int> key, const rapidjson::Value & from )
{
if( !from.IsString() )
throw std::runtime_error( "string value expected" );
if( 1 != std::sscanf( from.GetString(), "%d", &key.v ) )
throw std::runtime_error( "unable to parse key value" );
}
inline void write_json_value(
const_map_key_t<int> key,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator )
{
char buf[32];
std::sprintf( buf, "%d", key.v );
to.SetString( buf, allocator );
}
} /* namespace json_dto */
// Data type to be (de)serialized.
struct data_t
{
std::map< int, int > m_values;
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::mandatory( "values", m_values );
}
};
const std::string json_data{
R"JSON({
"values" : {"1": 1, "2": 2, "3": 3}
})JSON" };
int
main( int , char *[] )
{
try
{
{
auto data = json_dto::from_json< data_t >( json_data );
std::cout << "Deserialized from JSON:\n";
for( const auto & kv : data.m_values )
std::cout << kv.first << "->" << kv.second << ", ";
std::cout << std::endl;
}
{
data_t data;
data.m_values[ 3 ] = 33;
data.m_values[ 2 ] = 22;
data.m_values[ 1 ] = 11;
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;
}