config.h 6.7 KB

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