aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/json_reader.cpp
blob: 87ec7d27426f5088d9a5b839fc547bb89922fc94 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include "json_reader.h"

#include "rapidjson_helpers.h"

#include <contrib/libs/rapidjson/include/rapidjson/error/en.h>
#include <contrib/libs/rapidjson/include/rapidjson/error/error.h>
#include <contrib/libs/rapidjson/include/rapidjson/reader.h>

#include <util/generic/stack.h>
#include <util/string/cast.h>
#include <util/system/yassert.h>
#include <util/string/builder.h>

namespace NJson {
    namespace {
        TString PrintError(const rapidjson::ParseResult& result) {
            return TStringBuilder() << TStringBuf("Offset: ") << result.Offset()
                                    << TStringBuf(", Code: ") << (int)result.Code()
                                    << TStringBuf(", Error: ") << GetParseError_En(result.Code());
        }
    }

    static const size_t DEFAULT_BUFFER_LEN = 65536; 

    bool TParserCallbacks::OpenComplexValue(EJsonValueType type) { 
        TJsonValue* pvalue; 
        switch (CurrentState) { 
            case START: 
                Value.SetType(type); 
                ValuesStack.push_back(&Value); 
                break; 
            case IN_ARRAY: 
                pvalue = &ValuesStack.back()->AppendValue(type); 
                ValuesStack.push_back(pvalue); 
                break; 
            case AFTER_MAP_KEY: 
                pvalue = &ValuesStack.back()->InsertValue(Key, type); 
                ValuesStack.push_back(pvalue); 
                CurrentState = IN_MAP; 
                break; 
            default: 
                return false; 
        } 
        return true; 
    }

    bool TParserCallbacks::CloseComplexValue() { 
        if (ValuesStack.empty()) { 
            return false;
        }
 
        ValuesStack.pop_back(); 
        if (!ValuesStack.empty()) { 
            switch (ValuesStack.back()->GetType()) { 
                case JSON_ARRAY: 
                    CurrentState = IN_ARRAY; 
                    break; 
                case JSON_MAP: 
                    CurrentState = IN_MAP; 
                    break; 
                default: 
                    return false; 
            } 
        } else { 
            CurrentState = FINISH; 
        } 
        return true; 
    }

    TParserCallbacks::TParserCallbacks(TJsonValue& value, bool throwOnError, bool notClosedBracketIsError)
        : TJsonCallbacks(throwOnError) 
        , Value(value) 
        , NotClosedBracketIsError(notClosedBracketIsError)
        , CurrentState(START) 
    { 
    } 

    bool TParserCallbacks::OnNull() { 
        return SetValue(JSON_NULL); 
    } 

    bool TParserCallbacks::OnBoolean(bool val) { 
        return SetValue(val); 
    } 

    bool TParserCallbacks::OnInteger(long long val) { 
        return SetValue(val); 
    } 

    bool TParserCallbacks::OnUInteger(unsigned long long val) { 
        return SetValue(val); 
    } 

    bool TParserCallbacks::OnString(const TStringBuf& val) { 
        return SetValue(val); 
    } 

    bool TParserCallbacks::OnDouble(double val) { 
        return SetValue(val); 
    } 

    bool TParserCallbacks::OnOpenArray() { 
        bool res = OpenComplexValue(JSON_ARRAY); 
        if (res) 
            CurrentState = IN_ARRAY; 
        return res; 
    } 

    bool TParserCallbacks::OnCloseArray() { 
        return CloseComplexValue(); 
    } 

    bool TParserCallbacks::OnOpenMap() { 
        bool res = OpenComplexValue(JSON_MAP); 
        if (res) 
            CurrentState = IN_MAP; 
        return res; 
    } 

    bool TParserCallbacks::OnCloseMap() { 
        return CloseComplexValue(); 
    } 

    bool TParserCallbacks::OnMapKey(const TStringBuf& val) { 
        switch (CurrentState) { 
            case IN_MAP: 
                Key = val; 
                CurrentState = AFTER_MAP_KEY; 
                break; 
            default: 
                return false; 
        } 
        return true; 
    }

    bool TParserCallbacks::OnEnd() {
        if (NotClosedBracketIsError){
            return ValuesStack.empty();
        }
        return true;
    }

    TJsonReaderConfig::TJsonReaderConfig() 
        : BufferSize(DEFAULT_BUFFER_LEN) 
    { 
    } 

    void TJsonReaderConfig::SetBufferSize(size_t bufferSize) { 
        BufferSize = Max((size_t)1, Min(bufferSize, DEFAULT_BUFFER_LEN)); 
    } 

