Skip to content

Commit a97ee64

Browse files
committed
fable: Add to_json() method to all schema types
1 parent d771921 commit a97ee64

19 files changed

+31
-6
lines changed

fable/include/fable/schema.hpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,10 @@ class Schema : public schema::Interface {
246246
return j;
247247
}
248248

249-
Json to_json() const {
250-
Json j;
251-
to_json(j);
252-
return j;
253-
}
254-
255249
friend void to_json(Json& j, const Schema& s) { s.impl_->to_json(j); }
256250

257251
public: // Overrides
252+
using Interface::to_json;
258253
operator schema::Box() const { return schema::Box{impl_}; }
259254
Interface* clone() const override { return impl_->clone(); }
260255
JsonType type() const override { return impl_->type(); }

fable/include/fable/schema/array.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class Array : public Base<Array<T, P>> {
124124
}
125125
}
126126

127+
using Interface::to_json;
127128
void to_json(Json& j) const override {
128129
assert(ptr_ != nullptr);
129130
j = serialize(*ptr_);

fable/include/fable/schema/boolean.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Boolean : public Base<Boolean> {
5050

5151
void validate(const Conf& c) const override { this->validate_type(c); }
5252

53+
using Interface::to_json;
5354
void to_json(Json& j) const override {
5455
assert(ptr_ != nullptr);
5556
j = serialize(*ptr_);

fable/include/fable/schema/confable.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class FromConfable : public Base<FromConfable<T>> {
7676
}
7777
}
7878

79+
using Interface::to_json;
7980
void to_json(Json& j) const override {
8081
assert(ptr_ != nullptr);
8182
ptr_->to_json(j);

fable/include/fable/schema/const.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Const : public Base<Const<T, P>> {
7171
}
7272
}
7373

74+
using Interface::to_json;
7475
void to_json(Json& j) const override { j = serialize(constant_); }
7576

7677
void from_conf(const Conf& c) override { validate(c); }

fable/include/fable/schema/duration.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class Duration : public Base<Duration<T, Period>> {
125125
}
126126
}
127127

128+
using Interface::to_json;
128129
void to_json(Json& j) const override {
129130
assert(ptr_ != nullptr);
130131
j = serialize(*ptr_);

fable/include/fable/schema/enum.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Enum : public Base<Enum<T>> {
7373
}
7474
}
7575

76+
using Interface::to_json;
7677
void to_json(Json& j) const override {
7778
assert(ptr_ != nullptr);
7879
j = serialize(*ptr_);

fable/include/fable/schema/factory.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class FactoryBase : public Base<CRTP> {
280280
throw std::logic_error("FactoryBase::from_conf() should not be used");
281281
}
282282

283+
using Interface::to_json;
283284
void to_json(Json& j) const override {
284285
throw std::logic_error("FactoryBase::to_json() should not be used");
285286
}

fable/include/fable/schema/ignore.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Ignore : public Base<Ignore> {
5656
}
5757

5858
void validate(const Conf&) const override {}
59+
using Interface::to_json;
5960
void to_json(Json& j) const override { j = nullptr; }
6061
void from_conf(const Conf&) override {}
6162
void reset_ptr() override {}

fable/include/fable/schema/interface.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ class Interface {
211211
return true;
212212
}
213213

214+
/**
215+
* Return the current value of the destination.
216+
*
217+
* Warning: This is NOT an efficient operation, but it can be useful for
218+
* cases where speed is not important.
219+
*/
220+
virtual Json to_json() const {
221+
Json j;
222+
to_json(j);
223+
return j;
224+
}
225+
214226
/**
215227
* Return the current value of the destination.
216228
*
@@ -306,6 +318,7 @@ class Box : public Interface {
306318
}
307319

308320
public: // Overrides
321+
using Interface::to_json;
309322
Interface* clone() const override { return impl_->clone(); }
310323
JsonType type() const override { return impl_->type(); }
311324
std::string type_string() const override { return impl_->type_string(); }

fable/include/fable/schema/json.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class FromJson : public Base<FromJson<T>> {
6464

6565
void validate(const Conf& c) const override { this->validate_type(c); }
6666

67+
using Interface::to_json;
6768
void to_json(Json& j) const override {
6869
assert(ptr_ != nullptr);
6970
j = static_cast<const Type&>(*ptr_);

fable/include/fable/schema/map.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class Map : public Base<Map<T, P>> {
140140
}
141141
}
142142

143+
using Interface::to_json;
143144
void to_json(Json& j) const override {
144145
assert(ptr_ != nullptr);
145146
j = serialize(*ptr_);

fable/include/fable/schema/number.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class Number : public Base<Number<T>> {
196196
}
197197
}
198198

199+
using Interface::to_json;
199200
void to_json(Json& j) const override {
200201
assert(ptr_ != nullptr);
201202
j = serialize(*ptr_);

fable/include/fable/schema/optional.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Optional : public Base<Optional<T, P>> {
8181
prototype_.validate(c);
8282
}
8383

84+
using Interface::to_json;
8485
void to_json(Json& j) const override {
8586
assert(ptr_ != nullptr);
8687
j = serialize(*ptr_);

fable/include/fable/schema/passthru.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Passthru : public Base<Passthru<P>> {
7272
prototype_.validate(c);
7373
}
7474

75+
using Interface::to_json;
7576
void to_json(Json& j) const override {
7677
assert(ptr_ != nullptr);
7778
j = **ptr_;

fable/include/fable/schema/path.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class Path : public Base<Path> {
182182
Json json_schema() const override;
183183
void validate(const Conf& c) const override;
184184

185+
using Interface::to_json;
185186
void to_json(Json& j) const override {
186187
assert(ptr_ != nullptr);
187188
j = serialize(*ptr_);

fable/include/fable/schema/string.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class String : public Base<String> {
9797
Json json_schema() const override;
9898
void validate(const Conf& c) const override;
9999

100+
using Interface::to_json;
100101
void to_json(Json& j) const override {
101102
assert(ptr_ != nullptr);
102103
j = serialize(*ptr_);

fable/include/fable/schema/struct.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class Struct : public Base<Struct> {
202202
void reset_ptr() override;
203203

204204
public: // Overrides
205+
using Interface::to_json;
205206
Json usage() const override;
206207
Json json_schema() const override;
207208
void validate(const Conf& c) const override;

fable/include/fable/schema/variant.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Variant : public Interface {
104104
}
105105

106106
public: // Overrides
107+
using Interface::to_json;
107108
Json json_schema() const override;
108109
void validate(const Conf& c) const override { validate_index(c); }
109110
void to_json(Json& j) const override { schemas_[0].to_json(j); }

0 commit comments

Comments
 (0)