Skip to content

Commit eea1701

Browse files
committed
Avoid calling from_json directly
This lets us drop some of our own custom deserialization logic which existed because we clearly weren't using this library correctly.
1 parent 03241d6 commit eea1701

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

src/aur/json_internal.hh

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,15 @@
99
namespace aur {
1010

1111
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-
}
3112

3213
template <typename OutputType>
3314
using ValueCallback = std::function<void(const nlohmann::json&, OutputType&)>;
3415

3516
template <typename OutputType, typename FieldType>
3617
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+
};
3921
}
4022

4123
template <typename OutputType>
@@ -47,8 +29,12 @@ void DeserializeJsonObject(const nlohmann::json& j,
4729
const CallbackMap<OutputType>& callbacks,
4830
OutputType& o) {
4931
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()) {
5238
iter->second(value, o);
5339
}
5440
}

src/aur/package.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
#include "absl/base/no_destructor.h"
55
#include "aur/json_internal.hh"
66

7-
namespace aur {
8-
9-
void from_json(const nlohmann::json& j, absl::Time& t) {
10-
if (j.is_null()) {
11-
return;
12-
}
7+
namespace absl {
138

9+
void from_json(const nlohmann::json& j, Time& t) {
1410
t = absl::FromUnixSeconds(j);
1511
}
1612

13+
} // namespace absl
14+
15+
namespace aur {
16+
1717
void from_json(const nlohmann::json& j, Package& p) {
1818
// clang-format off
1919
static const absl::NoDestructor<CallbackMap<Package>> kCallbacks({

0 commit comments

Comments
 (0)