JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
function_serializer.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/function_serializer.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 "serializer_for.hpp"
16
17#include <concepts>
18#include <type_traits>
19#include <utility>
20
21namespace jsonv
22{
23
24/// \addtogroup Serialization
25/// \{
26
27/// A \c serializer which calls a function to perform serialization.
28///
29/// The function may take any of the shapes \c detail::invoke_serialize accepts: it may write into the \c writer itself
30/// or return a \c value to be written.
31template <typename T, typename FSerialize>
33 public serializer_for<T>
34{
35public:
36 /// Create a serializer which calls \a func.
37 // Constrained so that copying from a non-const lvalue reaches the copy constructor, which this would otherwise
38 // beat as the better match.
39 template <typename FUSerialize>
40 requires (!std::same_as<std::remove_cvref_t<FUSerialize>, function_serializer>)
41 explicit function_serializer(FUSerialize&& func) :
42 _func(std::forward<FUSerialize>(func))
43 { }
44
45protected:
46 virtual void serialize(const serialization_context& context, const T& from, writer& to) const override
47 {
48 detail::invoke_serialize<T>(_func, context, from, to);
49 }
50
51private:
52 FSerialize _func;
53};
54
55/// Create a \c serializer for \c T from \a func, which may take any of the shapes \c detail::invoke_serialize accepts.
56template <typename T, typename FSerialize>
62
63/// \}
64
65}
Provides extra information to routines used for deserialization and serialization.
Definition context.hpp:26
A serializer which calls a function to perform serialization.
function_serializer(FUSerialize &&func)
Create a serializer which calls func.
virtual void serialize(const serialization_context &context, const T &from, writer &to) const override
Write from into to.
Provides extra information to routines used for serialization: the formats to find other serializers ...
Definition serialize.hpp:42
A serializer for type T.
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.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:143
function_serializer< T, FSerialize > make_serializer(FSerialize func)
Create a serializer for T from func, which may take any of the shapes detail::invoke_serialize accept...
Serialization of C++ types into a JSON token stream.
Copyright (c) 2015-2026 by Travis Gockel.