aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/json_prettifier.cpp
blob: bb16aab44e599674c22557758903c8cbc272303a (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "json_prettifier.h"

#include <util/generic/deque.h>
#include <util/generic/algorithm.h>
#include <util/memory/pool.h>
#include <util/string/util.h>

#include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h>

namespace NJson {
    struct TRewritableOut {
        IOutputStream& Slave;

        char Last = 0;
        bool Dirty = false;

        TRewritableOut(IOutputStream& sl)
            : Slave(sl)
        {
        }

        template <typename T>
        void Write(const T& t) {
            Flush();
            Slave << t;
        }

        void Hold(char c) {
            if (Dirty)
                Flush();
            Last = c;
            Dirty = true;
        }

        void Flush() {
            if (Dirty) {
                Slave << Last;
                Dirty = false;
            }
        }

        void Revert() {
            Dirty = false;
        }
    };

    struct TSpaces {
        char S[256];

        TSpaces() {
            memset(&S, ' ', sizeof(S));
        }

        TStringBuf Get(ui8 sz) const {
            return TStringBuf(S, sz);
        }
    };

    bool TJsonPrettifier::MayUnquoteNew(TStringBuf s) {
        static str_spn alpha("a-zA-Z_@$", true);
        static str_spn alnum("a-zA-Z_@$0-9.-", true);
        static TStringBuf true0("true");
        static TStringBuf false0("false");
        static TStringBuf null0("null");

        return !!s && alpha.chars_table[(ui8)s[0]] && alnum.cbrk(s.begin() + 1, s.end()) == s.end() && !EqualToOneOf(s, null0, true0, false0);
    }

    // to keep arcadia tests happy
    bool TJsonPrettifier::MayUnquoteOld(TStringBuf s) {
        static str_spn alpha("a-zA-Z_@$", true);
        static str_spn alnum("a-zA-Z_@$0-9", true);
        static TStringBuf true0("true");
        static TStringBuf false0("false");
        static TStringBuf true1("on");
        static TStringBuf false1("off");
        static TStringBuf true2("da");
        static TStringBuf false2("net");
        static TStringBuf null0("null");

        return !!s && alpha.chars_table[(ui8)s[0]] && alnum.cbrk(s.begin() + 1, s.end()) == s.end() && !EqualToOneOf(s, null0, true0, false0, true1, false1, true2, false2);
    }

    class TPrettifier: public TJsonCallbacks {
        TRewritableOut Out;
        TStringBuf Spaces;
        TStringBuf Quote;
        TStringBuf Unsafe;
        TStringBuf Safe;

        ui32 Level = 0;
        ui32 MaxPaddingLevel;

        bool Unquote = false;
        bool Compactify = false;
        bool NewUnquote = false;

    public:
        TPrettifier(IOutputStream& out, const TJsonPrettifier& p)
            : Out(out)
            , MaxPaddingLevel(p.MaxPaddingLevel)
            , Unquote(p.Unquote)
            , Compactify(p.Compactify)
            , NewUnquote(p.NewUnquote)
        {
            static TSpaces spaces;
            Spaces = spaces.Get(p.Padding);
            if (p.SingleQuotes) {
                Quote = Unsafe = "'";
                Safe = "\"";
            } else {
                Quote = Unsafe = "\"";
                Safe = "'";
            }
        }

        void Pad(bool close = false) {
            if (Compactify) {
                Out.Flush();
                return;
            }
            if (Level > MaxPaddingLevel || (Level == MaxPaddingLevel && close)) {
                Out.Write(" ");
                return;
            }
            if (Level || close) {
                Out.Write(Spaces ? "\n" : " ");
            }
            for (ui32 i = 0; i < Level; ++i) {
                Out.Write(Spaces);
            }
        }

        void WriteSpace(char sp) {
            if (Compactify) {
                Out.Flush();
                return;
            }

            Out.Write(sp);
        }

        void OnVal() {
            if (Out.Dirty && ':' == Out.Last) {
                WriteSpace(' ');
            } else {
                Pad();
            }
        }

        void AfterVal() {
            Out.Hold(',');
        }

        template <typename T>
        bool WriteVal(const T& t) {
            OnVal();
            Out.Write(t);
            AfterVal();
            return true;
        }

        bool OnNull() override {
            return WriteVal(TStringBuf("null"));
        }

        bool OnBoolean(bool v) override {
            return WriteVal(v ? TStringBuf("true") : TStringBuf("false"));
        }

        bool OnInteger(long long i) override {
            return WriteVal(i);
        }

        bool OnUInteger(unsigned long long i) override {
            return WriteVal(i);
        }

        bool OnDouble(double d) override {
            return WriteVal(d);
        }

        void WriteString(TStringBuf s) {
            if (Unquote && (NewUnquote ? TJsonPrettifier::MayUnquoteNew(s) : TJsonPrettifier::MayUnquoteOld(s))) {
                Out.Slave << s;
            } else {
                Out.Slave << Quote;
                NEscJ::EscapeJ<false, true>(s, Out.Slave, Safe, Unsafe);
                Out.Slave << Quote;
            }
        }

        bool OnString(const TStringBuf& s) override {
            OnVal();
            WriteString(s);
            AfterVal();
            return true;
        }

        bool OnOpen(char c) {
            OnVal();
            Level++;
            Out.Hold(c);
            return true;
        }

        bool OnOpenMap() override {
            return OnOpen('{');
        }

        bool OnOpenArray() override {
            return OnOpen('[');
        }

        bool OnMapKey(const TStringBuf& k) override {
            OnVal();
            WriteString(k);
            WriteSpace(' ');
            Out.Hold(':');
            return true;
        }

        bool OnClose(char c) {
            if (!Level)
                return false;

            Level--;

            if (Out.Dirty && c == Out.Last) {
                WriteSpace(' ');
            } else {
                Out.Revert();
                Pad(true);
            }

            return true;
        }

        bool OnCloseMap() override {
            if (!OnClose('{'))
                return false;
            Out.Write("}");
            AfterVal();
            return true;
        }

        bool OnCloseArray() override {
            if (!OnClose('['))
                return false;
            Out.Write("]");
            AfterVal();
            return true;
        }

        bool OnEnd() override {
            return !Level;
        }
    };

    bool TJsonPrettifier::Prettify(TStringBuf in, IOutputStream& out) const {
        TPrettifier p(out, *this);
        if (Strict) {
            TMemoryInput mIn(in.data(), in.size());
            return ReadJson(&mIn, &p);
        } else {
            return ReadJsonFast(in, &p);
        }
    }

    TString TJsonPrettifier::Prettify(TStringBuf in) const {
        TStringStream s;
        if (Prettify(in, s))
            return s.Str();
        return TString();
    }

}