mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added support for JsonDocument to copyArray() (issue #1255)
				
					
				
			This commit is contained in:
		| @@ -7,6 +7,7 @@ HEAD | ||||
| * CMake: don't build tests when imported in another project | ||||
| * CMake: made project arch-independent | ||||
| * Visual Studio: fixed error C2766 with flag `/Zc:__cplusplus` (issue #1250) | ||||
| * Added support for `JsonDocument` to `copyArray()` (issue #1255) | ||||
|  | ||||
| v6.15.1 (2020-04-08) | ||||
| ------- | ||||
|   | ||||
| @@ -19,6 +19,18 @@ TEST_CASE("copyArray()") { | ||||
|     REQUIRE(std::string("[1,2,3]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("1D -> JsonDocument") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[32]; | ||||
|     int source[] = {1, 2, 3}; | ||||
|  | ||||
|     bool ok = copyArray(source, doc); | ||||
|     REQUIRE(ok); | ||||
|  | ||||
|     serializeJson(doc, json, sizeof(json)); | ||||
|     REQUIRE(std::string("[1,2,3]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("1D -> JsonArray, but not enough memory") { | ||||
|     const size_t SIZE = JSON_ARRAY_SIZE(2); | ||||
|     StaticJsonDocument<SIZE> doc; | ||||
| @@ -46,6 +58,18 @@ TEST_CASE("copyArray()") { | ||||
|     REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("2D -> JsonDocument") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[32]; | ||||
|     int source[][3] = {{1, 2, 3}, {4, 5, 6}}; | ||||
|  | ||||
|     bool ok = copyArray(source, doc); | ||||
|     REQUIRE(ok); | ||||
|  | ||||
|     serializeJson(doc, json, sizeof(json)); | ||||
|     REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json); | ||||
|   } | ||||
|  | ||||
|   SECTION("2D -> JsonArray, but not enough memory") { | ||||
|     const size_t SIZE = | ||||
|         JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); | ||||
| @@ -96,6 +120,22 @@ TEST_CASE("copyArray()") { | ||||
|     REQUIRE(2 == destination[1]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonDocument -> 1D") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[1,2,3]"; | ||||
|     DeserializationError err = deserializeJson(doc, json); | ||||
|     REQUIRE(err == DeserializationError::Ok); | ||||
|  | ||||
|     int destination[4] = {0}; | ||||
|     size_t result = copyArray(doc, destination); | ||||
|  | ||||
|     REQUIRE(3 == result); | ||||
|     REQUIRE(1 == destination[0]); | ||||
|     REQUIRE(2 == destination[1]); | ||||
|     REQUIRE(3 == destination[2]); | ||||
|     REQUIRE(0 == destination[3]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonArray -> 2D") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[[1,2],[3],[4]]"; | ||||
| @@ -114,4 +154,22 @@ TEST_CASE("copyArray()") { | ||||
|     REQUIRE(4 == destination[2][0]); | ||||
|     REQUIRE(0 == destination[2][1]); | ||||
|   } | ||||
|  | ||||
|   SECTION("JsonDocument -> 2D") { | ||||
|     DynamicJsonDocument doc(4096); | ||||
|     char json[] = "[[1,2],[3],[4]]"; | ||||
|  | ||||
|     DeserializationError err = deserializeJson(doc, json); | ||||
|     REQUIRE(err == DeserializationError::Ok); | ||||
|  | ||||
|     int destination[3][2] = {{0}}; | ||||
|     copyArray(doc, destination); | ||||
|  | ||||
|     REQUIRE(1 == destination[0][0]); | ||||
|     REQUIRE(2 == destination[0][1]); | ||||
|     REQUIRE(3 == destination[1][0]); | ||||
|     REQUIRE(0 == destination[1][1]); | ||||
|     REQUIRE(4 == destination[2][0]); | ||||
|     REQUIRE(0 == destination[2][1]); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -5,6 +5,7 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <ArduinoJson/Array/ArrayRef.hpp> | ||||
| #include <ArduinoJson/Document/JsonDocument.hpp> | ||||
|  | ||||
| namespace ARDUINOJSON_NAMESPACE { | ||||
|  | ||||
| @@ -14,6 +15,12 @@ inline bool copyArray(T (&src)[N], ArrayRef dst) { | ||||
|   return copyArray(src, N, dst); | ||||
| } | ||||
|  | ||||
| // Copy a 1D array to a JsonDocument | ||||
| template <typename T, size_t N> | ||||
| inline bool copyArray(T (&src)[N], JsonDocument& dst) { | ||||
|   return copyArray(src, dst.to<ArrayRef>()); | ||||
| } | ||||
|  | ||||
| // Copy a 1D array to a JsonArray | ||||
| template <typename T> | ||||
| inline bool copyArray(T* src, size_t len, ArrayRef dst) { | ||||
| @@ -24,6 +31,12 @@ inline bool copyArray(T* src, size_t len, ArrayRef dst) { | ||||
|   return ok; | ||||
| } | ||||
|  | ||||
| // Copy a 1D array to a JsonDocument | ||||
| template <typename T> | ||||
| inline bool copyArray(T* src, size_t len, JsonDocument& dst) { | ||||
|   return copyArray(src, len, dst.to<ArrayRef>()); | ||||
| } | ||||
|  | ||||
| // Copy a 2D array to a JsonArray | ||||
| template <typename T, size_t N1, size_t N2> | ||||
| inline bool copyArray(T (&src)[N1][N2], ArrayRef dst) { | ||||
| @@ -37,12 +50,24 @@ inline bool copyArray(T (&src)[N1][N2], ArrayRef dst) { | ||||
|   return ok; | ||||
| } | ||||
|  | ||||
| // Copy a 2D array to a JsonDocument | ||||
| template <typename T, size_t N1, size_t N2> | ||||
| inline bool copyArray(T (&src)[N1][N2], JsonDocument& dst) { | ||||
|   return copyArray(src, dst.to<ArrayRef>()); | ||||
| } | ||||
|  | ||||
| // Copy a JsonArray to a 1D array | ||||
| template <typename T, size_t N> | ||||
| inline size_t copyArray(ArrayConstRef src, T (&dst)[N]) { | ||||
|   return copyArray(src, dst, N); | ||||
| } | ||||
|  | ||||
| // Copy a JsonDocument to a 1D array | ||||
| template <typename T, size_t N> | ||||
| inline size_t copyArray(const JsonDocument& src, T (&dst)[N]) { | ||||
|   return copyArray(src.as<ArrayConstRef>(), dst, N); | ||||
| } | ||||
|  | ||||
| // Copy a JsonArray to a 1D array | ||||
| template <typename T> | ||||
| inline size_t copyArray(ArrayConstRef src, T* dst, size_t len) { | ||||
| @@ -63,4 +88,10 @@ inline void copyArray(ArrayConstRef src, T (&dst)[N1][N2]) { | ||||
|   } | ||||
| } | ||||
|  | ||||
| // Copy a JsonDocument to a 2D array | ||||
| template <typename T, size_t N1, size_t N2> | ||||
| inline void copyArray(const JsonDocument& src, T (&dst)[N1][N2]) { | ||||
|   copyArray(src.as<ArrayConstRef>(), dst); | ||||
| } | ||||
|  | ||||
| }  // namespace ARDUINOJSON_NAMESPACE | ||||
|   | ||||
		Reference in New Issue
	
	Block a user