-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlibrary_overview.cpp
More file actions
347 lines (240 loc) · 6.05 KB
/
library_overview.cpp
File metadata and controls
347 lines (240 loc) · 6.05 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <iostream>
#include <sstream>
#include <vector>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using JsonFunction = std::function<json(std::vector<json>)>;
using function_pointer = json(*)(std::vector<json>);
// g++ library_overview.cpp --std=c++11 -o out.out -I ~/Project/json/include && ./out.out
struct address
{
std::string m_val;
address(std::string rhs = "") : m_val(std::move(rhs)) {}
};
struct function_wrapper
{
JsonFunction function;
function_wrapper(JsonFunction function) {
this->function = function;
}
std::string to_string() const {
std::stringstream ss;
// std::cout << (void*)(this->function).target() << std::endl;
// ss << typeid(function).name();
ss << &function;
return ss.str();
}
};
static void to_json(nlohmann::json& j, const address& a)
{
j = a.m_val;
}
static void to_json(nlohmann::json& j, const function_wrapper& function)
{
// j = std::stoi(function.to_string());
j = reinterpret_cast<uintptr_t>(&function.function);
// std::cout << "COUT: " << &(function.function) << std::endl;
}
/*
template <typename BasicJsonType>
static void from_json(BasicJsonType& j, const JsonFunction& function)
{
}
*/
json d(std::vector<json> args) {
return 10101;
}
int main() {
{
// a JSON value
json j_document = R"({
"a": "b",
"b": 1
})"_json;
// a patch
json j_patch = R"({
"b": 2,
"c": 3
})"_json;
// apply the patch
j_document.merge_patch(j_patch);
std::cout << j_document << std::endl;
}
{
JsonFunction dd = d;
function_wrapper f = dd;
json obj = json(f);
std::cout << obj << std::endl;
std::cout << obj.get<uintptr_t>() << std::endl;
JsonFunction* ptr_function = reinterpret_cast<JsonFunction*>(obj.get<uintptr_t>());
// NOTE: VERY UNSAFE BUT SHOULD BE OK FOR FUNCTIONS DECLARED AT C LEVEL
std::cout << (*ptr_function)({}) << std::endl;
}
{
json d = "AAA";
json a = nullptr;
json b = "";
std::cout << (a == nullptr) << std::endl;
std::cout << (b == "") << std::endl;
if(d.is_string()) {
std::string conv = d.get<std::string>();
std::cout << conv << std::endl;
}
}
// vector conversion
{
json obj = json::parse("[1, 2, 3, \"A\"]");
// This conversion takes the same amount of auxiliary space both for std::move and copy operations
std::vector<json> obj1 = obj;
std::cout << obj << std::endl;
// std::cout << obj1 << std::endl;
}
{
json obj = json::parse("{}");
// non-existent key for this lookup creates an entry in the memory so contains or better, "find" is recommended.
std::cout << obj["a"] << std::endl;
std::cout << obj.contains("a") << std::endl; // True :)
}
{
// Non-string key lookup test
json a = json::parse("{\"1\": 2}");
json key = "1"; // fails
key = 1;
// a[1] fails
std::cout << a["1"] << std::endl;
std::cout << a[key.dump()] << std::endl;
json arr = json::parse("[ \"a\" ]");
// TODO: Add a testcase for this
key = "0a1"; // THIS WILL CAUSE PROBLEMS
std::cout << arr[std::stoi(key.get<std::string>())] << std::endl;
std::cout << json::parse(R"({"b": 1, "a": 2})") << std::endl;
std::cout << json::parse(R"(["b", "a"])") << std::endl;
/*
key = "0";
key.get<int>();
*/
}
{
assert(json::parse(R"({"a": 1})") == json::parse(R"({"a": 1.0})"));
assert(json::parse(R"({"a": 1})") != json::parse(R"({"a": 1.1})"));
assert(json::parse(R"({"a": 1})") == json::parse(R"({"a": 1})"));
}
{
json value = 1;
value = value.dump();
std::string c = value.get<std::string>();
assert(c == "1");
}
/*
{
// Provider check
Provider provider;
hash_table<std::string, Utility> utility = provider.utility();
Utility _struct = utility["struct"];
std::cout << "_struct key: " << _struct["isnode"]({ json::array() }) << std::endl;
}
*/
/*
// Equality Checks
{
// Shallow Equal
json obj1 = json::parse(R"({"a": 1, "b": 2})");
json obj2 = json::parse(R"({"a": 1, "b": 2})");
// Deep Equal
json obj3 = json::parse(R"({"a": {"b": 1}})");
json obj4 = json::parse(R"({"a": {"b": 1}})");
assert(obj1 == obj2);
assert(obj1 != obj3);
assert(obj2 != obj3);
assert(obj3 == obj4);
// List Equal
json list1 = json::parse(R"([ 1, 2, []])");
json list2 = json::parse(R"([ 1, 2])");
json list3 = json::parse(R"([ 1, 2, []])");
json list4 = json::parse(R"([ 1, 2, {}])");
assert(list1 != list2);
assert(list1 == list3);
assert(list3 != list4);
}
*/
/*
{
json null = nullptr;
std::cout << "null == nullptr: " << (null == nullptr) << std::endl;
}
*/
/*
{
json ex1 = json::parse(R"(
{
"happy": true,
"pi": 2
}
)");
std::cout << isNode({ ex1 }) << std::endl;
}
{
std::ifstream f("../build/test/test.json");
json alltests = json::parse(f);
std::cout << "spec: " << alltests["minor"]["isnode"] << std::endl;
}
*/
/*
{
Struct _struct {new isList()};
std::cout << _struct.islist->apply({ 1 }) << std::endl;
std::cout << _struct.islist->apply({ json::array() }) << std::endl;
std::cout << _struct.islist->apply({ json::object() }) << std::endl;
}
*/
/*
json ex1 = json::parse(R"(
{
"happy": true,
"pi": 2
}
)");
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
// Using initializer lists
json ex3 = {
{"happy", true},
{"pi", 3.141},
};
json happy = ex1.at("happy");
json list1 = json::parse("[ 1, \"a\"]");
const json& list2 = list1;
std::vector<json> vec1;
if(list1.is_array()) {
for(json::iterator it = list1.begin(); it != list1.end(); ++it) {
vec1.push_back(it.value());
}
}
std::cout << ex1.dump(2) << std::endl;
std::cout << happy << std::endl;
std::cout << ex1.is_object() << std::endl;
for(size_t i = 0; i < vec1.size(); i++) {
std::cout << "vec[i]: " << vec1[i] << std::endl;
}
// Deep Copy
{
json obj1 = json::parse("{\"a\": {\"1\": \"2\" }}");
json obj2 = obj1;
obj1["a"]["1"] = 3;
std::cout << obj1.dump(2) << std::endl;
std::cout << obj2.dump(2) << std::endl;
}
*/
}