| 
									
										
										
										
											2021-03-29 17:14:01 +02:00
										 |  |  | // ArduinoJson - https://arduinojson.org
 | 
					
						
							| 
									
										
										
										
											2024-01-03 08:47:06 +01:00
										 |  |  | // Copyright © 2014-2024, Benoit BLANCHON
 | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  | // MIT License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <ArduinoJson.h>
 | 
					
						
							|  |  |  | #include <catch.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  | #include "Literals.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  | TEST_CASE("serialize JsonArray to std::string") { | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc; | 
					
						
							| 
									
										
										
										
											2018-07-02 09:35:21 +02:00
										 |  |  |   JsonArray array = doc.to<JsonArray>(); | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |   array.add(4); | 
					
						
							|  |  |  |   array.add(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("serializeJson()") { | 
					
						
							| 
									
										
										
										
											2023-07-31 18:37:35 +02:00
										 |  |  |     std::string json = "erase me"; | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |     serializeJson(array, json); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |     REQUIRE("[4,2]" == json); | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("serializeJsonPretty") { | 
					
						
							| 
									
										
										
										
											2023-07-31 18:37:35 +02:00
										 |  |  |     std::string json = "erase me"; | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |     serializeJsonPretty(array, json); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |     REQUIRE("[\r\n  4,\r\n  2\r\n]" == json); | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("serialize JsonObject to std::string") { | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc; | 
					
						
							| 
									
										
										
										
											2018-07-02 09:35:21 +02:00
										 |  |  |   JsonObject obj = doc.to<JsonObject>(); | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |   obj["key"] = "value"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("object") { | 
					
						
							| 
									
										
										
										
											2023-07-31 18:37:35 +02:00
										 |  |  |     std::string json = "erase me"; | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |     serializeJson(doc, json); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |     REQUIRE("{\"key\":\"value\"}" == json); | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("serializeJsonPretty") { | 
					
						
							| 
									
										
										
										
											2023-07-31 18:37:35 +02:00
										 |  |  |     std::string json = "erase me"; | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |     serializeJsonPretty(doc, json); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |     REQUIRE("{\r\n  \"key\": \"value\"\r\n}" == json); | 
					
						
							| 
									
										
										
										
											2018-05-14 17:12:59 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-21 15:07:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("serialize an std::string containing a NUL") { | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc; | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |   doc.set("hello\0world"_s); | 
					
						
							| 
									
										
										
										
											2021-11-21 15:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-31 18:37:35 +02:00
										 |  |  |   std::string json = "erase me"; | 
					
						
							| 
									
										
										
										
											2021-11-21 15:07:56 +01:00
										 |  |  |   serializeJson(doc, json); | 
					
						
							|  |  |  |   CHECK("\"hello\\u0000world\"" == json); | 
					
						
							|  |  |  | } |