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>
15
16#include "adapter_for.hpp"
17
18#include <expected>
19#include <utility>
20
21namespace jsonv
22{
23
24/// \addtogroup Serialization
25/// \{
26
27/// An \c adapter which calls one function to deserialize and another to serialize.
28///
29/// The functions may take any of the shapes \c detail::invoke_deserialize and \c detail::invoke_serialize accept.
30template <typename T, typename FDeserialize, typename FSerialize>
32 public adapter_for<T>
33{
34public:
35 /// Create an adapter which deserializes by calling \a deserialize_ and serializes by calling \a serialize_.
36 template <typename FUDeserialize, typename FUSerialize>
37 explicit function_adapter(FUDeserialize&& deserialize_, FUSerialize&& serialize_) :
38 _deserialize(std::forward<FUDeserialize>(deserialize_)),
39 _serialize(std::forward<FUSerialize>(serialize_))
40 { }
41
42protected:
44 virtual std::expected<T, ast_node_type> create(deserialization_context& context, reader& from) const override
45 {
46 return detail::invoke_deserialize<T>(_deserialize, context, from);
47 }
48
49 virtual void serialize(const serialization_context& context, const T& from, writer& to) const override
50 {
51 detail::invoke_serialize<T>(_serialize, context, from, to);
52 }
53
54private:
55 FDeserialize _deserialize;
56 FSerialize _serialize;
57};
58
59/// Create an \c adapter from \a deserialize_ and \a serialize_, deducing the adapted type from what \a deserialize_
60/// returns.
61template <typename FDeserialize, typename FSerialize>
63auto make_adapter(FDeserialize deserialize_, FSerialize serialize_)
65{
67 (std::move(deserialize_), std::move(serialize_));
68}
69
70/// \}
71
72}
Copyright (c) 2015-2026 by Travis Gockel.
An adapter for the type T.
Provides extra information to routines used for deserialization and serialization.
Definition context.hpp:26
Provides extra information to routines used for deserialization, collects the problems they encounter...
An adapter which calls one function to deserialize and another to serialize.
virtual std::expected< T, ast_node_type > create(deserialization_context &context, reader &from) const override
Create an instance of T by reading from.
function_adapter(FUDeserialize &&deserialize_, FUSerialize &&serialize_)
Create an adapter which deserializes by calling deserialize_ and serializes by calling serialize_.
virtual void serialize(const serialization_context &context, const T &from, writer &to) const override
Write from into to.
A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON...
Definition reader.hpp:106
Provides extra information to routines used for serialization: the formats to find other serializers ...
Definition serialize.hpp:42
A writer instance writes a JSON ast_node sequence to some form of sink: an encoder,...
Definition writer.hpp:68
Copyright (c) 2014-2020 by Travis Gockel.
Deserialization of C++ types from a JSON AST.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:143
auto make_adapter(FDeserialize deserialize_, FSerialize serialize_) -> function_adapter< detail::deserialize_function_result_t< FDeserialize >, FDeserialize, FSerialize >
Create an adapter from deserialize_ and serialize_, deducing the adapted type from what deserialize_ ...
STL namespace.
Serialization of C++ types into a JSON token stream.