json_writer_ut.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <library/cpp/json/json_writer.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/stream/str.h>
  4. using namespace NJson;
  5. Y_UNIT_TEST_SUITE(TJsonWriterTest) {
  6. Y_UNIT_TEST(SimpleWriteTest) {
  7. TString expected1 = "{\"key1\":1,\"key2\":2,\"key3\":3";
  8. TString expected2 = expected1 + ",\"array\":[\"stroka\",false]";
  9. TString expected3 = expected2 + "}";
  10. TStringStream out;
  11. TJsonWriter json(&out, false);
  12. json.OpenMap();
  13. json.Write("key1", (ui16)1);
  14. json.WriteKey("key2");
  15. json.Write((i32)2);
  16. json.Write("key3", (ui64)3);
  17. UNIT_ASSERT(out.Empty());
  18. json.Flush();
  19. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected1);
  20. json.Write("array");
  21. json.OpenArray();
  22. json.Write("stroka");
  23. json.Write(false);
  24. json.CloseArray();
  25. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected1);
  26. json.Flush();
  27. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected2);
  28. json.CloseMap();
  29. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected2);
  30. json.Flush();
  31. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected3);
  32. }
  33. Y_UNIT_TEST(SimpleWriteValueTest) {
  34. TString expected = "{\"key1\":null,\"key2\":{\"subkey1\":[1,{\"subsubkey\":\"test2\"},null,true],\"subkey2\":\"test\"}}";
  35. TJsonValue v;
  36. v["key1"] = JSON_NULL;
  37. v["key2"]["subkey1"].AppendValue(1);
  38. v["key2"]["subkey1"].AppendValue(JSON_MAP)["subsubkey"] = "test2";
  39. v["key2"]["subkey1"].AppendValue(JSON_NULL);
  40. v["key2"]["subkey1"].AppendValue(true);
  41. v["key2"]["subkey2"] = "test";
  42. TStringStream out;
  43. WriteJson(&out, &v);
  44. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  45. }
  46. Y_UNIT_TEST(FormatOutput) {
  47. TString expected = "{\n \"key1\":null,\n \"key2\":\n {\n \"subkey1\":\n [\n 1,\n {\n \"subsubkey\":\"test2\"\n },\n null,\n true\n ],\n \"subkey2\":\"test\"\n }\n}";
  48. TJsonValue v;
  49. v["key1"] = JSON_NULL;
  50. v["key2"]["subkey1"].AppendValue(1);
  51. v["key2"]["subkey1"].AppendValue(JSON_MAP)["subsubkey"] = "test2";
  52. v["key2"]["subkey1"].AppendValue(JSON_NULL);
  53. v["key2"]["subkey1"].AppendValue(true);
  54. v["key2"]["subkey2"] = "test";
  55. TStringStream out;
  56. WriteJson(&out, &v, true);
  57. UNIT_ASSERT_STRINGS_EQUAL(out.Str(), expected);
  58. }
  59. Y_UNIT_TEST(SortKeys) {
  60. TString expected = "{\"a\":null,\"j\":null,\"n\":null,\"y\":null,\"z\":null}";
  61. TJsonValue v;
  62. v["z"] = JSON_NULL;
  63. v["n"] = JSON_NULL;
  64. v["a"] = JSON_NULL;
  65. v["y"] = JSON_NULL;
  66. v["j"] = JSON_NULL;
  67. TStringStream out;
  68. WriteJson(&out, &v, false, true);
  69. UNIT_ASSERT_STRINGS_EQUAL(out.Str(), expected);
  70. }
  71. Y_UNIT_TEST(SimpleUnsignedIntegerWriteTest) {
  72. {
  73. TString expected = "{\"test\":1}";
  74. TJsonValue v;
  75. v.InsertValue("test", 1ull);
  76. TStringStream out;
  77. WriteJson(&out, &v);
  78. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  79. } // 1
  80. {
  81. TString expected = "{\"test\":-1}";
  82. TJsonValue v;
  83. v.InsertValue("test", -1);
  84. TStringStream out;
  85. WriteJson(&out, &v);
  86. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  87. } // -1
  88. {
  89. TString expected = "{\"test\":18446744073709551615}";
  90. TJsonValue v;
  91. v.InsertValue("test", 18446744073709551615ull);
  92. TStringStream out;
  93. WriteJson(&out, &v);
  94. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  95. } // 18446744073709551615
  96. {
  97. TString expected = "{\"test\":[1,18446744073709551615]}";
  98. TJsonValue v;
  99. v.InsertValue("test", TJsonValue());
  100. v["test"].AppendValue(1);
  101. v["test"].AppendValue(18446744073709551615ull);
  102. TStringStream out;
  103. WriteJson(&out, &v);
  104. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  105. } // 18446744073709551615
  106. } // SimpleUnsignedIntegerWriteTest
  107. Y_UNIT_TEST(WriteOptionalTest) {
  108. {
  109. TString expected = "{\"test\":1}";
  110. TStringStream out;
  111. TJsonWriter json(&out, false);
  112. json.OpenMap();
  113. json.WriteOptional("test", MakeMaybe<int>(1));
  114. json.CloseMap();
  115. json.Flush();
  116. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  117. }
  118. {
  119. TString expected = "{}";
  120. TStringStream out;
  121. TMaybe<int> nothing = Nothing();
  122. TJsonWriter json(&out, false);
  123. json.OpenMap();
  124. json.WriteOptional("test", nothing);
  125. json.CloseMap();
  126. json.Flush();
  127. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  128. }
  129. {
  130. TString expected = "{}";
  131. TStringStream out;
  132. TMaybe<int> empty;
  133. TJsonWriter json(&out, false);
  134. json.OpenMap();
  135. json.WriteOptional("test", empty);
  136. json.CloseMap();
  137. json.Flush();
  138. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  139. }
  140. {
  141. TString expected = "{}";
  142. TStringStream out;
  143. TJsonWriter json(&out, false);
  144. json.OpenMap();
  145. json.WriteOptional("test", Nothing());
  146. json.CloseMap();
  147. json.Flush();
  148. UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected);
  149. }
  150. }
  151. Y_UNIT_TEST(Callback) {
  152. NJsonWriter::TBuf json;
  153. json.WriteString("A");
  154. UNIT_ASSERT_VALUES_EQUAL(json.Str(), "\"A\"");
  155. UNIT_ASSERT_VALUES_EQUAL(WrapJsonToCallback(json, ""), "\"A\"");
  156. UNIT_ASSERT_VALUES_EQUAL(WrapJsonToCallback(json, "Foo"), "Foo(\"A\")");
  157. }
  158. Y_UNIT_TEST(FloatPrecision) {
  159. const double value = 1517933989.4242;
  160. const NJson::TJsonValue json(value);
  161. NJson::TJsonWriterConfig config;
  162. {
  163. TString expected = "1517933989";
  164. TString actual = NJson::WriteJson(json);
  165. UNIT_ASSERT_VALUES_EQUAL(actual, expected);
  166. }
  167. {
  168. TString expected = "1517933989";
  169. TStringStream ss;
  170. NJson::WriteJson(&ss, &json, config);
  171. TString actual = ss.Str();
  172. UNIT_ASSERT_VALUES_EQUAL(actual, expected);
  173. }
  174. {
  175. config.DoubleNDigits = 13;
  176. TString expected = "1517933989.424";
  177. TStringStream ss;
  178. NJson::WriteJson(&ss, &json, config);
  179. TString actual = ss.Str();
  180. UNIT_ASSERT_VALUES_EQUAL(actual, expected);
  181. }
  182. {
  183. config.DoubleNDigits = 6;
  184. config.FloatToStringMode = PREC_POINT_DIGITS;
  185. TString expected = "1517933989.424200";
  186. TStringStream ss;
  187. NJson::WriteJson(&ss, &json, config);
  188. TString actual = ss.Str();
  189. UNIT_ASSERT_VALUES_EQUAL(actual, expected);
  190. }
  191. }
  192. }