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