    size_t TJsonReaderConfig::GetBufferSize() const { 
        return BufferSize; 
    } 

    namespace { 
        struct TJsonValueBuilder { 
#ifdef NDEBUG
            using TItem = TJsonValue*; 

            inline TJsonValue& Access(TItem& item) const { 
                return *item; 
            } 
#else
            struct TItem { 
                TJsonValue* V; 
                size_t DuplicateKeyCount; 

                TItem(TJsonValue* v) 
                    : V(v) 
                    , DuplicateKeyCount(0) 
                { 
                } 
            }; 
 
            inline TJsonValue& Access(TItem& item) const { 
                return *item.V; 
            }
#endif

            NJson::TJsonValue& V; 

            TStack<TItem> S; 

            TJsonValueBuilder(NJson::TJsonValue& v) 
                : V(v) 
            { 
                S.emplace(&V); 
            } 

            template <class T> 
            void Set(const T& t) { 
                if (Access(S.top()).IsArray()) { 
                    Access(S.top()).AppendValue(t); 
                } else { 
                    Access(S.top()) = t; 
                    S.pop(); 
                } 
            }

            bool Null() { 
                Set(NJson::JSON_NULL); 
                return true; 
            } 

            bool Bool(bool b) { 
                Set(b); 
                return true; 
            } 

            bool Int(int i) { 
                Set(i); 
                return true; 
            } 

            template <class U> 
            bool ProcessUint(U u) { 
                if (Y_LIKELY(u <= static_cast<ui64>(Max<i64>()))) { 
                    Set(i64(u)); 
                } else { 
                    Set(u); 
                } 
                return true; 
            }

            bool Uint(unsigned u) { 
                return ProcessUint(u); 
            } 

            bool Int64(i64 i) { 
                Set(i); 
                return true; 
            } 

            bool Uint64(ui64 u) { 
                return ProcessUint(u); 
            } 

            bool Double(double d) { 
                Set(d); 
                return true; 
            } 

            bool RawNumber(const char* str, rapidjson::SizeType length, bool copy) { 
                Y_ASSERT(false && "this method should never be called"); 
                Y_UNUSED(str); 
                Y_UNUSED(length); 
                Y_UNUSED(copy); 
                return true; 
            } 

            bool String(const char* str, rapidjson::SizeType length, bool copy) { 
                Y_ASSERT(copy); 
                Set(TStringBuf(str, length)); 
                return true; 
            } 

            bool StartObject() { 
                if (Access(S.top()).IsArray()) { 
                    S.emplace(&Access(S.top()).AppendValue(NJson::JSON_MAP)); 
                } else { 
                    Access(S.top()).SetType(NJson::JSON_MAP); 
                } 
                return true; 
            }

            bool Key(const char* str, rapidjson::SizeType length, bool copy) { 
                Y_ASSERT(copy); 
                auto& value = Access(S.top())[TStringBuf(str, length)]; 
                if (Y_UNLIKELY(value.GetType() != JSON_UNDEFINED)) {
#ifndef NDEBUG
                    ++S.top().DuplicateKeyCount; 
#endif
                    value.SetType(JSON_UNDEFINED); 
                } 
                S.emplace(&value); 
                return true; 
            }

            inline int GetDuplicateKeyCount() const { 
#ifdef NDEBUG
                return 0; 
#else
                return S.top().DuplicateKeyCount; 
#endif
            } 

            bool EndObject(rapidjson::SizeType memberCount) { 
                Y_ASSERT(memberCount == Access(S.top()).GetMap().size() + GetDuplicateKeyCount()); 
                S.pop(); 
                return true; 
            } 

            bool StartArray() { 
                if (Access(S.top()).IsArray()) { 
                    S.emplace(&Access(S.top()).AppendValue(NJson::JSON_ARRAY)); 
                } else { 
                    Access(S.top()).SetType(NJson::JSON_ARRAY); 
                } 
                return true; 
            } 
 
            bool EndArray(rapidjson::SizeType elementCount) { 
                Y_ASSERT(elementCount == Access(S.top()).GetArray().size()); 
                S.pop(); 
                return true; 
            } 
        }; 
 
