$darkmode
#include <chrono>#include <map>#include <memory>#include <string>#include <type_traits>#include <utility>#include <vector>#include <fable/fable_fwd.hpp>#include <fable/schema/array.hpp>#include <fable/schema/boolean.hpp>#include <fable/schema/confable.hpp>#include <fable/schema/const.hpp>#include <fable/schema/duration.hpp>#include <fable/schema/enum.hpp>#include <fable/schema/ignore.hpp>#include <fable/schema/interface.hpp>#include <fable/schema/json.hpp>#include <fable/schema/map.hpp>#include <fable/schema/number.hpp>#include <fable/schema/optional.hpp>#include <fable/schema/passthru.hpp>#include <fable/schema/path.hpp>#include <fable/schema/string.hpp>#include <fable/schema/struct.hpp>#include <fable/schema/variant.hpp>#include <fable/schema/vector.hpp>#include <fable/schema/xmagic.hpp>Go to the source code of this file.
Classes | |
| struct | fable::schema_type< T > |
| class | fable::Schema |
This file includes most types from the schema namespace and defines the Schema type.
Schema is a class that can be used to describe how to set the values of a C++ datatype from a Json. This description can be used to
The last activity is not necessarily the most effective way to serialize a datatype, as there is quite a lot of indirection involved. However, Schema is designed to be the way to deserialize JSON. It is recommended to do this by making your type derive from Confable. Part of the Confable interface is to provide a Schema, which is then used for validation and deserialization.
There are several ways to define a Schema.
Given the following type:
struct MyData { std::string host{"localhost"}; uint16_t port{0};
Schema schema_impl(); };
We can implement the schema_impl method in the three ways detailed above.
Using Schema interface:
Schema MyData::schema_impl() { return Schema{ {"host", Schema(&host, "hostname of connection")}, {"port", Schema(&port, "port of connection")}, }; }
Using make_schema functions:
Schema MyData::schema_impl() { return make_schema({ {"host", make_schema(&host, "hostname of connection").require()}, {"port", make_schema(&port, "port of connection").minimum(1024)}, }); }
Using types in schema namespace:
Schema MyData::schema_impl() { using namespace schema; return Struct{ {"host", String(&host, "hostname of connection").require()}, {"port", Number<uint16_t>(&port, "port of connection").minimum(1024)}, }; }
Note that Schema contains pointers to the variables of MyData. The schema is therefore invalidated whenever MyData is moved or copied. The Confable type takes care of these issues to ensure that you don't shoot yourself in the foot.