mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Replaced printTo()
with serializeJson()
* Added `serializeJson()` and `serializeJsonPretty()` * Added `measureJson()` and `measureJsonPretty()` * Removed `printTo()` and `prettyPrintTo()` * Removed `measureLength()` and `measurePrettyLength()`
This commit is contained in:
@ -69,6 +69,7 @@ add_subdirectory(IntegrationTests)
|
||||
add_subdirectory(JsonArray)
|
||||
add_subdirectory(JsonObject)
|
||||
add_subdirectory(JsonParser)
|
||||
add_subdirectory(JsonSerializer)
|
||||
add_subdirectory(JsonVariant)
|
||||
add_subdirectory(JsonWriter)
|
||||
add_subdirectory(Misc)
|
||||
|
@ -18,7 +18,8 @@ std::stringstream allocatorLog;
|
||||
|
||||
struct SpyingAllocator : DefaultAllocator {
|
||||
void* allocate(size_t n) {
|
||||
allocatorLog << "A" << (n - DynamicJsonBuffer::EmptyBlockSize);
|
||||
allocatorLog << static_cast<const char*>("A")
|
||||
<< (n - DynamicJsonBuffer::EmptyBlockSize);
|
||||
return DefaultAllocator::allocate(n);
|
||||
}
|
||||
void deallocate(void* p) {
|
||||
|
@ -10,11 +10,11 @@ void check(std::string originalJson) {
|
||||
|
||||
std::string prettyJson;
|
||||
deserializeJson(obj, originalJson);
|
||||
obj.prettyPrintTo(prettyJson);
|
||||
serializeJsonPretty(obj, prettyJson);
|
||||
|
||||
std::string finalJson;
|
||||
deserializeJson(obj, originalJson);
|
||||
obj.printTo(finalJson);
|
||||
serializeJson(obj, finalJson);
|
||||
|
||||
REQUIRE(originalJson == finalJson);
|
||||
}
|
||||
|
@ -9,8 +9,6 @@ add_executable(JsonArrayTests
|
||||
copyTo.cpp
|
||||
invalid.cpp
|
||||
iterator.cpp
|
||||
prettyPrintTo.cpp
|
||||
printTo.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
size.cpp
|
||||
|
@ -14,7 +14,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
serializeJson(array, json, sizeof(json));
|
||||
REQUIRE(std::string("[1,2,3]") == json);
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE_FALSE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
serializeJson(array, json, sizeof(json));
|
||||
REQUIRE(std::string("[1,2]") == json);
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
serializeJson(array, json, sizeof(json));
|
||||
REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
bool ok = array.copyFrom(source);
|
||||
REQUIRE_FALSE(ok);
|
||||
|
||||
array.printTo(json, sizeof(json));
|
||||
serializeJson(array, json, sizeof(json));
|
||||
REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ TEST_CASE("JsonArray::invalid()") {
|
||||
|
||||
SECTION("PrintToWritesBrackets") {
|
||||
char buffer[32];
|
||||
JsonArray::invalid().printTo(buffer, sizeof(buffer));
|
||||
serializeJson(JsonArray::invalid(), buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("[]"));
|
||||
}
|
||||
}
|
||||
|
@ -8,8 +8,6 @@ add_executable(JsonObjectTests
|
||||
get.cpp
|
||||
invalid.cpp
|
||||
iterator.cpp
|
||||
prettyPrintTo.cpp
|
||||
printTo.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
size.cpp
|
||||
|
@ -29,7 +29,7 @@ TEST_CASE("JsonObject::invalid()") {
|
||||
|
||||
SECTION("PrintToWritesBraces") {
|
||||
char buffer[32];
|
||||
obj.printTo(buffer, sizeof(buffer));
|
||||
serializeJson(obj, buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("{}"));
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ TEST_CASE("JsonObject::remove()") {
|
||||
}
|
||||
|
||||
std::string result;
|
||||
obj.printTo(result);
|
||||
serializeJson(obj, result);
|
||||
REQUIRE("{\"a\":0,\"c\":2}" == result);
|
||||
}
|
||||
}
|
||||
|
14
test/JsonSerializer/CMakeLists.txt
Normal file
14
test/JsonSerializer/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonSerializerTests
|
||||
JsonArray.cpp
|
||||
JsonArrayPretty.cpp
|
||||
JsonObject.cpp
|
||||
JsonObjectPretty.cpp
|
||||
JsonVariant.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonSerializerTests catch)
|
||||
add_test(JsonSerializer JsonSerializerTests)
|
@ -7,14 +7,14 @@
|
||||
|
||||
static void check(JsonArray &array, std::string expected) {
|
||||
std::string actual;
|
||||
size_t actualLen = array.printTo(actual);
|
||||
size_t actualLen = serializeJson(array, actual);
|
||||
REQUIRE(expected == actual);
|
||||
REQUIRE(actualLen == expected.size());
|
||||
size_t measuredLen = array.measureLength();
|
||||
size_t measuredLen = measureJson(array);
|
||||
REQUIRE(measuredLen == expected.size());
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::printTo()") {
|
||||
TEST_CASE("serializeJson(JsonArray)") {
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(2)> array;
|
||||
|
||||
SECTION("Empty") {
|
@ -7,14 +7,14 @@
|
||||
|
||||
static void check(JsonArray& array, std::string expected) {
|
||||
std::string actual;
|
||||
size_t actualLen = array.prettyPrintTo(actual);
|
||||
size_t measuredLen = array.measurePrettyLength();
|
||||
size_t actualLen = serializeJsonPretty(array, actual);
|
||||
size_t measuredLen = measureJsonPretty(array);
|
||||
CHECK(actualLen == expected.size());
|
||||
CHECK(measuredLen == expected.size());
|
||||
REQUIRE(expected == actual);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArray::prettyPrintTo()") {
|
||||
TEST_CASE("serializeJsonPretty(JsonArray)") {
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("Empty") {
|
@ -8,14 +8,15 @@
|
||||
|
||||
void check(const JsonObject &obj, const std::string &expected) {
|
||||
char actual[256];
|
||||
size_t actualLen = obj.printTo(actual);
|
||||
size_t measuredLen = obj.measureLength();
|
||||
size_t actualLen = serializeJson(obj, actual);
|
||||
size_t measuredLen = measureJson(obj);
|
||||
|
||||
REQUIRE(expected == actual);
|
||||
REQUIRE(expected.size() == actualLen);
|
||||
REQUIRE(expected.size() == measuredLen);
|
||||
}
|
||||
TEST_CASE("JsonObject::printTo()") {
|
||||
|
||||
TEST_CASE("serializeJson(JsonObject)") {
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("EmptyObject") {
|
@ -9,15 +9,15 @@
|
||||
void check(const JsonObject &obj, const std::string expected) {
|
||||
char json[256];
|
||||
|
||||
size_t actualLen = obj.prettyPrintTo(json);
|
||||
size_t measuredLen = obj.measurePrettyLength();
|
||||
size_t actualLen = serializeJsonPretty(obj, json);
|
||||
size_t measuredLen = measureJsonPretty(obj);
|
||||
|
||||
REQUIRE(json == expected);
|
||||
REQUIRE(expected.size() == actualLen);
|
||||
REQUIRE(expected.size() == measuredLen);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonObject::prettyPrintTo()") {
|
||||
TEST_CASE("serializeJsonPretty(JsonObject)") {
|
||||
DynamicJsonObject obj;
|
||||
|
||||
SECTION("EmptyObject") {
|
@ -8,12 +8,12 @@
|
||||
|
||||
void check(JsonVariant variant, const std::string &expected) {
|
||||
char buffer[256] = "";
|
||||
size_t returnValue = variant.printTo(buffer, sizeof(buffer));
|
||||
size_t returnValue = serializeJson(variant, buffer, sizeof(buffer));
|
||||
REQUIRE(expected == buffer);
|
||||
REQUIRE(expected.size() == returnValue);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::printTo()") {
|
||||
TEST_CASE("serializeJson(JsonVariant)") {
|
||||
SECTION("Empty") {
|
||||
check(JsonVariant(), "");
|
||||
}
|
@ -2,13 +2,12 @@
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonVariantTests
|
||||
add_executable(JsonVariantTests
|
||||
as.cpp
|
||||
compare.cpp
|
||||
copy.cpp
|
||||
is.cpp
|
||||
or.cpp
|
||||
printTo.cpp
|
||||
set_get.cpp
|
||||
subscript.cpp
|
||||
success.cpp
|
||||
|
@ -49,19 +49,19 @@ TEST_CASE("std::string") {
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("printTo()") {
|
||||
SECTION("serializeJson()") {
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.printTo(json);
|
||||
serializeJson(array, json);
|
||||
REQUIRE(std::string("[4,2]") == json);
|
||||
}
|
||||
|
||||
SECTION("prettyPrintTo") {
|
||||
SECTION("serializeJsonPretty()") {
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.prettyPrintTo(json);
|
||||
serializeJsonPretty(array, json);
|
||||
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
||||
}
|
||||
}
|
||||
@ -69,7 +69,7 @@ TEST_CASE("std::string") {
|
||||
SECTION("JsonObject") {
|
||||
DynamicJsonObject object;
|
||||
|
||||
SECTION("deserializeJson") {
|
||||
SECTION("deserializeJson()") {
|
||||
std::string json("{\"hello\":\"world\"}");
|
||||
|
||||
bool success = deserializeJson(object, json);
|
||||
@ -156,7 +156,7 @@ TEST_CASE("std::string") {
|
||||
char json[64];
|
||||
object.createNestedObject(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
serializeJson(object, json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":{}}") == json);
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ TEST_CASE("std::string") {
|
||||
char json[64];
|
||||
object.createNestedArray(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
serializeJson(object, json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":[]}") == json);
|
||||
}
|
||||
|
||||
@ -197,17 +197,17 @@ TEST_CASE("std::string") {
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("printTo") {
|
||||
SECTION("serializeJson()") {
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
serializeJson(object, json);
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
}
|
||||
|
||||
SECTION("prettyPrintTo") {
|
||||
SECTION("serializeJsonPretty()") {
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.prettyPrintTo(json);
|
||||
serializeJsonPretty(object, json);
|
||||
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user