        template <class TRapidJsonCompliantInputStream, class THandler> 
        auto Read(const TJsonReaderConfig& config, 
                  rapidjson::Reader& reader, 
                  TRapidJsonCompliantInputStream& is, 
                  THandler& handler) { 

            ui8 flags = ReaderConfigToRapidJsonFlags::NOCOMMENTS_VALID_NOESCAPE;

            if (config.AllowComments) { 
                flags |= ReaderConfigFlags::COMMENTS;
            }

            if (config.DontValidateUtf8) {
                flags &= ~(ReaderConfigFlags::VALIDATE);
            }

            if (config.AllowEscapedApostrophe) {
                flags |= ReaderConfigFlags::ESCAPE;
            }

            switch (flags) {
                case ReaderConfigToRapidJsonFlags::COMMENTS_NOVALID_NOESCAPE:
                    return reader.Parse<rapidjson::kParseCommentsFlag>(is, handler); 
                case ReaderConfigToRapidJsonFlags::COMMENTS_VALID_NOESCAPE:
                    return reader.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseValidateEncodingFlag>(is, handler); 
                case ReaderConfigToRapidJsonFlags::COMMENTS_VALID_ESCAPE:
                    return reader.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseValidateEncodingFlag | rapidjson::kParseEscapedApostropheFlag>(is, handler);
                case ReaderConfigToRapidJsonFlags::COMMENTS_NOVALID_ESCAPE:
                    return reader.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseEscapedApostropheFlag>(is, handler);
                case ReaderConfigToRapidJsonFlags::NOCOMMENTS_VALID_NOESCAPE:
                    return reader.Parse<rapidjson::kParseValidateEncodingFlag>(is, handler);
                case ReaderConfigToRapidJsonFlags::NOCOMMENTS_VALID_ESCAPE:
                    return reader.Parse<rapidjson::kParseValidateEncodingFlag | rapidjson::kParseEscapedApostropheFlag>(is, handler);
                case  ReaderConfigToRapidJsonFlags::NOCOMMENTS_NOVALID_ESCAPE:
                    return reader.Parse<rapidjson::kParseEscapedApostropheFlag>(is, handler);
                default:
                    return reader.Parse<rapidjson::kParseNoFlags>(is, handler); 
            }
        }

        template <class TRapidJsonCompliantInputStream, class THandler> 
        bool ReadJson(TRapidJsonCompliantInputStream& is, const TJsonReaderConfig* config, THandler& handler, bool throwOnError) { 
            rapidjson::Reader reader; 
 
            auto result = Read(*config, reader, is, handler); 
 
            if (result.IsError()) { 
                if (throwOnError) { 
                    ythrow TJsonException() << PrintError(result);
                } else { 
                    return false; 
                } 
            } 
 
            return true;
        }

