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
>
15
#include <
jsonv/serialization.hpp
>
16
17
#include <expected>
18
#include <utility>
19
20
#include "
adapter_for.hpp
"
21
22
namespace
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.
32
template
<
typename
TWrapper>
33
class
wrapper_adapter
:
34
public
adapter_for
<TWrapper>
35
{
36
using
element_type =
typename
TWrapper::value_type;
37
38
protected
:
39
JSONV_NODISCARD
40
virtual
std::expected<TWrapper, ast_node_type>
create
(
extraction_context
&
context
,
reader
& from)
const override
41
{
42
// Nothing to decide and nothing to position: the wrapped type's extractor reads the same value this one was
43
// handed and leaves the cursor where this one owes it.
44
// `extraction_context::extract` reports an ordinary failure by returning, so anything which *throws* here
45
// does so having already stepped the cursor: a `TWrapper` which rejects what it was handed, or a move of
46
// the extracted value. Either way the failure is behind the cursor rather than in front of it, and saying
47
// so is what stops whatever recovers from this skipping the following sibling as well.
48
try
49
{
50
auto
element =
context
.extract<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
JSONV_NODISCARD
64
virtual
value
to_json(
const
serialization_context
&
context
,
const
TWrapper& from)
const override
65
{
66
return
context
.to_json(element_type(from));
67
}
68
};
69
70
/// \}
71
72
}
adapter_for.hpp
Copyright (c) 2015-2026 by Travis Gockel.
ast.hpp
Utilities for directly dealing with a JSON AST.
jsonv::adapter_for
An adapter for the type T.
Definition
adapter_for.hpp:35
jsonv::context
Provides extra information to routines used for extraction and serialization.
Definition
context.hpp:26
jsonv::extraction_context
Provides extra information to routines used for extraction, collects the problems they encounter,...
Definition
extract.hpp:391
jsonv::reader
A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON...
Definition
reader.hpp:104
jsonv::serialization_context
Definition
serialization.hpp:34
jsonv::value
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition
value.hpp:113
jsonv::wrapper_adapter
An adapter for "wrapper" types.
Definition
wrapper_adapter.hpp:35
jsonv::wrapper_adapter::create
virtual std::expected< TWrapper, ast_node_type > create(extraction_context &context, reader &from) const override
Create an instance of T by reading from.
Definition
wrapper_adapter.hpp:40
config.hpp
Copyright (c) 2014-2020 by Travis Gockel.
JSONV_NODISCARD
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition
config.hpp:132
reader.hpp
Read a JSON AST.
serialization.hpp
Conversion between C++ types and JSON values.
jsonv
serialization
wrapper_adapter.hpp
Generated by
1.9.8