File tree 3 files changed +45
-18
lines changed
fable/include/fable/schema
3 files changed +45
-18
lines changed Original file line number Diff line number Diff line change @@ -87,11 +87,16 @@ class CustomDeserializer : public schema::Interface {
87
87
88
88
friend void to_json (Json& j, const CustomDeserializer& b) { b.impl_ ->to_json (j); }
89
89
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.
95
100
96
101
private:
97
102
std::shared_ptr<schema::Interface> impl_{nullptr };
Original file line number Diff line number Diff line change @@ -59,26 +59,43 @@ class FromJson : public Base<FromJson<T>> {
59
59
return j;
60
60
}
61
61
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
+ }
63
65
64
66
using Interface::to_json;
65
67
void to_json (Json& j) const override {
66
68
assert (ptr_ != nullptr );
67
- j = static_cast < const Type&>( *ptr_);
69
+ serialize_into (j, *ptr_);
68
70
}
69
71
70
72
void from_conf (const Conf& c) override {
71
73
assert (ptr_ != nullptr );
72
- *ptr_ = c. get <Type>( );
74
+ deserialize_into (c, *ptr_);
73
75
}
74
76
75
77
void reset_ptr () override { ptr_ = nullptr ; }
76
78
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
+ }
82
99
83
100
private:
84
101
Type* ptr_{nullptr };
Original file line number Diff line number Diff line change @@ -206,11 +206,16 @@ class Struct : public Base<Struct> {
206
206
void to_json (Json& j) const override ;
207
207
void from_conf (const Conf& c) override ;
208
208
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.
214
219
215
220
private:
216
221
void set_properties (const std::map<std::string, Box>& props);
You can’t perform that action at this time.
0 commit comments