serializeVariant.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Literals.hpp"
  7. template <typename T>
  8. static void checkVariant(T value, const char* expected_data,
  9. size_t expected_len) {
  10. JsonDocument doc;
  11. JsonVariant variant = doc.to<JsonVariant>();
  12. variant.set(value);
  13. std::string expected(expected_data, expected_data + expected_len);
  14. std::string actual;
  15. size_t len = serializeMsgPack(variant, actual);
  16. CAPTURE(variant);
  17. REQUIRE(len == expected_len);
  18. REQUIRE(actual == expected);
  19. }
  20. template <typename T, size_t N>
  21. static void checkVariant(T value, const char (&expected_data)[N]) {
  22. const size_t expected_len = N - 1;
  23. checkVariant(value, expected_data, expected_len);
  24. }
  25. template <typename T>
  26. static void checkVariant(T value, const std::string& expected) {
  27. checkVariant(value, expected.data(), expected.length());
  28. }
  29. TEST_CASE("serialize MsgPack value") {
  30. SECTION("unbound") {
  31. checkVariant(JsonVariant(), "\xC0"); // we represent undefined as nil
  32. }
  33. SECTION("nil") {
  34. const char* nil = 0; // ArduinoJson uses a string for null
  35. checkVariant(nil, "\xC0");
  36. }
  37. SECTION("bool") {
  38. checkVariant(false, "\xC2");
  39. checkVariant(true, "\xC3");
  40. }
  41. SECTION("positive fixint") {
  42. SECTION("signed") {
  43. checkVariant(0, "\x00");
  44. checkVariant(127, "\x7F");
  45. }
  46. SECTION("unsigned") {
  47. checkVariant(0U, "\x00");
  48. checkVariant(127U, "\x7F");
  49. }
  50. }
  51. SECTION("uint 8") {
  52. checkVariant(128, "\xCC\x80");
  53. checkVariant(255, "\xCC\xFF");
  54. }
  55. SECTION("uint 16") {
  56. checkVariant(256, "\xCD\x01\x00");
  57. checkVariant(0xFFFF, "\xCD\xFF\xFF");
  58. }
  59. SECTION("uint 32") {
  60. checkVariant(0x00010000U, "\xCE\x00\x01\x00\x00");
  61. checkVariant(0x12345678U, "\xCE\x12\x34\x56\x78");
  62. checkVariant(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF");
  63. }
  64. #if ARDUINOJSON_USE_LONG_LONG
  65. SECTION("uint 64") {
  66. checkVariant(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00");
  67. checkVariant(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
  68. checkVariant(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
  69. }
  70. #endif
  71. SECTION("negative fixint") {
  72. checkVariant(-1, "\xFF");
  73. checkVariant(-32, "\xE0");
  74. }
  75. SECTION("int 8") {
  76. checkVariant(-33, "\xD0\xDF");
  77. checkVariant(-128, "\xD0\x80");
  78. }
  79. SECTION("int 16") {
  80. checkVariant(-129, "\xD1\xFF\x7F");
  81. checkVariant(-32768, "\xD1\x80\x00");
  82. }
  83. SECTION("int 32") {
  84. checkVariant(-32769, "\xD2\xFF\xFF\x7F\xFF");
  85. checkVariant(-2147483647 - 1, "\xD2\x80\x00\x00\x00");
  86. }
  87. #if ARDUINOJSON_USE_LONG_LONG
  88. SECTION("int 64") {
  89. checkVariant(int64_t(0xFEDCBA9876543210),
  90. "\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10");
  91. }
  92. #endif
  93. SECTION("float 32") {
  94. checkVariant(1.25, "\xCA\x3F\xA0\x00\x00");
  95. checkVariant(9.22337204e+18f, "\xca\x5f\x00\x00\x00");
  96. }
  97. SECTION("float 64") {
  98. checkVariant(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F");
  99. }
  100. SECTION("fixstr") {
  101. checkVariant("", "\xA0");
  102. checkVariant("hello world hello world hello !",
  103. "\xBFhello world hello world hello !");
  104. }
  105. SECTION("str 8") {
  106. checkVariant("hello world hello world hello !!",
  107. "\xD9\x20hello world hello world hello !!");
  108. }
  109. SECTION("str 16") {
  110. std::string shortest(256, '?');
  111. checkVariant(shortest.c_str(), "\xDA\x01\x00"_s + shortest);
  112. std::string longest(65535, '?');
  113. checkVariant(longest.c_str(), "\xDA\xFF\xFF"_s + longest);
  114. }
  115. SECTION("str 32") {
  116. std::string shortest(65536, '?');
  117. checkVariant(JsonString(shortest.c_str(), true), // force store by pointer
  118. "\xDB\x00\x01\x00\x00"_s + shortest);
  119. }
  120. SECTION("serialized(const char*)") {
  121. checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");
  122. checkVariant(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
  123. }
  124. SECTION("bin 8") {
  125. checkVariant(MsgPackBinary("?", 1), "\xC4\x01?");
  126. }
  127. SECTION("bin 16") {
  128. auto str = std::string(256, '?');
  129. checkVariant(MsgPackBinary(str.data(), str.size()), "\xC5\x01\x00"_s + str);
  130. }
  131. // bin 32 is tested in string_length_size_4.cpp
  132. SECTION("fixext 1") {
  133. checkVariant(MsgPackExtension(1, "\x02", 1), "\xD4\x01\x02");
  134. }
  135. SECTION("fixext 2") {
  136. checkVariant(MsgPackExtension(1, "\x03\x04", 2), "\xD5\x01\x03\x04");
  137. }
  138. SECTION("fixext 4") {
  139. checkVariant(MsgPackExtension(1, "\x05\x06\x07\x08", 4),
  140. "\xD6\x01\x05\x06\x07\x08");
  141. }
  142. SECTION("fixext 8") {
  143. checkVariant(MsgPackExtension(1, "????????", 8), "\xD7\x01????????");
  144. }
  145. SECTION("fixext 16") {
  146. checkVariant(MsgPackExtension(1, "????????????????", 16),
  147. "\xD8\x01????????????????");
  148. }
  149. SECTION("ext 8") {
  150. checkVariant(MsgPackExtension(2, "???", 3), "\xC7\x03\x02???");
  151. checkVariant(MsgPackExtension(2, "?????", 5), "\xC7\x05\x02?????");
  152. checkVariant(MsgPackExtension(2, "???????", 7), "\xC7\x07\x02???????");
  153. checkVariant(MsgPackExtension(2, "?????????", 9), "\xC7\x09\x02?????????");
  154. checkVariant(MsgPackExtension(2, "???????????????", 15),
  155. "\xC7\x0F\x02???????????????");
  156. checkVariant(MsgPackExtension(2, "?????????????????", 17),
  157. "\xC7\x11\x02?????????????????");
  158. }
  159. SECTION("ext 16") {
  160. auto str = std::string(256, '?');
  161. checkVariant(MsgPackExtension(2, str.data(), str.size()),
  162. "\xC8\x01\x00\x02"_s + str);
  163. }
  164. SECTION("serialize round double as integer") { // Issue #1718
  165. checkVariant(-32768.0, "\xD1\x80\x00");
  166. checkVariant(-129.0, "\xD1\xFF\x7F");
  167. checkVariant(-128.0, "\xD0\x80");
  168. checkVariant(-33.0, "\xD0\xDF");
  169. checkVariant(-32.0, "\xE0");
  170. checkVariant(-1.0, "\xFF");
  171. checkVariant(0.0, "\x00");
  172. checkVariant(127.0, "\x7F");
  173. checkVariant(128.0, "\xCC\x80");
  174. checkVariant(255.0, "\xCC\xFF");
  175. checkVariant(256.0, "\xCD\x01\x00");
  176. }
  177. }