3#include <google/protobuf/message.h>
4#include <google/protobuf/util/json_util.h>
6#include <absl/strings/string_view.h>
14 template<
typename TPbMessage>
39 static_assert(std::is_base_of<google::protobuf::Message, TPbMessage>::value,
"template parameter type TPbMessage must derive from Protobuf Message class");
43 template<
typename TPbMessage>
46 auto pMessage = to_protobuf();
47 if (pMessage ==
nullptr)
return false;
48 return pMessage->SerializeToString(&pb_string);
51 template<
typename TPbMessage>
54 auto pMessage = std::make_unique<TPbMessage>();
55 if (!pMessage->ParseFromString(pb_string))
return false;
56 from_protobuf(*pMessage);
60 template<
typename TPbMessage>
63 auto pMessage = to_protobuf();
64 if (pMessage ==
nullptr)
return false;
65 return pMessage->SerializeToOstream(&outputStream);
68 template<
typename TPbMessage>
71 auto pMessage = std::make_unique<TPbMessage>();
72 if (!pMessage->ParseFromIstream(&inputStream))
return false;
73 from_protobuf(*pMessage);
77 template<
typename TPbMessage>
81 google::protobuf::util::JsonPrintOptions jsonOptions;
84 auto status = google::protobuf::util::MessageToJsonString(*to_protobuf(), &json, jsonOptions);
85 if (!status.ok()) json.clear();
89 template<
typename TPbMessage>
92 auto pMessage = std::make_unique<TPbMessage>();
93 auto status = google::protobuf::util::JsonStringToMessage(absl::string_view(json), pMessage.get());
94 if (!status.ok())
return;
95 from_protobuf(*pMessage);
std::string to_json() const override
bool to_stream(std::ostream &outputStream) const
virtual std::unique_ptr< TPbMessage > to_protobuf() const =0
bool from_stream(std::istream &inputStream)
void from_json(const std::string &json) override
virtual void from_protobuf(const TPbMessage &message)=0
bool from_pbstring(const std::string &pb_string)
bool to_pbstring(std::string &pb_string) const