2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-10-16 16:23:24 +02:00
|
|
|
#include <gtest/gtest.h>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/JsonArray.hpp>
|
|
|
|
#include <ArduinoJson/JsonObject.hpp>
|
|
|
|
#include <ArduinoJson/JsonValue.hpp>
|
|
|
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
struct Person {
|
|
|
|
int id;
|
|
|
|
char name[32];
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class Issue10 : public testing::Test {
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
virtual void SetUp() {
|
|
|
|
Person boss;
|
|
|
|
boss.id = 1;
|
|
|
|
strcpy(boss.name, "Jeff");
|
|
|
|
Person employee;
|
|
|
|
employee.id = 2;
|
|
|
|
strcpy(employee.name, "John");
|
|
|
|
persons[0] = boss;
|
|
|
|
persons[1] = employee;
|
|
|
|
}
|
|
|
|
|
2014-10-27 22:50:50 +01:00
|
|
|
void checkJsonString(JsonPrintable &p) {
|
2014-10-23 19:54:00 +02:00
|
|
|
char buffer[256];
|
|
|
|
p.printTo(buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]",
|
|
|
|
buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Person persons[2];
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(Issue10, PopulateArrayByAddingAnObject) {
|
2014-10-28 16:29:55 +01:00
|
|
|
StaticJsonBuffer<20> json;
|
2014-10-23 19:54:00 +02:00
|
|
|
JsonArray array = json.createArray();
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
JsonObject object = json.createObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["id"] = persons[i].id;
|
|
|
|
object["name"] = persons[i].name;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
array.add(object); // <- adds a reference to an existing objet (creates 2
|
|
|
|
// extra proxy nodes)
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
checkJsonString(array);
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
|
2014-10-28 16:29:55 +01:00
|
|
|
StaticJsonBuffer<20> json;
|
2014-10-23 19:54:00 +02:00
|
|
|
JsonArray array = json.createArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
JsonObject object = array.createNestedObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["id"] = persons[i].id;
|
|
|
|
object["name"] = persons[i].name;
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
checkJsonString(array);
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|