JsonPrintable.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright Benoit Blanchon 2014-2017
  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. #pragma once
  8. #include "../Configuration.hpp"
  9. #include "../TypeTraits/EnableIf.hpp"
  10. #include "DummyPrint.hpp"
  11. #include "DynamicStringBuilder.hpp"
  12. #include "IndentedPrint.hpp"
  13. #include "JsonSerializer.hpp"
  14. #include "JsonWriter.hpp"
  15. #include "Prettyfier.hpp"
  16. #include "StaticStringBuilder.hpp"
  17. #if ARDUINOJSON_ENABLE_STD_STREAM
  18. #include "StreamPrintAdapter.hpp"
  19. #endif
  20. namespace ArduinoJson {
  21. namespace Internals {
  22. // Implements all the overloads of printTo() and prettyPrintTo()
  23. // Caution: this class use a template parameter to avoid virtual methods.
  24. // This is a bit curious but allows to reduce the size of JsonVariant, JsonArray
  25. // and JsonObject.
  26. template <typename T>
  27. class JsonPrintable {
  28. public:
  29. size_t printTo(Print &print) const {
  30. JsonWriter writer(print);
  31. JsonSerializer::serialize(downcast(), writer);
  32. return writer.bytesWritten();
  33. }
  34. #if ARDUINOJSON_ENABLE_STD_STREAM
  35. std::ostream &printTo(std::ostream &os) const {
  36. StreamPrintAdapter adapter(os);
  37. printTo(adapter);
  38. return os;
  39. }
  40. #endif
  41. size_t printTo(char *buffer, size_t bufferSize) const {
  42. StaticStringBuilder sb(buffer, bufferSize);
  43. return printTo(sb);
  44. }
  45. template <size_t N>
  46. size_t printTo(char (&buffer)[N]) const {
  47. return printTo(buffer, N);
  48. }
  49. template <typename TString>
  50. typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
  51. printTo(TString &str) const {
  52. DynamicStringBuilder<TString> sb(str);
  53. return printTo(sb);
  54. }
  55. size_t prettyPrintTo(IndentedPrint &print) const {
  56. Prettyfier p(print);
  57. return printTo(p);
  58. }
  59. size_t prettyPrintTo(char *buffer, size_t bufferSize) const {
  60. StaticStringBuilder sb(buffer, bufferSize);
  61. return prettyPrintTo(sb);
  62. }
  63. template <size_t N>
  64. size_t prettyPrintTo(char (&buffer)[N]) const {
  65. return prettyPrintTo(buffer, N);
  66. }
  67. size_t prettyPrintTo(Print &print) const {
  68. IndentedPrint indentedPrint = IndentedPrint(print);
  69. return prettyPrintTo(indentedPrint);
  70. }
  71. template <typename TString>
  72. typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
  73. prettyPrintTo(TString &str) const {
  74. DynamicStringBuilder<TString> sb(str);
  75. return prettyPrintTo(sb);
  76. }
  77. size_t measureLength() const {
  78. DummyPrint dp;
  79. return printTo(dp);
  80. }
  81. size_t measurePrettyLength() const {
  82. DummyPrint dp;
  83. return prettyPrintTo(dp);
  84. }
  85. private:
  86. const T &downcast() const {
  87. 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. }