config.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /// Stringify long integers which are not exactly representable by float or double values
  72. enum EStringifyNumbersMode {
  73. StringifyLongNumbersNever = 0, // default
  74. StringifyLongNumbersForFloat,
  75. StringifyLongNumbersForDouble,
  76. StringifyInt64Always,
  77. };
  78. EStringifyNumbersMode StringifyNumbers = StringifyLongNumbersNever;
  79. /// Decode Any fields content
  80. bool ConvertAny = false;
  81. /// Custom field names generator.
  82. TNameGenerator NameGenerator = {};
  83. /// Custom enum values generator.
  84. TEnumValueGenerator EnumValueGenerator = {};
  85. bool WriteNanAsString = false;
  86. TSelf& SetDoubleNDigits(ui32 ndigits) {
  87. DoubleNDigits = ndigits;
  88. return *this;
  89. }
  90. TSelf& SetFloatNDigits(ui32 ndigits) {
  91. FloatNDigits = ndigits;
  92. return *this;
  93. }
  94. TSelf& SetFloatToStringMode(EFloatToStringMode mode) {
  95. FloatToStringMode = mode;
  96. return *this;
  97. }
  98. TSelf& SetFormatOutput(bool format) {
  99. FormatOutput = format;
  100. return *this;
  101. }
  102. TSelf& SetMissingSingleKeyMode(MissingKeyMode mode) {
  103. MissingSingleKeyMode = mode;
  104. return *this;
  105. }
  106. TSelf& SetMissingRepeatedKeyMode(MissingKeyMode mode) {
  107. MissingRepeatedKeyMode = mode;
  108. return *this;
  109. }
  110. TSelf& SetAddMissingFields(bool add) {
  111. AddMissingFields = add;
  112. return *this;
  113. }
  114. TSelf& SetEnumMode(EnumValueMode mode) {
  115. Y_ENSURE(!UseJsonEnumValue || mode == EnumNumber, "EnumValueMode and UseJsonEnumValue are mutually exclusive");
  116. EnumMode = mode;
  117. return *this;
  118. }
  119. TSelf& SetFieldNameMode(FldNameMode mode) {
  120. Y_ENSURE(mode == FieldNameOriginalCase || !UseJsonName, "FieldNameMode and UseJsonName are mutually exclusive");
  121. FieldNameMode = mode;
  122. return *this;
  123. }
  124. TSelf& SetUseJsonName(bool jsonName) {
  125. Y_ENSURE(!jsonName || FieldNameMode == FieldNameOriginalCase, "FieldNameMode and UseJsonName are mutually exclusive");
  126. UseJsonName = jsonName;
  127. return *this;
  128. }
  129. TSelf& SetUseJsonEnumValue(bool jsonEnumValue) {
  130. Y_ENSURE(!jsonEnumValue || EnumMode == EnumNumber, "EnumValueMode and UseJsonEnumValue are mutually exclusive");
  131. UseJsonEnumValue = jsonEnumValue;
  132. return *this;
  133. }
  134. TSelf& SetConvertTimeAsString(bool value) {
  135. ConvertTimeAsString = value;
  136. return *this;
  137. }
  138. TSelf& SetExtensionFieldNameMode(ExtFldNameMode mode) {
  139. ExtensionFieldNameMode = mode;
  140. return *this;
  141. }
  142. TSelf& AddStringTransform(TStringTransformPtr transform) {
  143. StringTransforms.push_back(transform);
  144. return *this;
  145. }
  146. TSelf& SetMapAsObject(bool value) {
  147. MapAsObject = value;
  148. return *this;
  149. }
  150. TSelf& SetStringifyNumbers(EStringifyNumbersMode stringify) {
  151. StringifyNumbers = stringify;
  152. return *this;
  153. }
  154. TSelf& SetNameGenerator(TNameGenerator callback) {
  155. NameGenerator = callback;
  156. return *this;
  157. }
  158. TSelf& SetEnumValueGenerator(TEnumValueGenerator callback) {
  159. EnumValueGenerator = callback;
  160. return *this;
  161. }
  162. TSelf& SetWriteNanAsString(bool value) {
  163. WriteNanAsString = value;
  164. return *this;
  165. }
  166. TSelf& SetConvertAny(bool value) {
  167. ConvertAny = value;
  168. return *this;
  169. }
  170. };
  171. }