Skip to content

Commit 4b9dab8

Browse files
committed
fable: Enable serializing and deserializing into refs
1 parent a923588 commit 4b9dab8

20 files changed

+121
-17
lines changed

fable/include/fable/schema/boolean.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Boolean : public Base<Boolean> {
4545
void from_conf(const Conf& c) override;
4646
Json serialize(const Type& x) const;
4747
Type deserialize(const Conf& c) const;
48+
void serialize_into(Json& j, const Type& x) const;
49+
void deserialize_into(const Conf& c, Type& x) const;
4850
void reset_ptr() override;
4951

5052
private:

fable/include/fable/schema/confable.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class FromConfable : public Base<FromConfable<T>> {
9393
return tmp;
9494
}
9595

96+
void serialize_into(Json& j, const Type& x) const { x.to_json(j); }
97+
98+
void deserialize_into(const Conf& c, Type& x) const { x.from_conf(c); }
99+
96100
void reset_ptr() override {
97101
ptr_ = nullptr;
98102
schema_.reset_ptr();

fable/include/fable/schema/const.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ class Const : public Base<Const<T, P>> {
8181
return constant_;
8282
}
8383

84+
void serialize_into(Json& j, const Type& x) const {
85+
prototype_.serialize_into(j, x);
86+
}
87+
88+
void deserialize_into(const Conf& c, Type& x) const {
89+
validate(c);
90+
x = constant_;
91+
}
92+
8493
void reset_ptr() override {}
8594

8695
private:

fable/include/fable/schema/custom.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class CustomDeserializer : public schema::Interface {
8484

8585
friend void to_json(Json& j, const CustomDeserializer& b) { b.impl_->to_json(j); }
8686

87+
// TODO: Implement or explain why we don't need the following methods:
88+
// - serialize
89+
// - serialize_into
90+
// - deserialize
91+
// - deserialize_into
92+
8793
private:
8894
std::shared_ptr<schema::Interface> impl_{nullptr};
8995
std::function<void(CustomDeserializer*, const Conf&)> from_conf_fn_{};

fable/include/fable/schema/duration.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class Duration : public Base<Duration<T, Period>> {
136136

137137
Type deserialize(const Conf& c) const { return Type(c.get<T>()); }
138138

139+
void serialize_into(Json& j, const Type& x) const { j = x.count(); }
140+
141+
void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
142+
139143
void reset_ptr() override { ptr_ = nullptr; }
140144

141145
private:

fable/include/fable/schema/enum.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class Enum : public Base<Enum<T>> {
9393
}
9494
}
9595

96+
void serialize_into(Json& j, const Type& x) const { j = mapping_to_.at(x); }
97+
98+
void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
99+
96100
void reset_ptr() override { ptr_ = nullptr; }
97101

98102
private:

fable/include/fable/schema/factory.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ class FactoryBase : public Base<CRTP> {
292292

293293
Json serialize(const Type& x) const { return x; }
294294

295+
void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
296+
297+
void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
298+
295299
void from_conf(const Conf& c) override {
296300
throw std::logic_error("FactoryBase::from_conf() should not be used");
297301
}

fable/include/fable/schema/ignore.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ namespace schema {
4343
*/
4444
class Ignore : public Base<Ignore> {
4545
public: // Constructors
46+
using Type = struct {};
47+
4648
Ignore() : Base(JsonType::object, "ignored") {}
4749
explicit Ignore(std::string desc, JsonType t = JsonType::object) : Base(t, std::move(desc)) {}
4850

@@ -58,6 +60,11 @@ class Ignore : public Base<Ignore> {
5860
void to_json(Json& j) const override { j = nullptr; }
5961
void from_conf(const Conf&) override {}
6062
void reset_ptr() override {}
63+
64+
Json serialize(const Type&) const { return nullptr; }
65+
Type deserialize(const Conf&) const { return {}; }
66+
void serialize_into(Json&, const Type&) const {}
67+
void deserialize_into(const Conf&, Type&) const {}
6168
};
6269

6370
} // namespace schema

fable/include/fable/schema/json.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class FromJson : public Base<FromJson<T>> {
7575

7676
void reset_ptr() override { ptr_ = nullptr; }
7777

78+
// TODO: Implement or explain why we don't need the following methods:
79+
// - serialize
80+
// - serialize_into
81+
// - deserialize
82+
// - deserialize_into
83+
7884
private:
7985
Type* ptr_{nullptr};
8086
};

fable/include/fable/schema/map.hpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,29 @@ class Map : public Base<Map<T, P>> {
155155
}
156156
}
157157

158-
Json serialize(const Type& xm) const {
158+
Json serialize(const Type& x) const {
159159
Json j;
160-
for (const auto& kv : xm) {
161-
j[kv.first] = prototype_.serialize(kv.second);
162-
}
160+
serialize_into(j, x);
163161
return j;
164162
}
165163

166164
Type deserialize(const Conf& c) const {
167165
Type tmp;
166+
deserialize_into(c, tmp);
167+
return tmp;
168+
}
169+
170+
void serialize_into(Json& j, const Type& x) const {
171+
for (const auto& kv : x) {
172+
j[kv.first] = prototype_.serialize(kv.second);
173+
}
174+
}
175+
176+
void deserialize_into(const Conf& c, Type& x) const {
168177
for (auto& i : c->items()) {
169178
const auto key = i.key();
170-
tmp.insert(std::make_pair(key, deserialize_item(c, key)));
179+
x.insert(std::make_pair(key, deserialize_item(c, key)));
171180
}
172-
return tmp;
173181
}
174182

175183
T deserialize_item(const Conf& c, const std::string& key) const {

0 commit comments

Comments
 (0)