You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
2.9 KiB
139 lines
2.9 KiB
2 years ago
|
// ArduinoJson - https://arduinojson.org
|
||
2 years ago
|
// Copyright © 2014-2023, Benoit BLANCHON
|
||
2 years ago
|
// MIT License
|
||
|
|
||
|
#include <ArduinoJson.h>
|
||
|
#include <stdint.h>
|
||
|
#include <catch.hpp>
|
||
|
|
||
2 years ago
|
#include <array>
|
||
2 years ago
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
2 years ago
|
namespace ArduinoJson {
|
||
2 years ago
|
template <typename T>
|
||
2 years ago
|
struct Converter<std::vector<T>> {
|
||
2 years ago
|
static void toJson(const std::vector<T>& src, JsonVariant dst) {
|
||
|
JsonArray array = dst.to<JsonArray>();
|
||
|
for (T item : src)
|
||
|
array.add(item);
|
||
|
}
|
||
|
|
||
|
static std::vector<T> fromJson(JsonVariantConst src) {
|
||
|
std::vector<T> dst;
|
||
|
for (T item : src.as<JsonArrayConst>())
|
||
|
dst.push_back(item);
|
||
|
return dst;
|
||
|
}
|
||
|
|
||
|
static bool checkJson(JsonVariantConst src) {
|
||
|
JsonArrayConst array = src;
|
||
|
bool result = array;
|
||
2 years ago
|
for (JsonVariantConst item : array)
|
||
|
result &= item.is<T>();
|
||
|
return result;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
template <typename T, size_t N>
|
||
2 years ago
|
struct Converter<std::array<T, N>> {
|
||
2 years ago
|
static void toJson(const std::array<T, N>& src, JsonVariant dst) {
|
||
|
JsonArray array = dst.to<JsonArray>();
|
||
|
for (T item : src)
|
||
|
array.add(item);
|
||
|
}
|
||
|
|
||
|
static std::array<T, N> fromJson(JsonVariantConst src) {
|
||
|
std::array<T, N> dst;
|
||
|
dst.fill(0);
|
||
|
size_t idx = 0;
|
||
|
for (T item : src.as<JsonArrayConst>())
|
||
|
dst[idx++] = item;
|
||
|
return dst;
|
||
|
}
|
||
|
|
||
|
static bool checkJson(JsonVariantConst src) {
|
||
|
JsonArrayConst array = src;
|
||
|
bool result = array;
|
||
|
size_t size = 0;
|
||
2 years ago
|
for (JsonVariantConst item : array) {
|
||
2 years ago
|
result &= item.is<T>();
|
||
|
size++;
|
||
2 years ago
|
}
|
||
2 years ago
|
return result && size == N;
|
||
2 years ago
|
}
|
||
|
};
|
||
2 years ago
|
} // namespace ArduinoJson
|
||
2 years ago
|
|
||
|
TEST_CASE("vector<int>") {
|
||
|
SECTION("toJson") {
|
||
|
std::vector<int> v = {1, 2};
|
||
|
|
||
|
StaticJsonDocument<128> doc;
|
||
|
doc.set(v);
|
||
|
REQUIRE(doc.as<std::string>() == "[1,2]");
|
||
|
}
|
||
|
|
||
|
SECTION("fromJson") {
|
||
|
StaticJsonDocument<128> doc;
|
||
|
doc.add(1);
|
||
|
doc.add(2);
|
||
|
|
||
2 years ago
|
auto v = doc.as<std::vector<int>>();
|
||
2 years ago
|
REQUIRE(v.size() == 2);
|
||
|
CHECK(v[0] == 1);
|
||
|
CHECK(v[1] == 2);
|
||
|
}
|
||
|
|
||
|
SECTION("checkJson") {
|
||
|
StaticJsonDocument<128> doc;
|
||
2 years ago
|
CHECK(doc.is<std::vector<int>>() == false);
|
||
2 years ago
|
|
||
|
doc.add(1);
|
||
|
doc.add(2);
|
||
2 years ago
|
CHECK(doc.is<std::vector<int>>() == true);
|
||
2 years ago
|
|
||
|
doc.add("foo");
|
||
2 years ago
|
CHECK(doc.is<std::vector<int>>() == false);
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
TEST_CASE("array<int, 2>") {
|
||
|
typedef std::array<int, 2> array_type;
|
||
|
|
||
|
SECTION("toJson") {
|
||
|
array_type v;
|
||
|
v[0] = 1;
|
||
|
v[1] = 2;
|
||
|
|
||
|
StaticJsonDocument<128> doc;
|
||
|
doc.set(v);
|
||
|
REQUIRE(doc.as<std::string>() == "[1,2]");
|
||
|
}
|
||
|
|
||
|
SECTION("fromJson") {
|
||
|
StaticJsonDocument<128> doc;
|
||
|
doc.add(1);
|
||
|
doc.add(2);
|
||
|
|
||
|
auto v = doc.as<array_type>();
|
||
|
REQUIRE(v.size() == 2);
|
||
|
CHECK(v[0] == 1);
|
||
|
CHECK(v[1] == 2);
|
||
|
}
|
||
|
|
||
|
SECTION("checkJson") {
|
||
|
StaticJsonDocument<128> doc;
|
||
|
CHECK(doc.is<array_type>() == false);
|
||
|
|
||
|
doc.add(1);
|
||
|
CHECK(doc.is<array_type>() == false);
|
||
|
|
||
|
doc.add(2);
|
||
|
CHECK(doc.is<array_type>() == true);
|
||
|
|
||
|
doc[0] = "foo";
|
||
|
CHECK(doc.is<array_type>() == false);
|
||
|
}
|
||
|
}
|