object_static.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 JSON object with StaticJsonDocument") {
  7. SECTION("BufferOfTheRightSizeForEmptyObject") {
  8. StaticJsonDocument<JSON_OBJECT_SIZE(0)> doc;
  9. char input[] = "{}";
  10. DeserializationError err = deserializeJson(doc, input);
  11. REQUIRE(err == DeserializationError::Ok);
  12. }
  13. SECTION("TooSmallBufferForObjectWithOneValue") {
  14. StaticJsonDocument<JSON_OBJECT_SIZE(0)> doc;
  15. char input[] = "{\"a\":1}";
  16. DeserializationError err = deserializeJson(doc, input);
  17. REQUIRE(err == DeserializationError::NoMemory);
  18. }
  19. SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
  20. StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
  21. char input[] = "{\"a\":1}";
  22. DeserializationError err = deserializeJson(doc, input);
  23. REQUIRE(err == DeserializationError::Ok);
  24. }
  25. SECTION("TooSmallBufferForObjectWithNestedObject") {
  26. StaticJsonDocument<JSON_OBJECT_SIZE(0) + JSON_ARRAY_SIZE(0)> doc;
  27. char input[] = "{\"a\":[]}";
  28. DeserializationError err = deserializeJson(doc, input);
  29. REQUIRE(err == DeserializationError::NoMemory);
  30. }
  31. SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
  32. StaticJsonDocument<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> doc;
  33. char input[] = "{\"a\":[]}";
  34. DeserializationError err = deserializeJson(doc, input);
  35. REQUIRE(err == DeserializationError::Ok);
  36. }
  37. SECTION("Should clear the JsonObject") {
  38. StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
  39. char input[] = "{\"hello\":\"world\"}";
  40. deserializeJson(doc, input);
  41. deserializeJson(doc, "{}");
  42. REQUIRE(doc.as<JsonObject>().size() == 0);
  43. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
  44. }
  45. }