        template <class TRapidJsonCompliantInputStream> 
        bool ReadJsonTree(TRapidJsonCompliantInputStream& is, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { 
            out->SetType(NJson::JSON_NULL); 
 
            TJsonValueBuilder handler(*out); 
 
            return ReadJson(is, config, handler, throwOnError); 
        }

        template <class TData> 
        bool ReadJsonTreeImpl(TData* in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { 
            std::conditional_t<std::is_same<TData, TStringBuf>::value, TStringBufStreamWrapper, TInputStreamWrapper> is(*in); 
            return ReadJsonTree(is, config, out, throwOnError); 
        } 

        template <class TData> 
        bool ReadJsonTreeImpl(TData* in, bool allowComments, TJsonValue* out, bool throwOnError) { 
            TJsonReaderConfig config; 
            config.AllowComments = allowComments; 
            return ReadJsonTreeImpl(in, &config, out, throwOnError); 
        } 

        template <class TData> 
        bool ReadJsonTreeImpl(TData* in, TJsonValue* out, bool throwOnError) { 
            return ReadJsonTreeImpl(in, false, out, throwOnError); 
        }
    } //namespace 

    bool ReadJsonTree(TStringBuf in, TJsonValue* out, bool throwOnError) { 
        return ReadJsonTreeImpl(&in, out, throwOnError); 
    }

    bool ReadJsonTree(TStringBuf in, bool allowComments, TJsonValue* out, bool throwOnError) { 
        return ReadJsonTreeImpl(&in, allowComments, out, throwOnError); 
    } 

    bool ReadJsonTree(TStringBuf in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { 
        return ReadJsonTreeImpl(&in, config, out, throwOnError); 
    }

    bool ReadJsonTree(IInputStream* in, TJsonValue* out, bool throwOnError) { 
        return ReadJsonTreeImpl(in, out, throwOnError); 
    }

    bool ReadJsonTree(IInputStream* in, bool allowComments, TJsonValue* out, bool throwOnError) { 
        return ReadJsonTreeImpl(in, allowComments, out, throwOnError); 
    }

    bool ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { 
        return ReadJsonTreeImpl(in, config, out, throwOnError); 
    }

    bool ReadJsonFastTree(TStringBuf in, TJsonValue* out, bool throwOnError, bool notClosedBracketIsError) {
        TParserCallbacks cb(*out, throwOnError, notClosedBracketIsError);

        return ReadJsonFast(in, &cb); 
    } 

    TJsonValue ReadJsonFastTree(TStringBuf in, bool notClosedBracketIsError) {
        TJsonValue value;
        // There is no way to report an error apart from throwing an exception when we return result by value.
        ReadJsonFastTree(in, &value, /* throwOnError = */ true, notClosedBracketIsError);
        return value;
    }

    namespace { 
        struct TJsonCallbacksWrapper { 
            TJsonCallbacks& Impl; 

            TJsonCallbacksWrapper(TJsonCallbacks& impl) 
                : Impl(impl) 
            { 
            } 

            bool Null() { 
                return Impl.OnNull(); 
            } 

            bool Bool(bool b) { 
                return Impl.OnBoolean(b); 
            } 

            template <class U> 
            bool ProcessUint(U u) { 
                if (Y_LIKELY(u <= ui64(Max<i64>()))) { 
                    return Impl.OnInteger(i64(u)); 
                } else { 
                    return Impl.OnUInteger(u); 
                } 
            } 
 
            bool Int(int i) { 
                return Impl.OnInteger(i); 
            } 
 
            bool Uint(unsigned u) { 
                return ProcessUint(u); 
            } 

            bool Int64(i64 i) { 
                return Impl.OnInteger(i); 
            } 

            bool Uint64(ui64 u) { 
                return ProcessUint(u); 
            } 

            bool Double(double d) { 
                return Impl.OnDouble(d); 
            } 

            bool RawNumber(const char* str, rapidjson::SizeType length, bool copy) { 
                Y_ASSERT(false && "this method should never be called"); 
                Y_UNUSED(str); 
                Y_UNUSED(length); 
                Y_UNUSED(copy); 
                return true; 
            }

            bool String(const char* str, rapidjson::SizeType length, bool copy) { 
                Y_ASSERT(copy); 
                return Impl.OnString(TStringBuf(str, length)); 
            } 

            bool StartObject() { 
                return Impl.OnOpenMap(); 
            } 

            bool Key(const char* str, rapidjson::SizeType length, bool copy) { 
                Y_ASSERT(copy); 
                return Impl.OnMapKey(TStringBuf(str, length)); 
            } 

            bool EndObject(rapidjson::SizeType memberCount) { 
                Y_UNUSED(memberCount); 
                return Impl.OnCloseMap(); 
            } 

            bool StartArray() { 
                return Impl.OnOpenArray(); 
            } 

            bool EndArray(rapidjson::SizeType elementCount) { 
                Y_UNUSED(elementCount); 
                return Impl.OnCloseArray(); 
            } 
        }; 
    } 

    bool ReadJson(IInputStream* in, TJsonCallbacks* cbs) { 
        return ReadJson(in, false, cbs); 
    } 

    bool ReadJson(IInputStream* in, bool allowComments, TJsonCallbacks* cbs) { 
        TJsonReaderConfig config; 
        config.AllowComments = allowComments; 
        return ReadJson(in, &config, cbs); 
    } 

    bool ReadJson(IInputStream* in, bool allowComments, bool allowEscapedApostrophe, TJsonCallbacks* cbs) {
        TJsonReaderConfig config;
        config.AllowComments = allowComments;
        config.AllowEscapedApostrophe = allowEscapedApostrophe;
        return ReadJson(in, &config, cbs);
    }

    bool ReadJson(IInputStream* in, const TJsonReaderConfig* config, TJsonCallbacks* cbs) { 
        TJsonCallbacksWrapper wrapper(*cbs); 
        TInputStreamWrapper is(*in); 

        rapidjson::Reader reader; 
        auto result = Read(*config, reader, is, wrapper); 

        if (result.IsError()) { 
            cbs->OnError(result.Offset(), PrintError(result));

            return false; 
        }

        return cbs->OnEnd(); 
    } 

    TJsonValue ReadJsonTree(IInputStream* in, bool throwOnError) { 
        TJsonValue out; 
        ReadJsonTree(in, &out, throwOnError); 
        return out; 
    } 

    TJsonValue ReadJsonTree(IInputStream* in, bool allowComments, bool throwOnError) { 
        TJsonValue out; 
        ReadJsonTree(in, allowComments, &out, throwOnError); 
        return out; 
    } 

    TJsonValue ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, bool throwOnError) { 
        TJsonValue out; 
        ReadJsonTree(in, config, &out, throwOnError); 
        return out; 
    }

}