|
JSON Voorhees
Killer JSON for C++
|
An adapter which can create polymorphic types. More...
#include <jsonv/serialization/polymorphic_adapter.hpp>
Inheritance diagram for jsonv::polymorphic_adapter< TPointer >:
Collaboration diagram for jsonv::polymorphic_adapter< TPointer >:Public Types | |
| using | match_predicate = std::function< bool(deserialization_context &, const value &)> |
Public Member Functions | |
| template<typename T > | |
| void | add_subtype (match_predicate pred) |
Add a subtype which can be transformed into TPointer which will be called if the discriminator pred is matched. | |
| template<typename T > | |
| void | add_subtype_keyed (std::string key, value expected_value, keyed_subtype_action action=keyed_subtype_action::none) |
Add a subtype which can be transformed into TPointer which will be called if given a JSON value with kind::object which has a member with key and the provided expected_value. | |
| void | check_null_input (bool on) |
When deserializing a C++ value, should kind::null in JSON automatically become a default-constructed TPointer (which is usually the null representation)? | |
| bool | check_null_input () const |
When deserializing a C++ value, should kind::null in JSON automatically become a default-constructed TPointer (which is usually the null representation)? | |
| void | check_null_output (bool on) |
When serializing, should an empty TPointer be written as null? | |
| bool | check_null_output () const |
When serializing, should an empty TPointer be written as null? | |
Public Member Functions inherited from jsonv::adapter_for< TPointer > | |
| virtual const std::type_info & | get_type () const noexcept override |
| virtual std::expected< void, ast_node_type > | deserialize (deserialization_context &context, reader &from, void *into) const override |
| virtual void | serialize (const serialization_context &context, const void *from, writer &to) const override |
Protected Member Functions | |
| virtual std::expected< TPointer, ast_node_type > | create (deserialization_context &context, reader &from) const override |
Create an instance of T by reading from. | |
| virtual void | serialize (const serialization_context &context, const TPointer &from, writer &to) const override |
Write the object from points to where to is, or null for an empty pointer under check_null_output. | |
An adapter which can create polymorphic types.
This allows you to parse JSON directly into a type heirarchy without some middle layer.
Choosing a subtype means looking at the value before deserializing it, which a reader – a forward cursor – cannot do by itself. Each discriminator is shown as little of the value as answers it, read without moving the reader, and the subtype it picks is then deserialized from the reader directly:
value, every discriminator is shown that value. Nothing is copied.add_subtype_keyed is shown an object holding only the discriminating members, found by stepping over every other member whole. One registered with add_subtype can ask anything of the value, so the first time one of those has to be asked the subtree is materialised for it. Registering the keyed subtypes first keeps a matching document from ever being materialised.Serializing writes the pointed-to object where the writer is, through the serializer registered for its dynamic type. A subtype registered with keyed_subtype_action::check or keyed_subtype_action::insert is the exception, since the action works on the finished object: it is built as a value through serialization_context::to_json, checked or completed, and only then written whole. Its members therefore come out sorted by key, as they do from to_json, and a serialization_error raised inside it reports a path from its own root rather than the document's. One the action refuses has written nothing.
| TPointer | Some pointer-like type (likely unique_ptr or shared_ptr) you wish to deserialize values into. It must support operator*, an explicit conversion to bool, construction with a pointer to a subtype of what it contains and default construction. |
Definition at line 88 of file polymorphic_adapter.hpp.
| using jsonv::polymorphic_adapter< TPointer >::match_predicate = std::function<bool (deserialization_context&, const value&)> |
Definition at line 92 of file polymorphic_adapter.hpp.
|
inline |
Add a subtype which can be transformed into TPointer which will be called if the discriminator pred is matched.
Definition at line 102 of file polymorphic_adapter.hpp.
|
inline |
Add a subtype which can be transformed into TPointer which will be called if given a JSON value with kind::object which has a member with key and the provided expected_value.
Definition at line 112 of file polymorphic_adapter.hpp.
Here is the call graph for this function:
|
inline |
When deserializing a C++ value, should kind::null in JSON automatically become a default-constructed TPointer (which is usually the null representation)?
Definition at line 145 of file polymorphic_adapter.hpp.
|
inline |
When deserializing a C++ value, should kind::null in JSON automatically become a default-constructed TPointer (which is usually the null representation)?
Definition at line 139 of file polymorphic_adapter.hpp.
|
inline |
When serializing, should an empty TPointer be written as null?
Definition at line 160 of file polymorphic_adapter.hpp.
|
inline |
When serializing, should an empty TPointer be written as null?
Definition at line 154 of file polymorphic_adapter.hpp.
|
inlineoverrideprotectedvirtual |
Create an instance of T by reading from.
| context | Extra information to help you decode sub-objects, such as looking up other deserializer implementations via formats. Record any problem you encounter with deserialization_context::problem. |
| from | The JSON reader to deserialize from. On a successful return it should sit one position past the value which was read, as reader::next_value would have left it. |
std::unexpected carrying the ast_node_type actually found when the failure was a type mismatch, or ast_node_type::error otherwise. Implements jsonv::adapter_for< TPointer >.
Definition at line 168 of file polymorphic_adapter.hpp.
Here is the call graph for this function:
|
inlineoverrideprotectedvirtual |
Write the object from points to where to is, or null for an empty pointer under check_null_output.
A subtype registered with keyed_subtype_action::check or keyed_subtype_action::insert is built as a value and written whole, as the class description says.
Implements jsonv::adapter_for< TPointer >.
Definition at line 258 of file polymorphic_adapter.hpp.
Here is the call graph for this function: