Skip to content

Commit 8559fc1

Browse files
committed
fable: Complete (de)?serialize(_into)? method implementations
There are four functions used in templates for types: serialize serialize_into deserialize deserialize_into But they can't be implemented on all types. This commit either implements them or explains why they can't be implemented.
1 parent dce2b8c commit 8559fc1

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

fable/include/fable/schema/custom.hpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,16 @@ class CustomDeserializer : public schema::Interface {
8787

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

90-
// TODO: Implement or explain why we don't need the following methods:
91-
// - serialize
92-
// - serialize_into
93-
// - deserialize
94-
// - deserialize_into
90+
// NOTE: The following methods cannot be implemented because
91+
// CustomDeserializer does not have an associated type:
92+
//
93+
// Json serialize(const Type&) const;
94+
// void serialize_into(Json&, const Type&) const;
95+
// Type deserialize(const Conf&) const;
96+
// void deserialize_into(const Conf&, Type&) const;
97+
//
98+
// This means that `CustomDeserializer` cannot be used as a prototype schema
99+
// directly, for example with `optional`. Use `Confable` instead.
95100

96101
private:
97102
std::shared_ptr<schema::Interface> impl_{nullptr};

fable/include/fable/schema/json.hpp

+25-8
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,43 @@ class FromJson : public Base<FromJson<T>> {
5959
return j;
6060
}
6161

62-
bool validate(const Conf& c, std::optional<SchemaError>& err) const override { return this->validate_type(c, err); }
62+
bool validate(const Conf& c, std::optional<SchemaError>& err) const override {
63+
return this->validate_type(c, err);
64+
}
6365

6466
using Interface::to_json;
6567
void to_json(Json& j) const override {
6668
assert(ptr_ != nullptr);
67-
j = static_cast<const Type&>(*ptr_);
69+
serialize_into(j, *ptr_);
6870
}
6971

7072
void from_conf(const Conf& c) override {
7173
assert(ptr_ != nullptr);
72-
*ptr_ = c.get<Type>();
74+
deserialize_into(c, *ptr_);
7375
}
7476

7577
void reset_ptr() override { ptr_ = nullptr; }
7678

77-
// TODO: Implement or explain why we don't need the following methods:
78-
// - serialize
79-
// - serialize_into
80-
// - deserialize
81-
// - deserialize_into
79+
[[nodiscard]] Json serialize(const Type& x) const {
80+
Json j;
81+
serialize_into(j, x);
82+
return j;
83+
}
84+
85+
void serialize_into(Json& j, const Type& x) const {
86+
to_json(j, x);
87+
}
88+
89+
template<typename = std::enable_if<std::is_default_constructible_v<Type>>>
90+
[[nodiscard]] Type deserialize(const Conf& c) const {
91+
Type x;
92+
deserialize_into(c, x);
93+
return x;
94+
}
95+
96+
void deserialize_into(const Conf& c, Type& x) const {
97+
from_json(*c, x);
98+
}
8299

83100
private:
84101
Type* ptr_{nullptr};

fable/include/fable/schema/struct.hpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,16 @@ class Struct : public Base<Struct> {
206206
void to_json(Json& j) const override;
207207
void from_conf(const Conf& c) override;
208208

209-
// TODO: Implement or explain why we don't need the following methods:
210-
// - serialize
211-
// - serialize_into
212-
// - deserialize
213-
// - deserialize_into
209+
// NOTE: The following methods cannot be implemented because
210+
// Struct does not have an associated type:
211+
//
212+
// Json serialize(const Type&) const;
213+
// void serialize_into(Json&, const Type&) const;
214+
// Type deserialize(const Conf&) const;
215+
// void deserialize_into(const Conf&, Type&) const;
216+
//
217+
// This means that `Struct` cannot be used as a prototype schema
218+
// directly, for example with `optional`. Use `Confable` instead.
214219

215220
private:
216221
void set_properties(const std::map<std::string, Box>& props);

0 commit comments

Comments
 (0)