TEST(DynamicJsonBuffer_Array_Tests, GrowsWithArray) { DynamicJsonBuffer jsonBuffer; JsonArray &array = jsonBuffer.createArray(); ASSERT_EQ(JSON_ARRAY_SIZE(0), jsonBuffer.size()); array.add("hello"); ASSERT_EQ(JSON_ARRAY_SIZE(1), jsonBuffer.size()); array.add("world"); ASSERT_EQ(JSON_ARRAY_SIZE(2), jsonBuffer.size()); }
TEST(StaticJsonBuffer_CreateArray_Tests, GrowsWithArray) { StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json; JsonArray &array = json.createArray(); ASSERT_EQ(JSON_ARRAY_SIZE(0), json.size()); array.add("hello"); ASSERT_EQ(JSON_ARRAY_SIZE(1), json.size()); array.add("world"); ASSERT_EQ(JSON_ARRAY_SIZE(2), json.size()); }
TEST(JsonArray_CopyFrom_Tests, TwoDimensions_JsonBufferTooSmall) { const size_t SIZE = JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); StaticJsonBuffer<SIZE> jsonBuffer; JsonArray& array = jsonBuffer.createArray(); char json[32]; int source[][3] = {{1, 2, 3}, {4, 5, 6}}; bool ok = array.copyFrom(source); ASSERT_FALSE(ok); array.printTo(json, sizeof(json)); ASSERT_STREQ("[[1,2,3],[4,5]]", json); }
TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForArrayWithOneValue) { StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize; with(bufferOfRightSize); whenInputIs("[1]"); parseMustSucceed(); }
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithNestedObject) { StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall; with(bufferTooSmall); whenInputIs("[{}]"); parseMustFail(); }
TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForArrayWithNestedObject) { StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize; with(bufferOfRightSize); whenInputIs("[{}]"); parseMustSucceed(); }
TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForObjectWithNestedObject) { StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> bufferOfRightSize; with(bufferOfRightSize); whenInputIs("{\"a\":[]}"); parseMustSucceed(); }
TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForObjectWithNestedObject) { StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> bufferTooSmall; with(bufferTooSmall); whenInputIs("{\"a\":[]}"); parseMustFail(); }
void HoofSerial::sendData(const DataPacket& dataPacket) { String output; const int BUFFER_SIZE = JSON_OBJECT_SIZE(1) + (JSON_ARRAY_SIZE(DATA_ARRAY_SIZE) * (JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(4))); StaticJsonBuffer<BUFFER_SIZE> jsonBuffer; JsonObject& root = jsonBuffer.createObject(); //Build object tree in memory root["type"] = dataPacket.type; JsonArray& dataObj = root.createNestedArray("sample"); for(unsigned int i = 0; i < DATA_ARRAY_SIZE; i++) { JsonObject& test = dataObj.createNestedObject(); test["t"] = dataPacket.data[i].timeStamp; JsonArray& obj = test.createNestedArray("f"); for(unsigned int j = 0; j < 4; j++) { obj.add(dataPacket.data[i].data[j]); } } root.printTo(output); //Generate the JSON string this->println(output, true); //Send over the Serial medium }
TEST(StaticJsonBuffer_CreateArray_Tests, ArrayDoesntGrowWhenFull) { StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json; JsonArray &array = json.createArray(); array.add("hello"); array.add("world"); EXPECT_EQ(1, array.size()); }
static void run_iterator_test() { StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc; JsonArray array = doc.to<JsonArray>(); array.add(12); array.add(34); TIterator it = array.begin(); TIterator end = array.end(); REQUIRE(end != it); REQUIRE(12 == it->template as<int>()); REQUIRE(12 == static_cast<int>(*it)); ++it; REQUIRE(end != it); REQUIRE(34 == it->template as<int>()); REQUIRE(34 == static_cast<int>(*it)); ++it; REQUIRE(end == it); }
static void run_iterator_test() { StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jsonBuffer; JsonArray &array = jsonBuffer.createArray(); array.add(12); array.add(34); TIterator it = array.begin(); TIterator end = array.end(); EXPECT_NE(end, it); EXPECT_EQ(12, it->template as<int>()); EXPECT_EQ(12, static_cast<int>(*it)); ++it; EXPECT_NE(end, it); EXPECT_EQ(34, it->template as<int>()); EXPECT_EQ(34, static_cast<int>(*it)); ++it; EXPECT_EQ(end, it); }
bool HttpBuyTransaction::parse() { StaticJsonBuffer<JSON_OBJECT_SIZE(4)+JSON_ARRAY_SIZE(2)> jsonBuffer; JsonObject& root = jsonBuffer.parseObject(buffer); if (!root.success()) {Serial.println("JSON error"); error = "JSON error"; return false;} melody = root["Melody"]; if (melody == NULL) {Serial.println("No melody sent"); error = "No melody sent"; return false;} JsonArray& messageArray = root["Message"]; if (!messageArray.success()) return false; messages[0] = messageArray[0]; messages[1] = messageArray[1]; time = root["Time"]; if (time == NULL) {Serial.println("No time sent"); error = "No time sent"; return false;} hash = root["Hash"]; if (hash == NULL) {Serial.println("No hash sent"); error = "No hash sent"; return false;} return true; }
TEST(StaticJsonBuffer_CreateArray_Tests, SucceedWhenBigEnough) { StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json; JsonArray &array = json.createArray(); ASSERT_TRUE(array.success()); }
// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2018 // MIT License #include <ArduinoJson.h> #include <catch.hpp> TEST_CASE("deserialize JSON array with a StaticJsonDocument") { SECTION("BufferOfTheRightSizeForEmptyArray") { StaticJsonDocument<JSON_ARRAY_SIZE(0)> doc; char input[] = "[]"; DeserializationError err = deserializeJson(doc, input); REQUIRE(err == DeserializationError::Ok); } SECTION("TooSmallBufferForArrayWithOneValue") { StaticJsonDocument<JSON_ARRAY_SIZE(1) - 1> doc; char input[] = "[1]"; DeserializationError err = deserializeJson(doc, input); REQUIRE(err == DeserializationError::NoMemory); } SECTION("BufferOfTheRightSizeForArrayWithOneValue") { StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc; char input[] = "[1]"; DeserializationError err = deserializeJson(doc, input);
TEST(StaticJsonBuffer_CreateArray_Tests, FailsWhenTooSmall) { StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json; JsonArray &array = json.createArray(); ASSERT_FALSE(array.success()); }
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) { StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall; with(bufferTooSmall); whenInputIs("[1]"); parseMustFail(); }
TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) { StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize; with(bufferOfRightSize); whenInputIs("[]"); parseMustSucceed(); }
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) { StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall; with(bufferTooSmall); whenInputIs("[]"); parseMustFail(); }
// Copyright Benoit Blanchon 2014-2017 // MIT License // // Arduino JSON library // https://bblanchon.github.io/ArduinoJson/ // If you like this project, please add a star! #include <ArduinoJson.h> #include <catch.hpp> TEST_CASE("StaticJsonBuffer::createArray()") { SECTION("GrowsWithArray") { StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json; JsonArray &array = json.createArray(); REQUIRE(JSON_ARRAY_SIZE(0) == json.size()); array.add("hello"); REQUIRE(JSON_ARRAY_SIZE(1) == json.size()); array.add("world"); REQUIRE(JSON_ARRAY_SIZE(2) == json.size()); } SECTION("SucceedWhenBigEnough") { StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json; JsonArray &array = json.createArray(); REQUIRE(array.success()); }
#define CONFLICTS_WITH_BUILTIN_OPERATOR #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wvla" #else #define VLA_NOT_SUPPORTED #endif #ifndef VLA_NOT_SUPPORTED TEST_CASE("Variable Length Array") { SECTION("ParseArray") { int i = 8; char vla[i]; strcpy(vla, "[42]"); StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer; JsonArray& arr = jsonBuffer.parseArray(vla); REQUIRE(true == arr.success()); } SECTION("ParseObject") { int i = 16; char vla[i]; strcpy(vla, "{\"a\":42}"); StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer; JsonObject& obj = jsonBuffer.parseObject(vla); REQUIRE(true == obj.success()); }
// Copyright Benoit Blanchon 2014-2017 // MIT License // // Arduino JSON library // https://bblanchon.github.io/ArduinoJson/ // If you like this project, please add a star! #include <ArduinoJson.h> #include <catch.hpp> TEST_CASE("DynamicJsonBuffer::createArray()") { DynamicJsonBuffer jsonBuffer; JsonArray &array = jsonBuffer.createArray(); SECTION("GrowsWithArray") { REQUIRE(JSON_ARRAY_SIZE(0) == jsonBuffer.size()); array.add("hello"); REQUIRE(JSON_ARRAY_SIZE(1) == jsonBuffer.size()); array.add("world"); REQUIRE(JSON_ARRAY_SIZE(2) == jsonBuffer.size()); } SECTION("CanAdd1000Values") { for (size_t i = 1; i <= 1000; i++) { array.add("hello"); REQUIRE(array.size() == i); } } }