JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
deserializer_for.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/deserializer_for.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 <expected>
16#include <new>
17#include <utility>
18
19namespace jsonv
20{
21
22/// \addtogroup Serialization
23/// \{
24
25/// A \c deserializer for type \c T. This is a utility class which converts the `void*` used in the \c deserializer
26/// interface into the more-friendly \c T.
27///
28/// \see adapter_for
29template <typename T>
31 public deserializer
32{
33public:
34 /// \see deserializer::get_type
36 virtual const std::type_info& get_type() const noexcept override
37 {
38 return typeid(T);
39 }
40
41 /// \see deserializer::deserialize
43 virtual std::expected<void, ast_node_type>
44 deserialize(deserialization_context& context, reader& from, void* into) const override
45 {
46 if (auto created = create(context, from))
47 {
48 try
49 {
50 new(into) T(std::move(created).value());
51 }
52 catch (...)
53 {
54 // `create` succeeded, so the cursor is already one past the value this was built from. Moving it
55 // into place is the last thing which can fail, and `T` is the caller's type; a loop recovering from
56 // that failure must not step over the value a second time.
57 context.note_value_consumed(from);
58 throw;
59 }
60 return {};
61 }
62 else
63 {
64 return std::unexpected(created.error());
65 }
66 }
67
68protected:
69 /// Create an instance of \c T by reading \a from.
70 ///
71 /// \see adapter_for::create
73 virtual std::expected<T, ast_node_type> create(deserialization_context& context, reader& from) const = 0;
74};
75
76/// \}
77
78}
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.
virtual std::expected< T, ast_node_type > create(deserialization_context &context, reader &from) const =0
Create an instance of T by reading from.
virtual std::expected< void, ast_node_type > deserialize(deserialization_context &context, reader &from, void *into) const override
virtual const std::type_info & get_type() const noexcept override
A deserializer holds the method for converting JSON source into an arbitrary C++ type.
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.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:132