proto2json_printer.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #include "proto2json_printer.h"
  2. #include "config.h"
  3. #include "util.h"
  4. #include <google/protobuf/util/time_util.h>
  5. #include <library/cpp/protobuf/json/proto/enum_options.pb.h>
  6. #include <util/generic/yexception.h>
  7. #include <util/string/ascii.h>
  8. #include <util/string/cast.h>
  9. #include <util/system/win_undef.h>
  10. namespace NProtobufJson {
  11. using namespace NProtoBuf;
  12. class TJsonKeyBuilder {
  13. public:
  14. TJsonKeyBuilder(const FieldDescriptor& field, const TProto2JsonConfig& config, TString& tmpBuf)
  15. : NewKeyStr(tmpBuf)
  16. {
  17. if (config.NameGenerator) {
  18. NewKeyStr = config.NameGenerator(field);
  19. NewKeyBuf = NewKeyStr;
  20. return;
  21. }
  22. if (config.UseJsonName) {
  23. Y_ASSERT(!field.json_name().empty());
  24. NewKeyStr = field.json_name();
  25. if (!field.has_json_name() && !NewKeyStr.empty()) {
  26. // FIXME: https://st.yandex-team.ru/CONTRIB-139
  27. NewKeyStr[0] = AsciiToLower(NewKeyStr[0]);
  28. }
  29. NewKeyBuf = NewKeyStr;
  30. return;
  31. }
  32. switch (config.FieldNameMode) {
  33. case TProto2JsonConfig::FieldNameOriginalCase: {
  34. NewKeyBuf = field.name();
  35. break;
  36. }
  37. case TProto2JsonConfig::FieldNameLowerCase: {
  38. NewKeyStr = field.name();
  39. NewKeyStr.to_lower();
  40. NewKeyBuf = NewKeyStr;
  41. break;
  42. }
  43. case TProto2JsonConfig::FieldNameUpperCase: {
  44. NewKeyStr = field.name();
  45. NewKeyStr.to_upper();
  46. NewKeyBuf = NewKeyStr;
  47. break;
  48. }
  49. case TProto2JsonConfig::FieldNameCamelCase: {
  50. NewKeyStr = field.name();
  51. if (!NewKeyStr.empty()) {
  52. NewKeyStr[0] = AsciiToLower(NewKeyStr[0]);
  53. }
  54. NewKeyBuf = NewKeyStr;
  55. break;
  56. }
  57. case TProto2JsonConfig::FieldNameSnakeCase: {
  58. NewKeyStr = field.name();
  59. ToSnakeCase(&NewKeyStr);
  60. NewKeyBuf = NewKeyStr;
  61. break;
  62. }
  63. case TProto2JsonConfig::FieldNameSnakeCaseDense: {
  64. NewKeyStr = field.name();
  65. ToSnakeCaseDense(&NewKeyStr);
  66. NewKeyBuf = NewKeyStr;
  67. break;
  68. }
  69. default:
  70. Y_DEBUG_ABORT_UNLESS(false, "Unknown FieldNameMode.");
  71. }
  72. }
  73. const TStringBuf& GetKey() const {
  74. return NewKeyBuf;
  75. }
  76. private:
  77. TStringBuf NewKeyBuf;
  78. TString& NewKeyStr;
  79. };
  80. TProto2JsonPrinter::TProto2JsonPrinter(const TProto2JsonConfig& cfg)
  81. : Config(cfg)
  82. {
  83. }
  84. TProto2JsonPrinter::~TProto2JsonPrinter() {
  85. }
  86. TStringBuf TProto2JsonPrinter::MakeKey(const FieldDescriptor& field) {
  87. return TJsonKeyBuilder(field, GetConfig(), TmpBuf).GetKey();
  88. }
  89. template <bool InMapContext, typename T>
  90. std::enable_if_t<InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& json, const TStringBuf& key, const T& value) {
  91. json.WriteKey(key).Write(value);
  92. }
  93. template <bool InMapContext, typename T>
  94. std::enable_if_t<!InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& array, const TStringBuf& key, const T& value) {
  95. Y_ASSERT(!key);
  96. array.Write(value);
  97. }
  98. template <bool InMapContext>
  99. Y_NO_INLINE void TProto2JsonPrinter::PrintStringValue(const FieldDescriptor& field,
  100. const TStringBuf& key, const TString& value,
  101. IJsonOutput& json) {
  102. if (!GetConfig().StringTransforms.empty()) {
  103. TString tmpBuf = value;
  104. for (const TStringTransformPtr& stringTransform : GetConfig().StringTransforms) {
  105. Y_ASSERT(stringTransform);
  106. if (stringTransform) {
  107. if (field.type() == FieldDescriptor::TYPE_BYTES)
  108. stringTransform->TransformBytes(tmpBuf);
  109. else
  110. stringTransform->Transform(tmpBuf);
  111. }
  112. }
  113. WriteWithMaybeEmptyKey<InMapContext>(json, key, tmpBuf);
  114. } else {
  115. WriteWithMaybeEmptyKey<InMapContext>(json, key, value);
  116. }
  117. }
  118. template <bool InMapContext>
  119. void TProto2JsonPrinter::PrintEnumValue(const TStringBuf& key,
  120. const EnumValueDescriptor* value,
  121. IJsonOutput& json) {
  122. if (Config.EnumValueGenerator) {
  123. WriteWithMaybeEmptyKey<InMapContext>(json, key, Config.EnumValueGenerator(*value));
  124. return;
  125. }
  126. if (Config.UseJsonEnumValue) {
  127. auto jsonEnumValue = value->options().GetExtension(json_enum_value);
  128. if (!jsonEnumValue) {
  129. ythrow yexception() << "Trying to using json enum value for field " << value->name() << " which is not set.";
  130. }
  131. WriteWithMaybeEmptyKey<InMapContext>(json, key, jsonEnumValue);
  132. return;
  133. }
  134. switch (GetConfig().EnumMode) {
  135. case TProto2JsonConfig::EnumNumber: {
  136. WriteWithMaybeEmptyKey<InMapContext>(json, key, value->number());
  137. break;
  138. }
  139. case TProto2JsonConfig::EnumName: {
  140. WriteWithMaybeEmptyKey<InMapContext>(json, key, value->name());
  141. break;
  142. }
  143. case TProto2JsonConfig::EnumFullName: {
  144. WriteWithMaybeEmptyKey<InMapContext>(json, key, value->full_name());
  145. break;
  146. }
  147. case TProto2JsonConfig::EnumNameLowerCase: {
  148. TString newName = value->name();
  149. newName.to_lower();
  150. WriteWithMaybeEmptyKey<InMapContext>(json, key, newName);
  151. break;
  152. }
  153. case TProto2JsonConfig::EnumFullNameLowerCase: {
  154. TString newName = value->full_name();
  155. newName.to_lower();
  156. WriteWithMaybeEmptyKey<InMapContext>(json, key, newName);
  157. break;
  158. }
  159. default:
  160. Y_DEBUG_ABORT_UNLESS(false, "Unknown EnumMode.");
  161. }
  162. }
  163. bool HandleTimeConversion(const Message& proto, IJsonOutput& json) {
  164. using namespace google::protobuf;
  165. auto type = proto.GetDescriptor()->well_known_type();
  166. if (type == Descriptor::WellKnownType::WELLKNOWNTYPE_DURATION) {
  167. const auto& duration = static_cast<const Duration&>(proto);
  168. json.Write(util::TimeUtil::ToString(duration));
  169. return true;
  170. } else if (type == Descriptor::WellKnownType::WELLKNOWNTYPE_TIMESTAMP) {
  171. const auto& timestamp = static_cast<const Timestamp&>(proto);
  172. json.Write(util::TimeUtil::ToString(timestamp));
  173. return true;
  174. }
  175. return false;
  176. }
  177. void TProto2JsonPrinter::PrintSingleField(const Message& proto,
  178. const FieldDescriptor& field,
  179. IJsonOutput& json,
  180. TStringBuf key) {
  181. Y_ABORT_UNLESS(!field.is_repeated(), "field is repeated.");
  182. if (!key) {
  183. key = MakeKey(field);
  184. }
  185. #define FIELD_TO_JSON(EProtoCppType, ProtoGet) \
  186. case FieldDescriptor::EProtoCppType: { \
  187. json.WriteKey(key).Write(reflection->ProtoGet(proto, &field)); \
  188. break; \
  189. }
  190. #define INT_FIELD_TO_JSON(EProtoCppType, ProtoGet) \
  191. case FieldDescriptor::EProtoCppType: { \
  192. const auto value = reflection->ProtoGet(proto, &field); \
  193. if (NeedStringifyNumber(value)) { \
  194. json.WriteKey(key).Write(ToString(value)); \
  195. } else { \
  196. json.WriteKey(key).Write(value); \
  197. } \
  198. break; \
  199. }
  200. const Reflection* reflection = proto.GetReflection();
  201. bool shouldPrintField = reflection->HasField(proto, &field);
  202. if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) {
  203. if (field.has_default_value()) {
  204. shouldPrintField = true;
  205. } else if (field.is_required()) {
  206. ythrow yexception() << "Empty required protobuf field: "
  207. << field.full_name() << ".";
  208. }
  209. }
  210. shouldPrintField = shouldPrintField ||
  211. (GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyDefault && !field.containing_oneof());
  212. if (shouldPrintField) {
  213. switch (field.cpp_type()) {
  214. INT_FIELD_TO_JSON(CPPTYPE_INT32, GetInt32);
  215. INT_FIELD_TO_JSON(CPPTYPE_INT64, GetInt64);
  216. INT_FIELD_TO_JSON(CPPTYPE_UINT32, GetUInt32);
  217. INT_FIELD_TO_JSON(CPPTYPE_UINT64, GetUInt64);
  218. FIELD_TO_JSON(CPPTYPE_DOUBLE, GetDouble);
  219. FIELD_TO_JSON(CPPTYPE_FLOAT, GetFloat);
  220. FIELD_TO_JSON(CPPTYPE_BOOL, GetBool);
  221. case FieldDescriptor::CPPTYPE_MESSAGE: {
  222. json.WriteKey(key);
  223. if (Config.ConvertTimeAsString && HandleTimeConversion(reflection->GetMessage(proto, &field), json)) {
  224. break;
  225. }
  226. Print(reflection->GetMessage(proto, &field), json);
  227. break;
  228. }
  229. case FieldDescriptor::CPPTYPE_ENUM: {
  230. PrintEnumValue<true>(key, reflection->GetEnum(proto, &field), json);
  231. break;
  232. }
  233. case FieldDescriptor::CPPTYPE_STRING: {
  234. TString scratch;
  235. const TString& value = reflection->GetStringReference(proto, &field, &scratch);
  236. PrintStringValue<true>(field, key, value, json);
  237. break;
  238. }
  239. default:
  240. ythrow yexception() << "Unknown protobuf field type: "
  241. << static_cast<int>(field.cpp_type()) << ".";
  242. }
  243. } else {
  244. switch (GetConfig().MissingSingleKeyMode) {
  245. case TProto2JsonConfig::MissingKeyNull: {
  246. json.WriteKey(key).WriteNull();
  247. break;
  248. }
  249. case TProto2JsonConfig::MissingKeySkip:
  250. case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
  251. default:
  252. break;
  253. }
  254. }
  255. #undef FIELD_TO_JSON
  256. }
  257. void TProto2JsonPrinter::PrintRepeatedField(const Message& proto,
  258. const FieldDescriptor& field,
  259. IJsonOutput& json,
  260. TStringBuf key) {
  261. Y_ABORT_UNLESS(field.is_repeated(), "field isn't repeated.");
  262. const bool isMap = field.is_map() && GetConfig().MapAsObject;
  263. if (!key) {
  264. key = MakeKey(field);
  265. }
  266. #define REPEATED_FIELD_TO_JSON(EProtoCppType, ProtoGet) \
  267. case FieldDescriptor::EProtoCppType: { \
  268. for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) \
  269. json.Write(reflection->ProtoGet(proto, &field, i)); \
  270. break; \
  271. }
  272. const Reflection* reflection = proto.GetReflection();
  273. if (reflection->FieldSize(proto, &field) > 0) {
  274. json.WriteKey(key);
  275. if (isMap) {
  276. json.BeginObject();
  277. } else {
  278. json.BeginList();
  279. }
  280. switch (field.cpp_type()) {
  281. REPEATED_FIELD_TO_JSON(CPPTYPE_INT32, GetRepeatedInt32);
  282. REPEATED_FIELD_TO_JSON(CPPTYPE_INT64, GetRepeatedInt64);
  283. REPEATED_FIELD_TO_JSON(CPPTYPE_UINT32, GetRepeatedUInt32);
  284. REPEATED_FIELD_TO_JSON(CPPTYPE_UINT64, GetRepeatedUInt64);
  285. REPEATED_FIELD_TO_JSON(CPPTYPE_DOUBLE, GetRepeatedDouble);
  286. REPEATED_FIELD_TO_JSON(CPPTYPE_FLOAT, GetRepeatedFloat);
  287. REPEATED_FIELD_TO_JSON(CPPTYPE_BOOL, GetRepeatedBool);
  288. case FieldDescriptor::CPPTYPE_MESSAGE: {
  289. if (isMap) {
  290. for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {
  291. PrintKeyValue(reflection->GetRepeatedMessage(proto, &field, i), json);
  292. }
  293. } else {
  294. for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {
  295. Print(reflection->GetRepeatedMessage(proto, &field, i), json);
  296. }
  297. }
  298. break;
  299. }
  300. case FieldDescriptor::CPPTYPE_ENUM: {
  301. for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i)
  302. PrintEnumValue<false>(TStringBuf(), reflection->GetRepeatedEnum(proto, &field, i), json);
  303. break;
  304. }
  305. case FieldDescriptor::CPPTYPE_STRING: {
  306. TString scratch;
  307. for (int i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) {
  308. const TString& value =
  309. reflection->GetRepeatedStringReference(proto, &field, i, &scratch);
  310. PrintStringValue<false>(field, TStringBuf(), value, json);
  311. }
  312. break;
  313. }
  314. default:
  315. ythrow yexception() << "Unknown protobuf field type: "
  316. << static_cast<int>(field.cpp_type()) << ".";
  317. }
  318. if (isMap) {
  319. json.EndObject();
  320. } else {
  321. json.EndList();
  322. }
  323. } else {
  324. switch (GetConfig().MissingRepeatedKeyMode) {
  325. case TProto2JsonConfig::MissingKeyNull: {
  326. json.WriteKey(key).WriteNull();
  327. break;
  328. }
  329. case TProto2JsonConfig::MissingKeyDefault: {
  330. json.WriteKey(key);
  331. if (isMap) {
  332. json.BeginObject().EndObject();
  333. } else {
  334. json.BeginList().EndList();
  335. }
  336. break;
  337. }
  338. case TProto2JsonConfig::MissingKeySkip:
  339. case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired:
  340. default:
  341. break;
  342. }
  343. }
  344. #undef REPEATED_FIELD_TO_JSON
  345. }
  346. void TProto2JsonPrinter::PrintKeyValue(const NProtoBuf::Message& proto,
  347. IJsonOutput& json) {
  348. const FieldDescriptor* keyField = proto.GetDescriptor()->FindFieldByName("key");
  349. Y_ABORT_UNLESS(keyField, "Map entry key field not found.");
  350. TString key = MakeKey(proto, *keyField);
  351. const FieldDescriptor* valueField = proto.GetDescriptor()->FindFieldByName("value");
  352. Y_ABORT_UNLESS(valueField, "Map entry value field not found.");
  353. PrintField(proto, *valueField, json, key);
  354. }
  355. TString TProto2JsonPrinter::MakeKey(const NProtoBuf::Message& proto,
  356. const NProtoBuf::FieldDescriptor& field) {
  357. const Reflection* reflection = proto.GetReflection();
  358. TString result;
  359. switch (field.cpp_type()) {
  360. case FieldDescriptor::CPPTYPE_INT32:
  361. result = ToString(reflection->GetInt32(proto, &field));
  362. break;
  363. case FieldDescriptor::CPPTYPE_INT64:
  364. result = ToString(reflection->GetInt64(proto, &field));
  365. break;
  366. case FieldDescriptor::CPPTYPE_UINT32:
  367. result = ToString(reflection->GetUInt32(proto, &field));
  368. break;
  369. case FieldDescriptor::CPPTYPE_UINT64:
  370. result = ToString(reflection->GetUInt64(proto, &field));
  371. break;
  372. case FieldDescriptor::CPPTYPE_DOUBLE:
  373. result = ToString(reflection->GetDouble(proto, &field));
  374. break;
  375. case FieldDescriptor::CPPTYPE_FLOAT:
  376. result = ToString(reflection->GetFloat(proto, &field));
  377. break;
  378. case FieldDescriptor::CPPTYPE_BOOL:
  379. result = ToString(reflection->GetBool(proto, &field));
  380. break;
  381. case FieldDescriptor::CPPTYPE_ENUM: {
  382. const EnumValueDescriptor* value = reflection->GetEnum(proto, &field);
  383. switch (GetConfig().EnumMode) {
  384. case TProto2JsonConfig::EnumNumber:
  385. result = ToString(value->number());
  386. break;
  387. case TProto2JsonConfig::EnumName:
  388. result = value->name();
  389. break;
  390. case TProto2JsonConfig::EnumFullName:
  391. result = value->full_name();
  392. break;
  393. case TProto2JsonConfig::EnumNameLowerCase:
  394. result = value->name();
  395. result.to_lower();
  396. break;
  397. case TProto2JsonConfig::EnumFullNameLowerCase:
  398. result = value->full_name();
  399. result.to_lower();
  400. break;
  401. default:
  402. ythrow yexception() << "Unsupported enum mode.";
  403. }
  404. break;
  405. }
  406. case FieldDescriptor::CPPTYPE_STRING:
  407. result = reflection->GetString(proto, &field);
  408. break;
  409. default:
  410. ythrow yexception() << "Unsupported key type.";
  411. }
  412. return result;
  413. }
  414. void TProto2JsonPrinter::PrintField(const Message& proto,
  415. const FieldDescriptor& field,
  416. IJsonOutput& json,
  417. const TStringBuf key) {
  418. if (field.is_repeated())
  419. PrintRepeatedField(proto, field, json, key);
  420. else
  421. PrintSingleField(proto, field, json, key);
  422. }
  423. void TProto2JsonPrinter::Print(const Message& proto, IJsonOutput& json, bool closeMap) {
  424. const Descriptor* descriptor = proto.GetDescriptor();
  425. Y_ASSERT(descriptor);
  426. json.BeginObject();
  427. // Iterate over all non-extension fields
  428. for (int f = 0, endF = descriptor->field_count(); f < endF; ++f) {
  429. const FieldDescriptor* field = descriptor->field(f);
  430. Y_ASSERT(field);
  431. PrintField(proto, *field, json);
  432. }
  433. // Check extensions via ListFields
  434. std::vector<const FieldDescriptor*> fields;
  435. auto* ref = proto.GetReflection();
  436. ref->ListFields(proto, &fields);
  437. for (const FieldDescriptor* field : fields) {
  438. Y_ASSERT(field);
  439. if (field->is_extension()) {
  440. switch (GetConfig().ExtensionFieldNameMode) {
  441. case TProto2JsonConfig::ExtFldNameFull:
  442. PrintField(proto, *field, json, field->full_name());
  443. break;
  444. case TProto2JsonConfig::ExtFldNameShort:
  445. PrintField(proto, *field, json);
  446. break;
  447. }
  448. }
  449. }
  450. if (closeMap) {
  451. json.EndObject();
  452. }
  453. }
  454. template <class T, class U>
  455. std::enable_if_t<!std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) {
  456. return value > -range && value < range;
  457. }
  458. template <class T, class U>
  459. std::enable_if_t<std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) {
  460. return value < (std::make_unsigned_t<U>)(range);
  461. }
  462. template <class T>
  463. bool TProto2JsonPrinter::NeedStringifyNumber(T value) const {
  464. constexpr long SAFE_INTEGER_RANGE_FLOAT = 1L << 24;
  465. constexpr long long SAFE_INTEGER_RANGE_DOUBLE = 1LL << 53;
  466. switch (GetConfig().StringifyNumbers) {
  467. case TProto2JsonConfig::StringifyLongNumbersNever:
  468. return false;
  469. case TProto2JsonConfig::StringifyLongNumbersForFloat:
  470. return !ValueInRange(value, SAFE_INTEGER_RANGE_FLOAT);
  471. case TProto2JsonConfig::StringifyLongNumbersForDouble:
  472. return !ValueInRange(value, SAFE_INTEGER_RANGE_DOUBLE);
  473. case TProto2JsonConfig::StringifyInt64Always:
  474. return std::is_same_v<T, i64> || std::is_same_v<T, ui64>;
  475. }
  476. return false;
  477. }
  478. }