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
32/// @warning This function is **deprecated** and will be removed, use @ref protobuf::json::MessageToJson .
33formats::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 .
38formats::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
45std::string ToJsonString(const google::protobuf::Message& message);
46
47/// @brief Returns Json-string representation of protobuf message
48/// @throws formats::json::Exception
49std::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 .
57template <typename Message>
58Message 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 .
67template <typename Message>
68Message 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.
76template <typename Message>
77Message 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.
85template <typename Message>
86Message 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
94namespace 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
106json::Value Serialize(const google::protobuf::Message& message, To<json::Value>);
107
108} // namespace formats::serialize
109
110namespace 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
122template <typename Message, typename = std::enable_if_t<std::is_base_of_v<google::protobuf::Message, Message>>>
123Message Parse(const json::Value& value, To<Message>) {
124 return ugrpc::JsonToMessage<Message>(value);
125}
126
127/// @cond
128// Implementation detail: optimization for `google::protobuf::Value` specifically.
129google::protobuf::Value Parse(const json::Value& value, To<google::protobuf::Value>);
130
131// Implementation detail: optimization for `google::protobuf::Struct` specifically.
132google::protobuf::Struct Parse(const json::Value& value, To<google::protobuf::Struct>);
133
134// Implementation detail: optimization for `google::protobuf::ListValue` specifically.
135google::protobuf::ListValue Parse(const json::Value& value, To<google::protobuf::ListValue>);
136/// @endcond
137
138} // namespace formats::parse
139
140USERVER_NAMESPACE_END