array_static.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("deserialize JSON array with a StaticJsonDocument") {
  7. SECTION("BufferOfTheRightSizeForEmptyArray") {
  8. StaticJsonDocument<JSON_ARRAY_SIZE(0)> doc;
  9. char input[] = "[]";
  10. DeserializationError err = deserializeJson(doc, input);
  11. REQUIRE(err == DeserializationError::Ok);
  12. }
  13. SECTION("TooSmallBufferForArrayWithOneValue") {
  14. StaticJsonDocument<JSON_ARRAY_SIZE(0)> doc;
  15. char input[] = "[1]";
  16. DeserializationError err = deserializeJson(doc, input);
  17. REQUIRE(err == DeserializationError::NoMemory);
  18. }
  19. SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
  20. StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
  21. char input[] = "[1]";
  22. DeserializationError err = deserializeJson(doc, input);
  23. REQUIRE(err == DeserializationError::Ok);
  24. }
  25. SECTION("TooSmallBufferForArrayWithNestedObject") {
  26. StaticJsonDocument<JSON_ARRAY_SIZE(0) + JSON_OBJECT_SIZE(0)> doc;
  27. char input[] = "[{}]";
  28. DeserializationError err = deserializeJson(doc, input);
  29. REQUIRE(err == DeserializationError::NoMemory);
  30. }
  31. SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
  32. StaticJsonDocument<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> doc;
  33. char input[] = "[{}]";
  34. DeserializationError err = deserializeJson(doc, input);
  35. REQUIRE(err == DeserializationError::Ok);
  36. }
  37. SECTION("CopyStringNotSpaces") {
  38. StaticJsonDocument<100> doc;
  39. deserializeJson(doc, " [ \"1234567\" ] ");
  40. REQUIRE(JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(8) == doc.memoryUsage());
  41. // note: we use a string of 8 bytes to be sure that the StaticMemoryPool
  42. // will not insert bytes to enforce alignement
  43. }
  44. SECTION("Should clear the JsonArray") {
  45. StaticJsonDocument<JSON_ARRAY_SIZE(4)> doc;
  46. char input[] = "[1,2,3,4]";
  47. deserializeJson(doc, input);
  48. deserializeJson(doc, "[]");
  49. JsonArray arr = doc.as<JsonArray>();
  50. REQUIRE(arr.size() == 0);
  51. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
  52. }
  53. SECTION("Array") {
  54. StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
  55. char input[] = "[1,2]";
  56. DeserializationError err = deserializeJson(doc, input);
  57. JsonArray arr = doc.as<JsonArray>();
  58. REQUIRE(err == DeserializationError::Ok);
  59. REQUIRE(doc.is<JsonArray>());
  60. REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2));
  61. REQUIRE(arr[0] == 1);
  62. REQUIRE(arr[1] == 2);
  63. }
  64. }