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
|
#pragma once
#include "string_transform.h"
#include "name_generator.h"
#include <util/generic/vector.h>
#include <util/generic/yexception.h>
#include <util/string/cast.h>
#include <library/cpp/json/json_writer.h>
#include <functional>
namespace NProtobufJson {
struct TProto2JsonConfig {
using TSelf = TProto2JsonConfig;
ui32 DoubleNDigits = NJson::TJsonWriterConfig::DefaultDoubleNDigits;
ui32 FloatNDigits = NJson::TJsonWriterConfig::DefaultFloatNDigits;
EFloatToStringMode FloatToStringMode = NJson::TJsonWriterConfig::DefaultFloatToStringMode;
bool FormatOutput = false;
enum MissingKeyMode {
// Skip missing keys
MissingKeySkip = 0,
// Fill missing keys with json null value.
MissingKeyNull,
// Use default value in any case.
// If default value is not explicitly defined, use default type value:
// i.e. 0 for integers, "" for strings
// For repeated keys, means []
MissingKeyDefault,
// Use default value if it is explicitly specified for optional fields.
// Skip if no explicitly defined default value for optional fields.
// Throw exception if required field is empty.
// For repeated keys, same as MissingKeySkip
MissingKeyExplicitDefaultThrowRequired
};
MissingKeyMode MissingSingleKeyMode = MissingKeySkip;
MissingKeyMode MissingRepeatedKeyMode = MissingKeySkip;
/// Add null value for missing fields (false by default).
bool AddMissingFields = false;
enum EnumValueMode {
EnumNumber = 0, // default
EnumName,
EnumFullName,
EnumNameLowerCase,
EnumFullNameLowerCase,
};
EnumValueMode EnumMode = EnumNumber;
enum FldNameMode {
FieldNameOriginalCase = 0, // default
FieldNameLowerCase,
FieldNameUpperCase,
FieldNameCamelCase,
FieldNameSnakeCase, // ABC -> a_b_c, UserID -> user_i_d
FieldNameSnakeCaseDense // ABC -> abc, UserID -> user_id
};
FldNameMode FieldNameMode = FieldNameOriginalCase;
enum ExtFldNameMode {
ExtFldNameFull = 0, // default, field.full_name()
ExtFldNameShort // field.name()
};
ExtFldNameMode ExtensionFieldNameMode = ExtFldNameFull;
/// Use 'json_name' protobuf option for field name, mutually exclusive
/// with FieldNameMode.
bool UseJsonName = false;
// Allow nonstandard conversions, e.g. from google.protobuf.Duration to string
bool ConvertTimeAsString = false;
/// Transforms will be applied only to string values (== protobuf fields of string / bytes type).
/// yajl_encode_string will be used if no transforms are specified.
TVector<TStringTransformPtr> StringTransforms;
/// Print map as object, otherwise print it as array of key/value objects
bool MapAsObject = false;
/// Stringify long integers which are not exactly representable by float or double values
enum EStringifyNumbersMode {
StringifyLongNumbersNever = 0, // default
StringifyLongNumbersForFloat,
StringifyLongNumbersForDouble,
StringifyInt64Always,
};
EStringifyNumbersMode StringifyNumbers = StringifyLongNumbersNever;
/// Custom field names generator.
TNameGenerator NameGenerator = {};
/// Custom enum values generator.
TEnumValueGenerator EnumValueGenerator = {};
bool WriteNanAsString = false;
TSelf& SetDoubleNDigits(ui32 ndigits) {
DoubleNDigits = ndigits;
return *this;
}
TSelf& SetFloatNDigits(ui32 ndigits) {
FloatNDigits = ndigits;
return *this;
}
TSelf& SetFloatToStringMode(EFloatToStringMode mode) {
FloatToStringMode = mode;
return *this;
}
TSelf& SetFormatOutput(bool format) {
FormatOutput = format;
return *this;
}
TSelf& SetMissingSingleKeyMode(MissingKeyMode mode) {
MissingSingleKeyMode = mode;
return *this;
}
TSelf& SetMissingRepeatedKeyMode(MissingKeyMode mode) {
MissingRepeatedKeyMode = mode;
return *this;
}
TSelf& SetAddMissingFields(bool add) {
AddMissingFields = add;
return *this;
}
TSelf& SetEnumMode(EnumValueMode mode) {
EnumMode = mode;
return *this;
}
TSelf& SetFieldNameMode(FldNameMode mode) {
Y_ENSURE(mode == FieldNameOriginalCase || !UseJsonName, "FieldNameMode and UseJsonName are mutually exclusive");
FieldNameMode = mode;
return *this;
}
TSelf& SetUseJsonName(bool jsonName) {
Y_ENSURE(!jsonName || FieldNameMode == FieldNameOriginalCase, "FieldNameMode and UseJsonName are mutually exclusive");
UseJsonName = jsonName;
return *this;
}
TSelf& SetConvertTimeAsString(bool value) {
ConvertTimeAsString = value;
return *this;
}
TSelf& SetExtensionFieldNameMode(ExtFldNameMode mode) {
ExtensionFieldNameMode = mode;
return *this;
}
TSelf& AddStringTransform(TStringTransformPtr transform) {
StringTransforms.push_back(transform);
return *this;
}
TSelf& SetMapAsObject(bool value) {
MapAsObject = value;
return *this;
}
TSelf& SetStringifyNumbers(EStringifyNumbersMode stringify) {
StringifyNumbers = stringify;
return *this;
}
TSelf& SetNameGenerator(TNameGenerator callback) {
NameGenerator = callback;
return *this;
}
TSelf& SetEnumValueGenerator(TEnumValueGenerator callback) {
EnumValueGenerator = callback;
return *this;
}
TSelf& SetWriteNanAsString(bool value) {
WriteNanAsString = value;
return *this;
}
};
}
|