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:
Benoit Blanchon
2018-03-01 09:24:58 +01:00
parent 7a2a64803a
commit 83d73c93f7
38 changed files with 238 additions and 233 deletions

View File

@ -9,8 +9,6 @@ add_executable(JsonArrayTests
copyTo.cpp
invalid.cpp
iterator.cpp
prettyPrintTo.cpp
printTo.cpp
remove.cpp
set.cpp
size.cpp

View File

@ -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);
}
}

View File

@ -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("[]"));
}
}

View File

@ -1,74 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void check(JsonArray& array, std::string expected) {
std::string actual;
size_t actualLen = array.prettyPrintTo(actual);
size_t measuredLen = array.measurePrettyLength();
CHECK(actualLen == expected.size());
CHECK(measuredLen == expected.size());
REQUIRE(expected == actual);
}
TEST_CASE("JsonArray::prettyPrintTo()") {
DynamicJsonArray array;
SECTION("Empty") {
check(array, "[]");
}
SECTION("OneElement") {
array.add(1);
check(array,
"[\r\n"
" 1\r\n"
"]");
}
SECTION("TwoElements") {
array.add(1);
array.add(2);
check(array,
"[\r\n"
" 1,\r\n"
" 2\r\n"
"]");
}
SECTION("EmptyNestedArrays") {
array.createNestedArray();
array.createNestedArray();
check(array,
"[\r\n"
" [],\r\n"
" []\r\n"
"]");
}
SECTION("NestedArrays") {
JsonArray& nested1 = array.createNestedArray();
nested1.add(1);
nested1.add(2);
JsonObject& nested2 = array.createNestedObject();
nested2["key"] = 3;
check(array,
"[\r\n"
" [\r\n"
" 1,\r\n"
" 2\r\n"
" ],\r\n"
" {\r\n"
" \"key\": 3\r\n"
" }\r\n"
"]");
}
}

View File

@ -1,128 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void check(JsonArray &array, std::string expected) {
std::string actual;
size_t actualLen = array.printTo(actual);
REQUIRE(expected == actual);
REQUIRE(actualLen == expected.size());
size_t measuredLen = array.measureLength();
REQUIRE(measuredLen == expected.size());
}
TEST_CASE("JsonArray::printTo()") {
StaticJsonArray<JSON_ARRAY_SIZE(2)> array;
SECTION("Empty") {
check(array, "[]");
}
SECTION("Null") {
array.add(static_cast<char *>(0));
check(array, "[null]");
}
SECTION("OneString") {
array.add("hello");
check(array, "[\"hello\"]");
}
SECTION("TwoStrings") {
array.add("hello");
array.add("world");
check(array, "[\"hello\",\"world\"]");
}
SECTION("OneStringOverCapacity") {
array.add("hello");
array.add("world");
array.add("lost");
check(array, "[\"hello\",\"world\"]");
}
SECTION("One double") {
array.add(3.1415927);
check(array, "[3.1415927]");
}
SECTION("OneInteger") {
array.add(1);
check(array, "[1]");
}
SECTION("TwoIntegers") {
array.add(1);
array.add(2);
check(array, "[1,2]");
}
SECTION("RawJson(const char*)") {
array.add(RawJson("{\"key\":\"value\"}"));
check(array, "[{\"key\":\"value\"}]");
}
SECTION("RawJson(char*)") {
char tmp[] = "{\"key\":\"value\"}";
array.add(RawJson(tmp));
check(array, "[{\"key\":\"value\"}]");
}
SECTION("OneIntegerOverCapacity") {
array.add(1);
array.add(2);
array.add(3);
check(array, "[1,2]");
}
SECTION("OneTrue") {
array.add(true);
check(array, "[true]");
}
SECTION("OneFalse") {
array.add(false);
check(array, "[false]");
}
SECTION("TwoBooleans") {
array.add(false);
array.add(true);
check(array, "[false,true]");
}
SECTION("OneBooleanOverCapacity") {
array.add(false);
array.add(true);
array.add(false);
check(array, "[false,true]");
}
SECTION("OneEmptyNestedArray") {
array.createNestedArray();
check(array, "[[]]");
}
SECTION("OneEmptyNestedHash") {
array.createNestedObject();
check(array, "[{}]");
}
}