add arduino json lib

#182
This commit is contained in:
1technophile committed 2018-03-07 20:52:36 +01:00
1 parent f0546fab8c
commit 3b4c9d2d98
209 files changed
+25587

No files matched your search

@@ -0,0 +1,20 @@
# ArduinoJson - arduinojson.org
# Copyright Benoit Blanchon 2014-2018
# MIT License
add_executable(JsonObjectTests
basics.cpp
containsKey.cpp
get.cpp
invalid.cpp
iterator.cpp
prettyPrintTo.cpp
printTo.cpp
remove.cpp
set.cpp
size.cpp
subscript.cpp
)
target_link_libraries(JsonObjectTests catch)
add_test(JsonObject JsonObjectTests)
@@ -0,0 +1,19 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject basics") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
SECTION("InitialSizeIsZero") {
REQUIRE(0 == _object.size());
}
SECTION("SuccessIsTrue") {
REQUIRE(_object.success());
}
}
@@ -0,0 +1,30 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::containsKey()") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
_object.set("hello", 42);
REQUIRE(false == _object.containsKey("world"));
}
SECTION("ContainsKeyReturnsTrueForDefinedValue") {
_object.set("hello", 42);
REQUIRE(true == _object.containsKey("hello"));
}
SECTION("ContainsKeyReturnsFalseAfterRemove") {
_object.set("hello", 42);
_object.remove("hello");
REQUIRE(false == _object.containsKey("hello"));
}
}
+19
View File
@@ -0,0 +1,19 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace Catch::Matchers;
TEST_CASE("JsonObject::get()") {
DynamicJsonBuffer jb;
JsonObject& obj = jb.createObject();
SECTION("GetConstCharPointer_GivenStringLiteral") {
obj.set("hello", "world");
const char* value = obj.get<const char*>("hello");
REQUIRE_THAT(value, Equals("world"));
}
}
@@ -0,0 +1,35 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace Catch::Matchers;
TEST_CASE("JsonObject::invalid()") {
JsonObject& obj = JsonObject::invalid();
SECTION("SubscriptFails") {
REQUIRE_FALSE(obj["key"].success());
}
SECTION("AddFails") {
obj.set("hello", "world");
REQUIRE(0 == obj.size());
}
SECTION("CreateNestedArrayFails") {
REQUIRE_FALSE(obj.createNestedArray("hello").success());
}
SECTION("CreateNestedObjectFails") {
REQUIRE_FALSE(obj.createNestedObject("world").success());
}
SECTION("PrintToWritesBraces") {
char buffer[32];
obj.printTo(buffer, sizeof(buffer));
REQUIRE_THAT(buffer, Equals("{}"));
}
}
@@ -0,0 +1,51 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace Catch::Matchers;
TEST_CASE("JsonObject::begin()/end()") {
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> jb;
JsonObject& obj = jb.createObject();
obj["ab"] = 12;
obj["cd"] = 34;
SECTION("NonConstIterator") {
JsonObject::iterator it = obj.begin();
REQUIRE(obj.end() != it);
REQUIRE_THAT(it->key, Equals("ab"));
REQUIRE(12 == it->value);
it->key = "a.b";
it->value = 1.2;
++it;
REQUIRE(obj.end() != it);
REQUIRE_THAT(it->key, Equals("cd"));
REQUIRE(34 == it->value);
it->key = "c.d";
it->value = 3.4;
++it;
REQUIRE(obj.end() == it);
REQUIRE(2 == obj.size());
REQUIRE(1.2 == obj["a.b"]);
REQUIRE(3.4 == obj["c.d"]);
}
SECTION("ConstIterator") {
const JsonObject& const_object = obj;
JsonObject::const_iterator it = const_object.begin();
REQUIRE(const_object.end() != it);
REQUIRE_THAT(it->key, Equals("ab"));
REQUIRE(12 == it->value);
++it;
REQUIRE(const_object.end() != it);
REQUIRE_THAT(it->key, Equals("cd"));
REQUIRE(34 == it->value);
++it;
REQUIRE(const_object.end() == it);
}
}
@@ -0,0 +1,76 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
void check(const JsonObject &obj, const std::string expected) {
char json[256];
size_t actualLen = obj.prettyPrintTo(json);
size_t measuredLen = obj.measurePrettyLength();
REQUIRE(json == expected);
REQUIRE(expected.size() == actualLen);
REQUIRE(expected.size() == measuredLen);
}
TEST_CASE("JsonObject::prettyPrintTo()") {
DynamicJsonBuffer jb;
JsonObject &obj = jb.createObject();
SECTION("EmptyObject") {
check(obj, "{}");
}
SECTION("OneMember") {
obj["key"] = "value";
check(obj,
"{\r\n"
" \"key\": \"value\"\r\n"
"}");
}
SECTION("TwoMembers") {
obj["key1"] = "value1";
obj["key2"] = "value2";
check(obj,
"{\r\n"
" \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n"
"}");
}
SECTION("EmptyNestedContainers") {
obj.createNestedObject("key1");
obj.createNestedArray("key2");
check(obj,
"{\r\n"
" \"key1\": {},\r\n"
" \"key2\": []\r\n"
"}");
}
SECTION("NestedContainers") {
JsonObject &nested1 = obj.createNestedObject("key1");
nested1["a"] = 1;
JsonArray &nested2 = obj.createNestedArray("key2");
nested2.add(2);
check(obj,
"{\r\n"
" \"key1\": {\r\n"
" \"a\": 1\r\n"
" },\r\n"
" \"key2\": [\r\n"
" 2\r\n"
" ]\r\n"
"}");
}
}
+109
View File
@@ -0,0 +1,109 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
void check(const JsonObject &obj, const std::string &expected) {
char actual[256];
size_t actualLen = obj.printTo(actual);
size_t measuredLen = obj.measureLength();
REQUIRE(expected == actual);
REQUIRE(expected.size() == actualLen);
REQUIRE(expected.size() == measuredLen);
}
TEST_CASE("JsonObject::printTo()") {
DynamicJsonBuffer _jsonBuffer;
JsonObject &obj = _jsonBuffer.createObject();
SECTION("EmptyObject") {
check(obj, "{}");
}
SECTION("TwoStrings") {
obj["key1"] = "value1";
obj.set("key2", "value2");
check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
SECTION("RemoveFirst") {
obj["key1"] = "value1";
obj["key2"] = "value2";
obj.remove("key1");
check(obj, "{\"key2\":\"value2\"}");
}
SECTION("RemoveLast") {
obj["key1"] = "value1";
obj["key2"] = "value2";
obj.remove("key2");
check(obj, "{\"key1\":\"value1\"}");
}
SECTION("RemoveUnexistingKey") {
obj["key1"] = "value1";
obj["key2"] = "value2";
obj.remove("key3");
check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
SECTION("ReplaceExistingKey") {
obj["key"] = "value1";
obj["key"] = "value2";
check(obj, "{\"key\":\"value2\"}");
}
SECTION("TwoIntegers") {
obj["a"] = 1;
obj.set("b", 2);
check(obj, "{\"a\":1,\"b\":2}");
}
SECTION("RawJson") {
obj["a"] = RawJson("[1,2]");
obj.set("b", RawJson("[4,5]"));
check(obj, "{\"a\":[1,2],\"b\":[4,5]}");
}
SECTION("Two doubles") {
obj["a"] = 12.34;
obj.set("b", 56.78);
check(obj, "{\"a\":12.34,\"b\":56.78}");
}
SECTION("TwoNull") {
obj["a"] = static_cast<char *>(0);
obj.set("b", static_cast<char *>(0));
check(obj, "{\"a\":null,\"b\":null}");
}
SECTION("TwoBooleans") {
obj["a"] = true;
obj.set("b", false);
check(obj, "{\"a\":true,\"b\":false}");
}
SECTION("ThreeNestedArrays") {
obj.createNestedArray("a");
obj["b"] = _jsonBuffer.createArray();
obj.set("c", _jsonBuffer.createArray());
check(obj, "{\"a\":[],\"b\":[],\"c\":[]}");
}
SECTION("ThreeNestedObjects") {
obj.createNestedObject("a");
obj["b"] = _jsonBuffer.createObject();
obj.set("c", _jsonBuffer.createObject());
check(obj, "{\"a\":{},\"b\":{},\"c\":{}}");
}
}
@@ -0,0 +1,41 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
TEST_CASE("JsonObject::remove()") {
DynamicJsonBuffer jb;
SECTION("SizeDecreased_WhenValuesAreRemoved") {
JsonObject& obj = jb.createObject();
obj["hello"] = 1;
obj.remove("hello");
REQUIRE(0 == obj.size());
}
SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
JsonObject& obj = jb.createObject();
obj["hello"] = 1;
obj.remove("world");
REQUIRE(1 == obj.size());
}
SECTION("RemoveByIterator") {
JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
if (it->value == 1) obj.remove(it);
}
std::string result;
obj.printTo(result);
REQUIRE("{\"a\":0,\"c\":2}" == result);
}
}
+138
View File
@@ -0,0 +1,138 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
TEST_CASE("JsonObject::set()") {
DynamicJsonBuffer jb;
JsonObject& _object = jb.createObject();
SECTION("int") {
_object.set("hello", 123);
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(_object["hello"].is<int>());
REQUIRE_FALSE(_object["hello"].is<bool>());
}
SECTION("double") {
_object.set("hello", 123.45);
REQUIRE(123.45 == _object["hello"].as<double>());
REQUIRE(_object["hello"].is<double>());
REQUIRE_FALSE(_object["hello"].is<bool>());
}
SECTION("bool") {
_object.set("hello", true);
REQUIRE(_object["hello"].as<bool>());
REQUIRE(_object["hello"].is<bool>());
REQUIRE_FALSE(_object["hello"].is<long>());
}
SECTION("const char*") {
_object.set("hello", "h3110");
REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
REQUIRE(_object["hello"].is<const char*>());
REQUIRE_FALSE(_object["hello"].is<long>());
}
SECTION("nested array") {
JsonArray& arr = jb.createArray();
_object.set("hello", arr);
REQUIRE(&arr == &_object["hello"].as<JsonArray>());
REQUIRE(_object["hello"].is<JsonArray&>());
REQUIRE_FALSE(_object["hello"].is<JsonObject&>());
}
SECTION("nested object") {
JsonObject& obj = jb.createObject();
_object.set("hello", obj);
REQUIRE(&obj == &_object["hello"].as<JsonObject>());
REQUIRE(_object["hello"].is<JsonObject&>());
REQUIRE_FALSE(_object["hello"].is<JsonArray&>());
}
SECTION("array subscript") {
JsonArray& arr = jb.createArray();
arr.add(42);
_object.set("a", arr[0]);
REQUIRE(42 == _object["a"]);
}
SECTION("object subscript") {
JsonObject& obj = jb.createObject();
obj.set("x", 42);
_object.set("a", obj["x"]);
REQUIRE(42 == _object["a"]);
}
SECTION("returns true when allocation succeeds") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject();
REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
}
SECTION("returns false when allocation fails") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject();
REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
}
SECTION("should not duplicate const char*") {
_object.set("hello", "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate char* value") {
_object.set("hello", const_cast<char*>("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate char* key") {
_object.set(const_cast<char*>("hello"), "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate char* key&value") {
_object.set(const_cast<char*>("hello"), const_cast<char*>("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= jb.size());
}
SECTION("should duplicate std::string value") {
_object.set("hello", std::string("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate std::string key") {
_object.set(std::string("hello"), "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == jb.size());
}
SECTION("should duplicate std::string key&value") {
_object.set(std::string("hello"), std::string("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= jb.size());
}
}
+23
View File
@@ -0,0 +1,23 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
TEST_CASE("JsonObject::size()") {
DynamicJsonBuffer jb;
JsonObject& _object = jb.createObject();
SECTION("increases when values are added") {
_object.set("hello", 42);
REQUIRE(1 == _object.size());
}
SECTION("doesn't increase when the smae key is added twice") {
_object["hello"] = 1;
_object["hello"] = 2;
REQUIRE(1 == _object.size());
}
}
@@ -0,0 +1,152 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::operator[]") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
SECTION("int") {
_object["hello"] = 123;
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(true == _object["hello"].is<int>());
REQUIRE(false == _object["hello"].is<bool>());
}
SECTION("volatile int") { // issue #415
volatile int i = 123;
_object["hello"] = i;
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(true == _object["hello"].is<int>());
REQUIRE(false == _object["hello"].is<bool>());
}
SECTION("double") {
_object["hello"] = 123.45;
REQUIRE(true == _object["hello"].is<double>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(123.45 == _object["hello"].as<double>());
}
SECTION("bool") {
_object["hello"] = true;
REQUIRE(true == _object["hello"].is<bool>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(true == _object["hello"].as<bool>());
}
SECTION("const char*") {
_object["hello"] = "h3110";
REQUIRE(true == _object["hello"].is<const char*>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
REQUIRE(std::string("h3110") ==
_object["hello"].as<char*>()); // <- short hand
}
SECTION("array") {
JsonArray& arr = _jsonBuffer.createArray();
_object["hello"] = arr;
REQUIRE(&arr == &_object["hello"].as<JsonArray&>());
REQUIRE(&arr == &_object["hello"].as<JsonArray>()); // <- short hand
REQUIRE(&arr == &_object["hello"].as<const JsonArray&>());
REQUIRE(&arr == &_object["hello"].as<const JsonArray>()); // <- short hand
REQUIRE(true == _object["hello"].is<JsonArray&>());
REQUIRE(true == _object["hello"].is<JsonArray>());
REQUIRE(true == _object["hello"].is<const JsonArray&>());
REQUIRE(true == _object["hello"].is<const JsonArray>());
REQUIRE(false == _object["hello"].is<JsonObject&>());
}
SECTION("object") {
JsonObject& obj = _jsonBuffer.createObject();
_object["hello"] = obj;
REQUIRE(&obj == &_object["hello"].as<JsonObject&>());
REQUIRE(&obj == &_object["hello"].as<JsonObject>()); // <- short hand
REQUIRE(&obj == &_object["hello"].as<const JsonObject&>());
REQUIRE(&obj == &_object["hello"].as<const JsonObject>()); // <- short hand
REQUIRE(true == _object["hello"].is<JsonObject&>());
REQUIRE(true == _object["hello"].is<JsonObject>());
REQUIRE(true == _object["hello"].is<const JsonObject&>());
REQUIRE(true == _object["hello"].is<const JsonObject>());
REQUIRE(false == _object["hello"].is<JsonArray&>());
}
SECTION("array subscript") {
JsonArray& arr = _jsonBuffer.createArray();
arr.add(42);
_object["a"] = arr[0];
REQUIRE(42 == _object["a"]);
}
SECTION("object subscript") {
JsonObject& obj = _jsonBuffer.createObject();
obj.set("x", 42);
_object["a"] = obj["x"];
REQUIRE(42 == _object["a"]);
}
SECTION("char key[]") { // issue #423
char key[] = "hello";
_object[key] = 42;
REQUIRE(42 == _object[key]);
}
SECTION("should not duplicate const char*") {
_object["hello"] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char* value") {
_object["hello"] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char* key") {
_object[const_cast<char*>("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate char* key&value") {
_object[const_cast<char*>("hello")] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _jsonBuffer.size());
}
SECTION("should duplicate std::string value") {
_object["hello"] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate std::string key") {
_object[std::string("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
}
SECTION("should duplicate std::string key&value") {
_object[std::string("hello")] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _jsonBuffer.size());
}
}