JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
function_deserializer.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/function_deserializer.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 "deserializer_for.hpp"
16
17#include <concepts>
18#include <expected>
19#include <type_traits>
20#include <utility>
21
22namespace jsonv
23{
24
25/// \addtogroup Serialization
26/// \{
27
28/// A \c deserializer which calls a function to perform deserialization.
29///
30/// The function may take any of the shapes \c detail::invoke_deserialize accepts and may return either a \c T or a
31/// \c std::expected<T, ast_node_type>.
32template <typename T, typename FDeserialize>
34 public deserializer_for<T>
35{
36public:
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 FUDeserialize>
40 requires (!std::same_as<std::remove_cvref_t<FUDeserialize>, function_deserializer>)
41 explicit function_deserializer(FUDeserialize&& func) :
42 _func(std::forward<FUDeserialize>(func))
43 { }
44
45protected:
47 virtual std::expected<T, ast_node_type> create(deserialization_context& context, reader& from) const override
48 {
49 return detail::invoke_deserialize<T>(_func, context, from);
50 }
51
52private:
53 FDeserialize _func;
54};
55
56/// Create a \c deserializer from \a func, deducing what it deserializes from its return type.
57template <typename FDeserialize>
64
65/// \}
66
67}
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...
A deserializer for type T.
A deserializer which calls a function to perform deserialization.
virtual std::expected< T, ast_node_type > create(deserialization_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:105
Copyright (c) 2014-2020 by Travis Gockel.
Deserialization of C++ types from a JSON AST.
Copyright (c) 2015-2026 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:132
auto make_deserializer(FDeserialize func) -> function_deserializer< detail::deserialize_function_result_t< FDeserialize >, FDeserialize >
Create a deserializer from func, deducing what it deserializes from its return type.