Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for custom writer classes (issue #1088)
- Loading branch information
Showing
10 changed files
with
259 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// ArduinoJson - arduinojson.org | ||
// Copyright Benoit Blanchon 2014-2019 | ||
// MIT License | ||
|
||
#include <ArduinoJson.h> | ||
#include <catch.hpp> | ||
|
||
struct CustomWriter { | ||
std::string str; | ||
|
||
size_t write(uint8_t c) { | ||
str.append(1, static_cast<char>(c)); | ||
return 1; | ||
} | ||
|
||
size_t write(const uint8_t *s, size_t n) { | ||
str.append(reinterpret_cast<const char *>(s), n); | ||
return n; | ||
} | ||
}; | ||
|
||
TEST_CASE("CustomWriter") { | ||
DynamicJsonDocument doc(4096); | ||
JsonArray array = doc.to<JsonArray>(); | ||
array.add(4); | ||
array.add(2); | ||
|
||
SECTION("serializeJson()") { | ||
CustomWriter writer; | ||
serializeJson(array, writer); | ||
|
||
REQUIRE("[4,2]" == writer.str); | ||
} | ||
|
||
SECTION("serializeJsonPretty") { | ||
CustomWriter writer; | ||
serializeJsonPretty(array, writer); | ||
|
||
REQUIRE("[\r\n 4,\r\n 2\r\n]" == writer.str); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.