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/yson/json | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/yson/json')
-rw-r--r-- | library/cpp/yson/json/json_writer.cpp | 220 | ||||
-rw-r--r-- | library/cpp/yson/json/json_writer.h | 89 | ||||
-rw-r--r-- | library/cpp/yson/json/ya.make | 17 | ||||
-rw-r--r-- | library/cpp/yson/json/yson2json_adapter.cpp | 82 | ||||
-rw-r--r-- | library/cpp/yson/json/yson2json_adapter.h | 53 |
5 files changed, 461 insertions, 0 deletions
diff --git a/library/cpp/yson/json/json_writer.cpp b/library/cpp/yson/json/json_writer.cpp new file mode 100644 index 00000000000..87481256ecd --- /dev/null +++ b/library/cpp/yson/json/json_writer.cpp @@ -0,0 +1,220 @@ +#include "json_writer.h" + +#include <library/cpp/json/json_writer.h> + +namespace NYT { + //////////////////////////////////////////////////////////////////////////////// + + static bool IsSpecialJsonKey(const TStringBuf& key) { + return key.size() > 0 && key[0] == '$'; + } + + //////////////////////////////////////////////////////////////////////////////// + + TJsonWriter::TJsonWriter( + IOutputStream* output, + ::NYson::EYsonType type, + EJsonFormat format, + EJsonAttributesMode attributesMode, + ESerializedBoolFormat booleanFormat) + : TJsonWriter( + output, + NJson::TJsonWriterConfig{}.SetFormatOutput(format == JF_PRETTY), + type, + attributesMode, + booleanFormat + ) + {} + + TJsonWriter::TJsonWriter( + IOutputStream* output, + NJson::TJsonWriterConfig config, + ::NYson::EYsonType type, + EJsonAttributesMode attributesMode, + ESerializedBoolFormat booleanFormat) + : Output(output) + , Type(type) + , AttributesMode(attributesMode) + , BooleanFormat(booleanFormat) + , Depth(0) + { + if (Type == ::NYson::EYsonType::MapFragment) { + ythrow ::NYson::TYsonException() << ("Map fragments are not supported by Json"); + } + + UnderlyingJsonWriter.Reset(new NJson::TJsonWriter( + output, + config)); + JsonWriter = UnderlyingJsonWriter.Get(); + HasAttributes = false; + InAttributesBalance = 0; + } + + void TJsonWriter::EnterNode() { + if (AttributesMode == JAM_NEVER) { + HasAttributes = false; + } else if (AttributesMode == JAM_ON_DEMAND) { + // Do nothing + } else if (AttributesMode == JAM_ALWAYS) { + if (!HasAttributes) { + JsonWriter->OpenMap(); + JsonWriter->Write("$attributes"); + JsonWriter->OpenMap(); + JsonWriter->CloseMap(); + } + HasAttributes = true; + } + HasUnfoldedStructureStack.push_back(HasAttributes); + + if (HasAttributes) { + JsonWriter->Write("$value"); + HasAttributes = false; + } + + Depth += 1; + } + + void TJsonWriter::LeaveNode() { + Y_ASSERT(!HasUnfoldedStructureStack.empty()); + if (HasUnfoldedStructureStack.back()) { + // Close map of the {$attributes, $value} + JsonWriter->CloseMap(); + } + HasUnfoldedStructureStack.pop_back(); + + Depth -= 1; + + if (Depth == 0 && Type == ::NYson::EYsonType::ListFragment && InAttributesBalance == 0) { + JsonWriter->Flush(); + Output->Write("\n"); + } + } + + bool TJsonWriter::IsWriteAllowed() { + if (AttributesMode == JAM_NEVER) { + return InAttributesBalance == 0; + } + return true; + } + + void TJsonWriter::OnStringScalar(TStringBuf value) { + if (IsWriteAllowed()) { + EnterNode(); + WriteStringScalar(value); + LeaveNode(); + } + } + + void TJsonWriter::OnInt64Scalar(i64 value) { + if (IsWriteAllowed()) { + EnterNode(); + JsonWriter->Write(value); + LeaveNode(); + } + } + + void TJsonWriter::OnUint64Scalar(ui64 value) { + if (IsWriteAllowed()) { + EnterNode(); + JsonWriter->Write(value); + LeaveNode(); + } + } + + void TJsonWriter::OnDoubleScalar(double value) { + if (IsWriteAllowed()) { + EnterNode(); + JsonWriter->Write(value); + LeaveNode(); + } + } + + void TJsonWriter::OnBooleanScalar(bool value) { + if (IsWriteAllowed()) { + if (BooleanFormat == SBF_STRING) { + OnStringScalar(value ? "true" : "false"); + } else { + EnterNode(); + JsonWriter->Write(value); + LeaveNode(); + } + } + } + + void TJsonWriter::OnEntity() { + if (IsWriteAllowed()) { + EnterNode(); + JsonWriter->WriteNull(); + LeaveNode(); + } + } + + void TJsonWriter::OnBeginList() { + if (IsWriteAllowed()) { + EnterNode(); + JsonWriter->OpenArray(); + } + } + + void TJsonWriter::OnListItem() { + } + + void TJsonWriter::OnEndList() { + if (IsWriteAllowed()) { + JsonWriter->CloseArray(); + LeaveNode(); + } + } + + void TJsonWriter::OnBeginMap() { + if (IsWriteAllowed()) { + EnterNode(); + JsonWriter->OpenMap(); + } + } + + void TJsonWriter::OnKeyedItem(TStringBuf name) { + if (IsWriteAllowed()) { + if (IsSpecialJsonKey(name)) { + WriteStringScalar(TString("$") + name); + } else { + WriteStringScalar(name); + } + } + } + + void TJsonWriter::OnEndMap() { + if (IsWriteAllowed()) { + JsonWriter->CloseMap(); + LeaveNode(); + } + } + + void TJsonWriter::OnBeginAttributes() { + InAttributesBalance += 1; + if (AttributesMode != JAM_NEVER) { + JsonWriter->OpenMap(); + JsonWriter->Write("$attributes"); + JsonWriter->OpenMap(); + } + } + + void TJsonWriter::OnEndAttributes() { + InAttributesBalance -= 1; + if (AttributesMode != JAM_NEVER) { + HasAttributes = true; + JsonWriter->CloseMap(); + } + } + + void TJsonWriter::WriteStringScalar(const TStringBuf& value) { + JsonWriter->Write(value); + } + + void TJsonWriter::Flush() { + JsonWriter->Flush(); + } + + //////////////////////////////////////////////////////////////////////////////// + +} diff --git a/library/cpp/yson/json/json_writer.h b/library/cpp/yson/json/json_writer.h new file mode 100644 index 00000000000..d84ac0de530 --- /dev/null +++ b/library/cpp/yson/json/json_writer.h @@ -0,0 +1,89 @@ +#pragma once + +#include <library/cpp/yson/public.h> +#include <library/cpp/yson/consumer.h> + +#include <library/cpp/json/json_writer.h> + +#include <util/generic/vector.h> + +namespace NYT { + //////////////////////////////////////////////////////////////////////////////// + + enum EJsonFormat { + JF_TEXT, + JF_PRETTY + }; + + enum EJsonAttributesMode { + JAM_NEVER, + JAM_ON_DEMAND, + JAM_ALWAYS + }; + + enum ESerializedBoolFormat { + SBF_BOOLEAN, + SBF_STRING + }; + + class TJsonWriter + : public ::NYson::TYsonConsumerBase { + public: + TJsonWriter( + IOutputStream* output, + ::NYson::EYsonType type = ::NYson::EYsonType::Node, + EJsonFormat format = JF_TEXT, + EJsonAttributesMode attributesMode = JAM_ON_DEMAND, + ESerializedBoolFormat booleanFormat = SBF_STRING); + + TJsonWriter( + IOutputStream* output, + NJson::TJsonWriterConfig config, + ::NYson::EYsonType type = ::NYson::EYsonType::Node, + EJsonAttributesMode attributesMode = JAM_ON_DEMAND, + ESerializedBoolFormat booleanFormat = SBF_STRING); + + void Flush(); + + void OnStringScalar(TStringBuf value) override; + void OnInt64Scalar(i64 value) override; + void OnUint64Scalar(ui64 value) override; + void OnDoubleScalar(double value) override; + void OnBooleanScalar(bool value) override; + + void OnEntity() override; + + void OnBeginList() override; + void OnListItem() override; + void OnEndList() override; + + void OnBeginMap() override; + void OnKeyedItem(TStringBuf key) override; + void OnEndMap() override; + + void OnBeginAttributes() override; + void OnEndAttributes() override; + + private: + THolder<NJson::TJsonWriter> UnderlyingJsonWriter; + NJson::TJsonWriter* JsonWriter; + IOutputStream* Output; + ::NYson::EYsonType Type; + EJsonAttributesMode AttributesMode; + ESerializedBoolFormat BooleanFormat; + + void WriteStringScalar(const TStringBuf& value); + + void EnterNode(); + void LeaveNode(); + bool IsWriteAllowed(); + + TVector<bool> HasUnfoldedStructureStack; + int InAttributesBalance; + bool HasAttributes; + int Depth; + }; + + //////////////////////////////////////////////////////////////////////////////// + +} diff --git a/library/cpp/yson/json/ya.make b/library/cpp/yson/json/ya.make new file mode 100644 index 00000000000..625a6b231e7 --- /dev/null +++ b/library/cpp/yson/json/ya.make @@ -0,0 +1,17 @@ +LIBRARY() + +OWNER( + ermolovd + g:yt +) + +SRCS( + json_writer.cpp + yson2json_adapter.cpp +) + +PEERDIR( + library/cpp/json +) + +END() diff --git a/library/cpp/yson/json/yson2json_adapter.cpp b/library/cpp/yson/json/yson2json_adapter.cpp new file mode 100644 index 00000000000..b5e7c49d4d8 --- /dev/null +++ b/library/cpp/yson/json/yson2json_adapter.cpp @@ -0,0 +1,82 @@ +#include "yson2json_adapter.h" + +namespace NYT { + TYson2JsonCallbacksAdapter::TYson2JsonCallbacksAdapter(::NYson::TYsonConsumerBase* impl, bool throwException) + : NJson::TJsonCallbacks(throwException) + , Impl_(impl) + { + } + + bool TYson2JsonCallbacksAdapter::OnNull() { + WrapIfListItem(); + Impl_->OnEntity(); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnBoolean(bool val) { + WrapIfListItem(); + Impl_->OnBooleanScalar(val); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnInteger(long long val) { + WrapIfListItem(); + Impl_->OnInt64Scalar(val); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnUInteger(unsigned long long val) { + WrapIfListItem(); + Impl_->OnUint64Scalar(val); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnString(const TStringBuf& val) { + WrapIfListItem(); + Impl_->OnStringScalar(val); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnDouble(double val) { + WrapIfListItem(); + Impl_->OnDoubleScalar(val); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnOpenArray() { + WrapIfListItem(); + State_.ContextStack.push(true); + Impl_->OnBeginList(); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnCloseArray() { + State_.ContextStack.pop(); + Impl_->OnEndList(); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnOpenMap() { + WrapIfListItem(); + State_.ContextStack.push(false); + Impl_->OnBeginMap(); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnCloseMap() { + State_.ContextStack.pop(); + Impl_->OnEndMap(); + return true; + } + + bool TYson2JsonCallbacksAdapter::OnMapKey(const TStringBuf& val) { + Impl_->OnKeyedItem(val); + return true; + } + + void TYson2JsonCallbacksAdapter::WrapIfListItem() { + if (!State_.ContextStack.empty() && State_.ContextStack.top()) { + Impl_->OnListItem(); + } + } +} diff --git a/library/cpp/yson/json/yson2json_adapter.h b/library/cpp/yson/json/yson2json_adapter.h new file mode 100644 index 00000000000..da1bf5ba709 --- /dev/null +++ b/library/cpp/yson/json/yson2json_adapter.h @@ -0,0 +1,53 @@ +#pragma once + +#include <library/cpp/yson/consumer.h> + +#include <library/cpp/json/json_reader.h> + +#include <util/generic/stack.h> + +namespace NYT { + class TYson2JsonCallbacksAdapter + : public NJson::TJsonCallbacks { + public: + class TState { + private: + // Stores current context stack + // If true - we are in a list + // If false - we are in a map + TStack<bool> ContextStack; + + friend class TYson2JsonCallbacksAdapter; + }; + + public: + TYson2JsonCallbacksAdapter(::NYson::TYsonConsumerBase* impl, bool throwException = false); + + bool OnNull() override; + bool OnBoolean(bool val) override; + bool OnInteger(long long val) override; + bool OnUInteger(unsigned long long val) override; + bool OnString(const TStringBuf& val) override; + bool OnDouble(double val) override; + bool OnOpenArray() override; + bool OnCloseArray() override; + bool OnOpenMap() override; + bool OnCloseMap() override; + bool OnMapKey(const TStringBuf& val) override; + + TState State() const { + return State_; + } + + void Reset(const TState& state) { + State_ = state; + } + + private: + void WrapIfListItem(); + + private: + ::NYson::TYsonConsumerBase* Impl_; + TState State_; + }; +} |