-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathfrom_json.cpp
253 lines (220 loc) · 9.38 KB
/
from_json.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
/************************************************************************************
* *
* Copyright (c) 2014 - 2018 Axel Menzel <[email protected]> *
* *
* This file is part of RTTR (Run Time Type Reflection) *
* License: MIT License *
* *
* 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. *
* *
*************************************************************************************/
#include <cstdio>
#include <string>
#include <vector>
#include <array>
#include <iostream>
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/prettywriter.h> // for stringify JSON
#include <rapidjson/document.h> // rapidjson's DOM-style API
#include <rttr/type>
using namespace rapidjson;
using namespace rttr;
namespace
{
/////////////////////////////////////////////////////////////////////////////////////////
void fromjson_recursively(instance obj, Value& json_object);
/////////////////////////////////////////////////////////////////////////////////////////
variant extract_basic_types(Value& json_value)
{
switch(json_value.GetType())
{
case kStringType:
{
return std::string(json_value.GetString());
break;
}
case kNullType: break;
case kFalseType:
case kTrueType:
{
return json_value.GetBool();
break;
}
case kNumberType:
{
if (json_value.IsInt())
return json_value.GetInt();
else if (json_value.IsDouble())
return json_value.GetDouble();
else if (json_value.IsUint())
return json_value.GetUint();
else if (json_value.IsInt64())
return json_value.GetInt64();
else if (json_value.IsUint64())
return json_value.GetUint64();
break;
}
// we handle only the basic types here
case kObjectType:
case kArrayType: return variant();
}
return variant();
}
/////////////////////////////////////////////////////////////////////////////////////////
static void write_array_recursively(variant_sequential_view& view, Value& json_array_value)
{
view.set_size(json_array_value.Size());
const type array_value_type = view.get_rank_type(1);
for (SizeType i = 0; i < json_array_value.Size(); ++i)
{
auto& json_index_value = json_array_value[i];
if (json_index_value.IsArray())
{
auto sub_array_view = view.get_value(i).create_sequential_view();
write_array_recursively(sub_array_view, json_index_value);
}
else if (json_index_value.IsObject())
{
variant var_tmp = view.get_value(i);
variant wrapped_var = var_tmp.extract_wrapped_value();
fromjson_recursively(wrapped_var, json_index_value);
view.set_value(i, wrapped_var);
}
else
{
variant extracted_value = extract_basic_types(json_index_value);
if (extracted_value.convert(array_value_type))
view.set_value(i, extracted_value);
}
}
}
variant extract_value(Value::MemberIterator& itr, const type& t)
{
auto& json_value = itr->value;
variant extracted_value = extract_basic_types(json_value);
const bool could_convert = extracted_value.convert(t);
if (!could_convert)
{
if (json_value.IsObject())
{
constructor ctor = t.get_constructor();
for (auto& item : t.get_constructors())
{
if (item.get_instantiated_type() == t)
ctor = item;
}
extracted_value = ctor.invoke();
fromjson_recursively(extracted_value, json_value);
}
}
return extracted_value;
}
static void write_associative_view_recursively(variant_associative_view& view, Value& json_array_value)
{
for (SizeType i = 0; i < json_array_value.Size(); ++i)
{
auto& json_index_value = json_array_value[i];
if (json_index_value.IsObject()) // a key-value associative view
{
Value::MemberIterator key_itr = json_index_value.FindMember("key");
Value::MemberIterator value_itr = json_index_value.FindMember("value");
if (key_itr != json_index_value.MemberEnd() &&
value_itr != json_index_value.MemberEnd())
{
auto key_var = extract_value(key_itr, view.get_key_type());
auto value_var = extract_value(value_itr, view.get_value_type());
if (key_var && value_var)
{
view.insert(key_var, value_var);
}
}
}
else // a key-only associative view
{
variant extracted_value = extract_basic_types(json_index_value);
if (extracted_value && extracted_value.convert(view.get_key_type()))
view.insert(extracted_value);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void fromjson_recursively(instance obj2, Value& json_object)
{
instance obj = obj2.get_type().get_raw_type().is_wrapper() ? obj2.get_wrapped_instance() : obj2;
const auto prop_list = obj.get_derived_type().get_properties();
for (auto prop : prop_list)
{
Value::MemberIterator ret = json_object.FindMember(prop.get_name().data());
if (ret == json_object.MemberEnd())
continue;
const type value_t = prop.get_type();
auto& json_value = ret->value;
switch(json_value.GetType())
{
case kArrayType:
{
variant var;
if (value_t.is_sequential_container())
{
var = prop.get_value(obj);
auto view = var.create_sequential_view();
write_array_recursively(view, json_value);
}
else if (value_t.is_associative_container())
{
var = prop.get_value(obj);
auto associative_view = var.create_associative_view();
write_associative_view_recursively(associative_view, json_value);
}
prop.set_value(obj, var);
break;
}
case kObjectType:
{
variant var = prop.get_value(obj);
fromjson_recursively(var, json_value);
prop.set_value(obj, var);
break;
}
default:
{
variant extracted_value = extract_basic_types(json_value);
if (extracted_value.convert(value_t)) // REMARK: CONVERSION WORKS ONLY WITH "const type", check whether this is correct or not!
prop.set_value(obj, extracted_value);
}
}
}
}
} // end namespace anonymous
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
namespace io
{
bool from_json(const std::string& json, rttr::instance obj)
{
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
// "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().
if (document.Parse(json.c_str()).HasParseError())
return 1;
fromjson_recursively(obj, document);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
} // end namespace io