diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d0b7b1c..ea1220c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ArduinoJson: change log ======================= +* Add `ARDUINOJSON_STRING_LENGTH_SIZE` to the namespace name + v7.0.4 (2024-03-12) ------ diff --git a/extras/tests/MixedConfiguration/CMakeLists.txt b/extras/tests/MixedConfiguration/CMakeLists.txt index 17ea0079..b8cad8e5 100644 --- a/extras/tests/MixedConfiguration/CMakeLists.txt +++ b/extras/tests/MixedConfiguration/CMakeLists.txt @@ -15,6 +15,9 @@ add_executable(MixedConfigurationTests enable_nan_1.cpp enable_progmem_1.cpp issue1707.cpp + string_length_size_1.cpp + string_length_size_2.cpp + string_length_size_4.cpp use_double_0.cpp use_double_1.cpp use_long_long_0.cpp diff --git a/extras/tests/MixedConfiguration/string_length_size_1.cpp b/extras/tests/MixedConfiguration/string_length_size_1.cpp new file mode 100644 index 00000000..8e17e3c9 --- /dev/null +++ b/extras/tests/MixedConfiguration/string_length_size_1.cpp @@ -0,0 +1,56 @@ +#define ARDUINOJSON_STRING_LENGTH_SIZE 1 +#include + +#include +#include + +TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") { + JsonDocument doc; + + SECTION("set() returns true if string has 255 characters") { + auto result = doc.set(std::string(255, '?')); + + REQUIRE(result == true); + REQUIRE(doc.overflowed() == false); + } + + SECTION("set() returns false if string has 256 characters") { + auto result = doc.set(std::string(256, '?')); + + REQUIRE(result == false); + REQUIRE(doc.overflowed() == true); + } + + SECTION("deserializeJson() returns Ok if string has 255 characters") { + auto input = "\"" + std::string(255, '?') + "\""; + + auto err = deserializeJson(doc, input); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION("deserializeJson() returns NoMemory if string has 256 characters") { + auto input = "\"" + std::string(256, '?') + "\""; + + auto err = deserializeJson(doc, input); + + REQUIRE(err == DeserializationError::NoMemory); + } + + SECTION("deserializeMsgPack() returns Ok of string has 255 characters") { + auto input = "\xd9\xff" + std::string(255, '?'); + + auto err = deserializeMsgPack(doc, input); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION( + "deserializeMsgPack() returns NoMemory of string has 256 characters") { + auto input = std::string("\xda\x01\x00", 3) + std::string(256, '?'); + + auto err = deserializeMsgPack(doc, input); + + REQUIRE(err == DeserializationError::NoMemory); + } +} diff --git a/extras/tests/MixedConfiguration/string_length_size_2.cpp b/extras/tests/MixedConfiguration/string_length_size_2.cpp new file mode 100644 index 00000000..669905ef --- /dev/null +++ b/extras/tests/MixedConfiguration/string_length_size_2.cpp @@ -0,0 +1,57 @@ +#define ARDUINOJSON_STRING_LENGTH_SIZE 2 +#include + +#include +#include + +TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") { + JsonDocument doc; + + SECTION("set() returns true if string has 65535 characters") { + auto result = doc.set(std::string(65535, '?')); + + REQUIRE(result == true); + REQUIRE(doc.overflowed() == false); + } + + SECTION("set() returns false if string has 65536 characters") { + auto result = doc.set(std::string(65536, '?')); + + REQUIRE(result == false); + REQUIRE(doc.overflowed() == true); + } + + SECTION("deserializeJson() returns Ok if string has 65535 characters") { + auto input = "\"" + std::string(65535, '?') + "\""; + + auto err = deserializeJson(doc, input); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION("deserializeJson() returns NoMemory if string has 65536 characters") { + auto input = "\"" + std::string(65536, '?') + "\""; + + auto err = deserializeJson(doc, input); + + REQUIRE(err == DeserializationError::NoMemory); + } + + SECTION("deserializeMsgPack() returns Ok of string has 65535 characters") { + auto input = "\xda\xff\xff" + std::string(65535, '?'); + + auto err = deserializeMsgPack(doc, input); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION( + "deserializeMsgPack() returns NoMemory of string has 65536 characters") { + auto input = + std::string("\xdb\x00\x01\x00\x00", 5) + std::string(65536, '?'); + + auto err = deserializeMsgPack(doc, input); + + REQUIRE(err == DeserializationError::NoMemory); + } +} diff --git a/extras/tests/MixedConfiguration/string_length_size_4.cpp b/extras/tests/MixedConfiguration/string_length_size_4.cpp new file mode 100644 index 00000000..e448743c --- /dev/null +++ b/extras/tests/MixedConfiguration/string_length_size_4.cpp @@ -0,0 +1,32 @@ +#define ARDUINOJSON_STRING_LENGTH_SIZE 4 +#include + +#include +#include + +TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") { + JsonDocument doc; + + SECTION("set() returns true if string has 65536 characters") { + auto result = doc.set(std::string(65536, '?')); + + REQUIRE(result == true); + REQUIRE(doc.overflowed() == false); + } + + SECTION("deserializeJson() returns Ok if string has 65536 characters") { + auto input = "\"" + std::string(65536, '?') + "\""; + + auto err = deserializeJson(doc, input); + + REQUIRE(err == DeserializationError::Ok); + } + + SECTION("deserializeMsgPack() returns Ok of string has 65536 characters") { + auto input = "\xda\xff\xff" + std::string(65536, '?'); + + auto err = deserializeMsgPack(doc, input); + + REQUIRE(err == DeserializationError::Ok); + } +} diff --git a/src/ArduinoJson/Namespace.hpp b/src/ArduinoJson/Namespace.hpp index 40e005f7..da7a4494 100644 --- a/src/ArduinoJson/Namespace.hpp +++ b/src/ArduinoJson/Namespace.hpp @@ -10,16 +10,16 @@ #ifndef ARDUINOJSON_VERSION_NAMESPACE -# define ARDUINOJSON_VERSION_NAMESPACE \ - ARDUINOJSON_CONCAT4(ARDUINOJSON_VERSION_MACRO, \ - ARDUINOJSON_BIN2ALPHA(ARDUINOJSON_ENABLE_PROGMEM, \ - ARDUINOJSON_USE_LONG_LONG, \ - ARDUINOJSON_USE_DOUBLE, 1), \ - ARDUINOJSON_BIN2ALPHA(ARDUINOJSON_ENABLE_NAN, \ - ARDUINOJSON_ENABLE_INFINITY, \ - ARDUINOJSON_ENABLE_COMMENTS, \ - ARDUINOJSON_DECODE_UNICODE), \ - ARDUINOJSON_SLOT_ID_SIZE) +# define ARDUINOJSON_VERSION_NAMESPACE \ + ARDUINOJSON_CONCAT5( \ + ARDUINOJSON_VERSION_MACRO, \ + ARDUINOJSON_BIN2ALPHA(ARDUINOJSON_ENABLE_PROGMEM, \ + ARDUINOJSON_USE_LONG_LONG, \ + ARDUINOJSON_USE_DOUBLE, 1), \ + ARDUINOJSON_BIN2ALPHA( \ + ARDUINOJSON_ENABLE_NAN, ARDUINOJSON_ENABLE_INFINITY, \ + ARDUINOJSON_ENABLE_COMMENTS, ARDUINOJSON_DECODE_UNICODE), \ + ARDUINOJSON_SLOT_ID_SIZE, ARDUINOJSON_STRING_LENGTH_SIZE) #endif diff --git a/src/ArduinoJson/Polyfills/preprocessor.hpp b/src/ArduinoJson/Polyfills/preprocessor.hpp index df76497f..ca565f72 100644 --- a/src/ArduinoJson/Polyfills/preprocessor.hpp +++ b/src/ArduinoJson/Polyfills/preprocessor.hpp @@ -6,8 +6,12 @@ #define ARDUINOJSON_CONCAT_(A, B) A##B #define ARDUINOJSON_CONCAT2(A, B) ARDUINOJSON_CONCAT_(A, B) +#define ARDUINOJSON_CONCAT3(A, B, C) \ + ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT2(A, B), C) #define ARDUINOJSON_CONCAT4(A, B, C, D) \ - ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT2(A, B), ARDUINOJSON_CONCAT2(C, D)) + ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT3(A, B, C), D) +#define ARDUINOJSON_CONCAT5(A, B, C, D, E) \ + ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT4(A, B, C, D), E) #define ARDUINOJSON_BIN2ALPHA_0000() A #define ARDUINOJSON_BIN2ALPHA_0001() B