diff options
| author | Anton Samokhvalov <[email protected]> | 2022-02-10 16:45:17 +0300 | 
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:45:17 +0300 | 
| commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
| tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/yson/json | |
| parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
Restoring authorship annotation for Anton Samokhvalov <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/yson/json')
| -rw-r--r-- | library/cpp/yson/json/json_writer.cpp | 320 | ||||
| -rw-r--r-- | library/cpp/yson/json/json_writer.h | 102 | ||||
| -rw-r--r-- | library/cpp/yson/json/yson2json_adapter.h | 2 | 
3 files changed, 212 insertions, 212 deletions
| diff --git a/library/cpp/yson/json/json_writer.cpp b/library/cpp/yson/json/json_writer.cpp index f65144bd674..87481256ecd 100644 --- a/library/cpp/yson/json/json_writer.cpp +++ b/library/cpp/yson/json/json_writer.cpp @@ -3,20 +3,20 @@  #include <library/cpp/json/json_writer.h>  namespace NYT { -    ////////////////////////////////////////////////////////////////////////////////  +    //////////////////////////////////////////////////////////////////////////////// -    static bool IsSpecialJsonKey(const TStringBuf& key) {  -        return key.size() > 0 && key[0] == '$';  -    }  +    static bool IsSpecialJsonKey(const TStringBuf& key) { +        return key.size() > 0 && key[0] == '$'; +    } -    ////////////////////////////////////////////////////////////////////////////////  +    //////////////////////////////////////////////////////////////////////////////// -    TJsonWriter::TJsonWriter(  -        IOutputStream* output,  +    TJsonWriter::TJsonWriter( +        IOutputStream* output,          ::NYson::EYsonType type, -        EJsonFormat format,  -        EJsonAttributesMode attributesMode,  -        ESerializedBoolFormat booleanFormat)  +        EJsonFormat format, +        EJsonAttributesMode attributesMode, +        ESerializedBoolFormat booleanFormat)          : TJsonWriter(              output,              NJson::TJsonWriterConfig{}.SetFormatOutput(format == JF_PRETTY), @@ -32,189 +32,189 @@ namespace NYT {          ::NYson::EYsonType type,          EJsonAttributesMode attributesMode,          ESerializedBoolFormat booleanFormat) -        : Output(output)  -        , Type(type)  -        , AttributesMode(attributesMode)  -        , BooleanFormat(booleanFormat)  -        , Depth(0)  -    {  +        : 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,  +        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;  -  +        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");  -        }  +            JsonWriter->Flush(); +            Output->Write("\n"); +        }      } -    bool TJsonWriter::IsWriteAllowed() {  -        if (AttributesMode == JAM_NEVER) {  -            return InAttributesBalance == 0;  -        }  -        return true;  +    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()) {  +        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->WriteNull();  +            JsonWriter->Write(value);              LeaveNode();          }      } -    void TJsonWriter::OnBeginList() {  -        if (IsWriteAllowed()) {  -            EnterNode();  -            JsonWriter->OpenArray();  -        }  +    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::OnListItem() {  +    void TJsonWriter::OnEntity() { +        if (IsWriteAllowed()) { +            EnterNode(); +            JsonWriter->WriteNull(); +            LeaveNode(); +        }      } -    void TJsonWriter::OnEndList() {  -        if (IsWriteAllowed()) {  -            JsonWriter->CloseArray();  -            LeaveNode();  -        }  -    }  +    void TJsonWriter::OnBeginList() { +        if (IsWriteAllowed()) { +            EnterNode(); +            JsonWriter->OpenArray(); +        } +    } -    void TJsonWriter::OnBeginMap() {  -        if (IsWriteAllowed()) {  -            EnterNode();  -            JsonWriter->OpenMap();  -        }  +    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);  -            }  -        }  +        if (IsWriteAllowed()) { +            if (IsSpecialJsonKey(name)) { +                WriteStringScalar(TString("$") + name); +            } else { +                WriteStringScalar(name); +            } +        } +    } + +    void TJsonWriter::OnEndMap() { +        if (IsWriteAllowed()) { +            JsonWriter->CloseMap(); +            LeaveNode(); +        }      } -    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::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::OnEndAttributes() {  -        InAttributesBalance -= 1;  -        if (AttributesMode != JAM_NEVER) {  -            HasAttributes = true;  -            JsonWriter->CloseMap();  -        }  +    void TJsonWriter::WriteStringScalar(const TStringBuf& value) { +        JsonWriter->Write(value);      } -    void TJsonWriter::WriteStringScalar(const TStringBuf& value) {  -        JsonWriter->Write(value);  +    void TJsonWriter::Flush() { +        JsonWriter->Flush();      } -    void TJsonWriter::Flush() {  -        JsonWriter->Flush();  -    }  +    //////////////////////////////////////////////////////////////////////////////// -    ////////////////////////////////////////////////////////////////////////////////  -   } diff --git a/library/cpp/yson/json/json_writer.h b/library/cpp/yson/json/json_writer.h index 8dca12fe0ef..d84ac0de530 100644 --- a/library/cpp/yson/json/json_writer.h +++ b/library/cpp/yson/json/json_writer.h @@ -8,33 +8,33 @@  #include <util/generic/vector.h>  namespace NYT { -    ////////////////////////////////////////////////////////////////////////////////  +    //////////////////////////////////////////////////////////////////////////////// -    enum EJsonFormat {  -        JF_TEXT,  -        JF_PRETTY  -    };  +    enum EJsonFormat { +        JF_TEXT, +        JF_PRETTY +    }; -    enum EJsonAttributesMode {  -        JAM_NEVER,  -        JAM_ON_DEMAND,  -        JAM_ALWAYS  -    };  +    enum EJsonAttributesMode { +        JAM_NEVER, +        JAM_ON_DEMAND, +        JAM_ALWAYS +    }; -    enum ESerializedBoolFormat {  -        SBF_BOOLEAN,  -        SBF_STRING  -    };  +    enum ESerializedBoolFormat { +        SBF_BOOLEAN, +        SBF_STRING +    }; -    class TJsonWriter  +    class TJsonWriter         : public ::NYson::TYsonConsumerBase { -    public:  -        TJsonWriter(  -            IOutputStream* output,  +    public: +        TJsonWriter( +            IOutputStream* output,              ::NYson::EYsonType type = ::NYson::EYsonType::Node, -            EJsonFormat format = JF_TEXT,  -            EJsonAttributesMode attributesMode = JAM_ON_DEMAND,  -            ESerializedBoolFormat booleanFormat = SBF_STRING);  +            EJsonFormat format = JF_TEXT, +            EJsonAttributesMode attributesMode = JAM_ON_DEMAND, +            ESerializedBoolFormat booleanFormat = SBF_STRING);          TJsonWriter(              IOutputStream* output, @@ -43,47 +43,47 @@ namespace NYT {              EJsonAttributesMode attributesMode = JAM_ON_DEMAND,              ESerializedBoolFormat booleanFormat = SBF_STRING); -        void Flush();  +        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 OnInt64Scalar(i64 value) override; +        void OnUint64Scalar(ui64 value) override; +        void OnDoubleScalar(double value) override; +        void OnBooleanScalar(bool value) override; -        void OnEntity() override;  +        void OnEntity() override; -        void OnBeginList() override;  -        void OnListItem() override;  -        void OnEndList() override;  +        void OnBeginList() override; +        void OnListItem() override; +        void OnEndList() override; -        void OnBeginMap() override;  +        void OnBeginMap() override;          void OnKeyedItem(TStringBuf key) override; -        void OnEndMap() override;  +        void OnEndMap() override; -        void OnBeginAttributes() override;  -        void OnEndAttributes() override;  +        void OnBeginAttributes() override; +        void OnEndAttributes() override; -    private:  -        THolder<NJson::TJsonWriter> UnderlyingJsonWriter;  -        NJson::TJsonWriter* JsonWriter;  -        IOutputStream* Output;  +    private: +        THolder<NJson::TJsonWriter> UnderlyingJsonWriter; +        NJson::TJsonWriter* JsonWriter; +        IOutputStream* Output;          ::NYson::EYsonType Type; -        EJsonAttributesMode AttributesMode;  -        ESerializedBoolFormat BooleanFormat;  +        EJsonAttributesMode AttributesMode; +        ESerializedBoolFormat BooleanFormat; -        void WriteStringScalar(const TStringBuf& value);  +        void WriteStringScalar(const TStringBuf& value); -        void EnterNode();  -        void LeaveNode();  -        bool IsWriteAllowed();  +        void EnterNode(); +        void LeaveNode(); +        bool IsWriteAllowed(); -        TVector<bool> HasUnfoldedStructureStack;  -        int InAttributesBalance;  -        bool HasAttributes;  -        int Depth;  -    };  +        TVector<bool> HasUnfoldedStructureStack; +        int InAttributesBalance; +        bool HasAttributes; +        int Depth; +    }; -    ////////////////////////////////////////////////////////////////////////////////  +    //////////////////////////////////////////////////////////////////////////////// -}  +} diff --git a/library/cpp/yson/json/yson2json_adapter.h b/library/cpp/yson/json/yson2json_adapter.h index 2a069c1f087..da1bf5ba709 100644 --- a/library/cpp/yson/json/yson2json_adapter.h +++ b/library/cpp/yson/json/yson2json_adapter.h @@ -50,4 +50,4 @@ namespace NYT {          ::NYson::TYsonConsumerBase* Impl_;          TState State_;      }; -}  +} | 
