proto2json_ut.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. #include "json.h"
  2. #include "proto.h"
  3. #include <library/cpp/protobuf/json/ut/test.pb.h>
  4. #include <library/cpp/json/json_value.h>
  5. #include <library/cpp/json/json_reader.h>
  6. #include <library/cpp/json/json_writer.h>
  7. #include <library/cpp/protobuf/json/proto2json.h>
  8. #include <library/cpp/testing/unittest/registar.h>
  9. #include <util/generic/hash_set.h>
  10. #include <util/generic/string.h>
  11. #include <util/generic/ylimits.h>
  12. #include <util/stream/str.h>
  13. #include <util/system/defaults.h>
  14. #include <util/system/yassert.h>
  15. #include <limits>
  16. using namespace NProtobufJson;
  17. using namespace NProtobufJsonTest;
  18. Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) {
  19. Y_UNIT_TEST(TestFlatDefault) {
  20. using namespace ::google::protobuf;
  21. TFlatDefault proto;
  22. NJson::TJsonValue json;
  23. TProto2JsonConfig cfg;
  24. cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyDefault);
  25. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, cfg));
  26. #define DEFINE_FIELD(name, value) \
  27. { \
  28. auto descr = proto.GetMetadata().descriptor->FindFieldByName(#name); \
  29. UNIT_ASSERT(descr); \
  30. UNIT_ASSERT(json.Has(#name)); \
  31. switch (descr->cpp_type()) { \
  32. case FieldDescriptor::CPPTYPE_INT32: \
  33. UNIT_ASSERT(descr->default_value_int32() == json[#name].GetIntegerRobust()); \
  34. break; \
  35. case FieldDescriptor::CPPTYPE_INT64: \
  36. UNIT_ASSERT(descr->default_value_int64() == json[#name].GetIntegerRobust()); \
  37. break; \
  38. case FieldDescriptor::CPPTYPE_UINT32: \
  39. UNIT_ASSERT(descr->default_value_uint32() == json[#name].GetUIntegerRobust()); \
  40. break; \
  41. case FieldDescriptor::CPPTYPE_UINT64: \
  42. UNIT_ASSERT(descr->default_value_uint32() == json[#name].GetUIntegerRobust()); \
  43. break; \
  44. case FieldDescriptor::CPPTYPE_DOUBLE: \
  45. UNIT_ASSERT(descr->default_value_double() == json[#name].GetDoubleRobust()); \
  46. break; \
  47. case FieldDescriptor::CPPTYPE_FLOAT: \
  48. UNIT_ASSERT(descr->default_value_float() == json[#name].GetDoubleRobust()); \
  49. break; \
  50. case FieldDescriptor::CPPTYPE_BOOL: \
  51. UNIT_ASSERT(descr->default_value_bool() == json[#name].GetBooleanRobust()); \
  52. break; \
  53. case FieldDescriptor::CPPTYPE_ENUM: \
  54. UNIT_ASSERT(descr->default_value_enum()->number() == json[#name].GetIntegerRobust()); \
  55. break; \
  56. case FieldDescriptor::CPPTYPE_STRING: \
  57. UNIT_ASSERT(descr->default_value_string() == json[#name].GetStringRobust()); \
  58. break; \
  59. default: \
  60. break; \
  61. } \
  62. }
  63. #include <library/cpp/protobuf/json/ut/fields.incl>
  64. #undef DEFINE_FIELD
  65. }
  66. Y_UNIT_TEST(TestOneOfDefault) {
  67. using namespace ::google::protobuf;
  68. TFlatOneOfDefault proto;
  69. NJson::TJsonValue json;
  70. TProto2JsonConfig cfg;
  71. cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyDefault);
  72. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, cfg));
  73. UNIT_ASSERT(!json.Has("ChoiceOne"));
  74. UNIT_ASSERT(!json.Has("ChoiceTwo"));
  75. proto.SetChoiceOne("one");
  76. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, cfg));
  77. UNIT_ASSERT_EQUAL("one", json["ChoiceOne"].GetStringRobust());
  78. }
  79. Y_UNIT_TEST(TestNameGenerator) {
  80. TNameGeneratorType proto;
  81. proto.SetField(42);
  82. TProto2JsonConfig cfg;
  83. cfg.SetNameGenerator([](const NProtoBuf::FieldDescriptor&) { return "42"; });
  84. TStringStream str;
  85. Proto2Json(proto, str, cfg);
  86. UNIT_ASSERT_STRINGS_EQUAL(R"({"42":42})", str.Str());
  87. }
  88. Y_UNIT_TEST(TestEnumValueGenerator) {
  89. TEnumValueGeneratorType proto;
  90. proto.SetEnum(TEnumValueGeneratorType::ENUM_42);
  91. TProto2JsonConfig cfg;
  92. cfg.SetEnumValueGenerator([](const NProtoBuf::EnumValueDescriptor&) { return "42"; });
  93. TStringStream str;
  94. Proto2Json(proto, str, cfg);
  95. UNIT_ASSERT_STRINGS_EQUAL(R"({"Enum":"42"})", str.Str());
  96. }
  97. Y_UNIT_TEST(TestFlatOptional){
  98. {TFlatOptional proto;
  99. FillFlatProto(&proto);
  100. const NJson::TJsonValue& modelJson = CreateFlatJson();
  101. {
  102. NJson::TJsonValue json;
  103. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  104. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  105. }
  106. {
  107. TStringStream jsonStream;
  108. NJson::TJsonValue json;
  109. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream));
  110. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json));
  111. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  112. } // streamed
  113. }
  114. // Try to skip each field
  115. #define DEFINE_FIELD(name, value) \
  116. { \
  117. THashSet<TString> skippedField; \
  118. skippedField.insert(#name); \
  119. TFlatOptional proto; \
  120. FillFlatProto(&proto, skippedField); \
  121. const NJson::TJsonValue& modelJson = CreateFlatJson(skippedField); \
  122. NJson::TJsonValue json; \
  123. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); \
  124. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  125. { \
  126. TStringStream jsonStream; \
  127. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream)); \
  128. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json)); \
  129. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  130. } \
  131. }
  132. #include <library/cpp/protobuf/json/ut/fields.incl>
  133. #undef DEFINE_FIELD
  134. } // TestFlatOptional
  135. Y_UNIT_TEST(TestFlatRequired){
  136. {TFlatRequired proto;
  137. FillFlatProto(&proto);
  138. const NJson::TJsonValue& modelJson = CreateFlatJson();
  139. {
  140. NJson::TJsonValue json;
  141. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  142. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  143. }
  144. {
  145. TStringStream jsonStream;
  146. NJson::TJsonValue json;
  147. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream));
  148. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json));
  149. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  150. } // streamed
  151. }
  152. // Try to skip each field
  153. #define DEFINE_FIELD(name, value) \
  154. { \
  155. THashSet<TString> skippedField; \
  156. skippedField.insert(#name); \
  157. TFlatRequired proto; \
  158. FillFlatProto(&proto, skippedField); \
  159. const NJson::TJsonValue& modelJson = CreateFlatJson(skippedField); \
  160. NJson::TJsonValue json; \
  161. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); \
  162. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  163. { \
  164. TStringStream jsonStream; \
  165. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream)); \
  166. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json)); \
  167. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  168. } \
  169. }
  170. #include <library/cpp/protobuf/json/ut/fields.incl>
  171. #undef DEFINE_FIELD
  172. } // TestFlatRequired
  173. Y_UNIT_TEST(TestFlatRepeated) {
  174. {
  175. TFlatRepeated proto;
  176. FillRepeatedProto(&proto);
  177. const NJson::TJsonValue& modelJson = CreateRepeatedFlatJson();
  178. {
  179. NJson::TJsonValue json;
  180. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  181. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  182. }
  183. {
  184. TStringStream jsonStream;
  185. NJson::TJsonValue json;
  186. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream));
  187. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json));
  188. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  189. } // streamed
  190. }
  191. TProto2JsonConfig config;
  192. config.SetMissingRepeatedKeyMode(TProto2JsonConfig::MissingKeySkip);
  193. // Try to skip each field
  194. #define DEFINE_REPEATED_FIELD(name, ...) \
  195. { \
  196. THashSet<TString> skippedField; \
  197. skippedField.insert(#name); \
  198. TFlatRepeated proto; \
  199. FillRepeatedProto(&proto, skippedField); \
  200. const NJson::TJsonValue& modelJson = CreateRepeatedFlatJson(skippedField); \
  201. NJson::TJsonValue json; \
  202. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config)); \
  203. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  204. { \
  205. TStringStream jsonStream; \
  206. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream, config)); \
  207. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json)); \
  208. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  209. } \
  210. }
  211. #include <library/cpp/protobuf/json/ut/repeated_fields.incl>
  212. #undef DEFINE_REPEATED_FIELD
  213. } // TestFlatRepeated
  214. Y_UNIT_TEST(TestCompositeOptional){
  215. {TCompositeOptional proto;
  216. FillCompositeProto(&proto);
  217. const NJson::TJsonValue& modelJson = CreateCompositeJson();
  218. {
  219. NJson::TJsonValue json;
  220. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  221. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  222. }
  223. {
  224. TStringStream jsonStream;
  225. NJson::TJsonValue json;
  226. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream));
  227. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json));
  228. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  229. } // streamed
  230. }
  231. // Try to skip each field
  232. #define DEFINE_FIELD(name, value) \
  233. { \
  234. THashSet<TString> skippedField; \
  235. skippedField.insert(#name); \
  236. TCompositeOptional proto; \
  237. FillCompositeProto(&proto, skippedField); \
  238. const NJson::TJsonValue& modelJson = CreateCompositeJson(skippedField); \
  239. NJson::TJsonValue json; \
  240. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); \
  241. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  242. { \
  243. TStringStream jsonStream; \
  244. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream)); \
  245. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json)); \
  246. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  247. } \
  248. }
  249. #include <library/cpp/protobuf/json/ut/fields.incl>
  250. #undef DEFINE_FIELD
  251. } // TestCompositeOptional
  252. Y_UNIT_TEST(TestCompositeRequired){
  253. {TCompositeRequired proto;
  254. FillCompositeProto(&proto);
  255. const NJson::TJsonValue& modelJson = CreateCompositeJson();
  256. {
  257. NJson::TJsonValue json;
  258. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  259. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  260. }
  261. {
  262. TStringStream jsonStream;
  263. NJson::TJsonValue json;
  264. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream));
  265. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json));
  266. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  267. } // streamed
  268. }
  269. // Try to skip each field
  270. #define DEFINE_FIELD(name, value) \
  271. { \
  272. THashSet<TString> skippedField; \
  273. skippedField.insert(#name); \
  274. TCompositeRequired proto; \
  275. FillCompositeProto(&proto, skippedField); \
  276. const NJson::TJsonValue& modelJson = CreateCompositeJson(skippedField); \
  277. NJson::TJsonValue json; \
  278. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json)); \
  279. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  280. { \
  281. TStringStream jsonStream; \
  282. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream)); \
  283. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json)); \
  284. UNIT_ASSERT_JSONS_EQUAL(json, modelJson); \
  285. } \
  286. }
  287. #include <library/cpp/protobuf/json/ut/fields.incl>
  288. #undef DEFINE_FIELD
  289. } // TestCompositeRequired
  290. Y_UNIT_TEST(TestCompositeRepeated) {
  291. {
  292. TFlatOptional partProto;
  293. FillFlatProto(&partProto);
  294. TCompositeRepeated proto;
  295. proto.AddPart()->CopyFrom(partProto);
  296. NJson::TJsonValue modelJson;
  297. NJson::TJsonValue modelArray;
  298. modelArray.AppendValue(CreateFlatJson());
  299. modelJson.InsertValue("Part", modelArray);
  300. {
  301. NJson::TJsonValue json;
  302. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  303. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  304. }
  305. {
  306. TStringStream jsonStream;
  307. NJson::TJsonValue json;
  308. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream));
  309. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json));
  310. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  311. } // streamed
  312. }
  313. {
  314. // Array of messages with each field skipped
  315. TCompositeRepeated proto;
  316. NJson::TJsonValue modelArray;
  317. #define DEFINE_REPEATED_FIELD(name, ...) \
  318. { \
  319. THashSet<TString> skippedField; \
  320. skippedField.insert(#name); \
  321. TFlatOptional partProto; \
  322. FillFlatProto(&partProto, skippedField); \
  323. proto.AddPart()->CopyFrom(partProto); \
  324. modelArray.AppendValue(CreateFlatJson(skippedField)); \
  325. }
  326. #include <library/cpp/protobuf/json/ut/repeated_fields.incl>
  327. #undef DEFINE_REPEATED_FIELD
  328. NJson::TJsonValue modelJson;
  329. modelJson.InsertValue("Part", modelArray);
  330. {
  331. NJson::TJsonValue json;
  332. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  333. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  334. }
  335. {
  336. TStringStream jsonStream;
  337. NJson::TJsonValue json;
  338. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStream));
  339. UNIT_ASSERT(ReadJsonTree(&jsonStream, &json));
  340. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  341. } // streamed
  342. }
  343. } // TestCompositeRepeated
  344. Y_UNIT_TEST(TestEnumConfig) {
  345. {
  346. TFlatOptional proto;
  347. proto.SetEnum(E_1);
  348. NJson::TJsonValue modelJson;
  349. modelJson.InsertValue("Enum", 1);
  350. NJson::TJsonValue json;
  351. TProto2JsonConfig config;
  352. config.EnumMode = TProto2JsonConfig::EnumNumber;
  353. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  354. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  355. }
  356. {
  357. TFlatOptional proto;
  358. proto.SetEnum(E_1);
  359. NJson::TJsonValue modelJson;
  360. modelJson.InsertValue("Enum", "E_1");
  361. NJson::TJsonValue json;
  362. TProto2JsonConfig config;
  363. config.EnumMode = TProto2JsonConfig::EnumName;
  364. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  365. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  366. }
  367. {
  368. TFlatOptional proto;
  369. proto.SetEnum(E_1);
  370. NJson::TJsonValue modelJson;
  371. modelJson.InsertValue("Enum", "NProtobufJsonTest.E_1");
  372. NJson::TJsonValue json;
  373. TProto2JsonConfig config;
  374. config.EnumMode = TProto2JsonConfig::EnumFullName;
  375. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  376. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  377. }
  378. {
  379. TFlatOptional proto;
  380. proto.SetEnum(E_1);
  381. NJson::TJsonValue modelJson;
  382. modelJson.InsertValue("Enum", "e_1");
  383. NJson::TJsonValue json;
  384. TProto2JsonConfig config;
  385. config.EnumMode = TProto2JsonConfig::EnumNameLowerCase;
  386. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  387. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  388. }
  389. {
  390. TFlatOptional proto;
  391. proto.SetEnum(E_1);
  392. NJson::TJsonValue modelJson;
  393. modelJson.InsertValue("Enum", "nprotobufjsontest.e_1");
  394. NJson::TJsonValue json;
  395. TProto2JsonConfig config;
  396. config.EnumMode = TProto2JsonConfig::EnumFullNameLowerCase;
  397. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  398. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  399. }
  400. } // TestEnumConfig
  401. Y_UNIT_TEST(TestMissingSingleKeyConfig) {
  402. {
  403. TFlatOptional proto;
  404. NJson::TJsonValue modelJson(NJson::JSON_MAP);
  405. NJson::TJsonValue json;
  406. TProto2JsonConfig config;
  407. config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeySkip;
  408. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  409. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  410. }
  411. {
  412. NJson::TJsonValue modelJson;
  413. #define DEFINE_FIELD(name, value) \
  414. modelJson.InsertValue(#name, NJson::TJsonValue(NJson::JSON_NULL));
  415. #include <library/cpp/protobuf/json/ut/fields.incl>
  416. #undef DEFINE_FIELD
  417. TFlatOptional proto;
  418. NJson::TJsonValue json;
  419. TProto2JsonConfig config;
  420. config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyNull;
  421. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  422. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  423. }
  424. {
  425. // Test MissingKeyExplicitDefaultThrowRequired for non explicit default values.
  426. TFlatOptional proto;
  427. NJson::TJsonValue modelJson(NJson::JSON_MAP);
  428. NJson::TJsonValue json;
  429. TProto2JsonConfig config;
  430. config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
  431. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  432. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  433. }
  434. {
  435. // Test MissingKeyExplicitDefaultThrowRequired for explicit default values.
  436. NJson::TJsonValue modelJson;
  437. modelJson["String"] = "value";
  438. TSingleDefaultString proto;
  439. NJson::TJsonValue json;
  440. TProto2JsonConfig config;
  441. config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
  442. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  443. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  444. }
  445. {
  446. // Test MissingKeyExplicitDefaultThrowRequired for empty required values.
  447. TFlatRequired proto;
  448. NJson::TJsonValue json;
  449. TProto2JsonConfig config;
  450. config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
  451. UNIT_ASSERT_EXCEPTION_CONTAINS(Proto2Json(proto, json, config), yexception, "Empty required protobuf field");
  452. }
  453. {
  454. // Test MissingKeyExplicitDefaultThrowRequired for required value.
  455. TSingleRequiredString proto;
  456. NJson::TJsonValue json;
  457. TProto2JsonConfig config;
  458. config.MissingSingleKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
  459. UNIT_ASSERT_EXCEPTION_CONTAINS(Proto2Json(proto, json, config), yexception, "Empty required protobuf field");
  460. NJson::TJsonValue modelJson;
  461. modelJson["String"] = "value";
  462. proto.SetString("value");
  463. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  464. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  465. }
  466. } // TestMissingSingleKeyConfig
  467. Y_UNIT_TEST(TestMissingRepeatedKeyNoConfig) {
  468. {
  469. TFlatRepeated proto;
  470. NJson::TJsonValue modelJson(NJson::JSON_MAP);
  471. NJson::TJsonValue json;
  472. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json));
  473. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  474. }
  475. } // TestMissingRepeatedKeyNoConfig
  476. Y_UNIT_TEST(TestMissingRepeatedKeyConfig) {
  477. {
  478. TFlatRepeated proto;
  479. NJson::TJsonValue modelJson(NJson::JSON_MAP);
  480. NJson::TJsonValue json;
  481. TProto2JsonConfig config;
  482. config.MissingRepeatedKeyMode = TProto2JsonConfig::MissingKeySkip;
  483. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  484. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  485. }
  486. {
  487. NJson::TJsonValue modelJson;
  488. #define DEFINE_FIELD(name, value) \
  489. modelJson.InsertValue(#name, NJson::TJsonValue(NJson::JSON_NULL));
  490. #include <library/cpp/protobuf/json/ut/fields.incl>
  491. #undef DEFINE_FIELD
  492. TFlatRepeated proto;
  493. NJson::TJsonValue json;
  494. TProto2JsonConfig config;
  495. config.MissingRepeatedKeyMode = TProto2JsonConfig::MissingKeyNull;
  496. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  497. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  498. }
  499. {
  500. TFlatRepeated proto;
  501. NJson::TJsonValue modelJson(NJson::JSON_MAP);
  502. NJson::TJsonValue json;
  503. TProto2JsonConfig config;
  504. config.MissingRepeatedKeyMode = TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired;
  505. // SHould be same as MissingKeySkip
  506. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, config));
  507. UNIT_ASSERT_JSONS_EQUAL(json, modelJson);
  508. }
  509. } // TestMissingRepeatedKeyConfig
  510. Y_UNIT_TEST(TestEscaping) {
  511. // No escape
  512. {
  513. TString modelStr(R"_({"String":"value\""})_");
  514. TFlatOptional proto;
  515. proto.SetString(R"_(value")_");
  516. TStringStream jsonStr;
  517. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr));
  518. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  519. }
  520. // TEscapeJTransform
  521. {
  522. TString modelStr(R"_({"String":"value\""})_");
  523. TFlatOptional proto;
  524. proto.SetString(R"_(value")_");
  525. TProto2JsonConfig config;
  526. config.StringTransforms.push_back(new TEscapeJTransform<false, true>());
  527. TStringStream jsonStr;
  528. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  529. UNIT_ASSERT_JSON_STRINGS_EQUAL(modelStr, jsonStr.Str());
  530. }
  531. // TCEscapeTransform
  532. {
  533. TString modelStr(R"_({"String":"value\""})_");
  534. TFlatOptional proto;
  535. proto.SetString(R"_(value")_");
  536. TProto2JsonConfig config;
  537. config.StringTransforms.push_back(new TCEscapeTransform());
  538. TStringStream jsonStr;
  539. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  540. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  541. }
  542. // TSafeUtf8CEscapeTransform
  543. {
  544. TString modelStr(R"_({"String":"value\""})_");
  545. TFlatOptional proto;
  546. proto.SetString(R"_(value")_");
  547. TProto2JsonConfig config;
  548. config.StringTransforms.push_back(new TSafeUtf8CEscapeTransform());
  549. TStringStream jsonStr;
  550. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  551. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  552. }
  553. } // TestEscaping
  554. class TBytesTransform: public IStringTransform {
  555. public:
  556. int GetType() const override {
  557. return 0;
  558. }
  559. void Transform(TString&) const override {
  560. }
  561. void TransformBytes(TString& str) const override {
  562. str = "bytes";
  563. }
  564. };
  565. Y_UNIT_TEST(TestBytesTransform) {
  566. // Test that string field is not changed
  567. {
  568. TString modelStr(R"_({"String":"value"})_");
  569. TFlatOptional proto;
  570. proto.SetString(R"_(value)_");
  571. TProto2JsonConfig config;
  572. config.StringTransforms.push_back(new TBytesTransform());
  573. TStringStream jsonStr;
  574. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  575. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  576. }
  577. // Test that bytes field is changed
  578. {
  579. TString modelStr(R"_({"Bytes":"bytes"})_");
  580. TFlatOptional proto;
  581. proto.SetBytes(R"_(value)_");
  582. TProto2JsonConfig config;
  583. config.StringTransforms.push_back(new TBytesTransform());
  584. TStringStream jsonStr;
  585. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  586. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  587. }
  588. }
  589. Y_UNIT_TEST(TestFieldNameMode) {
  590. // Original case 1
  591. {
  592. TString modelStr(R"_({"String":"value"})_");
  593. TFlatOptional proto;
  594. proto.SetString("value");
  595. TStringStream jsonStr;
  596. TProto2JsonConfig config;
  597. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  598. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  599. }
  600. // Original case 2
  601. {
  602. TString modelStr(R"_({"String":"value"})_");
  603. TFlatOptional proto;
  604. proto.SetString("value");
  605. TStringStream jsonStr;
  606. TProto2JsonConfig config;
  607. config.FieldNameMode = TProto2JsonConfig::FieldNameOriginalCase;
  608. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  609. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  610. }
  611. // Lowercase
  612. {
  613. TString modelStr(R"_({"string":"value"})_");
  614. TFlatOptional proto;
  615. proto.SetString("value");
  616. TStringStream jsonStr;
  617. TProto2JsonConfig config;
  618. config.FieldNameMode = TProto2JsonConfig::FieldNameLowerCase;
  619. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  620. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  621. }
  622. // Uppercase
  623. {
  624. TString modelStr(R"_({"STRING":"value"})_");
  625. TFlatOptional proto;
  626. proto.SetString("value");
  627. TStringStream jsonStr;
  628. TProto2JsonConfig config;
  629. config.FieldNameMode = TProto2JsonConfig::FieldNameUpperCase;
  630. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  631. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  632. }
  633. // Camelcase
  634. {
  635. TString modelStr(R"_({"string":"value"})_");
  636. TFlatOptional proto;
  637. proto.SetString("value");
  638. TStringStream jsonStr;
  639. TProto2JsonConfig config;
  640. config.FieldNameMode = TProto2JsonConfig::FieldNameCamelCase;
  641. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  642. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  643. }
  644. {
  645. TString modelStr(R"_({"oneString":"value"})_");
  646. TFlatOptional proto;
  647. proto.SetOneString("value");
  648. TStringStream jsonStr;
  649. TProto2JsonConfig config;
  650. config.FieldNameMode = TProto2JsonConfig::FieldNameCamelCase;
  651. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  652. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  653. }
  654. {
  655. TString modelStr(R"_({"oneTwoString":"value"})_");
  656. TFlatOptional proto;
  657. proto.SetOneTwoString("value");
  658. TStringStream jsonStr;
  659. TProto2JsonConfig config;
  660. config.FieldNameMode = TProto2JsonConfig::FieldNameCamelCase;
  661. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  662. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  663. }
  664. // snake_case
  665. {
  666. TString modelStr(R"_({"string":"value"})_");
  667. TFlatOptional proto;
  668. proto.SetString("value");
  669. TStringStream jsonStr;
  670. TProto2JsonConfig config;
  671. config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;
  672. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  673. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  674. }
  675. {
  676. TString modelStr(R"_({"one_string":"value"})_");
  677. TFlatOptional proto;
  678. proto.SetOneString("value");
  679. TStringStream jsonStr;
  680. TProto2JsonConfig config;
  681. config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;
  682. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  683. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  684. }
  685. {
  686. TString modelStr(R"_({"one_two_string":"value"})_");
  687. TFlatOptional proto;
  688. proto.SetOneTwoString("value");
  689. TStringStream jsonStr;
  690. TProto2JsonConfig config;
  691. config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;
  692. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  693. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  694. }
  695. {
  696. TString modelStr(R"_({"a_b_c":"value","user_i_d":"value"})_");
  697. TFlatOptional proto;
  698. proto.SetABC("value");
  699. proto.SetUserID("value");
  700. TStringStream jsonStr;
  701. TProto2JsonConfig config;
  702. config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCase;
  703. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  704. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  705. }
  706. // snake_case_dense
  707. {
  708. TString modelStr(R"_({"abc":"value","user_id":"value"})_");
  709. TFlatOptional proto;
  710. proto.SetABC("value");
  711. proto.SetUserID("value");
  712. TStringStream jsonStr;
  713. TProto2JsonConfig config;
  714. config.FieldNameMode = TProto2JsonConfig::FieldNameSnakeCaseDense;
  715. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  716. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  717. }
  718. // Original case, repeated
  719. {
  720. TString modelStr(R"_({"I32":[1,2]})_");
  721. TFlatRepeated proto;
  722. proto.AddI32(1);
  723. proto.AddI32(2);
  724. TStringStream jsonStr;
  725. TProto2JsonConfig config;
  726. config.FieldNameMode = TProto2JsonConfig::FieldNameOriginalCase;
  727. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  728. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  729. }
  730. // Lower case, repeated
  731. {
  732. TString modelStr(R"_({"i32":[1,2]})_");
  733. TFlatRepeated proto;
  734. proto.AddI32(1);
  735. proto.AddI32(2);
  736. TStringStream jsonStr;
  737. TProto2JsonConfig config;
  738. config.FieldNameMode = TProto2JsonConfig::FieldNameLowerCase;
  739. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  740. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  741. }
  742. // UseJsonName
  743. {
  744. // FIXME(CONTRIB-139): see the comment about UseJsonName in json2proto_ut.cpp:
  745. // Def_upper json name should be "DefUpper".
  746. TString modelStr(R"_({"My-Upper":1,"my-lower":2,"defUpper":3,"defLower":4})_");
  747. TWithJsonName proto;
  748. proto.Setmy_upper(1);
  749. proto.SetMy_lower(2);
  750. proto.SetDef_upper(3);
  751. proto.Setdef_lower(4);
  752. TStringStream jsonStr;
  753. TProto2JsonConfig config;
  754. config.SetUseJsonName(true);
  755. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  756. UNIT_ASSERT_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  757. }
  758. // UseJsonName and UseJsonEnumValue
  759. {
  760. TString modelStr(R"_({"json_enum":"enum_1"})_");
  761. TCustomJsonEnumValue proto;
  762. proto.SetJsonEnum(EJsonEnum::J_1);
  763. TStringStream jsonStr;
  764. TProto2JsonConfig config;
  765. config.SetUseJsonName(true);
  766. config.SetUseJsonEnumValue(true);
  767. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  768. UNIT_ASSERT_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  769. }
  770. // FieldNameMode with UseJsonName
  771. {
  772. TProto2JsonConfig config;
  773. config.SetFieldNameMode(TProto2JsonConfig::FieldNameLowerCase);
  774. UNIT_ASSERT_EXCEPTION_CONTAINS(
  775. config.SetUseJsonName(true), yexception, "mutually exclusive");
  776. }
  777. {
  778. TProto2JsonConfig config;
  779. config.SetUseJsonName(true);
  780. UNIT_ASSERT_EXCEPTION_CONTAINS(
  781. config.SetFieldNameMode(TProto2JsonConfig::FieldNameLowerCase), yexception, "mutually exclusive");
  782. }
  783. /// TODO: test missing keys
  784. } // TestFieldNameMode
  785. Y_UNIT_TEST(TestNan) {
  786. TFlatOptional proto;
  787. proto.SetDouble(std::numeric_limits<double>::quiet_NaN());
  788. UNIT_ASSERT_EXCEPTION(Proto2Json(proto, TProto2JsonConfig()), yexception);
  789. } // TestNan
  790. Y_UNIT_TEST(TestInf) {
  791. TFlatOptional proto;
  792. proto.SetFloat(std::numeric_limits<float>::infinity());
  793. UNIT_ASSERT_EXCEPTION(Proto2Json(proto, TProto2JsonConfig()), yexception);
  794. } // TestInf
  795. Y_UNIT_TEST(TestMap) {
  796. TMapType proto;
  797. auto& items = *proto.MutableItems();
  798. items["key1"] = "value1";
  799. items["key2"] = "value2";
  800. items["key3"] = "value3";
  801. TString modelStr(R"_({"Items":[{"key":"key3","value":"value3"},{"key":"key2","value":"value2"},{"key":"key1","value":"value1"}]})_");
  802. TStringStream jsonStr;
  803. TProto2JsonConfig config;
  804. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  805. NJson::TJsonValue jsonValue, modelValue;
  806. NJson::TJsonValue::TArray jsonItems, modelItems;
  807. UNIT_ASSERT(NJson::ReadJsonTree(jsonStr.Str(), &jsonValue));
  808. UNIT_ASSERT(NJson::ReadJsonTree(modelStr, &modelValue));
  809. UNIT_ASSERT(jsonValue.Has("Items"));
  810. jsonValue["Items"].GetArray(&jsonItems);
  811. modelValue["Items"].GetArray(&modelItems);
  812. auto itemKey = [](const NJson::TJsonValue& v) {
  813. return v["key"].GetString();
  814. };
  815. SortBy(jsonItems, itemKey);
  816. SortBy(modelItems, itemKey);
  817. UNIT_ASSERT_EQUAL(jsonItems, modelItems);
  818. } // TestMap
  819. Y_UNIT_TEST(TestMapAsObject) {
  820. TMapType proto;
  821. auto& items = *proto.MutableItems();
  822. items["key1"] = "value1";
  823. items["key2"] = "value2";
  824. items["key3"] = "value3";
  825. TString modelStr(R"_({"Items":{"key3":"value3","key2":"value2","key1":"value1"}})_");
  826. TStringStream jsonStr;
  827. TProto2JsonConfig config;
  828. config.MapAsObject = true;
  829. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  830. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  831. } // TestMapAsObject
  832. Y_UNIT_TEST(TestMapUsingGeneratedAsJSON) {
  833. TMapType proto;
  834. auto& items = *proto.MutableItems();
  835. items["key1"] = "value1";
  836. items["key2"] = "value2";
  837. items["key3"] = "value3";
  838. TString modelStr(R"_({"Items":{"key3":"value3","key2":"value2","key1":"value1"}})_");
  839. TStringStream jsonStr;
  840. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr));
  841. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  842. } // TestMapUsingGeneratedAsJSON
  843. Y_UNIT_TEST(TestMapDefaultValue) {
  844. TMapType proto;
  845. auto& items = *proto.MutableItems();
  846. items["key1"] = "";
  847. TString modelStr(R"_({"Items":{"key1":""}})_");
  848. TStringStream jsonStr;
  849. TProto2JsonConfig config;
  850. config.MapAsObject = true;
  851. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  852. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  853. jsonStr.Clear();
  854. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr));
  855. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  856. } // TestMapDefaultValue
  857. Y_UNIT_TEST(TestMapDefaultMessageValue) {
  858. TComplexMapType proto;
  859. auto& map = *proto.MutableNested();
  860. map["key1"]; // Creates an empty nested message
  861. TString modelStr(R"_({"Nested":{"key1":{}}})_");
  862. TStringStream jsonStr;
  863. TProto2JsonConfig config;
  864. config.MapAsObject = true;
  865. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  866. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  867. jsonStr.Clear();
  868. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr));
  869. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  870. } // TestMapDefaultMessageValue
  871. Y_UNIT_TEST(TestStringifyNumbers) {
  872. #define TEST_SINGLE(flag, field, value, expectString) \
  873. do { \
  874. TFlatOptional proto; \
  875. proto.Set##field(value); \
  876. \
  877. TStringStream jsonStr; \
  878. TProto2JsonConfig config; \
  879. config.SetStringifyNumbers(flag); \
  880. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); \
  881. if (expectString) { \
  882. UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"" #field "\":\"" #value "\"}"); \
  883. } else { \
  884. UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"" #field "\":" #value "}"); \
  885. } \
  886. } while (false)
  887. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 1, false);
  888. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 1000000000, false);
  889. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 10000000000000000, false);
  890. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -1, false);
  891. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -1000000000, false);
  892. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -10000000000000000, false);
  893. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 1, false);
  894. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 1000000000, false);
  895. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 10000000000000000, true);
  896. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -1, false);
  897. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -1000000000, false);
  898. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -10000000000000000, true);
  899. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 1, false);
  900. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 1000000000, true);
  901. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 10000000000000000, true);
  902. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -1, false);
  903. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -1000000000, true);
  904. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -10000000000000000, true);
  905. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 1, true);
  906. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI32, 1000000000, false);
  907. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 10000000000000000, true);
  908. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI64, -1, true);
  909. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI32, -1000000000, false);
  910. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI64, -10000000000000000, true);
  911. #undef TEST_SINGLE
  912. } // TestStringifyNumbers
  913. Y_UNIT_TEST(TestExtension) {
  914. TExtensionField proto;
  915. proto.SetExtension(bar, 1);
  916. Y_ASSERT(proto.HasExtension(bar));
  917. UNIT_ASSERT_EQUAL(Proto2Json(proto, TProto2JsonConfig()), "{\"NProtobufJsonTest.bar\":1}");
  918. TProto2JsonConfig cfg;
  919. cfg.SetExtensionFieldNameMode(TProto2JsonConfig::ExtFldNameShort);
  920. UNIT_ASSERT_EQUAL(Proto2Json(proto, cfg), "{\"bar\":1}");
  921. } // TestExtension
  922. Y_UNIT_TEST(TestSimplifiedDuration) {
  923. TString json;
  924. TSingleDuration simpleDuration;
  925. simpleDuration.mutable_duration()->set_seconds(10);
  926. simpleDuration.mutable_duration()->set_nanos(101);
  927. Proto2Json(simpleDuration, json, TProto2JsonConfig().SetConvertTimeAsString(true));
  928. UNIT_ASSERT_EQUAL_C(json, "{\"Duration\":\"10.000000101s\"}", "real value is " << json);
  929. } // TestSimplifiedDuration
  930. Y_UNIT_TEST(TestSimplifiedTimestamp) {
  931. TString json;
  932. TSingleTimestamp simpleTimestamp;
  933. simpleTimestamp.mutable_timestamp()->set_seconds(10000000);
  934. simpleTimestamp.mutable_timestamp()->set_nanos(504);
  935. Proto2Json(simpleTimestamp, json, TProto2JsonConfig().SetConvertTimeAsString(true));
  936. UNIT_ASSERT_EQUAL_C(json, "{\"Timestamp\":\"1970-04-26T17:46:40.000000504Z\"}", "real value is " << json);
  937. } // TestSimplifiedTimestamp
  938. Y_UNIT_TEST(TestFloatToString) {
  939. #define TEST_SINGLE(mode, value, expectedValue) \
  940. do { \
  941. TFlatOptional proto; \
  942. proto.SetFloat(value); \
  943. \
  944. TStringStream jsonStr; \
  945. TProto2JsonConfig config; \
  946. config.SetFloatNDigits(3).SetFloatToStringMode(mode); \
  947. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); \
  948. TString expectedStr = TStringBuilder() << "{\"Float\":" << expectedValue << "}"; \
  949. UNIT_ASSERT_EQUAL_C(jsonStr.Str(), expectedStr, "real value is " << jsonStr.Str()); \
  950. } while (false)
  951. TEST_SINGLE(EFloatToStringMode::PREC_NDIGITS, 1234.18345, "1.23e+03");
  952. TEST_SINGLE(EFloatToStringMode::PREC_NDIGITS, 12.18345, "12.2");
  953. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12345.18355, "12345.184");
  954. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12.18355, "12.184");
  955. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12.18, "12.180");
  956. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12345.18355, "12345.184");
  957. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12.18355, "12.184");
  958. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12.18, "12.18");
  959. #undef TEST_SINGLE
  960. } // TestFloatToString
  961. Y_UNIT_TEST(TestAny) {
  962. TProto2JsonConfig config;
  963. config.SetConvertAny(true);
  964. TString modelStr(R"_({"Any":{"@type":"type.googleapis.com/NProtobufJsonTest.TFlatOptional","String":"value\""}})_");
  965. TFlatOptional proto;
  966. proto.SetString(R"_(value")_");
  967. TContainsAny protoWithAny;
  968. protoWithAny.MutableAny()->PackFrom(proto);
  969. TStringStream jsonStr;
  970. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(protoWithAny, jsonStr, config));
  971. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  972. }
  973. } // TProto2JsonTest