JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
wrapper_adapter.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization/wrapper_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/ast.hpp>
13#include <jsonv/config.hpp>
14#include <jsonv/reader.hpp>
16
17#include <expected>
18#include <utility>
19
20#include "adapter_for.hpp"
21
22namespace jsonv
23{
24
25/// \addtogroup Serialization
26/// \{
27
28/// An adapter for "wrapper" types.
29///
30/// \tparam TWrapper A wrapper type with a member \c value_type which represents the underlying wrapped type. This must
31/// be explicitly convertible to and from the \c value_type.
32template <typename TWrapper>
34 public adapter_for<TWrapper>
35{
36 using element_type = typename TWrapper::value_type;
37
38protected:
40 virtual std::expected<TWrapper, ast_node_type> create(deserialization_context& context, reader& from) const override
41 {
42 // Nothing to decide and nothing to position: the wrapped type's deserializer reads the same value this one was
43 // handed and leaves the cursor where this one owes it. `deserialization_context::deserialize` reports an
44 // ordinary failure by returning, so anything which *throws* here does so having already stepped the cursor: a
45 // `TWrapper` which rejects what it was handed, or a move of the deserialized value. Either way the failure is
46 // behind the cursor rather than in front of it, and saying so is what stops whatever recovers from this
47 // skipping the following sibling as well.
48 try
49 {
50 auto element = context.deserialize<element_type>(from);
51 if (!element)
52 return std::unexpected(element.error());
53
54 return TWrapper(*std::move(element));
55 }
56 catch (...)
57 {
58 context.note_value_consumed(from);
59 throw;
60 }
61 }
62
63 virtual void serialize(const serialization_context& context, const TWrapper& from, writer& to) const override
64 {
65 context.serialize(element_type(from), to);
66 }
67};
68
69/// \}
70
71}
Copyright (c) 2015-2026 by Travis Gockel.
Utilities for directly dealing with a JSON AST.
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...
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
An adapter for "wrapper" types.
virtual void serialize(const serialization_context &context, const TWrapper &from, writer &to) const override
Write from into to.
virtual std::expected< TWrapper, ast_node_type > create(deserialization_context &context, reader &from) const override
Create an instance of T by reading from.
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
Read a JSON AST.
Conversion between C++ types and JSON values.