-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonparser.hpp
173 lines (156 loc) · 11 KB
/
jsonparser.hpp
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
/*
int <--> json::int32
unsigned int <--> json::uint32
double <--> json::double
bool <--> json::bool
std::string <--> json::string
std::vector <--> json::array
don't allow null object
allow null array
*/
/*
use tutorials:
struct test
{
type1 var1;
...
typeN varN;
}
REFLECTION(test, var1, ... , varN)
int main(void)
{
test t1;
std::string str1=...;
if(!json_from_str(t1, str1))
{
...;
}
std::string str2 = json_to_str(t1);
return 0;
}
*/
#pragma once
#include <type_traits>
#include "basemac.h"
#include "baseparser.hpp"
// json from mac
#define MAC_JSON_FROM(parm) do{if (!root1.isMember(#parm) || !json_from_str(obj.parm, root1[#parm])) \
return false;} while(false)
// mac overload method 1
#define MAC_OVERLOAD_METHOD_ONE1(arg, ...) MAC_JSON_FROM(arg);
#define MAC_OVERLOAD_METHOD_ONE2(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE1(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE3(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE2(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE4(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE3(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE5(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE4(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE6(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE5(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE7(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE6(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE8(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE7(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE9(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE8(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE10(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE9(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE11(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE10(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE12(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE11(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE13(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE12(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE14(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE13(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE15(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE14(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE16(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE15(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE17(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE16(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE18(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE17(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE19(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE18(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE20(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE19(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE21(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE20(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE22(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE21(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE23(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE22(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE24(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE23(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE25(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE24(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE26(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE25(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE27(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE26(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE28(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE27(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE29(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE28(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE30(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE29(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE31(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE30(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE32(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE31(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE33(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE32(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE34(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE33(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE35(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE34(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE36(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE35(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE37(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE36(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE38(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE37(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE39(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE38(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_ONE40(arg, ...) MAC_JSON_FROM(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_ONE39(__VA_ARGS__))
// json to mac
#define MAC_JSON_TO(parm) do{root1[#parm] = json_to_str_imp(obj.parm);} while(false)
// mac overload method 2
#define MAC_OVERLOAD_METHOD_TWO1(arg, ...) MAC_JSON_TO(arg);
#define MAC_OVERLOAD_METHOD_TWO2(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO1(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO3(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO2(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO4(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO3(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO5(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO4(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO6(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO5(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO7(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO6(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO8(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO7(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO9(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO8(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO10(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO9(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO11(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO10(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO12(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO11(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO13(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO12(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO14(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO13(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO15(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO14(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO16(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO15(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO17(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO16(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO18(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO17(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO19(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO18(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO20(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO19(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO21(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO20(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO22(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO21(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO23(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO22(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO24(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO23(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO25(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO24(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO26(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO25(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO27(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO26(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO28(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO27(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO29(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO28(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO30(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO29(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO31(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO30(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO32(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO31(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO33(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO32(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO34(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO33(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO35(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO34(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO36(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO35(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO37(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO36(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO38(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO37(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO39(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO38(__VA_ARGS__))
#define MAC_OVERLOAD_METHOD_TWO40(arg, ...) MAC_JSON_TO(arg); MARCO_EXPAND(MAC_OVERLOAD_METHOD_TWO39(__VA_ARGS__))
#define REFLECTION(objtype, ...) \
inline bool json_from_str(objtype &obj, const Json::Value &root1) \
{ \
if (root1.isNull() || !root1.isObject() || root1.isArray()) \
return false; \
MAC_VARGS_INVOKE(MAC_OVERLOAD_METHOD_ONE, ##__VA_ARGS__) \
return true; \
} \
template<typename T, \
typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> \
|| std::is_same_v<std::decay_t<T>, std::string_view>>> \
inline bool json_from_str(objtype &obj, T &&jsonstr) \
{ \
using objtype##type = typename std::conditional<std::is_same_v<std::decay_t<T>, std::string_view>, const std::string, const std::string&>::type; \
objtype##type jsonstr2(jsonstr); \
if(jsonstr2.empty()) \
return false; \
Json::Reader reader1; \
Json::Value root1; \
reader1.parse(jsonstr2, root1); \
return json_from_str(obj, root1); \
} \
inline Json::Value json_to_str_imp(const objtype& obj) \
{ \
Json::Value root1; \
MAC_VARGS_INVOKE(MAC_OVERLOAD_METHOD_TWO, ##__VA_ARGS__) \
return std::move(root1); \
} \
inline std::string json_to_str(const objtype& obj) \
{ \
Json::FastWriter writer1; \
Json::Value root1 = json_to_str_imp(obj); \
return writer1.write(root1); \
}