userver: userver/ugrpc/proto_json.hpp Source File
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
13USERVER_NAMESPACE_BEGIN
14
15namespace ugrpc {
16
17namespace impl {
18
19extern const google::protobuf::util::JsonPrintOptions kDefaultJsonPrintOptions;
20extern const google::protobuf::util::JsonParseOptions kDefaultJsonParseOptions;
21
22void 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
32formats::json::Value MessageToJson(const google::protobuf::Message& message);
33
34/// @brief Returns formats::json::Value representation of protobuf message
35/// @throws formats::json::Exception
37 const google::protobuf::Message& message,
38 const google::protobuf::util::JsonPrintOptions& options
39);
40
41/// @brief Returns Json-string representation of protobuf message
42/// @throws formats::json::Exception
43std::string ToJsonString(const google::protobuf::Message& message);
44
45/// @brief Returns Json-string representation of protobuf message
46/// @throws formats::json::Exception
47std::string ToJsonString(
48 const google::protobuf::Message& message,
49 const google::protobuf::util::JsonPrintOptions& options
50);
51
52/// @brief Parses Json to a protobuf message. Throws on unknown enum values and unknown fields by default.
53/// @throws formats::json::Exception on field type mismatch, unknown enum values and unknown fields.
54template <typename Message>
55Message JsonToMessage(const formats::json::Value& json) {
56 Message message;
57 impl::FromJsonStringImpl(formats::json::ToString(json), message, impl::kDefaultJsonParseOptions);
58 return message;
59}
60
61/// @brief Parses Json to a protobuf message. Throws on unknown enum values.
62/// @throws formats::json::Exception on field type mismatch and unknown enum values.
63template <typename Message>
64Message JsonToMessage(const formats::json::Value& json, const google::protobuf::util::JsonParseOptions& options) {
65 Message message;
66 impl::FromJsonStringImpl(formats::json::ToString(json), message, options);
67 return message;
68}
69
70/// @brief Parses Json to a protobuf message. Throws on unknown enum values and unknown fields by default.
71/// @throws formats::json::Exception on invalid json, field type mismatch, unknown enum values and unknown fields.
72template <typename Message>
73Message FromJsonString(std::string_view json_string) {
74 Message message;
75 impl::FromJsonStringImpl(json_string, message, impl::kDefaultJsonParseOptions);
76 return message;
77}
78
79/// @brief Parses Json to a protobuf message. Throws on unknown enum values.
80/// @throws formats::json::Exception on invalid json, field type mismatch and unknown enum values.
81template <typename Message>
82Message FromJsonString(std::string_view json_string, const google::protobuf::util::JsonParseOptions& options) {
83 Message message;
84 impl::FromJsonStringImpl(json_string, message, options);
85 return message;
86}
87
88} // namespace ugrpc
89
90namespace formats::serialize {
91
92/// @brief Conversion from any `google::protobuf::Message` to @ref formats::json::Value.
93/// Uses the same format as @ref ugrpc::MessageToJson with its default options.
94///
95/// Works for `google::protobuf::Value`, `google::protobuf::Struct`, `google::protobuf::ListValue`
96/// (top-level and nested) as well, converts them without extra objects in JSON representation.
97///
98/// Use as:
99/// @code{.cpp}
100/// auto json = formats::json::ValueBuilder{message}.ExtractValue();
101/// @endcode
102json::Value Serialize(const google::protobuf::Message& message, To<json::Value>);
103
104} // namespace formats::serialize
105
106namespace formats::parse {
107
108/// @brief Conversion from @ref formats::json::Value to `google::protobuf::Message`.
109/// Uses the same format as @ref ugrpc::JsonToMessage with its default options.
110///
111/// Works for `google::protobuf::Value`, `google::protobuf::Struct`, `google::protobuf::ListValue`
112/// (top-level and nested) as well, converts them without extra objects in JSON representation.
113///
114/// Use as:
115/// @code{.cpp}
116/// auto value = json.As<google::protobuf::Value>();
117/// @endcode
118template <typename Message, typename = std::enable_if_t<std::is_base_of_v<google::protobuf::Message, Message>>>
119Message Parse(const json::Value& value, To<Message>) {
120 return ugrpc::JsonToMessage<Message>(value);
121}
122
123/// @cond
124// Implementation detail: optimization for `google::protobuf::Value` specifically.
125google::protobuf::Value Parse(const json::Value& value, To<google::protobuf::Value>);
126
127// Implementation detail: optimization for `google::protobuf::Struct` specifically.
128google::protobuf::Struct Parse(const json::Value& value, To<google::protobuf::Struct>);
129
130// Implementation detail: optimization for `google::protobuf::ListValue` specifically.
131google::protobuf::ListValue Parse(const json::Value& value, To<google::protobuf::ListValue>);
132/// @endcond
133
134} // namespace formats::parse
135
136USERVER_NAMESPACE_END