Add operator<<(std::ostream&, const JsonString&)

This commit is contained in:
Benoit Blanchon
2021-11-21 15:07:56 +01:00
parent be70f6ddd7
commit 51937778dd
2 changed files with 19 additions and 0 deletions

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include <sstream>
TEST_CASE("JsonString") {
SECTION("Default constructor creates a null JsonString") {
JsonString s;
@ -74,4 +76,10 @@ TEST_CASE("JsonString") {
CHECK(a == b);
CHECK_FALSE(a != b);
}
SECTION("std::stream") {
std::stringstream ss;
ss << JsonString("hello world!");
CHECK(ss.str() == "hello world!");
}
}

View File

@ -6,6 +6,10 @@
#include <ArduinoJson/Misc/SafeBoolIdiom.hpp>
#if ARDUINOJSON_ENABLE_STD_STREAM
# include <ostream>
#endif
namespace ARDUINOJSON_NAMESPACE {
class String : public SafeBoolIdom<String> {
@ -61,6 +65,13 @@ class String : public SafeBoolIdom<String> {
return strcmp(lhs._data, rhs._data) != 0;
}
#if ARDUINOJSON_ENABLE_STD_STREAM
friend std::ostream& operator<<(std::ostream& lhs, const String& rhs) {
lhs.write(rhs.c_str(), static_cast<std::streamsize>(rhs.size()));
return lhs;
}
#endif
private:
const char* _data;
size_t _size;