userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
nullable_item_parser.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/formats/json/parser/nullable_item_parser.hpp
4
/// @brief @copybrief formats::json::parser::NullableItemParser
5
/// @ingroup userver_universal
6
7
#
include
<
cstdint
>
8
#
include
<
string
>
9
#
include
<
string_view
>
10
#
include
<
utility
>
11
12
#
include
<
userver
/
formats
/
json
/
parser
/
typed_parser
.
hpp
>
13
14
USERVER_NAMESPACE_BEGIN
15
16
namespace
formats::
json
::
parser
{
17
18
/// @brief SAX parser for nullable items of containers, e.g. for `std::optional<T>` items of
19
/// formats::json::parser::ArrayParser.
20
///
21
/// Sends a value-initialized `Item` for JSON `null`. Delegates any other value to `ItemParser` and sends its result
22
/// converted to `Item`.
23
template
<
typename
Item,
typename
ItemParser>
24
class
NullableItemParser
final
:
public
TypedParser
<Item>,
public
Subscriber
<
typename
ItemParser::
ResultType
> {
25
public
:
26
explicit
NullableItemParser(ItemParser& item_parser)
27
: item_parser_(item_parser)
28
{
29
item_parser_.Subscribe(*
this
);
30
}
31
32
protected
:
33
void
Null()
override
{
this
->SetResult(Item{}); }
34
35
void
Bool(
bool
value)
override
{ PushItemParser().Bool(value); }
36
37
void
Int64(std::int64_t value)
override
{ PushItemParser().Int64(value); }
38
39
void
Uint64(std::uint64_t value)
override
{ PushItemParser().Uint64(value); }
40
41
void
Double(
double
value)
override
{ PushItemParser().Double(value); }
42
43
void
String(std::string_view value)
override
{ PushItemParser().String(value); }
44
45
void
StartObject()
override
{ PushItemParser().StartObject(); }
46
47
void
StartArray()
override
{ PushItemParser().StartArray(); }
48
49
std::string GetPathItem()
const
override
{
return
{}; }
50
51
std::string Expected()
const
override
{
return
"null or item"
; }
52
53
private
:
54
void
OnSend(
typename
ItemParser::ResultType&& value)
override
{
this
->SetResult(Item(std::move(value))); }
55
56
BaseParser
& PushItemParser() {
57
item_parser_.Reset();
58
BaseParser
& parser = item_parser_.GetParser();
59
this
->parser_state_->PushParser(parser);
60
return
parser;
61
}
62
63
ItemParser& item_parser_;
64
};
65
66
}
// namespace formats::json::parser
67
68
USERVER_NAMESPACE_END
userver
formats
json
parser
nullable_item_parser.hpp
Generated on
for userver by
Doxygen
1.17.0