123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- // ArduinoJson - arduinojson.org
- // Copyright Benoit Blanchon 2014-2019
- // MIT License
- #include <ArduinoJson.h>
- #include <catch.hpp>
- TEST_CASE("deserialize JSON object") {
- DynamicJsonDocument doc(4096);
- SECTION("An empty object") {
- DeserializationError err = deserializeJson(doc, "{}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 0);
- }
- SECTION("Quotes") {
- SECTION("Double quotes") {
- DeserializationError err = deserializeJson(doc, "{\"key\":\"value\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["key"] == "value");
- }
- SECTION("Single quotes") {
- DeserializationError err = deserializeJson(doc, "{'key':'value'}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["key"] == "value");
- }
- SECTION("No quotes") {
- DeserializationError err = deserializeJson(doc, "{key:'value'}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["key"] == "value");
- }
- SECTION("No quotes, allow underscore in key") {
- DeserializationError err = deserializeJson(doc, "{_k_e_y_:42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["_k_e_y_"] == 42);
- }
- }
- SECTION("Spaces") {
- SECTION("Before the key") {
- DeserializationError err = deserializeJson(doc, "{ \"key\":\"value\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["key"] == "value");
- }
- SECTION("After the key") {
- DeserializationError err = deserializeJson(doc, "{\"key\" :\"value\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["key"] == "value");
- }
- SECTION("Before the value") {
- DeserializationError err = deserializeJson(doc, "{\"key\": \"value\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["key"] == "value");
- }
- SECTION("After the value") {
- DeserializationError err = deserializeJson(doc, "{\"key\":\"value\" }");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 1);
- REQUIRE(obj["key"] == "value");
- }
- SECTION("Before the colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 2);
- REQUIRE(obj["key1"] == "value1");
- REQUIRE(obj["key2"] == "value2");
- }
- SECTION("After the colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 2);
- REQUIRE(obj["key1"] == "value1");
- REQUIRE(obj["key2"] == "value2");
- }
- }
- SECTION("Values types") {
- SECTION("String") {
- DeserializationError err =
- deserializeJson(doc, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 2);
- REQUIRE(obj["key1"] == "value1");
- REQUIRE(obj["key2"] == "value2");
- }
- SECTION("Integer") {
- DeserializationError err =
- deserializeJson(doc, "{\"key1\":42,\"key2\":-42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 2);
- REQUIRE(obj["key1"] == 42);
- REQUIRE(obj["key2"] == -42);
- }
- SECTION("Double") {
- DeserializationError err =
- deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 2);
- REQUIRE(obj["key1"] == 12.345);
- REQUIRE(obj["key2"] == -7E89);
- }
- SECTION("Booleans") {
- DeserializationError err =
- deserializeJson(doc, "{\"key1\":true,\"key2\":false}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 2);
- REQUIRE(obj["key1"] == true);
- REQUIRE(obj["key2"] == false);
- }
- SECTION("Null") {
- DeserializationError err =
- deserializeJson(doc, "{\"key1\":null,\"key2\":null}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 2);
- REQUIRE(obj["key1"].as<char*>() == 0);
- REQUIRE(obj["key2"].as<char*>() == 0);
- }
- SECTION("Array") {
- char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
- DeserializationError err = deserializeJson(doc, jsonString);
- JsonObject obj = doc.as<JsonObject>();
- JsonArray array1 = obj["ab"];
- const JsonArray array2 = obj["cd"];
- JsonArray array3 = obj["ef"];
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(array1.isNull() == false);
- REQUIRE(array2.isNull() == false);
- REQUIRE(array3.isNull() == true);
- 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>());
- }
- }
- SECTION("Premature null terminator") {
- SECTION("After opening brace") {
- DeserializationError err = deserializeJson(doc, "{");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("After key") {
- DeserializationError err = deserializeJson(doc, "{\"hello\"");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("After colon") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("After value") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- SECTION("After comma") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\",");
- REQUIRE(err == DeserializationError::IncompleteInput);
- }
- }
- SECTION("Misc") {
- SECTION("A quoted key without value") {
- DeserializationError err = deserializeJson(doc, "{\"key\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("A non-quoted key without value") {
- DeserializationError err = deserializeJson(doc, "{key}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("A dangling comma") {
- DeserializationError err = deserializeJson(doc, "{\"key1\":\"value1\",}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("null as a key") {
- DeserializationError err = deserializeJson(doc, "{null:\"value\"}");
- REQUIRE(err == DeserializationError::Ok);
- }
- SECTION("Repeated key") {
- DeserializationError err = deserializeJson(doc, "{a:{b:{c:1}},a:2}");
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(doc["a"] == 2);
- }
- }
- SECTION("Block comments") {
- SECTION("Before opening brace") {
- DeserializationError err =
- deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After opening brace") {
- DeserializationError err =
- deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- SECTION("After comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- }
- SECTION("Trailing comments") {
- SECTION("Before opening brace") {
- DeserializationError err =
- deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After opening brace") {
- DeserializationError err =
- deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After colon") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("After closing brace") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- SECTION("After comma") {
- DeserializationError err = deserializeJson(
- doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- REQUIRE(obj["answer"] == 42);
- }
- }
- SECTION("Dangling slash") {
- SECTION("Before opening brace") {
- DeserializationError err = deserializeJson(doc, "/{\"hello\":\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After opening brace") {
- DeserializationError err = deserializeJson(doc, "{/\"hello\":\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("Before colon") {
- DeserializationError err = deserializeJson(doc, "{\"hello\"/:\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After colon") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":/\"world\"}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("Before closing brace") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"/}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After closing brace") {
- DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(err == DeserializationError::Ok);
- REQUIRE(obj["hello"] == "world");
- }
- SECTION("Before comma") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\"/,\"answer\":42}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- SECTION("After comma") {
- DeserializationError err =
- deserializeJson(doc, "{\"hello\":\"world\",/\"answer\":42}");
- REQUIRE(err == DeserializationError::InvalidInput);
- }
- }
- SECTION("Should clear the JsonObject") {
- deserializeJson(doc, "{\"hello\":\"world\"}");
- deserializeJson(doc, "{}");
- JsonObject obj = doc.as<JsonObject>();
- REQUIRE(doc.is<JsonObject>());
- REQUIRE(obj.size() == 0);
- REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
- }
- }
|