proto2json_ut.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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(TestMapSortedKeys) {
  844. TMapType proto;
  845. auto& items = *proto.MutableItems();
  846. items["key1"] = "value1";
  847. items["key2"] = "value2";
  848. items["key3"] = "value3";
  849. TString modelStr(R"_({"Items":{"key1":"value1","key2":"value2","key3":"value3"}})_");
  850. TStringStream jsonStr;
  851. auto config = TProto2JsonConfig().SetMapAsObject(true).SetSortMapKeys(false);
  852. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  853. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  854. } // TestMapSortedKeys
  855. Y_UNIT_TEST(TestMapDefaultValue) {
  856. TMapType proto;
  857. auto& items = *proto.MutableItems();
  858. items["key1"] = "";
  859. TString modelStr(R"_({"Items":{"key1":""}})_");
  860. TStringStream jsonStr;
  861. TProto2JsonConfig config;
  862. config.MapAsObject = true;
  863. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  864. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  865. jsonStr.Clear();
  866. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr));
  867. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  868. } // TestMapDefaultValue
  869. Y_UNIT_TEST(TestMapDefaultMessageValue) {
  870. TComplexMapType proto;
  871. auto& map = *proto.MutableNested();
  872. map["key1"]; // Creates an empty nested message
  873. TString modelStr(R"_({"Nested":{"key1":{}}})_");
  874. TStringStream jsonStr;
  875. TProto2JsonConfig config;
  876. config.MapAsObject = true;
  877. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config));
  878. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  879. jsonStr.Clear();
  880. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr));
  881. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  882. } // TestMapDefaultMessageValue
  883. Y_UNIT_TEST(TestStringifyNumbers) {
  884. #define TEST_SINGLE(flag, field, value, expectString) \
  885. do { \
  886. TFlatOptional proto; \
  887. proto.Set##field(value); \
  888. \
  889. TStringStream jsonStr; \
  890. TProto2JsonConfig config; \
  891. config.SetStringifyNumbers(flag); \
  892. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); \
  893. if (expectString) { \
  894. UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"" #field "\":\"" #value "\"}"); \
  895. } else { \
  896. UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"" #field "\":" #value "}"); \
  897. } \
  898. } while (false)
  899. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 1, false);
  900. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 1000000000, false);
  901. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 10000000000000000, false);
  902. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -1, false);
  903. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -1000000000, false);
  904. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -10000000000000000, false);
  905. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 1, false);
  906. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 1000000000, false);
  907. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 10000000000000000, true);
  908. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -1, false);
  909. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -1000000000, false);
  910. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -10000000000000000, true);
  911. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 1, false);
  912. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 1000000000, true);
  913. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 10000000000000000, true);
  914. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -1, false);
  915. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -1000000000, true);
  916. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -10000000000000000, true);
  917. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 1, true);
  918. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI32, 1000000000, false);
  919. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 10000000000000000, true);
  920. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI64, -1, true);
  921. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI32, -1000000000, false);
  922. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI64, -10000000000000000, true);
  923. #undef TEST_SINGLE
  924. } // TestStringifyNumbers
  925. Y_UNIT_TEST(TestStringifyNumbersRepeated) {
  926. #define TEST_SINGLE(flag, field, value) \
  927. do { \
  928. TFlatRepeated proto; \
  929. proto.Add##field(value); \
  930. \
  931. TStringStream jsonStr; \
  932. TProto2JsonConfig config; \
  933. config.SetStringifyNumbers(flag); \
  934. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); \
  935. UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"" #field "\":[" #value "]}"); \
  936. } while (false)
  937. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 1);
  938. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 10000000000000000);
  939. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 1);
  940. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 10000000000000000);
  941. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 1);
  942. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 10000000000000000);
  943. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 1);
  944. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 10000000000000000);
  945. #undef TEST_SINGLE
  946. } // TestStringifyNumbersRepeated
  947. Y_UNIT_TEST(TestStringifyNumbersRepeatedStringification){
  948. #define TEST_SINGLE(flag, field, value, expectString) \
  949. do { \
  950. TFlatRepeated proto; \
  951. proto.Add##field(value); \
  952. \
  953. TStringStream jsonStr; \
  954. TProto2JsonConfig config; \
  955. config.SetStringifyNumbersRepeated(flag); \
  956. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); \
  957. if (expectString) { \
  958. UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"" #field "\":[\"" #value "\"]}"); \
  959. } else { \
  960. UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"" #field "\":[" #value "]}"); \
  961. } \
  962. } while (false)
  963. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 1, false);
  964. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 1000000000, false);
  965. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, 10000000000000000, false);
  966. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -1, false);
  967. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -1000000000, false);
  968. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, SI64, -10000000000000000, false);
  969. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 1, false);
  970. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 1000000000, false);
  971. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, 10000000000000000, true);
  972. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -1, false);
  973. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -1000000000, false);
  974. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, SI64, -10000000000000000, true);
  975. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 1, false);
  976. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 1000000000, true);
  977. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, 10000000000000000, true);
  978. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -1, false);
  979. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -1000000000, true);
  980. TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, SI64, -10000000000000000, true);
  981. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 1, true);
  982. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI32, 1000000000, false);
  983. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, UI64, 10000000000000000, true);
  984. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI64, -1, true);
  985. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI32, -1000000000, false);
  986. TEST_SINGLE(TProto2JsonConfig::StringifyInt64Always, SI64, -10000000000000000, true);
  987. #undef TEST_SINGLE
  988. } // TestStringifyNumbersRepeatedStringification
  989. Y_UNIT_TEST(TestStringifyNumbersRepeatedStringificationList){
  990. using NJson::JSON_STRING;
  991. using NJson::JSON_UINTEGER;
  992. using NJson::JSON_INTEGER;
  993. TFlatRepeated proto;
  994. proto.AddUI64(1);
  995. proto.AddUI64(1000000000);
  996. proto.AddUI64(10000000000000000);
  997. proto.AddSI64(1);
  998. proto.AddSI64(1000000000);
  999. proto.AddSI64(10000000000000000);
  1000. proto.AddSI64(-1);
  1001. proto.AddSI64(-1000000000);
  1002. proto.AddSI64(-10000000000000000);
  1003. proto.AddUI32(1);
  1004. proto.AddUI32(1000000000);
  1005. proto.AddSI32(-1);
  1006. proto.AddSI32(-1000000000);
  1007. TProto2JsonConfig config;
  1008. NJson::TJsonValue jsonValue;
  1009. THashMap<TString, NJson::TJsonValue> jsonMap;
  1010. {
  1011. jsonValue = NJson::TJsonValue{};
  1012. config.SetStringifyNumbersRepeated(TProto2JsonConfig::StringifyLongNumbersNever);
  1013. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonValue, config));
  1014. jsonMap = jsonValue.GetMap();
  1015. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[0].GetType(), JSON_UINTEGER);
  1016. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[1].GetType(), JSON_UINTEGER);
  1017. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[2].GetType(), JSON_UINTEGER);
  1018. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[0].GetType(), JSON_INTEGER);
  1019. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[1].GetType(), JSON_INTEGER);
  1020. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[2].GetType(), JSON_INTEGER);
  1021. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[3].GetType(), JSON_INTEGER);
  1022. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[4].GetType(), JSON_INTEGER);
  1023. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[5].GetType(), JSON_INTEGER);
  1024. }
  1025. {
  1026. jsonValue = NJson::TJsonValue{};
  1027. config.SetStringifyNumbersRepeated(TProto2JsonConfig::StringifyLongNumbersForDouble);
  1028. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonValue, config));
  1029. jsonMap = jsonValue.GetMap();
  1030. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[0].GetType(), JSON_UINTEGER);
  1031. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[1].GetType(), JSON_UINTEGER);
  1032. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[2].GetType(), JSON_STRING);
  1033. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[0].GetType(), JSON_INTEGER);
  1034. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[1].GetType(), JSON_INTEGER);
  1035. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[2].GetType(), JSON_STRING);
  1036. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[3].GetType(), JSON_INTEGER);
  1037. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[4].GetType(), JSON_INTEGER);
  1038. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[5].GetType(), JSON_STRING);
  1039. UNIT_ASSERT_EQUAL(jsonMap.at("SI32")[0].GetType(), JSON_INTEGER);
  1040. UNIT_ASSERT_EQUAL(jsonMap.at("SI32")[1].GetType(), JSON_INTEGER);
  1041. UNIT_ASSERT_EQUAL(jsonMap.at("UI32")[0].GetType(), JSON_UINTEGER);
  1042. UNIT_ASSERT_EQUAL(jsonMap.at("UI32")[1].GetType(), JSON_UINTEGER);
  1043. }
  1044. {
  1045. jsonValue = NJson::TJsonValue{};
  1046. config.SetStringifyNumbersRepeated(TProto2JsonConfig::StringifyLongNumbersForFloat);
  1047. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonValue, config));
  1048. jsonMap = jsonValue.GetMap();
  1049. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[0].GetType(), JSON_UINTEGER);
  1050. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[1].GetType(), JSON_STRING);
  1051. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[2].GetType(), JSON_STRING);
  1052. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[0].GetType(), JSON_INTEGER);
  1053. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[1].GetType(), JSON_STRING);
  1054. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[2].GetType(), JSON_STRING);
  1055. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[3].GetType(), JSON_INTEGER);
  1056. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[4].GetType(), JSON_STRING);
  1057. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[5].GetType(), JSON_STRING);
  1058. UNIT_ASSERT_EQUAL(jsonMap.at("SI32")[0].GetType(), JSON_INTEGER);
  1059. UNIT_ASSERT_EQUAL(jsonMap.at("SI32")[1].GetType(), JSON_STRING);
  1060. UNIT_ASSERT_EQUAL(jsonMap.at("UI32")[0].GetType(), JSON_UINTEGER);
  1061. UNIT_ASSERT_EQUAL(jsonMap.at("UI32")[1].GetType(), JSON_STRING);
  1062. }
  1063. {
  1064. jsonValue = NJson::TJsonValue{};
  1065. config.SetStringifyNumbersRepeated(TProto2JsonConfig::StringifyInt64Always);
  1066. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonValue, config));
  1067. jsonMap = jsonValue.GetMap();
  1068. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[0].GetType(), JSON_STRING);
  1069. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[1].GetType(), JSON_STRING);
  1070. UNIT_ASSERT_EQUAL(jsonMap.at("UI64")[2].GetType(), JSON_STRING);
  1071. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[0].GetType(), JSON_STRING);
  1072. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[1].GetType(), JSON_STRING);
  1073. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[2].GetType(), JSON_STRING);
  1074. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[3].GetType(), JSON_STRING);
  1075. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[4].GetType(), JSON_STRING);
  1076. UNIT_ASSERT_EQUAL(jsonMap.at("SI64")[5].GetType(), JSON_STRING);
  1077. UNIT_ASSERT_EQUAL(jsonMap.at("SI32")[0].GetType(), JSON_INTEGER);
  1078. UNIT_ASSERT_EQUAL(jsonMap.at("SI32")[1].GetType(), JSON_INTEGER);
  1079. UNIT_ASSERT_EQUAL(jsonMap.at("UI32")[0].GetType(), JSON_UINTEGER);
  1080. UNIT_ASSERT_EQUAL(jsonMap.at("UI32")[1].GetType(), JSON_UINTEGER);
  1081. }
  1082. } // TestStringifyNumbersRepeatedStringificationList
  1083. Y_UNIT_TEST(TestExtension) {
  1084. TExtensionField proto;
  1085. proto.SetExtension(bar, 1);
  1086. Y_ASSERT(proto.HasExtension(bar));
  1087. UNIT_ASSERT_EQUAL(Proto2Json(proto, TProto2JsonConfig()), "{\"NProtobufJsonTest.bar\":1}");
  1088. TProto2JsonConfig cfg;
  1089. cfg.SetExtensionFieldNameMode(TProto2JsonConfig::ExtFldNameShort);
  1090. UNIT_ASSERT_EQUAL(Proto2Json(proto, cfg), "{\"bar\":1}");
  1091. } // TestExtension
  1092. Y_UNIT_TEST(TestSimplifiedDuration) {
  1093. TString json;
  1094. TSingleDuration simpleDuration;
  1095. simpleDuration.mutable_duration()->set_seconds(10);
  1096. simpleDuration.mutable_duration()->set_nanos(101);
  1097. Proto2Json(simpleDuration, json, TProto2JsonConfig().SetConvertTimeAsString(true));
  1098. UNIT_ASSERT_EQUAL_C(json, "{\"Duration\":\"10.000000101s\"}", "real value is " << json);
  1099. } // TestSimplifiedDuration
  1100. Y_UNIT_TEST(TestSimplifiedTimestamp) {
  1101. TString json;
  1102. TSingleTimestamp simpleTimestamp;
  1103. simpleTimestamp.mutable_timestamp()->set_seconds(10000000);
  1104. simpleTimestamp.mutable_timestamp()->set_nanos(504);
  1105. Proto2Json(simpleTimestamp, json, TProto2JsonConfig().SetConvertTimeAsString(true));
  1106. UNIT_ASSERT_EQUAL_C(json, "{\"Timestamp\":\"1970-04-26T17:46:40.000000504Z\"}", "real value is " << json);
  1107. } // TestSimplifiedTimestamp
  1108. Y_UNIT_TEST(TestFloatToString) {
  1109. #define TEST_SINGLE(mode, value, expectedValue) \
  1110. do { \
  1111. TFlatOptional proto; \
  1112. proto.SetFloat(value); \
  1113. \
  1114. TStringStream jsonStr; \
  1115. TProto2JsonConfig config; \
  1116. config.SetFloatNDigits(3).SetFloatToStringMode(mode); \
  1117. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); \
  1118. TString expectedStr = TStringBuilder() << "{\"Float\":" << expectedValue << "}"; \
  1119. UNIT_ASSERT_EQUAL_C(jsonStr.Str(), expectedStr, "real value is " << jsonStr.Str()); \
  1120. } while (false)
  1121. TEST_SINGLE(EFloatToStringMode::PREC_NDIGITS, 1234.18345, "1.23e+03");
  1122. TEST_SINGLE(EFloatToStringMode::PREC_NDIGITS, 12.18345, "12.2");
  1123. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12345.18355, "12345.184");
  1124. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12.18355, "12.184");
  1125. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12.18, "12.180");
  1126. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12345.18355, "12345.184");
  1127. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12.18355, "12.184");
  1128. TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12.18, "12.18");
  1129. #undef TEST_SINGLE
  1130. } // TestFloatToString
  1131. Y_UNIT_TEST(TestAny) {
  1132. TProto2JsonConfig config;
  1133. config.SetConvertAny(true);
  1134. TString modelStr(R"_({"Any":{"@type":"type.googleapis.com/NProtobufJsonTest.TFlatOptional","String":"value\""}})_");
  1135. TFlatOptional proto;
  1136. proto.SetString(R"_(value")_");
  1137. TContainsAny protoWithAny;
  1138. protoWithAny.MutableAny()->PackFrom(proto);
  1139. TStringStream jsonStr;
  1140. UNIT_ASSERT_NO_EXCEPTION(Proto2Json(protoWithAny, jsonStr, config));
  1141. UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr);
  1142. }
  1143. } // TProto2JsonTest