JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
adapter_for.hpp
1/// \file jsonv/serialization/optional_adapter.hpp
2///
3/// Copyright (c) 2015-2020 by Travis Gockel. All rights reserved.
4///
5/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
6/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
7/// version.
8///
9/// \author Travis Gockel (travis@gockelhut.com)
10#pragma once
11
12#include <jsonv/config.hpp>
14
15#include <new>
16
17namespace jsonv
18{
19
20/// \addtogroup Serialization
21/// \{
22
23/// An adapter for the type \c T. This is a utility class which converts the `void*`s used in the \c extractor and
24/// \c serializer interfaces into the more-friendly \c T.
25template <typename T>
27 public adapter
28{
29public:
30 virtual const std::type_info& get_type() const override
31 {
32 return typeid(T);
33 }
34
35 virtual void extract(const extraction_context& context,
36 const value& from,
37 void* into
38 ) const override
39 {
40 new(into) T(create(context, from));
41 }
42
44 const void* from
45 ) const override
46 {
47 return to_json(context, *static_cast<const T*>(from));
48 }
49
50protected:
51 virtual T create(const extraction_context& context, const value& from) const = 0;
52
53 virtual value to_json(const serialization_context& context, const T& from) const = 0;
54};
55
56/// \}
57
58}
An adapter for the type T.
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 const std::type_info & get_type() const override
Get the run-time type this extractor knows how to extract.
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.
An adapter is both an extractor and a serializer.
An adapter for enumeration types.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:107
Copyright (c) 2014-2020 by Travis Gockel.
Conversion between C++ types and JSON values.