|
JSON Voorhees
Killer JSON for C++
|
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. | |
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:
| TEnum | The 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. |
| FEnumComp | bool (*)(TEnum, TEnum) – a strict ordering for TEnum values. |
| FValueComp | bool (*)(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. |
Definition at line 149 of file enum_adapter.hpp.
|
inlineexplicit |
Create an adapter with mapping values from the range [first, last).
| TForwardIterator | An iterator yielding the type std::pair<TEnum, jsonv::value> |
Definition at line 157 of file enum_adapter.hpp.
|
inlineexplicit |
Create an adapter with the specified mapping values.
| enum_name | A user-friendly name for this enumeration to be used in error messages. |
| mapping | A 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:
Definition at line 190 of file enum_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< TEnum >.
Definition at line 196 of file enum_adapter.hpp.
Here is the call graph for this function:
|
inlineoverrideprotectedvirtual |
Write from into to.
Implements jsonv::adapter_for< TEnum >.
Definition at line 221 of file enum_adapter.hpp.
Here is the call graph for this function: