| 
									
										
										
										
											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-29 08:31:17 +02:00
										 |  |  | // MIT License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <ArduinoJson.h>
 | 
					
						
							|  |  |  | #include <catch.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  | #include "Literals.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 17:56:16 +02:00
										 |  |  | template <typename T> | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  | static void checkVariant(T value, const char* expected_data, | 
					
						
							|  |  |  |                          size_t expected_len) { | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc; | 
					
						
							| 
									
										
										
										
											2018-08-21 17:56:16 +02:00
										 |  |  |   JsonVariant variant = doc.to<JsonVariant>(); | 
					
						
							|  |  |  |   variant.set(value); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   std::string expected(expected_data, expected_data + expected_len); | 
					
						
							|  |  |  |   std::string actual; | 
					
						
							|  |  |  |   size_t len = serializeMsgPack(variant, actual); | 
					
						
							|  |  |  |   CAPTURE(variant); | 
					
						
							|  |  |  |   REQUIRE(len == expected_len); | 
					
						
							|  |  |  |   REQUIRE(actual == expected); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 17:56:16 +02:00
										 |  |  | template <typename T, size_t N> | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  | static void checkVariant(T value, const char (&expected_data)[N]) { | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   const size_t expected_len = N - 1; | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |   checkVariant(value, expected_data, expected_len); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 17:56:16 +02:00
										 |  |  | template <typename T> | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  | static void checkVariant(T value, const std::string& expected) { | 
					
						
							|  |  |  |   checkVariant(value, expected.data(), expected.length()); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_CASE("serialize MsgPack value") { | 
					
						
							| 
									
										
										
										
											2021-12-16 14:55:26 +01:00
										 |  |  |   SECTION("unbound") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(JsonVariant(), "\xC0");  // we represent undefined as nil
 | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("nil") { | 
					
						
							|  |  |  |     const char* nil = 0;  // ArduinoJson uses a string for null
 | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(nil, "\xC0"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("bool") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(false, "\xC2"); | 
					
						
							|  |  |  |     checkVariant(true, "\xC3"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("positive fixint") { | 
					
						
							| 
									
										
										
										
											2021-04-14 11:42:53 +02:00
										 |  |  |     SECTION("signed") { | 
					
						
							|  |  |  |       checkVariant(0, "\x00"); | 
					
						
							|  |  |  |       checkVariant(127, "\x7F"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     SECTION("unsigned") { | 
					
						
							|  |  |  |       checkVariant(0U, "\x00"); | 
					
						
							|  |  |  |       checkVariant(127U, "\x7F"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint 8") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(128, "\xCC\x80"); | 
					
						
							|  |  |  |     checkVariant(255, "\xCC\xFF"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint 16") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(256, "\xCD\x01\x00"); | 
					
						
							|  |  |  |     checkVariant(0xFFFF, "\xCD\xFF\xFF"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("uint 32") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(0x00010000U, "\xCE\x00\x01\x00\x00"); | 
					
						
							|  |  |  |     checkVariant(0x12345678U, "\xCE\x12\x34\x56\x78"); | 
					
						
							|  |  |  |     checkVariant(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-02 16:54:05 +02:00
										 |  |  | #if ARDUINOJSON_USE_LONG_LONG
 | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   SECTION("uint 64") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00"); | 
					
						
							|  |  |  |     checkVariant(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0"); | 
					
						
							|  |  |  |     checkVariant(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("negative fixint") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(-1, "\xFF"); | 
					
						
							|  |  |  |     checkVariant(-32, "\xE0"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int 8") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(-33, "\xD0\xDF"); | 
					
						
							|  |  |  |     checkVariant(-128, "\xD0\x80"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int 16") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(-129, "\xD1\xFF\x7F"); | 
					
						
							|  |  |  |     checkVariant(-32768, "\xD1\x80\x00"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("int 32") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(-32769, "\xD2\xFF\xFF\x7F\xFF"); | 
					
						
							|  |  |  |     checkVariant(-2147483647 - 1, "\xD2\x80\x00\x00\x00"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-02 16:54:05 +02:00
										 |  |  | #if ARDUINOJSON_USE_LONG_LONG
 | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   SECTION("int 64") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(int64_t(0xFEDCBA9876543210), | 
					
						
							|  |  |  |                  "\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("float 32") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(1.25, "\xCA\x3F\xA0\x00\x00"); | 
					
						
							| 
									
										
										
										
											2022-04-07 20:58:54 +02:00
										 |  |  |     checkVariant(9.22337204e+18f, "\xca\x5f\x00\x00\x00"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("float 64") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("fixstr") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant("", "\xA0"); | 
					
						
							|  |  |  |     checkVariant("hello world hello world hello !", | 
					
						
							|  |  |  |                  "\xBFhello world hello world hello !"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("str 8") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant("hello world hello world hello !!", | 
					
						
							|  |  |  |                  "\xD9\x20hello world hello world hello !!"); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("str 16") { | 
					
						
							|  |  |  |     std::string shortest(256, '?'); | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |     checkVariant(shortest.c_str(), "\xDA\x01\x00"_s + shortest); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     std::string longest(65535, '?'); | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |     checkVariant(longest.c_str(), "\xDA\xFF\xFF"_s + longest); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("str 32") { | 
					
						
							|  |  |  |     std::string shortest(65536, '?'); | 
					
						
							| 
									
										
										
										
											2024-11-26 10:10:52 +01:00
										 |  |  |     checkVariant(JsonString(shortest.c_str(), | 
					
						
							|  |  |  |                             JsonString::Linked),  // force store by pointer
 | 
					
						
							| 
									
										
										
										
											2024-11-25 11:32:03 +01:00
										 |  |  |                  "\xDB\x00\x01\x00\x00"_s + shortest); | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-12 09:08:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   SECTION("serialized(const char*)") { | 
					
						
							| 
									
										
										
										
											2019-12-24 16:41:00 +01:00
										 |  |  |     checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF"); | 
					
						
							|  |  |  |     checkVariant(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00"); | 
					
						
							| 
									
										
										
										
											2018-07-12 09:08:20 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-28 18:00:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 14:47:40 +02:00
										 |  |  |   SECTION("bin 8") { | 
					
						
							| 
									
										
										
										
											2024-06-06 18:33:48 +02:00
										 |  |  |     checkVariant(MsgPackBinary("?", 1), "\xC4\x01?"); | 
					
						
							| 
									
										
										
										
											2024-04-29 14:47:40 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("bin 16") { | 
					
						
							| 
									
										
										
										
											2024-06-06 18:33:48 +02:00
										 |  |  |     auto str = std::string(256, '?'); | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |     checkVariant(MsgPackBinary(str.data(), str.size()), "\xC5\x01\x00"_s + str); | 
					
						
							| 
									
										
										
										
											2024-04-29 14:47:40 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-06 18:33:48 +02:00
										 |  |  |   // bin 32 is tested in string_length_size_4.cpp
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("fixext 1") { | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(1, "\x02", 1), "\xD4\x01\x02"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("fixext 2") { | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(1, "\x03\x04", 2), "\xD5\x01\x03\x04"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("fixext 4") { | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(1, "\x05\x06\x07\x08", 4), | 
					
						
							|  |  |  |                  "\xD6\x01\x05\x06\x07\x08"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("fixext 8") { | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(1, "????????", 8), "\xD7\x01????????"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("fixext 16") { | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(1, "????????????????", 16), | 
					
						
							|  |  |  |                  "\xD8\x01????????????????"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("ext 8") { | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(2, "???", 3), "\xC7\x03\x02???"); | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(2, "?????", 5), "\xC7\x05\x02?????"); | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(2, "???????", 7), "\xC7\x07\x02???????"); | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(2, "?????????", 9), "\xC7\x09\x02?????????"); | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(2, "???????????????", 15), | 
					
						
							|  |  |  |                  "\xC7\x0F\x02???????????????"); | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(2, "?????????????????", 17), | 
					
						
							|  |  |  |                  "\xC7\x11\x02?????????????????"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SECTION("ext 16") { | 
					
						
							|  |  |  |     auto str = std::string(256, '?'); | 
					
						
							|  |  |  |     checkVariant(MsgPackExtension(2, str.data(), str.size()), | 
					
						
							| 
									
										
										
										
											2024-06-07 09:35:45 +02:00
										 |  |  |                  "\xC8\x01\x00\x02"_s + str); | 
					
						
							| 
									
										
										
										
											2024-06-06 18:33:48 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 18:00:43 +01:00
										 |  |  |   SECTION("serialize round double as integer") {  // Issue #1718
 | 
					
						
							|  |  |  |     checkVariant(-32768.0, "\xD1\x80\x00"); | 
					
						
							|  |  |  |     checkVariant(-129.0, "\xD1\xFF\x7F"); | 
					
						
							|  |  |  |     checkVariant(-128.0, "\xD0\x80"); | 
					
						
							|  |  |  |     checkVariant(-33.0, "\xD0\xDF"); | 
					
						
							|  |  |  |     checkVariant(-32.0, "\xE0"); | 
					
						
							|  |  |  |     checkVariant(-1.0, "\xFF"); | 
					
						
							|  |  |  |     checkVariant(0.0, "\x00"); | 
					
						
							|  |  |  |     checkVariant(127.0, "\x7F"); | 
					
						
							|  |  |  |     checkVariant(128.0, "\xCC\x80"); | 
					
						
							|  |  |  |     checkVariant(255.0, "\xCC\xFF"); | 
					
						
							|  |  |  |     checkVariant(256.0, "\xCD\x01\x00"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-29 08:31:17 +02:00
										 |  |  | } |