aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/flex_buffers/cvt.cpp
blob: fee0cea0b830d8ed67fb4d80f617008bbe131c36 (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
#include "cvt.h"

#include <flatbuffers/flexbuffers.h>

#include <library/cpp/json/fast_sax/parser.h>
#include <library/cpp/json/json_reader.h>

#include <util/generic/vector.h>
#include <util/stream/output.h>
#include <util/stream/input.h>
#include <util/memory/pool.h>

using namespace NJson;

namespace {
    struct TJsonToFlexCallbacks: public TJsonCallbacks {
        inline TJsonToFlexCallbacks()
            : P(8192)
        {
        }

        bool OnNull() override {
            B.Null();

            return true;
        }

        bool OnBoolean(bool v) override {
            B.Bool(v);

            return true;
        }

        bool OnInteger(long long v) override {
            B.Int(v);

            return true;
        }

        bool OnUInteger(unsigned long long v) override {
            B.UInt(v);

            return true;
        }

        bool OnDouble(double v) override {
            B.Double(v);

            return true;
        }

        bool OnString(const TStringBuf& v) override {
            B.String(v.data(), v.size());

            return true;
        }

        bool OnOpenMap() override {
            S.push_back(B.StartMap());

            return true;
        }

        bool OnMapKey(const TStringBuf& v) override {
            auto iv = P.AppendCString(v);

            B.Key(iv.data(), iv.size());

            return true;
        }

        bool OnCloseMap() override {
            B.EndMap(PopOffset());

            return true;
        }

        bool OnOpenArray() override {
            S.push_back(B.StartVector());

            return true;
        }

        bool OnCloseArray() override {
            B.EndVector(PopOffset(), false, false);

            return true;
        }

        bool OnStringNoCopy(const TStringBuf& s) override {
            return OnString(s);
        }

        bool OnMapKeyNoCopy(const TStringBuf& s) override {
            return OnMapKey(s);
        }

        bool OnEnd() override {
            B.Finish();

            Y_ENSURE(S.empty());

            return true;
        }

        void OnError(size_t, TStringBuf reason) override {
            ythrow yexception() << reason;
        }

        inline size_t PopOffset() {
            auto res = S.back();

            S.pop_back();

            return res;
        }

        inline auto& Buffer() {
            return B.GetBuffer();
        }

        flexbuffers::Builder B;
        TVector<size_t> S;
        TMemoryPool P;
    };
}

void NJson::ConvertJsonToFlexBuffers(TStringBuf input, TFlexBuffersData& result) {
    TJsonToFlexCallbacks cb;

    ReadJsonFast(input, &cb);
    result.swap(const_cast<std::vector<ui8>&>(cb.Buffer()));
}

TString NJson::FlexToString(const TFlexBuffersData& v) {
    auto root = flexbuffers::GetRoot(v.data(), v.size());

    return TString(root.ToString());
}