diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/protobuf/json/json_writer_output.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/protobuf/json/json_writer_output.h')
-rw-r--r-- | library/cpp/protobuf/json/json_writer_output.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/library/cpp/protobuf/json/json_writer_output.h b/library/cpp/protobuf/json/json_writer_output.h new file mode 100644 index 0000000000..3d8a2daa56 --- /dev/null +++ b/library/cpp/protobuf/json/json_writer_output.h @@ -0,0 +1,103 @@ +#pragma once + +#include "json_output.h" +#include "config.h" + +#include <library/cpp/json/json_writer.h> + +#include <util/string/builder.h> +#include <util/generic/store_policy.h> + +namespace NProtobufJson { + class TBaseJsonWriterOutput: public IJsonOutput { + public: + TBaseJsonWriterOutput(NJson::TJsonWriter& writer) + : Writer(writer) + { + } + + private: + void DoWrite(int i) override { + Writer.Write(i); + } + void DoWrite(unsigned int i) override { + Writer.Write(i); + } + void DoWrite(long long i) override { + Writer.Write(i); + } + void DoWrite(unsigned long long i) override { + Writer.Write(i); + } + void DoWrite(float f) override { + Writer.Write(f); + } + void DoWrite(double f) override { + Writer.Write(f); + } + void DoWrite(bool b) override { + Writer.Write(b); + } + void DoWriteNull() override { + Writer.WriteNull(); + } + void DoWrite(const TStringBuf& s) override { + Writer.Write(s); + } + void DoWrite(const TString& s) override { + Writer.Write(s); + } + + void DoBeginList() override { + Writer.OpenArray(); + } + void DoEndList() override { + Writer.CloseArray(); + } + + void DoBeginObject() override { + Writer.OpenMap(); + } + void DoWriteKey(const TStringBuf& key) override { + Writer.Write(key); + } + void DoEndObject() override { + Writer.CloseMap(); + } + + void DoWriteRawJson(const TStringBuf& str) override { + Writer.UnsafeWrite(str); + } + + NJson::TJsonWriter& Writer; + }; + + class TJsonWriterOutput: public TEmbedPolicy<NJson::TJsonWriter>, public TBaseJsonWriterOutput { + public: + TJsonWriterOutput(IOutputStream* outputStream, const NJson::TJsonWriterConfig& cfg) + : TEmbedPolicy<NJson::TJsonWriter>(outputStream, cfg) + , TBaseJsonWriterOutput(*Ptr()) + { + } + + TJsonWriterOutput(IOutputStream* outputStream, const TProto2JsonConfig& cfg) + : TEmbedPolicy<NJson::TJsonWriter>(outputStream, CreateJsonWriterConfig(cfg)) + , TBaseJsonWriterOutput(*Ptr()) + { + } + + private: + static NJson::TJsonWriterConfig CreateJsonWriterConfig(const TProto2JsonConfig& cfg); + }; + + class TJsonStringWriterOutput: public TEmbedPolicy<TStringOutput>, public TJsonWriterOutput { + public: + template <typename TConfig> + TJsonStringWriterOutput(TString* str, const TConfig& cfg) + : TEmbedPolicy<TStringOutput>(*str) + , TJsonWriterOutput(TEmbedPolicy<TStringOutput>::Ptr(), cfg) + { + } + }; + +} |