mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Test custom converter for std::array (issue #1840)
				
					
				
			This commit is contained in:
		| @@ -6,6 +6,7 @@ | |||||||
| #include <stdint.h> | #include <stdint.h> | ||||||
| #include <catch.hpp> | #include <catch.hpp> | ||||||
|  |  | ||||||
|  | #include <array> | ||||||
| #include <string> | #include <string> | ||||||
| #include <vector> | #include <vector> | ||||||
|  |  | ||||||
| @@ -28,14 +29,40 @@ struct Converter<std::vector<T> > { | |||||||
|   static bool checkJson(JsonVariantConst src) { |   static bool checkJson(JsonVariantConst src) { | ||||||
|     JsonArrayConst array = src; |     JsonArrayConst array = src; | ||||||
|     bool result = array; |     bool result = array; | ||||||
|     for (JsonVariantConst item : array) { |     for (JsonVariantConst item : array) | ||||||
|       if (!result) |       result &= item.is<T>(); | ||||||
|         break; |  | ||||||
|       result = item.is<T>(); |  | ||||||
|     } |  | ||||||
|     return result; |     return result; | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | template <typename T, size_t N> | ||||||
|  | struct Converter<std::array<T, N> > { | ||||||
|  |   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; | ||||||
|  |     for (JsonVariantConst item : array) { | ||||||
|  |       result &= item.is<T>(); | ||||||
|  |       size++; | ||||||
|  |     } | ||||||
|  |     return result && size == N; | ||||||
|  |   } | ||||||
|  | }; | ||||||
| }  // namespace ARDUINOJSON_NAMESPACE | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
|  |  | ||||||
| TEST_CASE("vector<int>") { | TEST_CASE("vector<int>") { | ||||||
| @@ -70,3 +97,42 @@ TEST_CASE("vector<int>") { | |||||||
|     CHECK(doc.is<std::vector<int> >() == false); |     CHECK(doc.is<std::vector<int> >() == false); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | 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); | ||||||
|  |   } | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user