diff options
author | mowgli <mowgli@yandex-team.ru> | 2022-02-10 16:49:25 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:25 +0300 |
commit | 89afbbe4ca0e02e386dd4df08f7945f190dc1b84 (patch) | |
tree | c4772201af6215d48734691b8796e4cfc77c2ac8 /library/cpp/protobuf/json/inline.h | |
parent | 7510cec1516d17cbc8d7749974e36aa45f547a26 (diff) | |
download | ydb-89afbbe4ca0e02e386dd4df08f7945f190dc1b84.tar.gz |
Restoring authorship annotation for <mowgli@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf/json/inline.h')
-rw-r--r-- | library/cpp/protobuf/json/inline.h | 100 |
1 files changed, 50 insertions, 50 deletions
diff --git a/library/cpp/protobuf/json/inline.h b/library/cpp/protobuf/json/inline.h index e2d7bb6ef0..31dbf6a16c 100644 --- a/library/cpp/protobuf/json/inline.h +++ b/library/cpp/protobuf/json/inline.h @@ -1,66 +1,66 @@ -#pragma once - -// A printer from protobuf to json string, with ability to inline some string fields of given protobuf message -// into output as ready json without additional escaping. These fields should be marked using special field option. -// An example of usage: -// 1) Define a field option in your .proto to identify fields which should be inlined, e.g. -// +#pragma once + +// A printer from protobuf to json string, with ability to inline some string fields of given protobuf message +// into output as ready json without additional escaping. These fields should be marked using special field option. +// An example of usage: +// 1) Define a field option in your .proto to identify fields which should be inlined, e.g. +// // import "google/protobuf/descriptor.proto"; -// extend google.protobuf.FieldOptions { -// optional bool this_is_json = 58253; // do not forget assign some more or less unique tag -// } -// -// 2) Mark some fields of your protobuf message with this option, e.g.: -// -// message TMyObject { -// optional string A = 1 [(this_is_json) = true]; -// } -// -// 3) In the C++ code you prepare somehow an object of TMyObject type -// -// TMyObject o; -// o.Set("{\"inner\":\"value\"}"); -// -// 4) And then serialize it to json string with inlining, e.g.: -// +// extend google.protobuf.FieldOptions { +// optional bool this_is_json = 58253; // do not forget assign some more or less unique tag +// } +// +// 2) Mark some fields of your protobuf message with this option, e.g.: +// +// message TMyObject { +// optional string A = 1 [(this_is_json) = true]; +// } +// +// 3) In the C++ code you prepare somehow an object of TMyObject type +// +// TMyObject o; +// o.Set("{\"inner\":\"value\"}"); +// +// 4) And then serialize it to json string with inlining, e.g.: +// // Cout << NProtobufJson::PrintInlined(o, MakeFieldOptionFunctor(this_is_json)) << Endl; -// +// // 5) Alternatively you can specify a some more abstract functor for defining raw json fields // -// which will print following json to stdout: -// {"A":{"inner":"value"}} -// instead of -// {"A":"{\"inner\":\"value\"}"} -// which would be printed with normal Proto2Json printer. -// -// See ut/inline_ut.cpp for additional examples of usage. - +// which will print following json to stdout: +// {"A":{"inner":"value"}} +// instead of +// {"A":"{\"inner\":\"value\"}"} +// which would be printed with normal Proto2Json printer. +// +// See ut/inline_ut.cpp for additional examples of usage. + #include "config.h" #include "proto2json_printer.h" #include "json_output_create.h" - + #include <library/cpp/protobuf/util/simple_reflection.h> - + #include <util/generic/maybe.h> #include <util/generic/yexception.h> #include <util/generic/utility.h> - + #include <functional> - -namespace NProtobufJson { + +namespace NProtobufJson { template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant class TInliningPrinter: public TBasePrinter { public: using TFieldPredicate = std::function<bool(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*)>; - + template <typename... TArgs> TInliningPrinter(TFieldPredicate isInlined, TArgs&&... args) : TBasePrinter(std::forward<TArgs>(args)...) , IsInlined(std::move(isInlined)) { } - + virtual void PrintField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, @@ -77,12 +77,12 @@ namespace NProtobufJson { json.WriteRawJson(f.Get<TString>(i)); json.EndList(); } - - } else { + + } else { TBasePrinter::PrintField(proto, field, json, key); - } + } } - + private: bool ShouldPrint(const NProtoBuf::TConstField& f) const { if (!f.IsString()) @@ -95,8 +95,8 @@ namespace NProtobufJson { // we may want write default value for given field in case of its absence const auto& cfg = this->GetConfig(); return (f.Field()->is_repeated() ? cfg.MissingRepeatedKeyMode : cfg.MissingSingleKeyMode) == TProto2JsonConfig::MissingKeyDefault; - } - + } + private: TFieldPredicate IsInlined; }; @@ -105,11 +105,11 @@ namespace NProtobufJson { TInliningPrinter<> printer(std::move(isInlined), config); printer.Print(msg, output); } - + inline TString PrintInlined(const NProtoBuf::Message& msg, TInliningPrinter<>::TFieldPredicate isInlined, const TProto2JsonConfig& config = TProto2JsonConfig()) { TString ret; PrintInlined(msg, std::move(isInlined), *CreateJsonMapOutput(ret, config), config); return ret; - } - -} + } + +} |