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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
#include "json_writer.h"
#include "helpers.h"
#include <yt/yt/core/misc/utf8_decoder.h>
#include <contrib/libs/yajl/api/yajl_gen.h>
#include <cmath>
namespace NYT::NJson {
using namespace NYTree;
using namespace NYson;
////////////////////////////////////////////////////////////////////////////////
class TJsonWriter
: public IJsonWriter
{
public:
TJsonWriter(IOutputStream* output, bool isPretty);
virtual ~TJsonWriter() override;
void Flush() override;
void OnStringScalar(TStringBuf value) override;
void OnInt64Scalar(i64 value) override;
void OnUint64Scalar(ui64 value) override;
void OnDoubleScalar(double value) override;
void OnBooleanScalar(bool value) override;
void OnEntity() override;
void OnBeginList() override;
void OnListItem() override;
void OnEndList() override;
void OnBeginMap() override;
void OnKeyedItem(TStringBuf name) override;
void OnEndMap() override;
void OnBeginAttributes() override;
void OnEndAttributes() override;
void OnRaw(TStringBuf yson, EYsonType type) override;
void StartNextValue() override;
ui64 GetWrittenByteCount() const override;
private:
void GenerateString(TStringBuf value);
TStringBuf GetBuffer() const;
private:
yajl_gen Handle_;
IOutputStream* Output_;
ui64 WrittenToOutputByteCount_ = 0;
};
////////////////////////////////////////////////////////////////////////////////
DEFINE_ENUM(ENanInfinityMode,
(NotSupported)
(WriteInfinitiesUnquoted)
(WriteAllQuouted)
);
class TJsonConsumer
: public TYsonConsumerBase
, public IJsonConsumer
{
public:
TJsonConsumer(
IJsonWriter* jsonWriter,
EYsonType type,
TJsonFormatConfigPtr config);
TJsonConsumer(
std::unique_ptr<IJsonWriter> jsonWriter,
EYsonType type,
TJsonFormatConfigPtr config);
void OnStringScalar(TStringBuf value) override;
void OnInt64Scalar(i64 value) override;
void OnUint64Scalar(ui64 value) override;
void OnDoubleScalar(double value) override;
void OnBooleanScalar(bool value) override;
void OnEntity() override;
void OnBeginList() override;
void OnListItem() override;
void OnEndList() override;
void OnBeginMap() override;
void OnKeyedItem(TStringBuf key) override;
void OnEndMap() override;
void OnBeginAttributes() override;
void OnEndAttributes() override;
void SetAnnotateWithTypesParameter(bool value) override;
void OnStringScalarWeightLimited(TStringBuf value, std::optional<i64> weightLimit) override;
void OnNodeWeightLimited(TStringBuf yson, std::optional<i64> weightLimit) override;
void Flush() override;
private:
void WriteStringScalar(TStringBuf value);
void WriteStringScalarWithAttributes(TStringBuf value, TStringBuf type, bool incomplete);
void EnterNode();
void LeaveNode();
bool IsWriteAllowed();
private:
IJsonWriter* const JsonWriter_;
std::unique_ptr<IJsonWriter> JsonWriterHolder_;
const EYsonType Type_;
const TJsonFormatConfigPtr Config_;
ENanInfinityMode NanInfinityMode_;
TUtf8Transcoder Utf8Transcoder_;
std::vector<bool> HasUnfoldedStructureStack_;
int InAttributesBalance_ = 0;
bool HasAttributes_ = false;
int Depth_ = 0;
bool CheckLimit_ = true;
};
////////////////////////////////////////////////////////////////////////////////
static void CheckYajlCode(int yajlCode)
{
if (yajlCode == yajl_gen_status_ok) {
return;
}
TString errorMessage;
switch (yajlCode)
{
case yajl_gen_keys_must_be_strings:
errorMessage = "JSON key must be a string";
break;
case yajl_max_depth_exceeded:
errorMessage = Format("JSON maximal depth exceeded %v", YAJL_MAX_DEPTH);
break;
case yajl_gen_in_error_state:
errorMessage = "JSON: a generator function (yajl_gen_XXX) was called while in an error state";
break;
case yajl_gen_generation_complete:
errorMessage = "Attempt to alter already completed JSON document";
break;
case yajl_gen_invalid_number:
errorMessage = "Invalid floating point value in JSON";
break;
case yajl_gen_invalid_string:
errorMessage = "Invalid UTF-8 string in JSON";
break;
default:
errorMessage = Format("Yajl writer failed with code %v", yajlCode);
}
THROW_ERROR_EXCEPTION(std::move(errorMessage), NYT::TError::DisableFormat);
}
TJsonWriter::TJsonWriter(IOutputStream* output, bool isPretty)
: Output_(output)
{
Handle_ = yajl_gen_alloc(nullptr);
yajl_gen_config(Handle_, yajl_gen_beautify, isPretty ? 1 : 0);
yajl_gen_config(Handle_, yajl_gen_skip_final_newline, 0);
yajl_gen_config(Handle_, yajl_gen_support_infinity, 1);
yajl_gen_config(Handle_, yajl_gen_disable_yandex_double_format, 1);
yajl_gen_config(Handle_, yajl_gen_validate_utf8, 1);
}
TJsonWriter::~TJsonWriter()
{
yajl_gen_free(Handle_);
}
void TJsonWriter::GenerateString(TStringBuf value)
{
CheckYajlCode(yajl_gen_string(Handle_, (const unsigned char*) value.data(), value.size()));
}
TStringBuf TJsonWriter::GetBuffer() const
{
size_t len = 0;
const unsigned char* buf = nullptr;
CheckYajlCode(yajl_gen_get_buf(Handle_, &buf, &len));
return TStringBuf(static_cast<const char*>(static_cast<const void*>(buf)), len);
}
void TJsonWriter::Flush()
{
auto buf = GetBuffer();
Output_->Write(buf);
WrittenToOutputByteCount_ += buf.size();
yajl_gen_clear(Handle_);
}
void TJsonWriter::StartNextValue()
{
Flush();
yajl_gen_reset(Handle_, nullptr);
Output_->Write('\n');
}
void TJsonWriter::OnBeginMap()
{
CheckYajlCode(yajl_gen_map_open(Handle_));
}
void TJsonWriter::OnKeyedItem(TStringBuf name)
{
GenerateString(name);
}
void TJsonWriter::OnEndMap()
{
CheckYajlCode(yajl_gen_map_close(Handle_));
}
void TJsonWriter::OnBeginList()
{
CheckYajlCode(yajl_gen_array_open(Handle_));
}
void TJsonWriter::OnListItem()
{ }
void TJsonWriter::OnEndList()
{
CheckYajlCode(yajl_gen_array_close(Handle_));
}
void TJsonWriter::OnStringScalar(TStringBuf value)
{
GenerateString(value);
}
void TJsonWriter::OnEntity()
{
CheckYajlCode(yajl_gen_null(Handle_));
}
void TJsonWriter::OnDoubleScalar(double value)
{
CheckYajlCode(yajl_gen_double(Handle_, value));
}
void TJsonWriter::OnInt64Scalar(i64 value)
{
CheckYajlCode(yajl_gen_integer(Handle_, value));
}
void TJsonWriter::OnUint64Scalar(ui64 value)
{
CheckYajlCode(yajl_gen_uinteger(Handle_, value));
}
void TJsonWriter::OnBooleanScalar(bool value)
{
CheckYajlCode(yajl_gen_bool(Handle_, value ? 1 : 0));
}
void TJsonWriter::OnBeginAttributes()
{
THROW_ERROR_EXCEPTION("TJsonWriter does not support attributes");
}
void TJsonWriter::OnEndAttributes()
{
THROW_ERROR_EXCEPTION("TJsonWriter does not support attributes");
}
void TJsonWriter::OnRaw(TStringBuf /*yson*/, NYT::NYson::EYsonType /*type*/)
{
THROW_ERROR_EXCEPTION("TJsonWriter does not support OnRaw()");
}
ui64 TJsonWriter::GetWrittenByteCount() const
{
return GetBuffer().size() + WrittenToOutputByteCount_;
}
////////////////////////////////////////////////////////////////////////////////
TJsonConsumer::TJsonConsumer(
IJsonWriter* jsonWriter,
EYsonType type,
TJsonFormatConfigPtr config)
: JsonWriter_(jsonWriter)
, Type_(type)
, Config_(std::move(config))
, Utf8Transcoder_(Config_->EncodeUtf8)
{
if (Type_ == EYsonType::MapFragment) {
THROW_ERROR_EXCEPTION("Map fragments are not supported by JSON");
}
NanInfinityMode_ = ENanInfinityMode::NotSupported;
if (Config_->SupportInfinity) {
NanInfinityMode_ = ENanInfinityMode::WriteInfinitiesUnquoted;
} else if (Config_->StringifyNanAndInfinity) {
NanInfinityMode_ = ENanInfinityMode::WriteAllQuouted;
}
}
TJsonConsumer::TJsonConsumer(
std::unique_ptr<IJsonWriter> jsonWriter,
EYsonType type,
TJsonFormatConfigPtr config)
: TJsonConsumer(jsonWriter.get(), type, std::move(config))
{
JsonWriterHolder_ = std::move(jsonWriter);
}
void TJsonConsumer::EnterNode()
{
if (Config_->AttributesMode == EJsonAttributesMode::Never) {
HasAttributes_ = false;
} else if (Config_->AttributesMode == EJsonAttributesMode::OnDemand) {
// Do nothing
} else if (Config_->AttributesMode == EJsonAttributesMode::Always) {
if (!HasAttributes_) {
JsonWriter_->OnBeginMap();
JsonWriter_->OnKeyedItem(TStringBuf("$attributes"));
JsonWriter_->OnBeginMap();
JsonWriter_->OnEndMap();
HasAttributes_ = true;
}
}
HasUnfoldedStructureStack_.push_back(HasAttributes_);
if (HasAttributes_) {
JsonWriter_->OnKeyedItem(TStringBuf("$value"));
HasAttributes_ = false;
}
Depth_ += 1;
}
void TJsonConsumer::LeaveNode()
{
YT_VERIFY(!HasUnfoldedStructureStack_.empty());
if (HasUnfoldedStructureStack_.back()) {
// Close map of the {$attributes, $value}
JsonWriter_->OnEndMap();
}
HasUnfoldedStructureStack_.pop_back();
Depth_ -= 1;
if (Depth_ == 0 && Type_ == EYsonType::ListFragment && InAttributesBalance_ == 0) {
JsonWriter_->StartNextValue();
}
}
bool TJsonConsumer::IsWriteAllowed()
{
if (Config_->AttributesMode == EJsonAttributesMode::Never) {
return InAttributesBalance_ == 0;
}
return true;
}
void TJsonConsumer::OnStringScalar(TStringBuf value)
{
TStringBuf writeValue = value;
bool incomplete = false;
if (Config_->AttributesMode != EJsonAttributesMode::Never) {
if (CheckLimit_ && Config_->StringLengthLimit && std::ssize(value) > *Config_->StringLengthLimit) {
writeValue = value.substr(0, *Config_->StringLengthLimit);
incomplete = true;
}
}
WriteStringScalarWithAttributes(writeValue, TStringBuf("string"), incomplete);
}
void TJsonConsumer::OnInt64Scalar(i64 value)
{
if (IsWriteAllowed()) {
if (Config_->AnnotateWithTypes && Config_->AttributesMode != EJsonAttributesMode::Never) {
if (!HasAttributes_) {
JsonWriter_->OnBeginMap();
HasAttributes_ = true;
}
JsonWriter_->OnKeyedItem(TStringBuf("$type"));
JsonWriter_->OnStringScalar(TStringBuf("int64"));
}
EnterNode();
if (Config_->Stringify) {
WriteStringScalar(::ToString(value));
} else {
JsonWriter_->OnInt64Scalar(value);
}
LeaveNode();
}
}
void TJsonConsumer::OnUint64Scalar(ui64 value)
{
if (IsWriteAllowed()) {
if (Config_->AnnotateWithTypes && Config_->AttributesMode != EJsonAttributesMode::Never) {
if (!HasAttributes_) {
JsonWriter_->OnBeginMap();
HasAttributes_ = true;
}
JsonWriter_->OnKeyedItem(TStringBuf("$type"));
JsonWriter_->OnStringScalar(TStringBuf("uint64"));
}
EnterNode();
if (Config_->Stringify) {
WriteStringScalar(::ToString(value));
} else {
JsonWriter_->OnUint64Scalar(value);
}
LeaveNode();
}
}
void TJsonConsumer::OnDoubleScalar(double value)
{
if (IsWriteAllowed()) {
if (Config_->AnnotateWithTypes && Config_->AttributesMode != EJsonAttributesMode::Never) {
if (!HasAttributes_) {
JsonWriter_->OnBeginMap();
HasAttributes_ = true;
}
JsonWriter_->OnKeyedItem(TStringBuf("$type"));
JsonWriter_->OnStringScalar(TStringBuf("double"));
}
EnterNode();
if (Config_->Stringify) {
char buf[256];
auto str = TStringBuf(buf, FloatToString(value, buf, sizeof(buf)));
WriteStringScalar(str);
} else {
switch (NanInfinityMode_) {
case ENanInfinityMode::WriteAllQuouted:
if (std::isnan(value)) {
JsonWriter_->OnStringScalar(TStringBuf("nan"));
} else if (std::isinf(value)) {
if (value < 0) {
JsonWriter_->OnStringScalar(TStringBuf("-inf"));
} else {
JsonWriter_->OnStringScalar(TStringBuf("inf"));
}
} else {
JsonWriter_->OnDoubleScalar(value);
}
break;
case ENanInfinityMode::WriteInfinitiesUnquoted:
if (std::isnan(value)) {
THROW_ERROR_EXCEPTION(
"Unexpected NaN encountered during JSON writing; "
"consider \"stringify_nan_and_infinity\" config option");
}
JsonWriter_->OnDoubleScalar(value);
break;
case ENanInfinityMode::NotSupported:
if (std::isnan(value) || std::isinf(value)) {
THROW_ERROR_EXCEPTION(
"Unexpected NaN or infinity encountered during JSON writing; "
"consider using either \"support_infinity\" or \"stringify_nan_and_infinity\" config options");
}
JsonWriter_->OnDoubleScalar(value);
break;
}
}
LeaveNode();
}
}
void TJsonConsumer::OnBooleanScalar(bool value)
{
if (IsWriteAllowed()) {
if (Config_->AnnotateWithTypes && Config_->AttributesMode != EJsonAttributesMode::Never) {
if (!HasAttributes_) {
JsonWriter_->OnBeginMap();
HasAttributes_ = true;
}
JsonWriter_->OnKeyedItem(TStringBuf("$type"));
JsonWriter_->OnStringScalar(TStringBuf("boolean"));
}
EnterNode();
if (Config_->Stringify) {
WriteStringScalar(FormatBool(value));
} else {
JsonWriter_->OnBooleanScalar(value);
}
LeaveNode();
}
}
void TJsonConsumer::OnEntity()
{
if (IsWriteAllowed()) {
EnterNode();
JsonWriter_->OnEntity();
LeaveNode();
}
}
void TJsonConsumer::OnBeginList()
{
if (IsWriteAllowed()) {
EnterNode();
JsonWriter_->OnBeginList();
}
}
void TJsonConsumer::OnListItem()
{
if (IsWriteAllowed()) {
JsonWriter_->OnListItem();
}
}
void TJsonConsumer::OnEndList()
{
if (IsWriteAllowed()) {
JsonWriter_->OnEndList();
LeaveNode();
}
}
void TJsonConsumer::OnBeginMap()
{
if (IsWriteAllowed()) {
EnterNode();
JsonWriter_->OnBeginMap();
}
}
void TJsonConsumer::OnKeyedItem(TStringBuf name)
{
if (IsWriteAllowed()) {
if (IsSpecialJsonKey(name)) {
JsonWriter_->OnKeyedItem(Utf8Transcoder_.Encode(TString("$") + name));
} else {
JsonWriter_->OnKeyedItem(Utf8Transcoder_.Encode(name));
}
}
}
void TJsonConsumer::OnEndMap()
{
if (IsWriteAllowed()) {
JsonWriter_->OnEndMap();
LeaveNode();
}
}
void TJsonConsumer::OnBeginAttributes()
{
InAttributesBalance_ += 1;
if (Config_->AttributesMode != EJsonAttributesMode::Never) {
JsonWriter_->OnBeginMap();
JsonWriter_->OnKeyedItem(TStringBuf("$attributes"));
JsonWriter_->OnBeginMap();
}
}
void TJsonConsumer::OnEndAttributes()
{
InAttributesBalance_ -= 1;
if (Config_->AttributesMode != EJsonAttributesMode::Never) {
JsonWriter_->OnEndMap();
HasAttributes_ = true;
}
}
void TJsonConsumer::Flush()
{
JsonWriter_->Flush();
}
void TJsonConsumer::WriteStringScalar(TStringBuf value)
{
JsonWriter_->OnStringScalar(Utf8Transcoder_.Encode(value));
}
void TJsonConsumer::WriteStringScalarWithAttributes(
TStringBuf value,
TStringBuf type,
bool incomplete)
{
if (IsWriteAllowed()) {
if (Config_->AttributesMode != EJsonAttributesMode::Never) {
if (incomplete) {
if (!HasAttributes_) {
JsonWriter_->OnBeginMap();
HasAttributes_ = true;
}
JsonWriter_->OnKeyedItem(TStringBuf("$incomplete"));
JsonWriter_->OnBooleanScalar(true);
}
if (Config_->AnnotateWithTypes) {
if (!HasAttributes_) {
JsonWriter_->OnBeginMap();
HasAttributes_ = true;
}
JsonWriter_->OnKeyedItem(TStringBuf("$type"));
JsonWriter_->OnStringScalar(type);
}
}
EnterNode();
WriteStringScalar(value);
LeaveNode();
}
}
void TJsonConsumer::SetAnnotateWithTypesParameter(bool value)
{
Config_->AnnotateWithTypes = value;
}
void TJsonConsumer::OnStringScalarWeightLimited(TStringBuf value, std::optional<i64> weightLimit)
{
TStringBuf writeValue = value;
bool incomplete = false;
if (CheckLimit_ && weightLimit && std::ssize(value) > *weightLimit) {
writeValue = value.substr(0, *weightLimit);
incomplete = true;
}
WriteStringScalarWithAttributes(writeValue, TStringBuf("string"), incomplete);
}
void TJsonConsumer::OnNodeWeightLimited(TStringBuf yson, std::optional<i64> weightLimit)
{
if (CheckLimit_ && weightLimit && std::ssize(yson) > *weightLimit) {
WriteStringScalarWithAttributes({}, TStringBuf("any"), true);
return;
}
OnRaw(yson, EYsonType::Node);
}
std::unique_ptr<IJsonConsumer> CreateJsonConsumer(
IOutputStream* output,
EYsonType type,
TJsonFormatConfigPtr config)
{
auto jsonWriter = CreateJsonWriter(
output,
/*pretty*/ config->Format == EJsonFormat::Pretty);
return std::make_unique<TJsonConsumer>(std::move(jsonWriter), type, std::move(config));
}
std::unique_ptr<IJsonConsumer> CreateJsonConsumer(
IJsonWriter* jsonWriter,
EYsonType type,
TJsonFormatConfigPtr config)
{
return std::make_unique<TJsonConsumer>(jsonWriter, type, std::move(config));
}
std::unique_ptr<IJsonWriter> CreateJsonWriter(
IOutputStream* output,
bool pretty)
{
return std::make_unique<TJsonWriter>(output, pretty);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NJson
|