CustomWriter.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. class CustomWriter {
  7. public:
  8. CustomWriter() {}
  9. size_t write(uint8_t c) {
  10. _str.append(1, static_cast<char>(c));
  11. return 1;
  12. }
  13. size_t write(const uint8_t* s, size_t n) {
  14. _str.append(reinterpret_cast<const char*>(s), n);
  15. return n;
  16. }
  17. const std::string& str() const {
  18. return _str;
  19. }
  20. private:
  21. CustomWriter(const CustomWriter&); // non-copiable
  22. CustomWriter& operator=(const CustomWriter&);
  23. std::string _str;
  24. };
  25. TEST_CASE("CustomWriter") {
  26. DynamicJsonDocument doc(4096);
  27. JsonArray array = doc.to<JsonArray>();
  28. array.add(4);
  29. array.add(2);
  30. SECTION("serializeJson()") {
  31. CustomWriter writer;
  32. serializeJson(array, writer);
  33. REQUIRE("[4,2]" == writer.str());
  34. }
  35. SECTION("serializeJsonPretty") {
  36. CustomWriter writer;
  37. serializeJsonPretty(array, writer);
  38. REQUIRE("[\r\n 4,\r\n 2\r\n]" == writer.str());
  39. }
  40. }