userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
proto_json.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/ugrpc/proto_json.hpp
4
/// @brief Utilities for conversion Protobuf -> Json
5
/// @ingroup userver_formats_serialize userver_formats_parse
6
7
#
include
<
google
/
protobuf
/
struct
.
pb
.
h
>
8
#
include
<
google
/
protobuf
/
util
/
json_util
.
h
>
9
10
#
include
<
userver
/
formats
/
json
/
serialize
.
hpp
>
11
#
include
<
userver
/
formats
/
json
/
value
.
hpp
>
12
13
USERVER_NAMESPACE_BEGIN
14
15
namespace
ugrpc {
16
17
namespace
impl {
18
19
extern
const
google::protobuf::util::JsonPrintOptions kDefaultJsonPrintOptions;
20
extern
const
google::protobuf::util::JsonParseOptions kDefaultJsonParseOptions;
21
22
void
FromJsonStringImpl(
23
std::string_view json_string,
24
google::protobuf::Message& output,
25
const
google::protobuf::util::JsonParseOptions& options
26
);
27
28
}
// namespace impl
29
30
/// @brief Returns formats::json::Value representation of protobuf message
31
/// @throws formats::json::Exception
32
/// @warning This function is **deprecated** and will be removed, use @ref protobuf::json::MessageToJson .
33
formats::
json
::
Value
MessageToJson
(
const
google::protobuf::Message& message);
34
35
/// @brief Returns formats::json::Value representation of protobuf message
36
/// @throws formats::json::Exception
37
/// @warning This function is **deprecated** and will be removed, use @ref protobuf::json::MessageToJson .
38
formats::
json
::
Value
MessageToJson
(
39
const
google::protobuf::Message& message,
40
const
google::protobuf::util::JsonPrintOptions& options
41
);
42
43
/// @brief Returns Json-string representation of protobuf message
44
/// @throws formats::json::Exception
45
std::string
ToJsonString
(
const
google::protobuf::Message& message);
46
47
/// @brief Returns Json-string representation of protobuf message
48
/// @throws formats::json::Exception
49
std::string
ToJsonString
(
50
const
google::protobuf::Message& message,
51
const
google::protobuf::util::JsonPrintOptions& options
52
);
53
54
/// @brief Parses Json to a protobuf message. Throws on unknown enum values and unknown fields by default.
55
/// @throws formats::json::Exception on field type mismatch, unknown enum values and unknown fields.
56
/// @warning This function is **deprecated** and will be removed, use @ref protobuf::json::JsonToMessage .
57
template
<
typename
Message>
58
Message
JsonToMessage
(
const
formats::
json
::
Value
& json) {
59
Message message;
60
impl::FromJsonStringImpl(formats
::
json
::
ToString
(
json
)
, message, impl::kDefaultJsonParseOptions);
61
return
message;
62
}
63
64
/// @brief Parses Json to a protobuf message. Throws on unknown enum values.
65
/// @throws formats::json::Exception on field type mismatch and unknown enum values.
66
/// @warning This function is **deprecated** and will be removed, use @ref protobuf::json::JsonToMessage .
67
template
<
typename
Message>
68
Message
JsonToMessage
(
const
formats::
json
::
Value
& json,
const
google::protobuf::util::JsonParseOptions& options) {
69
Message message;
70
impl::FromJsonStringImpl(formats
::
json
::
ToString
(
json
)
, message, options);
71
return
message;
72
}
73
74
/// @brief Parses Json to a protobuf message. Throws on unknown enum values and unknown fields by default.
75
/// @throws formats::json::Exception on invalid json, field type mismatch, unknown enum values and unknown fields.
76
template
<
typename
Message>
77
Message
FromJsonString
(std::string_view json_string) {
78
Message message;
79
impl::FromJsonStringImpl(json_string, message, impl::kDefaultJsonParseOptions);
80
return
message;
81
}
82
83
/// @brief Parses Json to a protobuf message. Throws on unknown enum values.
84
/// @throws formats::json::Exception on invalid json, field type mismatch and unknown enum values.
85
template
<
typename
Message>
86
Message
FromJsonString
(std::string_view json_string,
const
google::protobuf::util::JsonParseOptions& options) {
87
Message message;
88
impl::FromJsonStringImpl(json_string, message, options);
89
return
message;
90
}
91
92
}
// namespace ugrpc
93
94
namespace
formats::
serialize
{
95
96
/// @brief Conversion from any `google::protobuf::Message` to @ref formats::json::Value.
97
/// Uses the same format as @ref ugrpc::MessageToJson with its default options.
98
///
99
/// Works for `google::protobuf::Value`, `google::protobuf::Struct`, `google::protobuf::ListValue`
100
/// (top-level and nested) as well, converts them without extra objects in JSON representation.
101
///
102
/// Use as:
103
/// @code{.cpp}
104
/// auto json = formats::json::ValueBuilder{message}.ExtractValue();
105
/// @endcode
106
json
::
Value
Serialize
(
const
google::protobuf::Message& message,
To
<
json
::
Value
>);
107
108
}
// namespace formats::serialize
109
110
namespace
formats::
parse
{
111
112
/// @brief Conversion from @ref formats::json::Value to `google::protobuf::Message`.
113
/// Uses the same format as @ref ugrpc::JsonToMessage with its default options.
114
///
115
/// Works for `google::protobuf::Value`, `google::protobuf::Struct`, `google::protobuf::ListValue`
116
/// (top-level and nested) as well, converts them without extra objects in JSON representation.
117
///
118
/// Use as:
119
/// @code{.cpp}
120
/// auto value = json.As<google::protobuf::Value>();
121
/// @endcode
122
template
<
typename
Message>
123
requires
std::is_base_of_v<google::protobuf::Message, Message>
124
Message
Parse
(
const
json
::
Value
& value,
To
<Message>) {
125
return
ugrpc::JsonToMessage<Message>(value);
126
}
127
128
/// @cond
129
// Implementation detail: optimization for `google::protobuf::Value` specifically.
130
google::protobuf::Value Parse(
const
json
::
Value
& value,
To
<google::protobuf::Value>);
131
132
// Implementation detail: optimization for `google::protobuf::Struct` specifically.
133
google::protobuf::Struct Parse(
const
json
::
Value
& value,
To
<google::protobuf::Struct>);
134
135
// Implementation detail: optimization for `google::protobuf::ListValue` specifically.
136
google::protobuf::ListValue Parse(
const
json
::
Value
& value,
To
<google::protobuf::ListValue>);
137
/// @endcond
138
139
}
// namespace formats::parse
140
141
USERVER_NAMESPACE_END
userver
ugrpc
proto_json.hpp
Generated on
for userver by
Doxygen
1.17.0