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
36formats::json::Value
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
88json::Value Serialize(const google::protobuf::Message& message, To<json::Value>);
89
90} // namespace formats::serialize
91
92namespace formats::parse {
93
94google::protobuf::Value Parse(const formats::json::Value& value, To<google::protobuf::Value>);
95
96}
97
98USERVER_NAMESPACE_END