mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Fixed serializeJson(obj[key], dst) (closes #794)
				
					
				
			This commit is contained in:
		| @@ -6,6 +6,7 @@ HEAD | ||||
|  | ||||
| * Implemented reference semantics for `JsonVariant` | ||||
| * Replace `JsonPair`'s `key` and `value` with `key()` and `value()` | ||||
| * Fixed `serializeJson(obj[key], dst)` (issue #794) | ||||
|  | ||||
| > ### BREAKING CHANGES | ||||
| > | ||||
|   | ||||
| @@ -91,7 +91,7 @@ class JsonSerializer { | ||||
| }  // namespace Internals | ||||
|  | ||||
| template <typename TSource, typename TDestination> | ||||
| size_t serializeJson(TSource &source, TDestination &destination) { | ||||
| size_t serializeJson(const TSource &source, TDestination &destination) { | ||||
|   using namespace Internals; | ||||
|   return serialize<JsonSerializer>(source, destination); | ||||
| } | ||||
|   | ||||
| @@ -8,6 +8,7 @@ add_executable(JsonSerializerTests | ||||
| 	JsonObject.cpp | ||||
| 	JsonObjectPretty.cpp | ||||
| 	JsonVariant.cpp | ||||
| 	misc.cpp | ||||
| 	std_stream.cpp | ||||
| 	std_string.cpp | ||||
| ) | ||||
|   | ||||
							
								
								
									
										46
									
								
								test/JsonSerializer/misc.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								test/JsonSerializer/misc.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
| #include <limits> | ||||
|  | ||||
| template <typename T> | ||||
| void check(T value, const std::string &expected) { | ||||
|   DynamicJsonDocument doc; | ||||
|   doc.to<JsonVariant>().set(value); | ||||
|   char buffer[256] = ""; | ||||
|   size_t returnValue = serializeJson(doc, buffer, sizeof(buffer)); | ||||
|   REQUIRE(expected == buffer); | ||||
|   REQUIRE(expected.size() == returnValue); | ||||
| } | ||||
|  | ||||
| TEST_CASE("serializeJson(JsonObjectSubscript)") { | ||||
|   DynamicJsonDocument doc; | ||||
|   deserializeJson(doc, "{\"hello\":42}"); | ||||
|   JsonObject obj = doc.as<JsonObject>(); | ||||
|   std::string result; | ||||
|  | ||||
|   serializeJson(obj["hello"], result); | ||||
|  | ||||
|   REQUIRE(result == "42"); | ||||
| } | ||||
|  | ||||
| TEST_CASE("serializeJson(JsonArraySubscript)") { | ||||
|   DynamicJsonDocument doc; | ||||
|   deserializeJson(doc, "[42]"); | ||||
|   JsonArray arr = doc.as<JsonArray>(); | ||||
|   std::string result; | ||||
|  | ||||
|   serializeJson(arr[0], result); | ||||
|  | ||||
|   REQUIRE(result == "42"); | ||||
| } | ||||
|  | ||||
| TEST_CASE("serializeJson(JsonVariantSubscript)") { | ||||
|   DynamicJsonDocument doc; | ||||
|   deserializeJson(doc, "[42]"); | ||||
|   JsonVariant var = doc.as<JsonVariant>(); | ||||
|   std::string result; | ||||
|  | ||||
|   serializeJson(var[0], result); | ||||
|  | ||||
|   REQUIRE(result == "42"); | ||||
| } | ||||
| @@ -5,6 +5,7 @@ | ||||
| add_executable(MsgPackSerializerTests | ||||
| 	destination_types.cpp | ||||
| 	measure.cpp | ||||
| 	misc.cpp | ||||
| 	serializeArray.cpp | ||||
| 	serializeObject.cpp | ||||
| 	serializeVariant.cpp | ||||
|   | ||||
							
								
								
									
										46
									
								
								test/MsgPackSerializer/misc.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								test/MsgPackSerializer/misc.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
| #include <limits> | ||||
|  | ||||
| template <typename T> | ||||
| void check(T value, const std::string &expected) { | ||||
|   DynamicJsonDocument doc; | ||||
|   doc.to<JsonVariant>().set(value); | ||||
|   char buffer[256] = ""; | ||||
|   size_t returnValue = serializeMsgPack(doc, buffer, sizeof(buffer)); | ||||
|   REQUIRE(expected == buffer); | ||||
|   REQUIRE(expected.size() == returnValue); | ||||
| } | ||||
|  | ||||
| TEST_CASE("serializeMsgPack(JsonObjectSubscript)") { | ||||
|   DynamicJsonDocument doc; | ||||
|   deserializeJson(doc, "{\"hello\":42}"); | ||||
|   JsonObject obj = doc.as<JsonObject>(); | ||||
|   std::string result; | ||||
|  | ||||
|   serializeMsgPack(obj["hello"], result); | ||||
|  | ||||
|   REQUIRE(result == "*"); | ||||
| } | ||||
|  | ||||
| TEST_CASE("serializeMsgPack(JsonArraySubscript)") { | ||||
|   DynamicJsonDocument doc; | ||||
|   deserializeJson(doc, "[42]"); | ||||
|   JsonArray arr = doc.as<JsonArray>(); | ||||
|   std::string result; | ||||
|  | ||||
|   serializeMsgPack(arr[0], result); | ||||
|  | ||||
|   REQUIRE(result == "*"); | ||||
| } | ||||
|  | ||||
| TEST_CASE("serializeMsgPack(JsonVariantSubscript)") { | ||||
|   DynamicJsonDocument doc; | ||||
|   deserializeJson(doc, "[42]"); | ||||
|   JsonVariant var = doc.as<JsonVariant>(); | ||||
|   std::string result; | ||||
|  | ||||
|   serializeMsgPack(var[0], result); | ||||
|  | ||||
|   REQUIRE(result == "*"); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user