JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
jsonv::polymorphic_adapter< TPointer > Class Template Reference

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(extraction_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)
 
bool check_null_input () const
 
void check_null_output (bool on)
 }
 
bool check_null_output () const
 
- Public Member Functions inherited from jsonv::adapter_for< T >
virtual const std::type_info & get_type () const noexcept override
 
virtual std::expected< void, ast_node_type > extract (extraction_context &context, reader &from, void *into) const override
 
virtual value to_json (const serialization_context &context, const void *from) const override
 

Protected Member Functions

virtual TPointer create (extraction_context &context, const value &from) const override
 Create an instance of T from the materialised from.
 
virtual value to_json (const serialization_context &context, const TPointer &from) const override
 
- Protected Member Functions inherited from jsonv::value_adapter_for< TPointer >
virtual std::expected< TPointer, ast_node_type > create (extraction_context &context, reader &from) const final override
 Create an instance of T by reading from.
 
- Protected Member Functions inherited from jsonv::adapter_for< T >
virtual value to_json (const serialization_context &context, const T &from) const =0
 

Detailed Description

template<typename TPointer>
class jsonv::polymorphic_adapter< TPointer >

An adapter which can create polymorphic types.

This allows you to parse JSON directly into a type heirarchy without some middle layer.

[
{
"type": "worker",
"name": "Adam"
},
{
"type": "manager",
"name": "Bob",
"head": "Development"
}
]
Template Parameters
TPointerSome pointer-like type (likely unique_ptr or shared_ptr) you wish to extract 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 59 of file polymorphic_adapter.hpp.

Member Typedef Documentation

◆ match_predicate

template<typename TPointer >
using jsonv::polymorphic_adapter< TPointer >::match_predicate = std::function<bool (extraction_context&, const value&)>

Definition at line 63 of file polymorphic_adapter.hpp.

Member Function Documentation

◆ add_subtype()

template<typename TPointer >
template<typename T >
void jsonv::polymorphic_adapter< TPointer >::add_subtype ( match_predicate  pred)
inline

Add a subtype which can be transformed into TPointer which will be called if the discriminator pred is matched.

See also
add_subtype_keyed

Definition at line 73 of file polymorphic_adapter.hpp.

◆ add_subtype_keyed()

template<typename TPointer >
template<typename T >
void jsonv::polymorphic_adapter< TPointer >::add_subtype_keyed ( std::string  key,
value  expected_value,
keyed_subtype_action  action = keyed_subtype_action::none 
)
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.

See also
add_subtype

Definition at line 88 of file polymorphic_adapter.hpp.

+ Here is the call graph for this function:

◆ check_null_input() [1/2]

template<typename TPointer >
bool jsonv::polymorphic_adapter< TPointer >::check_null_input ( ) const
inline

Definition at line 116 of file polymorphic_adapter.hpp.

◆ check_null_input() [2/2]

template<typename TPointer >
void jsonv::polymorphic_adapter< TPointer >::check_null_input ( bool  on)
inline

When extracting a C++ value, should kind::null in JSON automatically become a default-constructed TPointer (which is usually the null representation)?

Definition at line 110 of file polymorphic_adapter.hpp.

◆ check_null_output() [1/2]

template<typename TPointer >
bool jsonv::polymorphic_adapter< TPointer >::check_null_output ( ) const
inline

Definition at line 130 of file polymorphic_adapter.hpp.

◆ check_null_output() [2/2]

template<typename TPointer >
void jsonv::polymorphic_adapter< TPointer >::check_null_output ( bool  on)
inline

}

When converting with to_json, should a null input translate into a kind::null?

Definition at line 124 of file polymorphic_adapter.hpp.

◆ create()

template<typename TPointer >
virtual TPointer jsonv::polymorphic_adapter< TPointer >::create ( extraction_context &  context,
const value &  from 
) const
inlineoverrideprotectedvirtual

Create an instance of T from the materialised from.

Exceptions
extraction_errorif from cannot be converted to a T.

Implements jsonv::value_adapter_for< TPointer >.

Definition at line 138 of file polymorphic_adapter.hpp.

◆ to_json()

template<typename TPointer >
virtual value jsonv::polymorphic_adapter< TPointer >::to_json ( const serialization_context &  context,
const TPointer &  from 
) const
inlineoverrideprotectedvirtual

Definition at line 161 of file polymorphic_adapter.hpp.


The documentation for this class was generated from the following file: