From 4f6253aa9fec99260b8bb7b9b2e9003f5259b600 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 17 Sep 2018 10:02:07 +0200 Subject: Import arduinojson and ubjson. Only partially working at the moment --- .../ArduinoJson/Serialization/JsonPrintable.hpp | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 include/lib/ArduinoJson/Serialization/JsonPrintable.hpp (limited to 'include/lib/ArduinoJson/Serialization/JsonPrintable.hpp') diff --git a/include/lib/ArduinoJson/Serialization/JsonPrintable.hpp b/include/lib/ArduinoJson/Serialization/JsonPrintable.hpp new file mode 100644 index 0000000..43d413a --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/JsonPrintable.hpp @@ -0,0 +1,117 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" +#include "../TypeTraits/EnableIf.hpp" +#include "DummyPrint.hpp" +#include "DynamicStringBuilder.hpp" +#include "IndentedPrint.hpp" +#include "JsonSerializer.hpp" +#include "JsonWriter.hpp" +#include "Prettyfier.hpp" +#include "StaticStringBuilder.hpp" + +#if ARDUINOJSON_ENABLE_STD_STREAM +#include "StreamPrintAdapter.hpp" +#endif + +namespace ArduinoJson { +namespace Internals { + +// Implements all the overloads of printTo() and prettyPrintTo() +// Caution: this class use a template parameter to avoid virtual methods. +// This is a bit curious but allows to reduce the size of JsonVariant, JsonArray +// and JsonObject. +template +class JsonPrintable { + public: + template + typename EnableIf::has_append, size_t>::type printTo( + Print &print) const { + JsonWriter writer(print); + JsonSerializer >::serialize(downcast(), writer); + return writer.bytesWritten(); + } + +#if ARDUINOJSON_ENABLE_STD_STREAM + std::ostream &printTo(std::ostream &os) const { + StreamPrintAdapter adapter(os); + printTo(adapter); + return os; + } +#endif + + size_t printTo(char *buffer, size_t bufferSize) const { + StaticStringBuilder sb(buffer, bufferSize); + return printTo(sb); + } + + template + size_t printTo(char (&buffer)[N]) const { + return printTo(buffer, N); + } + + template + typename EnableIf::has_append, size_t>::type printTo( + TString &str) const { + DynamicStringBuilder sb(str); + return printTo(sb); + } + + template + size_t prettyPrintTo(IndentedPrint &print) const { + Prettyfier p(print); + return printTo(p); + } + + size_t prettyPrintTo(char *buffer, size_t bufferSize) const { + StaticStringBuilder sb(buffer, bufferSize); + return prettyPrintTo(sb); + } + + template + size_t prettyPrintTo(char (&buffer)[N]) const { + return prettyPrintTo(buffer, N); + } + + template + typename EnableIf::has_append, size_t>::type + prettyPrintTo(Print &print) const { + IndentedPrint indentedPrint(print); + return prettyPrintTo(indentedPrint); + } + + template + typename EnableIf::has_append, size_t>::type + prettyPrintTo(TString &str) const { + DynamicStringBuilder sb(str); + return prettyPrintTo(sb); + } + + size_t measureLength() const { + DummyPrint dp; + return printTo(dp); + } + + size_t measurePrettyLength() const { + DummyPrint dp; + return prettyPrintTo(dp); + } + + private: + const T &downcast() const { + return *static_cast(this); + } +}; + +#if ARDUINOJSON_ENABLE_STD_STREAM +template +inline std::ostream &operator<<(std::ostream &os, const JsonPrintable &v) { + return v.printTo(os); +} +#endif +} +} -- cgit v1.2.3