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
|
#include "proto2json_printer.h"
#include "config.h"
#include "util.h"
#include <google/protobuf/util/time_util.h>
#include <library/cpp/protobuf/json/proto/enum_options.pb.h>
#include <util/generic/yexception.h>
#include <util/string/ascii.h>
#include <util/string/cast.h>
#include <util/system/win_undef.h>
namespace NProtobufJson {
using namespace NProtoBuf;
class TJsonKeyBuilder {
public:
TJsonKeyBuilder(const FieldDescriptor& field, const TProto2JsonConfig& config, TString& tmpBuf)
: NewKeyStr(tmpBuf)
{
if (config.NameGenerator) {
NewKeyStr = config.NameGenerator(field);
NewKeyBuf = NewKeyStr;
return;
}
if (config.UseJsonName) {
Y_ASSERT(!field.json_name().empty());
NewKeyStr = field.json_name();
if (!field.has_json_name() && !NewKeyStr.empty()) {
// FIXME: https://st.yandex-team.ru/CONTRIB-139
NewKeyStr[0] = AsciiToLower(NewKeyStr[0]);
}
NewKeyBuf = NewKeyStr;
return;
}
switch (config.FieldNameMode) {
case TProto2JsonConfig::FieldNameOriginalCase: {
NewKeyBuf = field.name();
break;
}
case TProto2JsonConfig::FieldNameLowerCase: {
NewKeyStr = field.name();
NewKeyStr.to_lower();
NewKeyBuf = NewKeyStr;
break;
}
case TProto2JsonConfig::FieldNameUpperCase: {
NewKeyStr = field.name();
NewKeyStr.to_upper();
NewKeyBuf = NewKeyStr;
break;
}
case TProto2JsonConfig::FieldNameCamelCase: {
NewKeyStr = field.name();
if (!NewKeyStr.empty()) {
NewKeyStr[0] = AsciiToLower(NewKeyStr[0]);
}
NewKeyBuf = NewKeyStr;
break;
}
case TProto2JsonConfig::FieldNameSnakeCase: {
NewKeyStr = field.name();
ToSnakeCase(&NewKeyStr);
NewKeyBuf = NewKeyStr;
break;
}
case TProto2JsonConfig::FieldNameSnakeCaseDense: {
NewKeyStr = field.name();
ToSnakeCaseDense(&NewKeyStr);
NewKeyBuf = NewKeyStr;
break;
}
default:
Y_DEBUG_ABORT_UNLESS(false, "Unknown FieldNameMode.");
}
}
const TStringBuf& GetKey() const {
return NewKeyBuf;
}
private:
TStringBuf NewKeyBuf;
TString& NewKeyStr;
};
TProto2JsonPrinter::TProto2JsonPrinter(const TProto2JsonConfig& cfg)
: Config(cfg)
{
}
TProto2JsonPrinter::~TProto2JsonPrinter() {
}
TStringBuf TProto2JsonPrinter::MakeKey(const FieldDescriptor& field) {
return TJsonKeyBuilder(field, GetConfig(), TmpBuf).GetKey();
}
template <bool InMapContext, typename T>
std::enable_if_t<InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& json, const TStringBuf& key, const T& value) {
json.WriteKey(key).Write(value);
}
template <bool InMapContext, typename T>
std::enable_if_t<!InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& array, const TStringBuf& key, const T& value) {
Y_ASSERT(!key);
array.Write(value);
}
template <bool InMapContext>
Y_NO_INLINE void TProto2JsonPrinter::PrintStringValue(const FieldDescriptor& field,
const TStringBuf& key, const TString& value,
IJsonOutput& json) {
if (!GetConfig().StringTransforms.empty()) {
TString tmpBuf = value;
for (const TStringTransformPtr& stringTransform : GetConfig().StringTransforms) {
Y_ASSERT(stringTransform);
if (stringTransform) {
if (field.type() == FieldDescriptor::TYPE_BYTES)
stringTransform->TransformBytes(tmpBuf);
else
stringTransform->Transform(tmpBuf);
}
}
WriteWithMaybeEmptyKey<InMapContext>(json, key, tmpBuf);
} else {
WriteWithMaybeEmptyKey<InMapContext>(json, key, value);
}
}
template <bool InMapContext>
void TProto2JsonPrinter::PrintEnumValue(const TStringBuf& key,
const EnumValueDescriptor* value,
IJsonOutput& json) {
if (Config.EnumValueGenerator) {
WriteWithMaybeEmptyKey<InMapContext>(json, key, Config.EnumValueGenerator(*value));
return;
}
if (Config.UseJsonEnumValue) {
auto jsonEnumValue = value->options().GetExtension(json_enum_value);
if (!jsonEnumValue) {
ythrow yexception() << "Trying to using json enum value for field " << value->name() << " which is not set.";
}
WriteWithMaybeEmptyKey<InMapContext>(json, key, jsonEnumValue);
return;
}
switch (GetConfig().EnumMode) {
case TProto2JsonConfig::EnumNumber: {
WriteWithMaybeEmptyKey<InMapContext>(json, key, value->number());
break;
}
case TProto2JsonConfig::EnumName: {
WriteWithMaybeEmptyKey<InMapContext>(json, key, value->name());
break;
}
case TProto2JsonConfig::EnumFullName: {
WriteWithMaybeEmptyKey<InMapContext>(json, key, value->full_name());
break;
}
case TProto2JsonConfig::EnumNameLowerCase: {
TString newName = value->name();
newName.to_lower();
WriteWithMaybeEmptyKey<InMapContext>(json, key, newName);
break;
}
case TProto2JsonConfig::EnumFullNameLowerCase: {
TString newName = value->full_name();
newName.to_lower();
WriteWithMaybeEmptyKey<InMapContext>(json, key, newName);
break;
}
default:
Y_DEBUG_ABORT_UNLESS(false, "Unknown EnumMode.");
}
}
bool HandleTimeConversion(const Message& proto, IJsonOutput& json) {
using namespace google::protobuf;
auto type = proto.GetDescriptor()->well_known_type();
// XXX static_cast will cause UB if used with dynamic messages
// (can be created by a DynamicMessageFactory with SetDelegateToGeneratedFactory(false). Unlikely, but still possible).
// See workaround with CopyFrom in JsonString2Duration, JsonString2Timestamp (json2proto.cpp)
if (type == Descriptor::WellKnownType::WELLKNOWNTYPE_DURATION) {
const auto& duration = static_cast<const Duration&>(proto);
json.Write(util::TimeUtil::ToString(duration));
return true;
} else if (type == Descriptor::WellKnownType::WELLKNOWNTYPE_TIMESTAMP) {
const auto& timestamp = static_cast<const Timestamp&>(proto);
json.Write(util::TimeUtil::ToString(timestamp));
return true;
}
return false;
}
void TProto2JsonPrinter::PrintSingleField(const Message& proto,
const FieldDescriptor& field,
IJsonOutput& json,
TStringBuf key,
bool inProtoMap) {
Y_ABORT_UNLESS(!field.is_repeated(), "field is repeated.");
if (!key) {
key = MakeKey(field);
}
#define FIELD_TO_JSON(EProtoCppType, ProtoGet) \
case FieldDescriptor::EProtoCppType: { \
json.WriteKey(key).Write(reflection->ProtoGet(proto, &field)); \
break; \
}
#define INT_FIELD_TO_JSON(EProtoCppType, ProtoGet) \
case FieldDescriptor::EProtoCppType: { \
const auto value = reflection->ProtoGet(proto, &field); \
if (NeedStringifyNumber(value)) { \
json.WriteKey(key).Write(ToString(value)); \
} else { \
json.WriteKey(key).Write(value); \
} \
break; \
}
const Reflection* reflection = proto.GetReflection();
bool shouldPrintField = inProtoMap || reflection->HasField(proto, &field);
if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) {
if (field.has_default_value()) {
shouldPrintField = true;
} else if (field.is_required()) {
ythrow yexception() << "Empty required protobuf field: "
<< field.full_name() << ".";
}
}
shouldPrintField = shouldPrintField ||
(GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyDefault && !field.containing_oneof());
if (shouldPrintField) {
switch (field.cpp_type()) {
INT_FIELD_TO_JSON(CPPTYPE_INT32, GetInt32);
INT_FIELD_TO_JSON(CPPTYPE_INT64, GetInt64);
INT_FIELD_TO_JSON(CPPTYPE_UINT32, GetUInt32);
INT_FIELD_TO_JSON(CPPTYPE_UINT64, GetUInt64);
FIELD_TO_JSON(CPPTYPE_DOUBLE, GetDouble);
FIELD_TO_JSON(CPPTYPE_FLOAT, GetFloat);
FIELD_TO_JSON(CPPTYPE_BOOL, GetBool);
case FieldDescriptor::CPPTYPE_MESSAGE: {
json.WriteKey(key);
if (Config.ConvertTimeAsString && HandleTimeConversion(reflection->GetMessage(proto, &field), json)) {
break;
}
Print(reflection->GetMessage(proto, &field), json);
break;
}
case FieldDescriptor::CPPTYPE_ENUM: {
PrintEnumValue<true>(key, reflection->GetEnum(proto, &field), json);
break;
}
case FieldDescriptor::CPPTYPE_STRING: {
TString scratch;
const TString& value = reflection->GetStringReference(proto, &field, &scratch);
PrintStringValue<true>(field, key, value, json);
break;
}
default:
ythrow yexception() << "Unknown protobuf field type: "
<< static_cast<int>(field.cpp_type()) << ".";
}
} else {
switch (GetConfig().MissingSingleKeyMode) {
case TProto2JsonConfig::MissingKeyNull: {
json.WriteKey(key).WriteNull();
break;
}
case TProto2JsonConfig::MissingKeySkip:
case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
default:
break;
}
}
#undef FIELD_TO_JSON
}
void TProto2JsonPrinter::PrintRepeatedField(const Message& proto,
const FieldDescriptor& field,
IJsonOutput& json,
TStringBuf key) {
Y_ABORT_UNLESS(field.is_repeated(), "field isn't repeated.");
const bool isMap = field.is_map() && GetConfig().MapAsObject;
if (!key) {
key = MakeKey(field);
}
#define REPEATED_FIELD_TO_JSON(EProtoCppType, ProtoGet) \
case FieldDescriptor::EProtoCppType: { \
for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) \
json.Write(reflection->ProtoGet(proto, &field, i)); \
break; \
}
const Reflection* reflection = proto.GetReflection();
if (reflection->FieldSize(proto, &field) > 0) {
json.WriteKey(key);
if (isMap) {
json.BeginObject();
} else {
json.BeginList();
}
switch (field.cpp_type()) {
REPEATED_FIELD_TO_JSON(CPPTYPE_INT32, GetRepeatedInt32);
REPEATED_FIELD_TO_JSON(CPPTYPE_INT64, GetRepeatedInt64);
REPEATED_FIELD_TO_JSON(CPPTYPE_UINT32, GetRepeatedUInt32);
REPEATED_FIELD_TO_JSON(CPPTYPE_UINT64, GetRepeatedUInt64);
REPEATED_FIELD_TO_JSON(CPPTYPE_DOUBLE, GetRepeatedDouble);
REPEATED_FIELD_TO_JSON(CPPTYPE_FLOAT, GetRepeatedFloat);
REPEATED_FIELD_TO_JSON(CPPTYPE_BOOL, GetRepeatedBool);
case FieldDescriptor::CPPTYPE_MESSAGE: {
if (isMap) {
for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {
PrintKeyValue(reflection->GetRepeatedMessage(proto, &field, i), json);
}
} else {
for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {
Print(reflection->GetRepeatedMessage(proto, &field, i), json);
}
}
break;
}
case FieldDescriptor::CPPTYPE_ENUM: {
for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i)
PrintEnumValue<false>(TStringBuf(), reflection->GetRepeatedEnum(proto, &field, i), json);
break;
}
case FieldDescriptor::CPPTYPE_STRING: {
TString scratch;
for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {
const TString& value =
reflection->GetRepeatedStringReference(proto, &field, i, &scratch);
PrintStringValue<false>(field, TStringBuf(), value, json);
}
break;
}
default:
ythrow yexception() << "Unknown protobuf field type: "
<< static_cast<int>(field.cpp_type()) << ".";
}
if (isMap) {
json.EndObject();
} else {
json.EndList();
}
} else {
switch (GetConfig().MissingRepeatedKeyMode) {
case TProto2JsonConfig::MissingKeyNull: {
json.WriteKey(key).WriteNull();
break;
}
case TProto2JsonConfig::MissingKeyDefault: {
json.WriteKey(key);
if (isMap) {
json.BeginObject().EndObject();
} else {
json.BeginList().EndList();
}
break;
}
case TProto2JsonConfig::MissingKeySkip:
case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
default:
break;
}
}
#undef REPEATED_FIELD_TO_JSON
}
void TProto2JsonPrinter::PrintKeyValue(const NProtoBuf::Message& proto,
IJsonOutput& json) {
const FieldDescriptor* keyField = proto.GetDescriptor()->FindFieldByName("key");
Y_ABORT_UNLESS(keyField, "Map entry key field not found.");
TString key = MakeKey(proto, *keyField);
const FieldDescriptor* valueField = proto.GetDescriptor()->FindFieldByName("value");
Y_ABORT_UNLESS(valueField, "Map entry value field not found.");
PrintSingleField(proto, *valueField, json, key, true);
}
TString TProto2JsonPrinter::MakeKey(const NProtoBuf::Message& proto,
const NProtoBuf::FieldDescriptor& field) {
const Reflection* reflection = proto.GetReflection();
TString result;
switch (field.cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
result = ToString(reflection->GetInt32(proto, &field));
break;
case FieldDescriptor::CPPTYPE_INT64:
result = ToString(reflection->GetInt64(proto, &field));
break;
case FieldDescriptor::CPPTYPE_UINT32:
result = ToString(reflection->GetUInt32(proto, &field));
break;
case FieldDescriptor::CPPTYPE_UINT64:
result = ToString(reflection->GetUInt64(proto, &field));
break;
case FieldDescriptor::CPPTYPE_DOUBLE:
result = ToString(reflection->GetDouble(proto, &field));
break;
case FieldDescriptor::CPPTYPE_FLOAT:
result = ToString(reflection->GetFloat(proto, &field));
break;
case FieldDescriptor::CPPTYPE_BOOL:
result = ToString(reflection->GetBool(proto, &field));
break;
case FieldDescriptor::CPPTYPE_ENUM: {
const EnumValueDescriptor* value = reflection->GetEnum(proto, &field);
switch (GetConfig().EnumMode) {
case TProto2JsonConfig::EnumNumber:
result = ToString(value->number());
break;
case TProto2JsonConfig::EnumName:
result = value->name();
break;
case TProto2JsonConfig::EnumFullName:
result = value->full_name();
break;
case TProto2JsonConfig::EnumNameLowerCase:
result = value->name();
result.to_lower();
break;
case TProto2JsonConfig::EnumFullNameLowerCase:
result = value->full_name();
result.to_lower();
break;
default:
ythrow yexception() << "Unsupported enum mode.";
}
break;
}
case FieldDescriptor::CPPTYPE_STRING:
result = reflection->GetString(proto, &field);
break;
default:
ythrow yexception() << "Unsupported key type.";
}
return result;
}
void TProto2JsonPrinter::PrintField(const Message& proto,
const FieldDescriptor& field,
IJsonOutput& json,
const TStringBuf key) {
if (field.is_repeated())
PrintRepeatedField(proto, field, json, key);
else
PrintSingleField(proto, field, json, key);
}
void TProto2JsonPrinter::Print(const Message& proto, IJsonOutput& json, bool closeMap) {
const Descriptor* descriptor = proto.GetDescriptor();
Y_ASSERT(descriptor);
json.BeginObject();
// Iterate over all non-extension fields
for (int f = 0, endF = descriptor->field_count(); f < endF; ++f) {
const FieldDescriptor* field = descriptor->field(f);
Y_ASSERT(field);
PrintField(proto, *field, json);
}
// Check extensions via ListFields
std::vector<const FieldDescriptor*> fields;
auto* ref = proto.GetReflection();
ref->ListFields(proto, &fields);
for (const FieldDescriptor* field : fields) {
Y_ASSERT(field);
if (field->is_extension()) {
switch (GetConfig().ExtensionFieldNameMode) {
case TProto2JsonConfig::ExtFldNameFull:
PrintField(proto, *field, json, field->full_name());
break;
case TProto2JsonConfig::ExtFldNameShort:
PrintField(proto, *field, json);
break;
}
}
}
if (closeMap) {
json.EndObject();
}
}
template <class T, class U>
std::enable_if_t<!std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) {
return value > -range && value < range;
}
template <class T, class U>
std::enable_if_t<std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) {
return value < (std::make_unsigned_t<U>)(range);
}
template <class T>
bool TProto2JsonPrinter::NeedStringifyNumber(T value) const {
constexpr long SAFE_INTEGER_RANGE_FLOAT = 1L << 24;
constexpr long long SAFE_INTEGER_RANGE_DOUBLE = 1LL << 53;
switch (GetConfig().StringifyNumbers) {
case TProto2JsonConfig::StringifyLongNumbersNever:
return false;
case TProto2JsonConfig::StringifyLongNumbersForFloat:
return !ValueInRange(value, SAFE_INTEGER_RANGE_FLOAT);
case TProto2JsonConfig::StringifyLongNumbersForDouble:
return !ValueInRange(value, SAFE_INTEGER_RANGE_DOUBLE);
case TProto2JsonConfig::StringifyInt64Always:
return std::is_same_v<T, i64> || std::is_same_v<T, ui64>;
}
return false;
}
}
|