2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2023-02-16 11:45:01 +01:00
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
2018-02-26 16:05:16 +01:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
2023-07-17 14:39:57 +02:00
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
2023-03-29 19:18:06 +02:00
|
|
|
using ArduinoJson::detail::sizeofObject;
|
2023-05-25 09:21:54 +02:00
|
|
|
using ArduinoJson::detail::sizeofString;
|
2023-03-29 19:18:06 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
TEST_CASE("deserialize JSON object") {
|
2023-07-24 17:21:25 +02:00
|
|
|
SpyingAllocator allocator;
|
|
|
|
JsonDocument doc(&allocator);
|
2018-02-26 16:05:16 +01:00
|
|
|
|
|
|
|
SECTION("An empty object") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Quotes") {
|
|
|
|
SECTION("Double quotes") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"key\":\"value\"}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["key"] == "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Single quotes") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{'key':'value'}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["key"] == "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("No quotes") {
|
2018-07-04 11:57:30 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{key:'value'}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["key"] == "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("No quotes, allow underscore in key") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{_k_e_y_:42}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["_k_e_y_"] == 42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Spaces") {
|
|
|
|
SECTION("Before the key") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{ \"key\":\"value\"}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["key"] == "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("After the key") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"key\" :\"value\"}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["key"] == "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Before the value") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"key\": \"value\"}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["key"] == "value");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("After the value") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"key\":\"value\" }");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 1);
|
|
|
|
REQUIRE(obj["key"] == "value");
|
|
|
|
}
|
|
|
|
|
2023-01-12 20:53:26 +01:00
|
|
|
SECTION("Before the comma") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err =
|
2018-04-17 21:27:45 +02:00
|
|
|
deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 2);
|
|
|
|
REQUIRE(obj["key1"] == "value1");
|
|
|
|
REQUIRE(obj["key2"] == "value2");
|
|
|
|
}
|
|
|
|
|
2023-01-12 20:53:26 +01:00
|
|
|
SECTION("After the comma") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err =
|
2023-01-12 20:53:26 +01:00
|
|
|
deserializeJson(doc, "{\"key1\":\"value1\", \"key2\":\"value2\"}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 2);
|
|
|
|
REQUIRE(obj["key1"] == "value1");
|
|
|
|
REQUIRE(obj["key2"] == "value2");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Values types") {
|
|
|
|
SECTION("String") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err =
|
2018-04-17 21:27:45 +02:00
|
|
|
deserializeJson(doc, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 2);
|
|
|
|
REQUIRE(obj["key1"] == "value1");
|
|
|
|
REQUIRE(obj["key2"] == "value2");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Integer") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err =
|
|
|
|
deserializeJson(doc, "{\"key1\":42,\"key2\":-42}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 2);
|
|
|
|
REQUIRE(obj["key1"] == 42);
|
|
|
|
REQUIRE(obj["key2"] == -42);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Double") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err =
|
|
|
|
deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 2);
|
|
|
|
REQUIRE(obj["key1"] == 12.345);
|
|
|
|
REQUIRE(obj["key2"] == -7E89);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Booleans") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err =
|
|
|
|
deserializeJson(doc, "{\"key1\":true,\"key2\":false}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 2);
|
|
|
|
REQUIRE(obj["key1"] == true);
|
|
|
|
REQUIRE(obj["key2"] == false);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Null") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err =
|
|
|
|
deserializeJson(doc, "{\"key1\":null,\"key2\":null}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(obj.size() == 2);
|
2021-03-08 09:58:09 +01:00
|
|
|
REQUIRE(obj["key1"].as<const char*>() == 0);
|
|
|
|
REQUIRE(obj["key2"].as<const char*>() == 0);
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Array") {
|
|
|
|
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, jsonString);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-02-26 16:05:16 +01:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array1 = obj["ab"];
|
|
|
|
const JsonArray array2 = obj["cd"];
|
|
|
|
JsonArray array3 = obj["ef"];
|
2018-02-26 16:05:16 +01:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-02-26 16:05:16 +01:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
REQUIRE(array1.isNull() == false);
|
|
|
|
REQUIRE(array2.isNull() == false);
|
|
|
|
REQUIRE(array3.isNull() == true);
|
2018-02-26 16:05:16 +01:00
|
|
|
|
|
|
|
REQUIRE(2 == array1.size());
|
|
|
|
REQUIRE(2 == array2.size());
|
|
|
|
REQUIRE(0 == array3.size());
|
|
|
|
|
|
|
|
REQUIRE(1 == array1[0].as<int>());
|
|
|
|
REQUIRE(2 == array1[1].as<int>());
|
|
|
|
|
|
|
|
REQUIRE(3 == array2[0].as<int>());
|
|
|
|
REQUIRE(4 == array2[1].as<int>());
|
|
|
|
|
|
|
|
REQUIRE(0 == array3[0].as<int>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:12:59 +02:00
|
|
|
SECTION("Premature null terminator") {
|
|
|
|
SECTION("After opening brace") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{");
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::IncompleteInput);
|
2018-05-14 17:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("After key") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"hello\"");
|
2018-05-14 17:12:59 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::IncompleteInput);
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:12:59 +02:00
|
|
|
SECTION("After colon") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"hello\":");
|
2018-05-14 17:12:59 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::IncompleteInput);
|
2018-05-14 17:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("After value") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"");
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::IncompleteInput);
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:12:59 +02:00
|
|
|
SECTION("After comma") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\",");
|
2018-05-14 17:12:59 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::IncompleteInput);
|
2018-05-14 17:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Misc") {
|
2018-02-26 16:05:16 +01:00
|
|
|
SECTION("A quoted key without value") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"key\"}");
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::InvalidInput);
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("A non-quoted key without value") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{key}");
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::InvalidInput);
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("A dangling comma") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{\"key1\":\"value1\",}");
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::InvalidInput);
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("null as a key") {
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, "{null:\"value\"}");
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
2019-02-15 13:29:30 +01:00
|
|
|
|
|
|
|
SECTION("Repeated key") {
|
|
|
|
DeserializationError err = deserializeJson(doc, "{a:{b:{c:1}},a:2}");
|
|
|
|
|
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2023-07-24 17:21:25 +02:00
|
|
|
REQUIRE(doc.as<std::string>() == "{\"a\":2}");
|
|
|
|
REQUIRE(allocator.log() ==
|
|
|
|
AllocatorLog()
|
|
|
|
// a
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
|
|
|
|
// pool
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
// b
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
|
|
|
|
// c
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
|
|
|
|
// string builder
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
// remove b & c
|
|
|
|
<< AllocatorLog::Deallocate(sizeofString(1)) * 2
|
|
|
|
// string builder
|
|
|
|
<< AllocatorLog::Deallocate(sizeofString(31))
|
|
|
|
|
|
|
|
);
|
2019-02-15 13:29:30 +01:00
|
|
|
}
|
2022-01-13 16:12:56 +01:00
|
|
|
|
|
|
|
SECTION("Repeated key with zero copy mode") { // issue #1697
|
|
|
|
char input[] = "{a:{b:{c:1}},a:2}";
|
|
|
|
DeserializationError err = deserializeJson(doc, input);
|
|
|
|
|
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
|
|
|
REQUIRE(doc["a"] == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("NUL in keys") { // we don't support NULs in keys
|
|
|
|
DeserializationError err =
|
|
|
|
deserializeJson(doc, "{\"x\\u0000a\":1,\"x\\u0000b\":2}");
|
|
|
|
|
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
|
|
|
REQUIRE(doc.as<std::string>() == "{\"x\":2}");
|
|
|
|
}
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
2018-03-18 14:50:52 +01:00
|
|
|
|
|
|
|
SECTION("Should clear the JsonObject") {
|
2018-04-17 21:27:45 +02:00
|
|
|
deserializeJson(doc, "{\"hello\":\"world\"}");
|
|
|
|
deserializeJson(doc, "{}");
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
|
|
|
REQUIRE(doc.is<JsonObject>());
|
2018-03-18 14:50:52 +01:00
|
|
|
REQUIRE(obj.size() == 0);
|
2023-07-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() ==
|
|
|
|
AllocatorLog()
|
|
|
|
// string "hello"
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(5))
|
|
|
|
// pool
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
// string "world"
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(31))
|
|
|
|
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(5))
|
|
|
|
// free pool
|
|
|
|
<< AllocatorLog::Deallocate(sizeofPool())
|
|
|
|
// free "hello" and "world"
|
|
|
|
<< AllocatorLog::Deallocate(sizeofString(5))
|
|
|
|
<< AllocatorLog::Deallocate(sizeofString(5)));
|
2018-03-18 14:50:52 +01:00
|
|
|
}
|
2020-08-04 09:52:42 +02:00
|
|
|
|
|
|
|
SECTION("Issue #1335") {
|
|
|
|
std::string json("{\"a\":{},\"b\":{}}");
|
|
|
|
deserializeJson(doc, json);
|
|
|
|
CHECK(doc.as<std::string>() == json);
|
|
|
|
}
|
2018-02-26 16:05:16 +01:00
|
|
|
}
|
2023-03-15 14:54:55 +01:00
|
|
|
|
|
|
|
TEST_CASE("deserialize JSON object under memory constraints") {
|
2023-07-17 14:39:57 +02:00
|
|
|
TimebombAllocator allocator(1024);
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc(&allocator);
|
2023-07-17 14:39:57 +02:00
|
|
|
|
|
|
|
SECTION("empty object requires no allocation") {
|
|
|
|
allocator.setCountdown(0);
|
2023-03-15 14:54:55 +01:00
|
|
|
char input[] = "{}";
|
|
|
|
|
|
|
|
DeserializationError err = deserializeJson(doc, input);
|
|
|
|
|
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2023-07-17 14:39:57 +02:00
|
|
|
REQUIRE(doc.as<std::string>() == "{}");
|
2023-03-15 14:54:55 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 14:39:57 +02:00
|
|
|
SECTION("key allocation fails") {
|
|
|
|
allocator.setCountdown(0);
|
2023-03-15 14:54:55 +01:00
|
|
|
char input[] = "{\"a\":1}";
|
|
|
|
|
|
|
|
DeserializationError err = deserializeJson(doc, input);
|
|
|
|
|
|
|
|
REQUIRE(err == DeserializationError::NoMemory);
|
2023-07-17 14:39:57 +02:00
|
|
|
REQUIRE(doc.as<std::string>() == "{}");
|
2023-03-15 14:54:55 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 14:39:57 +02:00
|
|
|
SECTION("pool allocation fails") {
|
2023-07-21 10:38:35 +02:00
|
|
|
allocator.setCountdown(2);
|
2023-07-17 14:39:57 +02:00
|
|
|
char input[] = "{\"a\":1}";
|
2023-03-15 14:54:55 +01:00
|
|
|
|
|
|
|
DeserializationError err = deserializeJson(doc, input);
|
|
|
|
|
|
|
|
REQUIRE(err == DeserializationError::NoMemory);
|
2023-07-17 14:39:57 +02:00
|
|
|
REQUIRE(doc.as<std::string>() == "{}");
|
2023-03-15 14:54:55 +01:00
|
|
|
}
|
|
|
|
|
2023-07-17 14:39:57 +02:00
|
|
|
SECTION("string allocation fails") {
|
2023-07-21 10:38:35 +02:00
|
|
|
allocator.setCountdown(3);
|
2023-07-17 14:39:57 +02:00
|
|
|
char input[] = "{\"a\":\"b\"}";
|
2023-03-15 14:54:55 +01:00
|
|
|
|
|
|
|
DeserializationError err = deserializeJson(doc, input);
|
|
|
|
|
2023-07-17 14:39:57 +02:00
|
|
|
REQUIRE(err == DeserializationError::NoMemory);
|
|
|
|
REQUIRE(doc.as<std::string>() == "{\"a\":null}");
|
2023-03-15 14:54:55 +01:00
|
|
|
}
|
|
|
|
}
|