Remove JSON_ARRAY_SIZE(), JSON_OBJECT_SIZE(), and JSON_STRING_SIZE()

This commit is contained in:
Benoit Blanchon
2023-03-29 19:18:06 +02:00
parent 0328f66340
commit 3f43c2b816
36 changed files with 395 additions and 340 deletions

View File

@ -5,6 +5,10 @@
#include <ArduinoJson.h>
#include <catch.hpp>
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("deserialize JSON array") {
JsonDocument doc(4096);
@ -248,13 +252,13 @@ TEST_CASE("deserialize JSON array") {
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0);
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
REQUIRE(doc.memoryUsage() == sizeofArray(0));
}
}
TEST_CASE("deserialize JSON array under memory constraints") {
SECTION("buffer of the right size for an empty array") {
JsonDocument doc(JSON_ARRAY_SIZE(0));
JsonDocument doc(sizeofArray(0));
char input[] = "[]";
DeserializationError err = deserializeJson(doc, input);
@ -263,7 +267,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
}
SECTION("buffer too small for an array with one element") {
JsonDocument doc(JSON_ARRAY_SIZE(0));
JsonDocument doc(sizeofArray(0));
char input[] = "[1]";
DeserializationError err = deserializeJson(doc, input);
@ -272,7 +276,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
}
SECTION("buffer of the right size for an array with one element") {
JsonDocument doc(JSON_ARRAY_SIZE(1));
JsonDocument doc(sizeofArray(1));
char input[] = "[1]";
DeserializationError err = deserializeJson(doc, input);
@ -281,7 +285,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
}
SECTION("buffer too small for an array with a nested object") {
JsonDocument doc(JSON_ARRAY_SIZE(0) + JSON_OBJECT_SIZE(0));
JsonDocument doc(sizeofArray(0) + sizeofObject(0));
char input[] = "[{}]";
DeserializationError err = deserializeJson(doc, input);
@ -290,7 +294,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
}
SECTION("buffer of the right size for an array with a nested object") {
JsonDocument doc(JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0));
JsonDocument doc(sizeofArray(1) + sizeofObject(0));
char input[] = "[{}]";
DeserializationError err = deserializeJson(doc, input);
@ -303,13 +307,13 @@ TEST_CASE("deserialize JSON array under memory constraints") {
deserializeJson(doc, " [ \"1234567\" ] ");
REQUIRE(JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(7) == doc.memoryUsage());
REQUIRE(sizeofArray(1) + sizeofString(7) == doc.memoryUsage());
// note: we use a string of 8 bytes to be sure that the MemoryPool
// will not insert bytes to enforce alignement
}
SECTION("Should clear the JsonArray") {
JsonDocument doc(JSON_ARRAY_SIZE(4));
JsonDocument doc(sizeofArray(4));
char input[] = "[1,2,3,4]";
deserializeJson(doc, input);
@ -317,11 +321,11 @@ TEST_CASE("deserialize JSON array under memory constraints") {
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0);
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
REQUIRE(doc.memoryUsage() == sizeofArray(0));
}
SECTION("buffer of the right size for an array with two element") {
JsonDocument doc(JSON_ARRAY_SIZE(2));
JsonDocument doc(sizeofArray(2));
char input[] = "[1,2]";
DeserializationError err = deserializeJson(doc, input);
@ -329,7 +333,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonArray>());
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2));
REQUIRE(doc.memoryUsage() == sizeofArray(2));
REQUIRE(arr[0] == 1);
REQUIRE(arr[1] == 2);
}

View File

