aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/yson/json2yson.h
blob: 25449a0cf74d7f9979105c6fc1a0d1e13301fbf7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#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<TBase>(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);
}