JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
function_adapter.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/function_adapter.hpp
2///
3/// Copyright (c) 2015-2026 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 "adapter_for.hpp"
16
17#include <expected>
18#include <utility>
19
20namespace jsonv
21{
22
23/// \addtogroup Serialization
24/// \{
25
26template <typename T, typename FExtract, typename FToJson>
28 public adapter_for<T>
29{
30public:
31 template <typename FUExtract, typename FUToJson>
32 explicit function_adapter(FUExtract&& extract_, FUToJson&& to_json_) :
33 _extract(std::forward<FUExtract>(extract_)),
34 _to_json(std::forward<FUToJson>(to_json_))
35 { }
36
37protected:
39 virtual std::expected<T, ast_node_type> create(extraction_context& context, reader& from) const override
40 {
41 return detail::invoke_extract<T>(_extract, context, from);
42 }
43
45 virtual value to_json(const serialization_context& context, const T& from) const override
46 {
47 return to_json_impl(_to_json, context, from);
48 }
49
50private:
51 template <typename FUToJson>
52 static auto to_json_impl(const FUToJson& func, const serialization_context& context, const T& from)
53 -> decltype(func(context, from))
54 {
55 return func(context, from);
56 }
57
58 template <typename FUToJson, typename = void>
59 static auto to_json_impl(const FUToJson& func, const serialization_context&, const T& from)
60 -> decltype(func(from))
61 {
62 return func(from);
63 }
64
65private:
66 FExtract _extract;
67 FToJson _to_json;
68};
69
70/// Create an \c adapter from \a extract and \a to_json_, deducing the adapted type from what \a extract returns.
71template <typename FExtract, typename FToJson>
73auto make_adapter(FExtract extract, FToJson to_json_)
75{
77 (std::move(extract), std::move(to_json_));
78}
79
80/// \}
81
82}
Copyright (c) 2015-2026 by Travis Gockel.
An adapter for the type T.
Provides extra information to routines used for extraction and serialization.
Definition context.hpp:26
Provides extra information to routines used for extraction, collects the problems they encounter,...
Definition extract.hpp:391
virtual std::expected< T, ast_node_type > create(extraction_context &context, reader &from) const override
Create an instance of T by reading from.
A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON...
Definition reader.hpp:104
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:113
Copyright (c) 2014-2020 by Travis Gockel.
Extraction of C++ types from a JSON AST.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:132
auto make_adapter(FExtract extract, FToJson to_json_) -> function_adapter< detail::extract_function_result_t< FExtract >, FExtract, FToJson >
Create an adapter from extract and to_json_, deducing the adapted type from what extract returns.
T extract(const value &from, const formats &fmts)
Extract a C++ value from from using the provided fmts.
Definition extract.hpp:1299