9
9
namespace aur {
10
10
11
11
void from_json (const nlohmann::json& j, Package& p);
12
- void from_json (const nlohmann::json& j, absl::Time& t);
13
-
14
- template <typename T>
15
- void from_json (const nlohmann::json& j, T& v) {
16
- if (j.is_null ()) {
17
- return ;
18
- }
19
-
20
- v = T (j);
21
- }
22
-
23
- template <typename T>
24
- void from_json (const nlohmann::json& j, std::vector<T>& v) {
25
- if (j.is_null ()) {
26
- return ;
27
- }
28
-
29
- v = std::vector<T>(j.cbegin (), j.cend ());
30
- }
31
12
32
13
template <typename OutputType>
33
14
using ValueCallback = std::function<void (const nlohmann::json&, OutputType&)>;
34
15
35
16
template <typename OutputType, typename FieldType>
36
17
ValueCallback<OutputType> MakeValueCallback (FieldType OutputType::*field) {
37
- return
38
- [=](const nlohmann::json& j, OutputType& o) { from_json (j, o.*field); };
18
+ return [=](const nlohmann::json& j, OutputType& o) {
19
+ j.get_to <FieldType>(o.*field);
20
+ };
39
21
}
40
22
41
23
template <typename OutputType>
@@ -47,8 +29,12 @@ void DeserializeJsonObject(const nlohmann::json& j,
47
29
const CallbackMap<OutputType>& callbacks,
48
30
OutputType& o) {
49
31
for (const auto & [key, value] : j.items ()) {
50
- const auto iter = callbacks.find (key);
51
- if (iter != callbacks.end ()) {
32
+ // Skip all null-valued entries.
33
+ if (value.is_null ()) {
34
+ continue ;
35
+ }
36
+
37
+ if (const auto iter = callbacks.find (key); iter != callbacks.end ()) {
52
38
iter->second (value, o);
53
39
}
54
40
}
0 commit comments