@ -9,6 +9,9 @@
#include <sstream>
#include <string>
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
TEST_CASE("Filtering") {
struct TestCase {
const char* input;
@ -43,7 +46,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"abcdefg\":\"hijklmn\"}",
JSON_OBJECT_SIZE(1) + 16
sizeofObject(1) + 16
},
{
"{\"hello\":\"world\"}",
@ -51,7 +54,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// Input in an object, but filter wants an array
@ -69,7 +72,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":null}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// Member is a number, but filter wants an array
@ -78,7 +81,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":null}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// Input is an array, but filter wants an object
@ -114,7 +117,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// skip a float
@ -123,7 +126,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// skip false
@ -132,7 +135,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// skip true
@ -141,7 +144,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// skip null
@ -150,7 +153,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip a double-quoted string
@ -159,7 +162,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip a single-quoted string
@ -168,7 +171,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip an empty array
@ -177,7 +180,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip an empty array with spaces in it
@ -186,7 +189,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip an array
@ -195,7 +198,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip an array with spaces in it
@ -204,7 +207,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip an empty object
@ -213,7 +216,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip an empty object with spaces in it
@ -222,7 +225,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// can skip an object
@ -231,7 +234,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// skip an object with spaces in it
@ -240,7 +243,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":42}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
"{\"an_integer\": 0,\"example\":{\"type\":\"int\",\"outcome\":42}}",
@ -248,7 +251,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":{\"outcome\":42}}",
2 * JSON_OBJECT_SIZE(1) + 16
2 * sizeofObject(1) + 16
},
{
// wildcard
@ -257,7 +260,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":{\"outcome\":42}}",
2 * JSON_OBJECT_SIZE(1) + 16
2 * sizeofObject(1) + 16
},
{
// exclusion filter (issue #1628)
@ -266,7 +269,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{\"example\":1}",
JSON_OBJECT_SIZE(1) + 8
sizeofObject(1) + 8
},
{
// only the first element of array counts
@ -275,7 +278,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"[1,2,3]",
JSON_ARRAY_SIZE(3)
sizeofArray(3)
},
{
// only the first element of array counts
@ -284,7 +287,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"[]",
JSON_ARRAY_SIZE(0)
sizeofArray(0)
},
{
// filter members of object in array
@ -293,7 +296,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"[{\"example\":1},{\"example\":3}]",
JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(1) + 8
sizeofArray(2) + 2 * sizeofObject(1) + 8
},
{
"[',2,3]",
@ -301,7 +304,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::IncompleteInput,
"[]",
JSON_ARRAY_SIZE(0)
sizeofArray(0)
},
{
"[\",2,3]",
@ -309,7 +312,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::IncompleteInput,
"[]",
JSON_ARRAY_SIZE(0)
sizeofArray(0)
},
{
// detect errors in skipped value
@ -318,7 +321,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::InvalidInput,
"[]",
JSON_ARRAY_SIZE(0)
sizeofArray(0)
},
{
// detect incomplete string event if it's skipped
@ -471,7 +474,7 @@ TEST_CASE("Filtering") {
1,
DeserializationError::TooDeep,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// check nesting limit even for ignored arrays
@ -498,7 +501,7 @@ TEST_CASE("Filtering") {
1,
DeserializationError::TooDeep,
"[]",
JSON_ARRAY_SIZE(0)
sizeofArray(0)
},
{
// supports back-slash at the end of skipped string
@ -543,7 +546,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::InvalidInput,
"[]",
JSON_ARRAY_SIZE(0)
sizeofArray(0)
},
{
// incomplete comment at the begining of an array
@ -552,7 +555,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::IncompleteInput,
"[]",
JSON_ARRAY_SIZE(0)
sizeofArray(0)
},
{
// invalid comment before key
@ -561,7 +564,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::InvalidInput,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// incomplete comment before key
@ -570,7 +573,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::IncompleteInput,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// invalid comment after key
@ -579,7 +582,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::InvalidInput,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// incomplete comment after key
@ -588,7 +591,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::IncompleteInput,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// invalid comment after colon
@ -597,7 +600,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::InvalidInput,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// incomplete comment after colon
@ -606,7 +609,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::IncompleteInput,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// comment next to an integer
@ -615,7 +618,7 @@ TEST_CASE("Filtering") {
10,
DeserializationError::Ok,
"{}",
JSON_OBJECT_SIZE(0)
sizeofObject(0)
},
{
// invalid comment after opening brace of a skipped object

View File

@ -9,6 +9,8 @@
#include "CustomReader.hpp"
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeJson(char*)") {
JsonDocument doc(1024);
@ -18,9 +20,9 @@ TEST_CASE("deserializeJson(char*)") {
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
CHECK(doc.memoryUsage() == JSON_OBJECT_SIZE(1));
CHECK(doc.memoryUsage() == sizeofObject(1));
CHECK(doc.as<JsonVariant>().memoryUsage() ==
JSON_OBJECT_SIZE(1)); // issue #1318
sizeofObject(1)); // issue #1318
}
}
@ -149,7 +151,7 @@ TEST_CASE("deserializeJson(VLA)") {
char vla[i];
strcpy(vla, "{\"a\":42}");
JsonDocument doc(JSON_OBJECT_SIZE(1));
JsonDocument doc(sizeofObject(1));
DeserializationError err = deserializeJson(doc, vla);
REQUIRE(err == DeserializationError::Ok);

View File

@ -7,6 +7,8 @@
using namespace Catch::Matchers;
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeJson(JsonDocument&)") {
JsonDocument doc(4096);
@ -112,6 +114,6 @@ TEST_CASE("deserializeJson(JsonDocument&)") {
deserializeJson(doc, "{}");
REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
REQUIRE(doc.memoryUsage() == sizeofObject(0));
}
}

View File

@ -5,6 +5,9 @@
#include <ArduinoJson.h>
#include <catch.hpp>
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserialize JSON object") {
JsonDocument doc(4096);
@ -304,7 +307,7 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
REQUIRE(doc.memoryUsage() == sizeofObject(0));
}
SECTION("Issue #1335") {
@ -316,7 +319,7 @@ TEST_CASE("deserialize JSON object") {
TEST_CASE("deserialize JSON object under memory constraints") {
SECTION("buffer for the right size for an empty object") {
JsonDocument doc(JSON_OBJECT_SIZE(0));
JsonDocument doc(sizeofObject(0));
char input[] = "{}";
DeserializationError err = deserializeJson(doc, input);
@ -325,7 +328,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
}
SECTION("buffer too small for an empty object") {
JsonDocument doc(JSON_OBJECT_SIZE(0));
JsonDocument doc(sizeofObject(0));
char input[] = "{\"a\":1}";
DeserializationError err = deserializeJson(doc, input);
@ -334,7 +337,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
}
SECTION("buffer of the right size for an object with one member") {
JsonDocument doc(JSON_OBJECT_SIZE(1));
JsonDocument doc(sizeofObject(1));
char input[] = "{\"a\":1}";
DeserializationError err = deserializeJson(doc, input);
@ -343,7 +346,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
}
SECTION("buffer too small for an object with a nested array") {
JsonDocument doc(JSON_OBJECT_SIZE(0) + JSON_ARRAY_SIZE(0));
JsonDocument doc(sizeofObject(0) + sizeofArray(0));
char input[] = "{\"a\":[]}";
DeserializationError err = deserializeJson(doc, input);
@ -352,7 +355,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
}
SECTION("buffer of the right size for an object with a nested array") {
JsonDocument doc(JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0));
JsonDocument doc(sizeofObject(1) + sizeofArray(0));
char input[] = "{\"a\":[]}";
DeserializationError err = deserializeJson(doc, input);
@ -361,13 +364,13 @@ TEST_CASE("deserialize JSON object under memory constraints") {
}
SECTION("Should clear the JsonObject") {
JsonDocument doc(JSON_OBJECT_SIZE(1));
JsonDocument doc(sizeofObject(1));
char input[] = "{\"hello\":\"world\"}";
deserializeJson(doc, input);
deserializeJson(doc, "{}");
REQUIRE(doc.as<JsonObject>().size() == 0);
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
REQUIRE(doc.memoryUsage() == sizeofObject(0));
}
}

View File

@ -6,6 +6,9 @@
#include <ArduinoJson.h>
#include <catch.hpp>
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("Valid JSON strings value") {
struct TestCase {
const char* input;
@ -93,7 +96,7 @@ TEST_CASE("Invalid JSON string") {
}
TEST_CASE("Not enough room to save the key") {
JsonDocument doc(JSON_OBJECT_SIZE(1) + 8);
JsonDocument doc(sizeofObject(1) + 8);
SECTION("Quoted string") {
REQUIRE(deserializeJson(doc, "{\"example\":1}") ==