JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
jsonv::enum_adapter< TEnum, FEnumComp, FValueComp > Class Template Reference

An adapter for enumeration types. More...

#include <jsonv/serialization/enum_adapter.hpp>

+ Inheritance diagram for jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >:
+ Collaboration diagram for jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >:

Public Member Functions

template<typename TForwardIterator >
 enum_adapter (std::string enum_name, TForwardIterator first, TForwardIterator last)
 Create an adapter with mapping values from the range [first, last).
 
 enum_adapter (std::string enum_name, std::initializer_list< std::pair< TEnum, value > > mapping)
 Create an adapter with the specified mapping values.
 
- Public Member Functions inherited from jsonv::adapter_for< TEnum >
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< TEnum, 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 &, const TEnum &from, writer &to) const override
 Write from into to.
 

Detailed Description

template<typename TEnum, typename FEnumComp = std::less<TEnum>, typename FValueComp = std::less<value>>
class jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >

An adapter for enumeration types.

The most common use of this is to map enum values in C++ to string values in a JSON representation (and vice versa).

Deserialization reads the reader directly. A value the mapping does not hold is refused with the reader still on it, and the problem names the value and lists every JSON value the mapping accepts, in the mapping's order:

Invalid value for ring: "bogus" (expected one of "earth", "fire", "heart", "useless", "water", "wind")
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:113
Template Parameters
TEnumThe type to map. This is not restricted to C++ enumerations (types defined with the enum keyword), but any type you wish to restrict to a subset of values.
FEnumCompbool (*)(TEnum, TEnum) – a strict ordering for TEnum values.
FValueCompbool (*)(value, value) – a strict ordering for value objects. By default, this is a case-sensitive comparison, but this can be replaced with anything you desire (for example, use value_less_icase to ignore case in deserializing from JSON). Under the library's own orderings – std::less<value>, value_less and value_less_icase – a string read from JSON text is looked up by its text, with no value built to hold it. Any other ordering can only be asked about a value, so one is built for every string deserialized from text.
See also
enum_adapter_icase

Definition at line 149 of file enum_adapter.hpp.

Constructor & Destructor Documentation

◆ enum_adapter() [1/2]

template<typename TEnum , typename FEnumComp = std::less<TEnum>, typename FValueComp = std::less<value>>
template<typename TForwardIterator >
jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >::enum_adapter ( std::string  enum_name,
TForwardIterator  first,
TForwardIterator  last 
)
inlineexplicit

Create an adapter with mapping values from the range [first, last).

Template Parameters
TForwardIteratorAn iterator yielding the type std::pair<TEnum, jsonv::value>

Definition at line 157 of file enum_adapter.hpp.

◆ enum_adapter() [2/2]

template<typename TEnum , typename FEnumComp = std::less<TEnum>, typename FValueComp = std::less<value>>
jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >::enum_adapter ( std::string  enum_name,
std::initializer_list< std::pair< TEnum, value > >  mapping 
)
inlineexplicit

Create an adapter with the specified mapping values.

Parameters
enum_nameA user-friendly name for this enumeration to be used in error messages.
mappingA list of C++ types and values to use in to_json and deserialize. It is okay to have a C++ value with more than one JSON representation. In this case, the first JSON representation will be used in to_json, but all JSON representations will be interpreted as the C++ value. It is also okay to have the same JSON representation for multiple C++ values. In this case, the first JSON representation provided for that value will be used in deserialize.

For example:

{
{ ring::fire, "fire" },
{ ring::wind, "wind" },
{ ring::earth, "earth" },
{ ring::water, "water" },
{ ring::heart, "heart" }, // "heart" is preferred for to_json
{ ring::heart, "useless" }, // "useless" is interpreted as ring::heart in deserialize
}
);
An adapter for enumeration types.

Definition at line 190 of file enum_adapter.hpp.

Member Function Documentation

◆ create()

template<typename TEnum , typename FEnumComp = std::less<TEnum>, typename FValueComp = std::less<value>>
virtual std::expected< TEnum, ast_node_type > jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >::create ( deserialization_context &  context,
reader &  from 
) const
inlineoverrideprotectedvirtual

Create an instance of T by reading from.

Parameters
contextExtra 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.
fromThe 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.
Returns
The created instance; otherwise a 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< TEnum >.

Definition at line 196 of file enum_adapter.hpp.

+ Here is the call graph for this function:

◆ serialize()

template<typename TEnum , typename FEnumComp = std::less<TEnum>, typename FValueComp = std::less<value>>
virtual void jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >::serialize ( const serialization_context &  context,
const TEnum &  from,
writer &  to 
) const
inlineoverrideprotectedvirtual

Write from into to.

See also
serializer::serialize

Implements jsonv::adapter_for< TEnum >.

Definition at line 221 of file enum_adapter.hpp.

+ Here is the call graph for this function:

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