mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Fixed value returned by serializeXxx() when writing to a String
This commit is contained in:
		| @@ -14,6 +14,7 @@ HEAD | |||||||
| * Added `measureJson`, `measureJsonPretty`, and `measureMsgPack` to `keywords.txt` | * Added `measureJson`, `measureJsonPretty`, and `measureMsgPack` to `keywords.txt` | ||||||
|   (This file is used for syntax highlighting in the Arduino IDE)  |   (This file is used for syntax highlighting in the Arduino IDE)  | ||||||
| * Fixed `variant.is<nullptr_t>()` | * Fixed `variant.is<nullptr_t>()` | ||||||
|  | * Fixed value returned by `serializeJson()`, `serializeJsonPretty()`, and `serializeMsgPack()` when writing to a `String` | ||||||
|  |  | ||||||
| > ### BREAKING CHANGES | > ### BREAKING CHANGES | ||||||
| >  | >  | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								extras/tests/Helpers/WString.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								extras/tests/Helpers/WString.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | |||||||
|  | // ArduinoJson - arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2020 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | // Reproduces Arduino's String class | ||||||
|  | class String { | ||||||
|  |  public: | ||||||
|  |   String& operator+=(char c) { | ||||||
|  |     _str += c; | ||||||
|  |     return *this; | ||||||
|  |   } | ||||||
|  |   String& operator+=(int);  // no used, just to add ambiguity | ||||||
|  |  | ||||||
|  |   unsigned char reserve(size_t capacity) { | ||||||
|  |     _str.reserve(capacity); | ||||||
|  |     return 1; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   size_t length() const { | ||||||
|  |     return _str.size(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   const char* c_str() const { | ||||||
|  |     return _str.c_str(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |  private: | ||||||
|  |   std::string _str; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | class StringSumHelper; | ||||||
|  |  | ||||||
|  | bool operator==(const std::string& lhs, const ::String& rhs) { | ||||||
|  |   return lhs == rhs.c_str(); | ||||||
|  | } | ||||||
| @@ -2,6 +2,7 @@ | |||||||
| // Copyright Benoit Blanchon 2014-2020 | // Copyright Benoit Blanchon 2014-2020 | ||||||
| // MIT License | // MIT License | ||||||
|  |  | ||||||
|  | #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 | ||||||
| #include <ArduinoJson.h> | #include <ArduinoJson.h> | ||||||
| #include <catch.hpp> | #include <catch.hpp> | ||||||
| #include "custom_string.hpp" | #include "custom_string.hpp" | ||||||
| @@ -55,6 +56,12 @@ TEST_CASE("Writer<std::string>") { | |||||||
|   common_tests(sb, output); |   common_tests(sb, output); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | TEST_CASE("Writer<String>") { | ||||||
|  |   ::String output; | ||||||
|  |   Writer< ::String> sb(output); | ||||||
|  |   common_tests(sb, output); | ||||||
|  | } | ||||||
|  |  | ||||||
| TEST_CASE("Writer<custom_string>") { | TEST_CASE("Writer<custom_string>") { | ||||||
|   custom_string output; |   custom_string output; | ||||||
|   Writer<custom_string> sb(output); |   Writer<custom_string> sb(output); | ||||||
|   | |||||||
| @@ -22,9 +22,8 @@ class Writer< ::String, void> { | |||||||
|     // CAUTION: Arduino String doesn't have append() |     // CAUTION: Arduino String doesn't have append() | ||||||
|     // and old version doesn't have size() either |     // and old version doesn't have size() either | ||||||
|     _str->reserve(_str->length() + n); |     _str->reserve(_str->length() + n); | ||||||
|     while (n > 0) { |     for (size_t i = 0; i < n; i++) { | ||||||
|       _str->operator+=(static_cast<char>(*s++)); |       _str->operator+=(static_cast<char>(*s++)); | ||||||
|       n--; |  | ||||||
|     } |     } | ||||||
|     return n; |     return n; | ||||||
|   } |   } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user