-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathJsonUtil.cpp
More file actions
288 lines (229 loc) · 7.72 KB
/
JsonUtil.cpp
File metadata and controls
288 lines (229 loc) · 7.72 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
// Copyright(c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "winget/JsonUtil.h"
#include "AppInstallerStrings.h"
namespace AppInstaller::JSON
{
template<>
std::optional<std::string> GetValue(const Json::Value& node)
{
std::optional<std::string> value;
if (node.isString())
{
value = node.asString();
}
return value;
}
template<>
std::optional<uint32_t> GetValue(const Json::Value& node)
{
std::optional<uint32_t> value;
if (node.isUInt())
{
value = node.asUInt();
}
return value;
}
template<>
std::optional<bool> GetValue(const Json::Value& node)
{
std::optional<bool> value;
if (node.isBool())
{
value = node.asBool();
}
return value;
}
template<>
std::optional<std::vector<std::string>> GetValue(const Json::Value& node)
{
std::vector<std::string> result;
if (node.isArray())
{
for (const Json::Value& entry : node)
{
if (!entry.isString())
{
return std::nullopt;
}
result.emplace_back(entry.asString());
}
return result;
}
return std::nullopt;
}
template<>
std::optional<std::map<std::string, bool>> GetValue(const Json::Value& node)
{
std::map<std::string, bool> result;
if (node.isObject())
{
for (const auto& entry : node.getMemberNames())
{
auto& value = node[entry];
if (value.isBool())
{
result[entry] = value.asBool();
}
}
return result;
}
return std::nullopt;
}
#ifndef WINGET_DISABLE_FOR_FUZZING
utility::string_t GetUtilityString(std::string_view nodeName)
{
return utility::conversions::to_string_t(nodeName.data());
}
web::json::value GetStringValue(std::string_view value)
{
return web::json::value::string(Utility::ConvertToUTF16(value));
}
std::optional<std::reference_wrapper<const web::json::value>> GetJsonValueFromNode(const web::json::value& node, const utility::string_t& keyName)
{
if (node.is_null() || !node.has_field(keyName))
{
return {};
}
return node.at(keyName);
}
std::optional<std::string> GetRawStringValueFromJsonValue(const web::json::value& value)
{
if (value.is_null() || !value.is_string())
{
return {};
}
return utility::conversions::to_utf8string(value.as_string());
}
std::optional<std::string> GetRawStringValueFromJsonNode(const web::json::value& node, const utility::string_t& keyName)
{
std::optional<std::reference_wrapper<const web::json::value>> jsonValue = GetJsonValueFromNode(node, keyName);
if (jsonValue)
{
return GetRawStringValueFromJsonValue(jsonValue.value().get());
}
return {};
}
std::optional<int> GetRawIntValueFromJsonValue(const web::json::value& value)
{
if (value.is_null() || !value.is_integer())
{
return {};
}
return value.as_integer();
}
std::optional<int> GetRawIntValueFromJsonNode(const web::json::value& node, const utility::string_t& keyName)
{
std::optional<std::reference_wrapper<const web::json::value>> jsonValue = GetJsonValueFromNode(node, keyName);
if (jsonValue)
{
return GetRawIntValueFromJsonValue(jsonValue.value().get());
}
return {};
}
std::optional<bool> GetRawBoolValueFromJsonValue(const web::json::value& value)
{
if (value.is_null() || !value.is_boolean())
{
return {};
}
return value.as_bool();
}
std::optional<bool> GetRawBoolValueFromJsonNode(const web::json::value& node, const utility::string_t& keyName)
{
std::optional<std::reference_wrapper<const web::json::value>> jsonValue = GetJsonValueFromNode(node, keyName);
if (jsonValue)
{
return GetRawBoolValueFromJsonValue(jsonValue.value().get());
}
return {};
}
std::optional<std::reference_wrapper<const web::json::array>> GetRawJsonArrayFromJsonNode(const web::json::value& node, const utility::string_t& keyName)
{
std::optional<std::reference_wrapper<const web::json::value>> jsonValue = GetJsonValueFromNode(node, keyName);
if (!jsonValue || !jsonValue.value().get().is_array())
{
return {};
}
return jsonValue.value().get().as_array();
}
std::vector<std::string> GetRawStringArrayFromJsonNode(
const web::json::value& node, const utility::string_t& keyName)
{
std::optional<std::reference_wrapper<const web::json::array>> arrayValue = GetRawJsonArrayFromJsonNode(node, keyName);
std::vector<std::string> result;
if (!arrayValue)
{
return result;
}
for (auto& value : arrayValue.value().get())
{
std::optional<std::string> item = GetRawStringValueFromJsonValue(value);
if (item)
{
result.emplace_back(std::move(item.value()));
}
}
return result;
}
std::set<std::string> GetRawStringSetFromJsonNode(
const web::json::value& node, const utility::string_t& keyName)
{
std::optional<std::reference_wrapper<const web::json::array>> arrayValue = GetRawJsonArrayFromJsonNode(node, keyName);
std::set<std::string> result;
if (!arrayValue)
{
return result;
}
for (auto& value : arrayValue.value().get())
{
std::optional<std::string> item = GetRawStringValueFromJsonValue(value);
if (item)
{
result.emplace(std::move(item.value()));
}
}
return result;
}
std::string Base64Encode(const std::vector<BYTE>& input)
{
if (input.size() == 0)
{
return {};
}
std::wstring result;
DWORD resultSize = 0;
CryptBinaryToStringW(input.data(), static_cast<DWORD>(input.size()), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, nullptr, &resultSize);
THROW_LAST_ERROR_IF(resultSize == 0);
result.resize(resultSize);
THROW_LAST_ERROR_IF(!CryptBinaryToStringW(input.data(), static_cast<DWORD>(input.size()), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, result.data(), &resultSize));
// Resize to remove trailing null terminator
result.resize(resultSize);
return Utility::ConvertToUTF8(result);
}
std::vector<BYTE> Base64Decode(const std::string& input)
{
if (input.empty())
{
return {};
}
auto inputWide = Utility::ConvertToUTF16(input);
std::vector<BYTE> result;
DWORD resultSize = 0;
CryptStringToBinaryW(inputWide.data(), static_cast<DWORD>(inputWide.size()), CRYPT_STRING_BASE64, nullptr, &resultSize, nullptr, nullptr);
THROW_LAST_ERROR_IF(resultSize == 0);
result.resize(resultSize);
THROW_LAST_ERROR_IF(!CryptStringToBinaryW(inputWide.data(), static_cast<DWORD>(inputWide.size()), CRYPT_STRING_BASE64, result.data(), &resultSize, nullptr, nullptr));
return result;
}
#endif
bool IsValidNonEmptyStringValue(std::optional<std::string>& value)
{
if (Utility::IsEmptyOrWhitespace(value.value_or("")))
{
return false;
}
return true;
}
}