forked from bblanchon/ArduinoJson
Don't use JsonBuffer to create or parse objects and arrays.
* Added DynamicJsonArray and StaticJsonArray * Added DynamicJsonObject and StaticJsonObject * Added DynamicJsonVariant and StaticJsonVariant * Added deserializeJson() * Removed JsonBuffer::parseArray(), parseObject() and parse() * Removed JsonBuffer::createArray() and createObject()
This commit is contained in:
@ -16,15 +16,15 @@
|
||||
#endif
|
||||
|
||||
TEST_CASE("Deprecated functions") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
SECTION("JsonVariant::asArray()") {
|
||||
JsonVariant variant = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
JsonVariant variant = array;
|
||||
REQUIRE(variant.asArray().success());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::asObject()") {
|
||||
JsonVariant variant = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
JsonVariant variant = obj;
|
||||
REQUIRE(variant.asObject().success());
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray::removeAt()") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.removeAt(0);
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript::set(double, uint8_t)") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(666);
|
||||
arr[0].set(123.45, 2);
|
||||
REQUIRE(123.45 == arr[0].as<double>());
|
||||
@ -68,26 +68,26 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray::add(double, uint8_t)") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(3.14159265358979323846, 4);
|
||||
}
|
||||
|
||||
SECTION("JsonArray::add(float, uint8_t)") {
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(3.14159265358979323846f, 4);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set(unsigned char[], double, uint8_t)") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(key, 3.14, 2);
|
||||
|
||||
REQUIRE(3.14 == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set(const char*, double, uint8_t)") {
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("hello", 123.45, 2);
|
||||
|
||||
REQUIRE(123.45 == obj["hello"].as<double>());
|
||||
@ -96,7 +96,7 @@ TEST_CASE("Deprecated functions") {
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript::set(double, uint8_t)") {
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"].set(123.45, 2);
|
||||
|
||||
REQUIRE(true == obj["hello"].is<double>());
|
||||
|
@ -23,8 +23,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonObject") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
DynamicJsonObject object;
|
||||
object["key"] = "value";
|
||||
os << object;
|
||||
REQUIRE("{\"key\":\"value\"}" == os.str());
|
||||
@ -32,8 +31,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonObjectSubscript") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
DynamicJsonObject object;
|
||||
object["key"] = "value";
|
||||
os << object["key"];
|
||||
REQUIRE("\"value\"" == os.str());
|
||||
@ -41,8 +39,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonArray") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
array.add("value");
|
||||
os << array;
|
||||
REQUIRE("[\"value\"]" == os.str());
|
||||
@ -50,8 +47,7 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("JsonArraySubscript") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
DynamicJsonArray array;
|
||||
array.add("value");
|
||||
os << array[0];
|
||||
REQUIRE("\"value\"" == os.str());
|
||||
@ -59,26 +55,28 @@ TEST_CASE("std::stream") {
|
||||
|
||||
SECTION("ParseArray") {
|
||||
std::istringstream json(" [ 42 /* comment */ ] ");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
REQUIRE(true == arr.success());
|
||||
DynamicJsonArray arr;
|
||||
bool success = deserializeJson(arr, json);
|
||||
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(42 == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("ParseObject") {
|
||||
std::istringstream json(" { hello : world // comment\n }");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
REQUIRE(true == obj.success());
|
||||
DynamicJsonObject obj;
|
||||
bool success = deserializeJson(obj, json);
|
||||
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(1 == obj.size());
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("ShouldNotReadPastTheEnd") {
|
||||
std::istringstream json("{}123");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
jsonBuffer.parseObject(json);
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, json);
|
||||
REQUIRE('1' == json.get());
|
||||
}
|
||||
}
|
||||
|
@ -13,231 +13,224 @@ static void eraseString(std::string &str) {
|
||||
TEST_CASE("std::string") {
|
||||
DynamicJsonBuffer jb;
|
||||
|
||||
SECTION("JsonBuffer_ParseArray") {
|
||||
std::string json("[\"hello\"]");
|
||||
JsonArray &array = jb.parseArray(json);
|
||||
eraseString(json);
|
||||
REQUIRE(true == array.success());
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
SECTION("JsonArray") {
|
||||
DynamicJsonArray array;
|
||||
|
||||
SECTION("deserializeJson") {
|
||||
std::string json("[\"hello\"]");
|
||||
|
||||
bool success = deserializeJson(array, json);
|
||||
eraseString(json);
|
||||
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("add()") {
|
||||
std::string value("hello");
|
||||
array.add(value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("set()") {
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array.set(0, value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("operator[]") {
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array[0] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("printTo()") {
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.printTo(json);
|
||||
REQUIRE(std::string("[4,2]") == json);
|
||||
}
|
||||
|
||||
SECTION("prettyPrintTo") {
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.prettyPrintTo(json);
|
||||
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_ParseObject") {
|
||||
std::string json("{\"hello\":\"world\"}");
|
||||
JsonObject &object = jb.parseObject(json);
|
||||
eraseString(json);
|
||||
REQUIRE(true == object.success());
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("JsonObject") {
|
||||
DynamicJsonObject object;
|
||||
|
||||
SECTION("JsonObject_Subscript") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
SECTION("deserializeJson") {
|
||||
std::string json("{\"hello\":\"world\"}");
|
||||
|
||||
SECTION("JsonObject_ConstSubscript") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
bool success = deserializeJson(object, json);
|
||||
eraseString(json);
|
||||
|
||||
SECTION("JsonObject_SetKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object.set(key, "world");
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
REQUIRE(true == success);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_SetValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string value("world");
|
||||
object.set("hello", value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("operator[]") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
SECTION("JsonObject_SetKeyValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
std::string value("world");
|
||||
object.set(key, value);
|
||||
eraseString(key);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
deserializeJson(object, json);
|
||||
|
||||
SECTION("JsonObject_SetToArraySubscript") {
|
||||
JsonArray &arr = jb.createArray();
|
||||
arr.add("world");
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
|
||||
JsonObject &object = jb.createObject();
|
||||
object.set(std::string("hello"), arr[0]);
|
||||
SECTION("operator[] const") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
deserializeJson(object, json);
|
||||
const JsonObject &obj = object;
|
||||
|
||||
SECTION("JsonObject_SetToObjectSubscript") {
|
||||
JsonObject &arr = jb.createObject();
|
||||
arr.set("x", "world");
|
||||
REQUIRE(std::string("value") == obj[std::string("key")]);
|
||||
}
|
||||
|
||||
JsonObject &object = jb.createObject();
|
||||
object.set(std::string("hello"), arr["x"]);
|
||||
SECTION("set(key)") {
|
||||
std::string key("hello");
|
||||
object.set(key, "world");
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("set(value)") {
|
||||
std::string value("world");
|
||||
object.set("hello", value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Get") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
SECTION("set(key,value)") {
|
||||
std::string key("hello");
|
||||
std::string value("world");
|
||||
object.set(key, value);
|
||||
eraseString(key);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_GetT") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
SECTION("set(JsonArraySubscript)") {
|
||||
DynamicJsonArray arr;
|
||||
arr.add("world");
|
||||
|
||||
SECTION("JsonObject_IsT") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(true == object.is<const char *>(std::string("key")));
|
||||
}
|
||||
object.set(std::string("hello"), arr[0]);
|
||||
|
||||
SECTION("JsonObject_CreateNestedObject") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = jb.createObject();
|
||||
object.createNestedObject(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":{}}") == json);
|
||||
}
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_CreateNestedArray") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = jb.createObject();
|
||||
object.createNestedArray(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":[]}") == json);
|
||||
}
|
||||
SECTION("set(JsonObjectSubscript)") {
|
||||
DynamicJsonObject obj;
|
||||
obj.set("x", "world");
|
||||
|
||||
SECTION("JsonObject_ContainsKey") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(true == object.containsKey(std::string("key")));
|
||||
}
|
||||
object.set(std::string("hello"), obj["x"]);
|
||||
|
||||
SECTION("JsonObject_Remove") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(1 == object.size());
|
||||
object.remove(std::string("key"));
|
||||
REQUIRE(0 == object.size());
|
||||
}
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript_SetKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object[key] = "world";
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
SECTION("get<T>()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
|
||||
SECTION("JsonObjectSubscript_SetValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string value("world");
|
||||
object["hello"] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonArray_Add") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("hello");
|
||||
array.add(value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
SECTION("is<T>()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
REQUIRE(true == object.is<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonArray_Set") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array.set(0, value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
SECTION("createNestedObject()") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
object.createNestedObject(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":{}}") == json);
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array[0] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
SECTION("createNestedArray()") {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
object.createNestedArray(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"key\":[]}") == json);
|
||||
}
|
||||
|
||||
SECTION("JsonArray_PrintTo") {
|
||||
JsonArray &array = jb.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.printTo(json);
|
||||
REQUIRE(std::string("[4,2]") == json);
|
||||
}
|
||||
SECTION("containsKey()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
REQUIRE(true == object.containsKey(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonArray_PrettyPrintTo") {
|
||||
JsonArray &array = jb.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.prettyPrintTo(json);
|
||||
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
||||
}
|
||||
SECTION("remove()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(object, json);
|
||||
REQUIRE(1 == object.size());
|
||||
object.remove(std::string("key"));
|
||||
REQUIRE(0 == object.size());
|
||||
}
|
||||
|
||||
SECTION("JsonObject_PrintTo") {
|
||||
JsonObject &object = jb.createObject();
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
}
|
||||
SECTION("operator[], set key") {
|
||||
std::string key("hello");
|
||||
object[key] = "world";
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_PrettyPrintTo") {
|
||||
JsonObject &object = jb.createObject();
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.prettyPrintTo(json);
|
||||
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
||||
}
|
||||
SECTION("operator[], set value") {
|
||||
std::string value("world");
|
||||
object["hello"] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_GrowWhenAddingNewKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key1("hello"), key2("world");
|
||||
SECTION("printTo") {
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
}
|
||||
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = jb.size();
|
||||
object[key2] = 2;
|
||||
size_t sizeAfter = jb.size();
|
||||
SECTION("prettyPrintTo") {
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.prettyPrintTo(json);
|
||||
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
||||
}
|
||||
|
||||
REQUIRE(sizeAfter - sizeBefore >= key2.size());
|
||||
}
|
||||
SECTION("memoryUsage() increases when adding a new key") {
|
||||
std::string key1("hello"), key2("world");
|
||||
|
||||
SECTION("JsonBuffer_DontGrowWhenReusingKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = object.memoryUsage();
|
||||
object[key2] = 2;
|
||||
size_t sizeAfter = object.memoryUsage();
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = jb.size();
|
||||
object[key] = 2;
|
||||
size_t sizeAfter = jb.size();
|
||||
REQUIRE(sizeAfter - sizeBefore >= key2.size());
|
||||
}
|
||||
|
||||
REQUIRE(sizeBefore == sizeAfter);
|
||||
SECTION("memoryUsage() remains when adding the same key") {
|
||||
std::string key("hello");
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = object.memoryUsage();
|
||||
object[key] = 2;
|
||||
size_t sizeAfter = object.memoryUsage();
|
||||
|
||||
REQUIRE(sizeBefore == sizeAfter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,19 +13,19 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonBuffer::parseArray") {
|
||||
unsigned char json[] = "[42]";
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||
bool success = deserializeJson(arr, json);
|
||||
|
||||
REQUIRE(true == arr.success());
|
||||
REQUIRE(true == success);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer::parseObject") {
|
||||
unsigned char json[] = "{\"a\":42}";
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||
bool success = deserializeJson(obj, json);
|
||||
|
||||
REQUIRE(true == obj.success());
|
||||
REQUIRE(true == success);
|
||||
}
|
||||
|
||||
SECTION("JsonVariant constructor") {
|
||||
@ -49,8 +49,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
@ -60,8 +60,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
@ -70,8 +70,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator==") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "hello";
|
||||
|
||||
REQUIRE(comparand == variant);
|
||||
REQUIRE(variant == comparand);
|
||||
@ -82,8 +82,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonVariant::operator!=") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "world";
|
||||
|
||||
REQUIRE(comparand != variant);
|
||||
REQUIRE(variant != comparand);
|
||||
@ -95,8 +95,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj[key] = "world";
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -106,8 +105,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObjectSubscript::operator=") { // issue #416
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"] = value;
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -116,8 +114,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObjectSubscript::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"].set(value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -127,8 +124,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj[key]);
|
||||
}
|
||||
@ -137,8 +134,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::get()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj.get<char*>(key));
|
||||
}
|
||||
@ -146,8 +143,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::set() key") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(key, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -156,8 +152,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::set() value") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("hello", value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -166,8 +161,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::set key&value") {
|
||||
unsigned char key[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(key, key);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
@ -176,8 +170,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::containsKey()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(true == obj.containsKey(key));
|
||||
}
|
||||
@ -185,8 +179,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::remove()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
obj.remove(key);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
@ -195,8 +189,8 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::is()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":42}");
|
||||
|
||||
REQUIRE(true == obj.is<int>(key));
|
||||
}
|
||||
@ -204,24 +198,21 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonObject::createNestedArray()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedArray(key);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::createNestedObject()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedObject(key);
|
||||
}
|
||||
|
||||
SECTION("JsonArray::add()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
@ -230,8 +221,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonArray::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr.set(0, value);
|
||||
|
||||
@ -241,8 +231,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonArraySubscript::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0].set(value);
|
||||
|
||||
@ -252,8 +241,7 @@ TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonArraySubscript::operator=") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0] = value;
|
||||
|
||||
|
@ -22,10 +22,10 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "[42]");
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(vla);
|
||||
StaticJsonArray<JSON_ARRAY_SIZE(1)> arr;
|
||||
bool success = deserializeJson(arr, vla);
|
||||
|
||||
REQUIRE(true == arr.success());
|
||||
REQUIRE(true == success);
|
||||
}
|
||||
|
||||
SECTION("ParseObject") {
|
||||
@ -33,8 +33,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "{\"a\":42}");
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(vla);
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1)> obj;
|
||||
deserializeJson(obj, vla);
|
||||
|
||||
REQUIRE(true == obj.success());
|
||||
}
|
||||
@ -44,8 +44,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
StaticJsonBuffer<1> jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse(vla);
|
||||
StaticJsonVariant<> variant;
|
||||
deserializeJson(variant, vla);
|
||||
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
@ -77,8 +77,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
}
|
||||
@ -90,8 +90,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonVariant variant;
|
||||
deserializeJson(variant, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
}
|
||||
@ -102,8 +102,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "hello";
|
||||
|
||||
REQUIRE((vla == variant));
|
||||
REQUIRE((variant == vla));
|
||||
@ -116,8 +116,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
DynamicJsonVariant variant;
|
||||
variant = "world";
|
||||
|
||||
REQUIRE((vla != variant));
|
||||
REQUIRE((variant != vla));
|
||||
@ -131,8 +131,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj[vla] = "world";
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -144,8 +143,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"] = vla;
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
@ -156,8 +154,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj["hello"].set(vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
@ -169,8 +166,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj[vla]);
|
||||
}
|
||||
@ -181,8 +178,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(std::string("world") == obj.get<char*>(vla));
|
||||
}
|
||||
@ -192,8 +189,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(vla, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -204,8 +200,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("hello", vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -216,8 +211,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set(vla, vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
@ -228,8 +222,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(true == obj.containsKey(vla));
|
||||
}
|
||||
@ -239,8 +233,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":\"world\"}");
|
||||
obj.remove(vla);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
@ -251,8 +245,8 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
DynamicJsonObject obj;
|
||||
deserializeJson(obj, "{\"hello\":42}");
|
||||
|
||||
REQUIRE(true == obj.is<int>(vla));
|
||||
}
|
||||
@ -262,8 +256,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedArray(vla);
|
||||
}
|
||||
|
||||
@ -272,8 +265,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.createNestedObject(vla);
|
||||
}
|
||||
|
||||
@ -282,8 +274,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(vla);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
@ -294,8 +285,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr.set(0, vla);
|
||||
|
||||
@ -307,8 +297,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0].set(vla);
|
||||
|
||||
@ -320,8 +309,7 @@ TEST_CASE("Variable Length Array") {
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add("hello");
|
||||
arr[0] = vla;
|
||||
|
||||
|
Reference in New Issue
Block a user