deserializeObject.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("deserialize MsgPack object") {
  7. DynamicJsonDocument doc(4096);
  8. SECTION("fixmap") {
  9. SECTION("empty") {
  10. const char* input = "\x80";
  11. DeserializationError error = deserializeMsgPack(doc, input);
  12. JsonObject obj = doc.as<JsonObject>();
  13. REQUIRE(error == DeserializationError::Ok);
  14. REQUIRE(doc.is<JsonObject>());
  15. REQUIRE(obj.size() == 0);
  16. }
  17. SECTION("two integers") {
  18. const char* input = "\x82\xA3one\x01\xA3two\x02";
  19. DeserializationError error = deserializeMsgPack(doc, input);
  20. JsonObject obj = doc.as<JsonObject>();
  21. REQUIRE(error == DeserializationError::Ok);
  22. REQUIRE(doc.is<JsonObject>());
  23. REQUIRE(obj.size() == 2);
  24. REQUIRE(obj["one"] == 1);
  25. REQUIRE(obj["two"] == 2);
  26. }
  27. SECTION("key is str 8") {
  28. const char* input = "\x82\xd9\x03one\x01\xd9\x03two\x02";
  29. DeserializationError error = deserializeMsgPack(doc, input);
  30. JsonObject obj = doc.as<JsonObject>();
  31. REQUIRE(error == DeserializationError::Ok);
  32. REQUIRE(doc.is<JsonObject>());
  33. REQUIRE(obj.size() == 2);
  34. REQUIRE(obj["one"] == 1);
  35. REQUIRE(obj["two"] == 2);
  36. }
  37. SECTION("key is str 16") {
  38. const char* input = "\x82\xda\x00\x03one\x01\xda\x00\x03two\x02";
  39. DeserializationError error = deserializeMsgPack(doc, input);
  40. JsonObject obj = doc.as<JsonObject>();
  41. REQUIRE(error == DeserializationError::Ok);
  42. REQUIRE(doc.is<JsonObject>());
  43. REQUIRE(obj.size() == 2);
  44. REQUIRE(obj["one"] == 1);
  45. REQUIRE(obj["two"] == 2);
  46. }
  47. SECTION("key is str 32") {
  48. const char* input =
  49. "\x82\xdb\x00\x00\x00\x03one\x01\xdb\x00\x00\x00\x03two\x02";
  50. DeserializationError error = deserializeMsgPack(doc, input);
  51. JsonObject obj = doc.as<JsonObject>();
  52. REQUIRE(error == DeserializationError::Ok);
  53. REQUIRE(doc.is<JsonObject>());
  54. REQUIRE(obj.size() == 2);
  55. REQUIRE(obj["one"] == 1);
  56. REQUIRE(obj["two"] == 2);
  57. }
  58. }
  59. SECTION("map 16") {
  60. SECTION("empty") {
  61. const char* input = "\xDE\x00\x00";
  62. DeserializationError error = deserializeMsgPack(doc, input);
  63. JsonObject obj = doc.as<JsonObject>();
  64. REQUIRE(error == DeserializationError::Ok);
  65. REQUIRE(doc.is<JsonObject>());
  66. REQUIRE(obj.size() == 0);
  67. }
  68. SECTION("two strings") {
  69. const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
  70. DeserializationError error = deserializeMsgPack(doc, input);
  71. JsonObject obj = doc.as<JsonObject>();
  72. REQUIRE(error == DeserializationError::Ok);
  73. REQUIRE(doc.is<JsonObject>());
  74. REQUIRE(obj.size() == 2);
  75. REQUIRE(obj["H"] == "hello");
  76. REQUIRE(obj["W"] == "world");
  77. }
  78. }
  79. SECTION("map 32") {
  80. SECTION("empty") {
  81. const char* input = "\xDF\x00\x00\x00\x00";
  82. DeserializationError error = deserializeMsgPack(doc, input);
  83. JsonObject obj = doc.as<JsonObject>();
  84. REQUIRE(error == DeserializationError::Ok);
  85. REQUIRE(doc.is<JsonObject>());
  86. REQUIRE(obj.size() == 0);
  87. }
  88. SECTION("two floats") {
  89. const char* input =
  90. "\xDF\x00\x00\x00\x02\xA4zero\xCA\x00\x00\x00\x00\xA2pi\xCA\x40\x48"
  91. "\xF5\xC3";
  92. DeserializationError error = deserializeMsgPack(doc, input);
  93. JsonObject obj = doc.as<JsonObject>();
  94. REQUIRE(error == DeserializationError::Ok);
  95. REQUIRE(doc.is<JsonObject>());
  96. REQUIRE(obj.size() == 2);
  97. REQUIRE(obj["zero"] == 0.0f);
  98. REQUIRE(obj["pi"] == 3.14f);
  99. }
  100. }
  101. }