forked from d-led/picojson_serializer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicojson_serializer.h
240 lines (198 loc) · 7.4 KB
/
picojson_serializer.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* picojson_serializer - a simple serialization solution based on picojson
* --------------------------------------------------------
* Copyright (C) 2013, Dmitry Ledentsov ([email protected])
*
* This software is distributed under the MIT License. See notice at the end
* of this file.
*
* This work requires picojson: https://github.com/kazuho/picojson
*/
#pragma once
#include "picojson.h"
#include <string>
namespace picojson {
namespace convert {
/// http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector
template<typename T>
class has_json_member
{
struct Fallback { int json; };
struct Derived : T, Fallback { };
template<typename U, U> struct Check;
typedef char ArrayOfOne[1]; // typedef for an array of size one.
typedef char ArrayOfTwo[2]; // typedef for an array of size two.
template<typename U>
static ArrayOfOne & func(Check<int Fallback::*, &U::json> *);
template<typename U>
static ArrayOfTwo & func(...);
public:
typedef has_json_member type;
enum { value = sizeof(func<Derived>(0)) == 2 };
};
// pre-c++11 enable_if (http://en.cppreference.com/w/cpp/types/enable_if)
template <bool B, class T = void> struct enable_if {};
template <class T> struct enable_if<true, T> { typedef T type; };
template<typename T>
struct key_value {
std::string key;
T& value;
};
template <typename T>
key_value<T> member(std::string const& k, T& v) {
key_value<T> res = { k, v };
return res;
}
class access {
object o;
public:
template<typename T>
void operator & (key_value<T> kv);
inline std::string serialize() const;
inline value get_value() const;
};
template <typename T>
class write_access {
T& t;
value const& v;
public:
write_access(value const& v_,T& t_);
template<typename TT>
void operator & (key_value<TT> kv);
};
template<typename T,class Enable = void> struct value_converter {
static value to_value(T& v) {
access a;
json(a,v);
return a.get_value();
}
static void from_value(value const& ov, T& v) {
write_access<T> a(ov,v);
json(a,v);
}
};
/// standard types
template<> struct value_converter<double> {
static value to_value(double v) { return value(v); }
static void from_value(value const& ov, double& v) { if ( ov.is<double>() ) v = ov.get<double>(); }
};
template<> struct value_converter< std::string > {
static value to_value(std::string const& v) { return value(v); }
static void from_value(value const& ov, std::string& v) { if ( ov.is<std::string>() ) v = ov.get<std::string>(); }
};
template<> struct value_converter< int > {
static value to_value(int v) { return value(static_cast<double>(v)); }
static void from_value(value const& ov, int& v) { if ( ov.is<double>() ) v = static_cast<int>(ov.get<double>()); }
};
template<> struct value_converter< bool > {
static value to_value(bool v) { return value(v); }
static void from_value(value const& ov, bool& v) { if ( ov.is<bool>() ) v = ov.get<bool>(); }
};
template <typename T>
value to_value(T& t) {
return value_converter<T>::to_value(t);
};
template <typename T>
void from_value(value const& v, T& t) {
value_converter<T>::from_value(v, t);
};
template<typename T>
void access::operator & (key_value<T> kv) {
o[kv.key] = to_value(kv.value);
}
inline std::string access::serialize() const {
return get_value().serialize();
}
inline value access::get_value() const {
return value(o);
}
template <typename T>
write_access<T>::write_access(value const& v_,T& t_) :t(t_),v(v_){}
template <typename T>
template<typename TT>
void write_access<T>::operator & (key_value<TT> kv) {
if ( !v.is<picojson::object>() )
return;
picojson::object const& o = v.get<picojson::object>();
picojson::object::const_iterator found =
o.find(kv.key);
if ( found == o.end() )
return;
if ( found->second.is<picojson::object>() ) {
from_value(found->second, kv.value);
return;
}
if ( found->second.is<picojson::array>() ) {
from_value(found->second,kv.value);
return;
}
value_converter<TT>::from_value(found->second, kv.value);
}
template <typename T>
struct value_converter<T, typename enable_if< has_json_member<T>::value >::type> {
static value to_value(T& v) {
access a;
v.json(a);
return a.get_value();
}
static void from_value(value const& ov, T& v) {
write_access<T> a(ov,v);
v.json(a);
}
};
template < typename T>
std::string to_string(T& t) {
return to_value(t).serialize();
}
template < typename T>
void from_string(std::string const& json,T& t) {
picojson::value v;
std::string err;
picojson::parse(v, json.begin(), json.end(),&err);
from_value(v, t);
}
namespace operators {
template<typename T>
T from_value(picojson::value const& v) {
T t{}; //Fixes unititialized data warnings from GCC
value_converter<T>::from_value(v, t);
return t;
}
template <typename KeyType,typename ValueType>
value to_value(std::pair< KeyType, ValueType >const& p) {
picojson::object o;
o["Key"]=value_converter<KeyType>::to_value(const_cast<KeyType&>(p.first));
o["Value"]=value_converter<ValueType>::to_value(p.second);
return value(o);
}
template <typename KeyType,typename ValueType>
std::pair<KeyType,ValueType> from_value(picojson::value const& v) {
return std::pair<KeyType,ValueType>();//todo
}
}
}
}
/**
* Copyright (c) 2013 Dmitry Ledentsov
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/