123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #pragma once
- #include <library/cpp/json/json_reader.h>
- #include <library/cpp/json/json_value.h>
- #include <library/cpp/yson/writer.h>
- namespace NJson2Yson {
- class TJsonBuilderImpl: public NYson::TYsonConsumerBase {
- public:
- TJsonBuilderImpl(NJson::TJsonCallbacks* parserCallbacks)
- : ParserCallbacks_(parserCallbacks)
- {
- }
- void OnStringScalar(TStringBuf value) override {
- ParserCallbacks_->OnString(value);
- }
- void OnInt64Scalar(i64 value) override {
- ParserCallbacks_->OnInteger(value);
- }
- void OnUint64Scalar(ui64 value) override {
- ParserCallbacks_->OnUInteger(value);
- }
- void OnDoubleScalar(double value) override {
- ParserCallbacks_->OnDouble(value);
- }
- void OnBooleanScalar(bool value) override {
- ParserCallbacks_->OnBoolean(value);
- }
- void OnEntity() override {
- ParserCallbacks_->OnNull();
- }
- void OnBeginList() override {
- ParserCallbacks_->OnOpenArray();
- }
- void OnListItem() override {
- }
- void OnEndList() override {
- ParserCallbacks_->OnCloseArray();
- }
- void OnBeginMap() override {
- ParserCallbacks_->OnOpenMap();
- }
- void OnKeyedItem(TStringBuf key) override {
- ParserCallbacks_->OnMapKey(key);
- }
- void OnEndMap() override {
- ParserCallbacks_->OnCloseMap();
- }
- void OnBeginAttributes() override {
- }
- void OnEndAttributes() override {
- }
- private:
- NJson::TJsonCallbacks* ParserCallbacks_;
- };
- template <typename TBase>
- class TSkipAttributesProxy: public TBase {
- public:
- template <typename... TArgs>
- TSkipAttributesProxy(TArgs&&... args)
- : TBase(std::forward<TArgs>(args)...)
- {
- }
- void OnStringScalar(TStringBuf value) override {
- if (AttributesDepth == 0) {
- TBase::OnStringScalar(value);
- }
- }
- void OnInt64Scalar(i64 value) override {
- if (AttributesDepth == 0) {
- TBase::OnInt64Scalar(value);
- }
- }
- void OnUint64Scalar(ui64 value) override {
- if (AttributesDepth == 0) {
- TBase::OnUint64Scalar(value);
- }
- }
- void OnDoubleScalar(double value) override {
- if (AttributesDepth == 0) {
- TBase::OnDoubleScalar(value);
- }
- }
- void OnBooleanScalar(bool value) override {
- if (AttributesDepth == 0) {
- TBase::OnBooleanScalar(value);
- }
- }
- void OnEntity() override {
- if (AttributesDepth == 0) {
- TBase::OnEntity();
- }
- }
- void OnBeginList() override {
- if (AttributesDepth == 0) {
- TBase::OnBeginList();
- }
- }
- void OnListItem() override {
- if (AttributesDepth == 0) {
- TBase::OnListItem();
- }
- }
- void OnEndList() override {
- if (AttributesDepth == 0) {
- TBase::OnEndList();
- }
- }
- void OnBeginMap() override {
- if (AttributesDepth == 0) {
- TBase::OnBeginMap();
- }
- }
- void OnKeyedItem(TStringBuf key) override {
- if (AttributesDepth == 0) {
- TBase::OnKeyedItem(key);
- }
- }
- void OnEndMap() override {
- if (AttributesDepth == 0) {
- TBase::OnEndMap();
- }
- }
- void OnBeginAttributes() override {
- ++AttributesDepth;
- }
- void OnEndAttributes() override {
- --AttributesDepth;
- Y_ASSERT(AttributesDepth >= 0);
- }
- private:
- int AttributesDepth = 0;
- };
- using TJsonBuilder = TSkipAttributesProxy<TJsonBuilderImpl>;
- void ConvertYson2Json(IInputStream* inputStream, IOutputStream* outputStream);
- void ConvertYson2Json(TStringBuf yson, IOutputStream* outputStream);
- TString ConvertYson2Json(TStringBuf yson);
- bool DeserializeYsonAsJsonValue(IInputStream* inputStream, NJson::TJsonValue* outputValue, bool throwOnError = false);
- bool DeserializeYsonAsJsonValue(TStringBuf str, NJson::TJsonValue* outputValue, bool throwOnError = false);
- void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, NYson::TYsonWriter* ysonWriter);
- void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, IOutputStream* outputStream);
- void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, TString& result);
- TString SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue);
- }
|