// ArduinoJson - https://arduinojson.org // Copyright Benoit Blanchon 2014-2021 // MIT License #include #include TEST_CASE("deserialize JSON array with a StaticJsonDocument") { SECTION("BufferOfTheRightSizeForEmptyArray") { StaticJsonDocument doc; char input[] = "[]"; DeserializationError err = deserializeJson(doc, input); REQUIRE(err == DeserializationError::Ok); } SECTION("TooSmallBufferForArrayWithOneValue") { StaticJsonDocument doc; char input[] = "[1]"; DeserializationError err = deserializeJson(doc, input); REQUIRE(err == DeserializationError::NoMemory); } SECTION("BufferOfTheRightSizeForArrayWithOneValue") { StaticJsonDocument doc; char input[] = "[1]"; DeserializationError err = deserializeJson(doc, input); REQUIRE(err == DeserializationError::Ok); } SECTION("TooSmallBufferForArrayWithNestedObject") { StaticJsonDocument doc; char input[] = "[{}]"; DeserializationError err = deserializeJson(doc, input); REQUIRE(err == DeserializationError::NoMemory); } SECTION("BufferOfTheRightSizeForArrayWithNestedObject") { StaticJsonDocument doc; char input[] = "[{}]"; DeserializationError err = deserializeJson(doc, input); REQUIRE(err == DeserializationError::Ok); } SECTION("CopyStringNotSpaces") { StaticJsonDocument<100> doc; deserializeJson(doc, " [ \"1234567\" ] "); REQUIRE(JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(7) == doc.memoryUsage()); // note: we use a string of 8 bytes to be sure that the StaticMemoryPool // will not insert bytes to enforce alignement } SECTION("Should clear the JsonArray") { StaticJsonDocument doc; char input[] = "[1,2,3,4]"; deserializeJson(doc, input); deserializeJson(doc, "[]"); JsonArray arr = doc.as(); REQUIRE(arr.size() == 0); REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0)); } SECTION("Array") { StaticJsonDocument doc; char input[] = "[1,2]"; DeserializationError err = deserializeJson(doc, input); JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2)); REQUIRE(arr[0] == 1); REQUIRE(arr[1] == 2); } }