-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
219 lines (188 loc) · 3.76 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
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
#include "jstream.h"
#include "test.h"
#include <stdio.h>
#include <string_view>
#include <map>
#include <iostream>
struct GoogleAccessToken {
std::string access_token;
std::string token_type;
std::string expires_in;
std::string scope;
std::string refresh_token;
std::string id_token;
std::string stringify() const
{
std::string ret;
jstream::Writer w([&](char const *p, int n){
ret += {p, n};
});
w.object({}, [&](){
w.string(access_token, "access_token");
w.string(expires_in, "expires_in");
w.string(scope, "scope");
w.string(token_type, "token_type");
w.string(id_token, "id_token");
});
return ret;
}
};
std::ostream &operator << (std::ostream &out, GoogleAccessToken const &token)
{
out << token.stringify();
return out;
}
GoogleAccessToken parse_google_access_token()
{
std::string input = R"---(
{
"access_token": "qwerty123",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/userinfo.profile",
"token_type": "Bearer",
"id_token": "abcdefg"
}
)---";
GoogleAccessToken out;
jstream::Reader json(input.c_str(), input.size());
while (json.next()) {
if (json.match("{access_token")) {
out.access_token = json.string();
} else if (json.match("{expires_in")) {
out.expires_in = json.string();
} else if (json.match("{scope")) {
out.scope = json.string();
} else if (json.match("{token_type")) {
out.token_type = json.string();
} else if (json.match("{id_token")) {
out.id_token = json.string();
}
}
return out;
}
GoogleAccessToken parse_google_access_token2()
{
std::string input = R"---(
{
"access_token": "qwerty123",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/userinfo.profile",
"token_type": "Bearer",
"id_token": "abcdefg"
}
)---";
GoogleAccessToken out;
jstream::Reader::rule_for_string_t rule;
jstream::Reader json(input.c_str(), input.size());
rule["{access_token"] = &out.access_token;
rule["{expires_in"] = &out.expires_in;
rule["{scope"] = &out.scope;
rule["{token_type"] = &out.token_type;
rule["{id_token"] = &out.id_token;
json.parse(rule);
return out;
}
void main2()
{
std::string input = R"---(
{
"items": {
"hoge": {
"name": [
"aaa",
"bbb",
"ccc"
]
},
"fuga": {
"name": "bar"
}
}
}
)---";
std::vector<std::string> hogenames;
std::vector<std::string> fuganames;
jstream::Reader::rule_for_strings_t rule;
rule["{items{hoge{name["] = &hogenames;
rule["{items{fuga{name"] = &fuganames;
jstream::Reader json(input.c_str(), input.size());
json.parse(rule);
for (auto const &s : hogenames) {
puts(s.c_str());
}
for (auto const &s : fuganames) {
puts(s.c_str());
}
}
void main3()
{
std::string input = R"---(
{
"items": {
"hoge": {
"name": [
"aaa",
"bbb",
"ccc"
]
},
"fuga": {
"name": "bar"
}
}
}
)---";
std::vector<std::string> hogenames;
std::vector<std::string> fuganames;
jstream::Reader::rule_for_strings_t rule;
rule["{items{*{name["] = &hogenames;
rule["{items{*{name"] = &fuganames;
jstream::Reader json(input.c_str(), input.size());
json.parse(rule);
for (auto const &s : hogenames) {
puts(s.c_str());
}
for (auto const &s : fuganames) {
puts(s.c_str());
}
}
void main4()
{
std::string input = R"---(
{
"items": {
"hoge": {
"name": [
"aaa",
"bbb",
"ccc"
]
},
"fuga": {
"name": "bar"
}
}
}
)---";
std::string hogename;
std::string fuganame;
jstream::Reader::rule_for_string_t rule;
rule["{items{hoge{name["] = &hogename;
rule["{items{fuga{name"] = &fuganame;
jstream::Reader json(input.c_str(), input.size());
json.parse(rule);
puts(hogename.c_str());
puts(fuganame.c_str());
}
int main()
{
#if 0
main4();
#elif 0
test_all();
#else
auto t = parse_google_access_token();
std::cout << t;
#endif
return 0;
}