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(const 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< TPointer >
virtual const std::type_info & get_type () const override
 Get the run-time type this extractor knows how to extract.
 
virtual void extract (const extraction_context &context, const value &from, void *into) const override
 Extract a the type from a value into a region of memory.
 
virtual value to_json (const serialization_context &context, const void *from) const override
 Create a value from the value in the given region of memory.
 

Protected Member Functions

virtual TPointer create (const extraction_context &context, const value &from) const override
 
virtual value to_json (const serialization_context &context, const TPointer &from) const override
 

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 56 of file polymorphic_adapter.hpp.

Member Typedef Documentation

◆ match_predicate

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

Definition at line 60 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 70 of file polymorphic_adapter.hpp.

+ Here is the call graph for this function:

◆ 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 85 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 112 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 107 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 125 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 120 of file polymorphic_adapter.hpp.

◆ create()

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

Implements jsonv::adapter_for< TPointer >.

Definition at line 132 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

Implements jsonv::adapter_for< TPointer >.

Definition at line 154 of file polymorphic_adapter.hpp.


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