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

+2
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

+4
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

+9
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

+6
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

+4
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

+4
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

+4
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

+7
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

+6
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

+14-6
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 {

fable/include/fable/schema/number.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class Number : public Base<Number<T>> {
8686
void from_conf(const Conf& c) override;
8787
Json serialize(const Type& x) const;
8888
Type deserialize(const Conf& c) const;
89+
void serialize_into(Json& j, const Type& x) const;
90+
void deserialize_into(const Conf& c, Type& x) const;
8991
void reset_ptr() override;
9092

9193
private:

fable/include/fable/schema/number_impl.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ Json Number<T>::serialize(const T& x) const { return x; }
209209
template <typename T>
210210
T Number<T>::deserialize(const Conf& c) const { return c.get<T>(); }
211211

212+
template <typename T>
213+
void Number<T>::serialize_into(Json& j, const T& x) const { j = x; }
214+
215+
template <typename T>
216+
void Number<T>::deserialize_into(const Conf& c, T& x) const { x = c.get<T>(); }
217+
212218
template <typename T>
213219
void Number<T>::reset_ptr() { ptr_ = nullptr; }
214220

fable/include/fable/schema/optional.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ class Optional : public Base<Optional<T, P>> {
133133
return prototype_.deserialize(c);
134134
}
135135

136+
void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
137+
138+
void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
139+
136140
void reset_ptr() override { ptr_ = nullptr; }
137141

138142
private:

fable/include/fable/schema/passthru.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class Passthru : public Base<Passthru<P>> {
8282

8383
Type deserialize(const Conf& c) const { return c; }
8484

85+
void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
86+
87+
void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
88+
8589
void reset_ptr() override { ptr_ = nullptr; }
8690

8791
private:

fable/include/fable/schema/path.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ class Path : public Base<Path<T>> {
215215

216216
Type deserialize(const Conf& c) const;
217217

218+
void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
219+
220+
void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
221+
218222
void reset_ptr() override { ptr_ = nullptr; }
219223

220224
private:

fable/include/fable/schema/string.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ class String : public Base<String> {
249249
void from_conf(const Conf& c) override;
250250
Json serialize(const Type& x) const;
251251
Type deserialize(const Conf& c) const;
252+
void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
253+
void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
252254
void reset_ptr() override;
253255

254256
private:

fable/include/fable/schema/struct.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ class Struct : public Base<Struct> {
207207
void to_json(Json& j) const override;
208208
void from_conf(const Conf& c) override;
209209

210+
// TODO: Implement or explain why we don't need the following methods:
211+
// - serialize
212+
// - serialize_into
213+
// - deserialize
214+
// - deserialize_into
215+
210216
private:
211217
void set_properties(const std::map<std::string, Box>& props);
212218

fable/include/fable/schema/variant.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ class Variant : public Interface {
111111
schemas_[index].from_conf(c);
112112
}
113113

114+
// TODO: Implement or explain why we don't need the following methods:
115+
// - serialize
116+
// - serialize_into
117+
// - deserialize
118+
// - deserialize_into
119+
114120
void reset_ptr() override;
115121

116122
private:

fable/include/fable/schema/vector.hpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,21 @@ class Vector : public Base<Vector<T, P>> {
125125
using Interface::to_json;
126126
void to_json(Json& j) const override {
127127
assert(ptr_ != nullptr);
128-
j = serialize(*ptr_);
128+
if (j.type() != JsonType::array) {
129+
j = Json::array();
130+
}
131+
serialize_into(j, *ptr_);
129132
}
130133

131134
void from_conf(const Conf& c) override {
132135
assert(ptr_ != nullptr);
133136
assert(c->type() == this->type_);
134-
135-
if (!option_extend_) {
136-
ptr_->clear();
137-
}
138-
139-
ptr_->reserve(c->size() + ptr_->size());
140-
fill(*ptr_, c);
137+
deserialize_into(c, *ptr_);
141138
}
142139

143140
Json serialize(const Type& xs) const {
144141
Json j = Json::array();
145-
for (const auto& x : xs) {
146-
j.emplace_back(prototype_.serialize(x));
147-
}
142+
serialize_into(j, xs);
148143
return j;
149144
}
150145

@@ -155,6 +150,23 @@ class Vector : public Base<Vector<T, P>> {
155150
return vec;
156151
}
157152

153+
void serialize_into(Json& j, const Type& xs) const {
154+
for (const auto& x : xs) {
155+
j.emplace_back(prototype_.serialize(x));
156+
}
157+
}
158+
159+
void deserialize_into(const Conf& c, Type& x) const {
160+
auto size = c->size();
161+
if (option_extend_) {
162+
size += x.size();
163+
} else {
164+
x.clear();
165+
}
166+
x.reserve(size);
167+
fill(x, c);
168+
}
169+
158170
void reset_ptr() override { ptr_ = nullptr; }
159171

160172
private:

fable/src/fable/schema/boolean.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ void Boolean::from_conf(const Conf& c) {
5353

5454
Json Boolean::serialize(const Type& x) const { return x; }
5555

56+
void Boolean::serialize_into(Json& j, const Type& x) const { j = x; }
57+
5658
Boolean::Type Boolean::deserialize(const Conf& c) const { return c.get<Type>(); }
5759

60+
void Boolean::deserialize_into(const Conf& c, Type& x) const { x = c.get<Type>(); }
61+
5862
void Boolean::reset_ptr() { ptr_ = nullptr; }
5963

6064
} // namespace schema

0 commit comments

Comments
 (0)