config.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // Sort keys in maps before serialization.
  89. bool SortMapKeys = false;
  90. TSelf& SetDoubleNDigits(ui32 ndigits) {
  91. DoubleNDigits = ndigits;
  92. return *this;
  93. }
  94. TSelf& SetFloatNDigits(ui32 ndigits) {
  95. FloatNDigits = ndigits;
  96. return *this;
  97. }
  98. TSelf& SetFloatToStringMode(EFloatToStringMode mode) {
  99. FloatToStringMode = mode;
  100. return *this;
  101. }
  102. TSelf& SetFormatOutput(bool format) {
  103. FormatOutput = format;
  104. return *this;
  105. }
  106. TSelf& SetMissingSingleKeyMode(MissingKeyMode mode) {
  107. MissingSingleKeyMode = mode;
  108. return *this;
  109. }
  110. TSelf& SetMissingRepeatedKeyMode(MissingKeyMode mode) {
  111. MissingRepeatedKeyMode = mode;
  112. return *this;
  113. }
  114. TSelf& SetAddMissingFields(bool add) {
  115. AddMissingFields = add;
  116. return *this;
  117. }
  118. TSelf& SetEnumMode(EnumValueMode mode) {
  119. Y_ENSURE(!UseJsonEnumValue || mode == EnumNumber, "EnumValueMode and UseJsonEnumValue are mutually exclusive");
  120. EnumMode = mode;
  121. return *this;
  122. }
  123. TSelf& SetFieldNameMode(FldNameMode mode) {
  124. Y_ENSURE(mode == FieldNameOriginalCase || !UseJsonName, "FieldNameMode and UseJsonName are mutually exclusive");
  125. FieldNameMode = mode;
  126. return *this;
  127. }
  128. TSelf& SetUseJsonName(bool jsonName) {
  129. Y_ENSURE(!jsonName || FieldNameMode == FieldNameOriginalCase, "FieldNameMode and UseJsonName are mutually exclusive");
  130. UseJsonName = jsonName;
  131. return *this;
  132. }
  133. TSelf& SetUseJsonEnumValue(bool jsonEnumValue) {
  134. Y_ENSURE(!jsonEnumValue || EnumMode == EnumNumber, "EnumValueMode and UseJsonEnumValue are mutually exclusive");
  135. UseJsonEnumValue = jsonEnumValue;
  136. return *this;
  137. }
  138. TSelf& SetConvertTimeAsString(bool value) {
  139. ConvertTimeAsString = value;
  140. return *this;
  141. }
  142. TSelf& SetExtensionFieldNameMode(ExtFldNameMode mode) {
  143. ExtensionFieldNameMode = mode;
  144. return *this;
  145. }
  146. TSelf& AddStringTransform(TStringTransformPtr transform) {
  147. StringTransforms.push_back(transform);
  148. return *this;
  149. }
  150. TSelf& SetMapAsObject(bool value) {
  151. MapAsObject = value;
  152. return *this;
  153. }
  154. TSelf& SetSortMapKeys(bool value) {
  155. SortMapKeys = value;
  156. return *this;
  157. }
  158. TSelf& SetStringifyNumbers(EStringifyNumbersMode stringify) {
  159. StringifyNumbers = stringify;
  160. return *this;
  161. }
  162. TSelf& SetStringifyNumbersRepeated(EStringifyNumbersMode stringify) {
  163. StringifyNumbersRepeated = stringify;
  164. return *this;
  165. }
  166. TSelf& SetNameGenerator(TNameGenerator callback) {
  167. NameGenerator = callback;
  168. return *this;
  169. }
  170. TSelf& SetEnumValueGenerator(TEnumValueGenerator callback) {
  171. EnumValueGenerator = callback;
  172. return *this;
  173. }
  174. TSelf& SetWriteNanAsString(bool value) {
  175. WriteNanAsString = value;
  176. return *this;
  177. }
  178. TSelf& SetConvertAny(bool value) {
  179. ConvertAny = value;
  180. return *this;
  181. }
  182. };
  183. }