config.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #pragma once
  2. #include "string_transform.h"
  3. #include "name_generator.h"
  4. #include <util/generic/vector.h>
  5. #include <util/generic/yexception.h>
  6. #include <util/string/cast.h>
  7. #include <library/cpp/json/json_writer.h>
  8. #include <functional>
  9. namespace NProtobufJson {
  10. struct TProto2JsonConfig {
  11. using TSelf = TProto2JsonConfig;
  12. ui32 DoubleNDigits = NJson::TJsonWriterConfig::DefaultDoubleNDigits;
  13. ui32 FloatNDigits = NJson::TJsonWriterConfig::DefaultFloatNDigits;
  14. EFloatToStringMode FloatToStringMode = NJson::TJsonWriterConfig::DefaultFloatToStringMode;
  15. bool FormatOutput = false;
  16. enum MissingKeyMode {
  17. // Skip missing keys
  18. MissingKeySkip = 0,
  19. // Fill missing keys with json null value.
  20. MissingKeyNull,
  21. // Use default value in any case.
  22. // If default value is not explicitly defined, use default type value:
  23. // i.e. 0 for integers, "" for strings
  24. // For repeated keys, means []
  25. MissingKeyDefault,
  26. // Use default value if it is explicitly specified for optional fields.
  27. // Skip if no explicitly defined default value for optional fields.
  28. // Throw exception if required field is empty.
  29. // For repeated keys, same as MissingKeySkip
  30. MissingKeyExplicitDefaultThrowRequired
  31. };
  32. MissingKeyMode MissingSingleKeyMode = MissingKeySkip;
  33. MissingKeyMode MissingRepeatedKeyMode = MissingKeySkip;
  34. /// Add null value for missing fields (false by default).
  35. bool AddMissingFields = false;
  36. enum EnumValueMode {
  37. EnumNumber = 0, // default
  38. EnumName,
  39. EnumFullName,
  40. EnumNameLowerCase,
  41. EnumFullNameLowerCase,
  42. };
  43. EnumValueMode EnumMode = EnumNumber;
  44. enum FldNameMode {
  45. FieldNameOriginalCase = 0, // default
  46. FieldNameLowerCase,
  47. FieldNameUpperCase,
  48. FieldNameCamelCase,
  49. FieldNameSnakeCase, // ABC -> a_b_c, UserID -> user_i_d
  50. FieldNameSnakeCaseDense // ABC -> abc, UserID -> user_id
  51. };
  52. FldNameMode FieldNameMode = FieldNameOriginalCase;
  53. enum ExtFldNameMode {
  54. ExtFldNameFull = 0, // default, field.full_name()
  55. ExtFldNameShort // field.name()
  56. };
  57. ExtFldNameMode ExtensionFieldNameMode = ExtFldNameFull;
  58. /// Use 'json_name' protobuf option for field name, mutually exclusive
  59. /// with FieldNameMode.
  60. bool UseJsonName = false;
  61. /// Use 'json_enum_value' protobuf option for enum value, mutually exclusive
  62. /// with EnumValueMode
  63. bool UseJsonEnumValue = false;
  64. // Allow nonstandard conversions, e.g. from google.protobuf.Duration to string
  65. bool ConvertTimeAsString = false;
  66. /// Transforms will be applied only to string values (== protobuf fields of string / bytes type).
  67. /// yajl_encode_string will be used if no transforms are specified.
  68. TVector<TStringTransformPtr> StringTransforms;
  69. /// Print map as object, otherwise print it as array of key/value objects
  70. bool MapAsObject = false;
  71. enum EStringifyNumbersMode {
  72. StringifyLongNumbersNever = 0, // default
  73. StringifyLongNumbersForFloat,
  74. StringifyLongNumbersForDouble,
  75. StringifyInt64Always,
  76. };
  77. /// Stringify long integers which are not exactly representable by float or double values. Not affect repeated numbers, for repeated use StringifyNumbersRepeated
  78. EStringifyNumbersMode StringifyNumbers = StringifyLongNumbersNever;
  79. /// Stringify repeated long integers which are not exactly representable by float or double values. May cause heterogenous arrays, use StringifyInt64Always or StringifyLongNumbersNever to avoid
  80. EStringifyNumbersMode StringifyNumbersRepeated = StringifyLongNumbersNever;
  81. /// Decode Any fields content
  82. bool ConvertAny = false;
  83. /// Custom field names generator.
  84. TNameGenerator NameGenerator = {};
  85. /// Custom enum values generator.
  86. TEnumValueGenerator EnumValueGenerator = {};
  87. bool WriteNanAsString = false;
  88. TSelf& SetDoubleNDigits(ui32 ndigits) {
  89. DoubleNDigits = ndigits;
  90. return *this;
  91. }
  92. TSelf& SetFloatNDigits(ui32 ndigits) {
  93. FloatNDigits = ndigits;
  94. return *this;
  95. }
  96. TSelf& SetFloatToStringMode(EFloatToStringMode mode) {
  97. FloatToStringMode = mode;
  98. return *this;
  99. }
  100. TSelf& SetFormatOutput(bool format) {
  101. FormatOutput = format;
  102. return *this;
  103. }
  104. TSelf& SetMissingSingleKeyMode(MissingKeyMode mode) {
  105. MissingSingleKeyMode = mode;
  106. return *this;
  107. }
  108. TSelf& SetMissingRepeatedKeyMode(MissingKeyMode mode) {
  109. MissingRepeatedKeyMode = mode;
  110. return *this;
  111. }
  112. TSelf& SetAddMissingFields(bool add) {
  113. AddMissingFields = add;
  114. return *this;
  115. }
  116. TSelf& SetEnumMode(EnumValueMode mode) {
  117. Y_ENSURE(!UseJsonEnumValue || mode == EnumNumber, "EnumValueMode and UseJsonEnumValue are mutually exclusive");
  118. EnumMode = mode;
  119. return *this;
  120. }
  121. TSelf& SetFieldNameMode(FldNameMode mode) {
  122. Y_ENSURE(mode == FieldNameOriginalCase || !UseJsonName, "FieldNameMode and UseJsonName are mutually exclusive");
  123. FieldNameMode = mode;
  124. return *this;
  125. }
  126. TSelf& SetUseJsonName(bool jsonName) {
  127. Y_ENSURE(!jsonName || FieldNameMode == FieldNameOriginalCase, "FieldNameMode and UseJsonName are mutually exclusive");
  128. UseJsonName = jsonName;
  129. return *this;
  130. }
  131. TSelf& SetUseJsonEnumValue(bool jsonEnumValue) {
  132. Y_ENSURE(!jsonEnumValue || EnumMode == EnumNumber, "EnumValueMode and UseJsonEnumValue are mutually exclusive");
  133. UseJsonEnumValue = jsonEnumValue;
  134. return *this;
  135. }
  136. TSelf& SetConvertTimeAsString(bool value) {
  137. ConvertTimeAsString = value;
  138. return *this;
  139. }
  140. TSelf& SetExtensionFieldNameMode(ExtFldNameMode mode) {
  141. ExtensionFieldNameMode = mode;
  142. return *this;
  143. }
  144. TSelf& AddStringTransform(TStringTransformPtr transform) {
  145. StringTransforms.push_back(transform);
  146. return *this;
  147. }
  148. TSelf& SetMapAsObject(bool value) {
  149. MapAsObject = value;
  150. return *this;
  151. }
  152. TSelf& SetStringifyNumbers(EStringifyNumbersMode stringify) {
  153. StringifyNumbers = stringify;
  154. return *this;
  155. }
  156. TSelf& SetStringifyNumbersRepeated(EStringifyNumbersMode stringify) {
  157. StringifyNumbersRepeated = stringify;
  158. return *this;
  159. }
  160. TSelf& SetNameGenerator(TNameGenerator callback) {
  161. NameGenerator = callback;
  162. return *this;
  163. }
  164. TSelf& SetEnumValueGenerator(TEnumValueGenerator callback) {
  165. EnumValueGenerator = callback;
  166. return *this;
  167. }
  168. TSelf& SetWriteNanAsString(bool value) {
  169. WriteNanAsString = value;
  170. return *this;
  171. }
  172. TSelf& SetConvertAny(bool value) {
  173. ConvertAny = value;
  174. return *this;
  175. }
  176. };
  177. }