[209] | 1 | // Copyright Benoit Blanchon 2014-2016
|
---|
| 2 | // MIT License
|
---|
| 3 | //
|
---|
| 4 | // Arduino JSON library
|
---|
| 5 | // https://github.com/bblanchon/ArduinoJson
|
---|
| 6 | // If you like this project, please add a star!
|
---|
| 7 |
|
---|
| 8 | #pragma once
|
---|
| 9 |
|
---|
| 10 | #include "../Configuration.hpp"
|
---|
| 11 | #include "DummyPrint.hpp"
|
---|
| 12 | #include "IndentedPrint.hpp"
|
---|
| 13 | #include "JsonWriter.hpp"
|
---|
| 14 | #include "Prettyfier.hpp"
|
---|
| 15 | #include "StaticStringBuilder.hpp"
|
---|
| 16 | #include "DynamicStringBuilder.hpp"
|
---|
| 17 |
|
---|
| 18 | #if ARDUINOJSON_ENABLE_STD_STREAM
|
---|
| 19 | #include "StreamPrintAdapter.hpp"
|
---|
| 20 | #endif
|
---|
| 21 |
|
---|
| 22 | namespace ArduinoJson {
|
---|
| 23 | namespace Internals {
|
---|
| 24 |
|
---|
| 25 | // Implements all the overloads of printTo() and prettyPrintTo()
|
---|
| 26 | // Caution: this class use a template parameter to avoid virtual methods.
|
---|
| 27 | // This is a bit curious but allows to reduce the size of JsonVariant, JsonArray
|
---|
| 28 | // and JsonObject.
|
---|
| 29 | template <typename T>
|
---|
| 30 | class JsonPrintable {
|
---|
| 31 | public:
|
---|
| 32 | size_t printTo(Print &print) const {
|
---|
| 33 | JsonWriter writer(print);
|
---|
| 34 | downcast().writeTo(writer);
|
---|
| 35 | return writer.bytesWritten();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | #if ARDUINOJSON_ENABLE_STD_STREAM
|
---|
| 39 | std::ostream &printTo(std::ostream &os) const {
|
---|
| 40 | StreamPrintAdapter adapter(os);
|
---|
| 41 | printTo(adapter);
|
---|
| 42 | return os;
|
---|
| 43 | }
|
---|
| 44 | #endif
|
---|
| 45 |
|
---|
| 46 | size_t printTo(char *buffer, size_t bufferSize) const {
|
---|
| 47 | StaticStringBuilder sb(buffer, bufferSize);
|
---|
| 48 | return printTo(sb);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | size_t printTo(String &str) const {
|
---|
| 52 | DynamicStringBuilder sb(str);
|
---|
| 53 | return printTo(sb);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | size_t prettyPrintTo(IndentedPrint &print) const {
|
---|
| 57 | Prettyfier p(print);
|
---|
| 58 | return printTo(p);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | size_t prettyPrintTo(char *buffer, size_t bufferSize) const {
|
---|
| 62 | StaticStringBuilder sb(buffer, bufferSize);
|
---|
| 63 | return prettyPrintTo(sb);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | size_t prettyPrintTo(Print &print) const {
|
---|
| 67 | IndentedPrint indentedPrint = IndentedPrint(print);
|
---|
| 68 | return prettyPrintTo(indentedPrint);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | size_t prettyPrintTo(String &str) const {
|
---|
| 72 | DynamicStringBuilder sb(str);
|
---|
| 73 | return prettyPrintTo(sb);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | size_t measureLength() const {
|
---|
| 77 | DummyPrint dp;
|
---|
| 78 | return printTo(dp);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | size_t measurePrettyLength() const {
|
---|
| 82 | DummyPrint dp;
|
---|
| 83 | return prettyPrintTo(dp);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private:
|
---|
| 87 | const T &downcast() const { return *static_cast<const T *>(this); }
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | #if ARDUINOJSON_ENABLE_STD_STREAM
|
---|
| 91 | template <typename T>
|
---|
| 92 | inline std::ostream &operator<<(std::ostream &os, const JsonPrintable<T> &v) {
|
---|
| 93 | return v.printTo(os);
|
---|
| 94 | }
|
---|
| 95 | #endif
|
---|
| 96 | }
|
---|
| 97 | }
|
---|