aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/json/yson2json_adapter.cpp
blob: 1dbe770e5b222b28bc8f3d473b14e7efaedabc08 (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
#include "yson2json_adapter.h"

namespace NYT {
    TYson2JsonCallbacksAdapter::TYson2JsonCallbacksAdapter(
        ::NYson::TYsonConsumerBase* impl,
        bool throwException,
        ui64 maxDepth)
        : NJson::TJsonCallbacks(throwException)
        , Impl_(impl)
        , MaxDepth_(maxDepth)
    {
    }

    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);
        if (State_.ContextStack.size() > MaxDepth_) {
            return false;
        }
        Impl_->OnBeginList();
        return true;
    }

    bool TYson2JsonCallbacksAdapter::OnCloseArray() {
        State_.ContextStack.pop();
        Impl_->OnEndList();
        return true;
    }

    bool TYson2JsonCallbacksAdapter::OnOpenMap() {
        WrapIfListItem();
        State_.ContextStack.push(false);
        if (State_.ContextStack.size() > MaxDepth_) {
            return 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();
        }
    }
}