PrintHandler.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // PrintHandler.h
  3. //
  4. // Library: JSON
  5. // Package: JSON
  6. // Module: PrintHandler
  7. //
  8. // Definition of the PrintHandler class.
  9. //
  10. // Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef JSON_PrintHandler_INCLUDED
  16. #define JSON_PrintHandler_INCLUDED
  17. #include "Poco/JSON/JSON.h"
  18. #include "Poco/JSON/Handler.h"
  19. #include "Poco/JSONString.h"
  20. namespace Poco {
  21. namespace JSON {
  22. class JSON_API PrintHandler: public Handler
  23. /// PrintHandler formats and prints the JSON object
  24. /// to either user-provided std::ostream or standard output.
  25. /// If indent is zero, the output is a condensed JSON string,
  26. /// otherwise, the proper indentation is applied to elements.
  27. {
  28. public:
  29. typedef SharedPtr<PrintHandler> Ptr;
  30. static const unsigned JSON_PRINT_FLAT = 0;
  31. PrintHandler(unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
  32. /// Creates the PrintHandler.
  33. PrintHandler(std::ostream& out, unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
  34. /// Creates the PrintHandler.
  35. ~PrintHandler();
  36. /// Destroys the PrintHandler.
  37. void reset();
  38. /// Resets the handler state.
  39. void startObject();
  40. /// The parser has read a '{'; a new object is started.
  41. /// If indent is greater than zero, a newline will be appended.
  42. void endObject();
  43. /// The parser has read a '}'; the object is closed.
  44. void startArray();
  45. /// The parser has read a [; a new array will be started.
  46. /// If indent is greater than zero, a newline will be appended.
  47. void endArray();
  48. /// The parser has read a ]; the array is closed.
  49. void key(const std::string& k);
  50. /// A key of an object is read; it will be written to the output,
  51. /// followed by a ':'. If indent is greater than zero, the colon
  52. /// is padded by a space before and after.
  53. void null();
  54. /// A null value is read; "null" will be written to the output.
  55. void value(int v);
  56. /// An integer value is read.
  57. void value(unsigned v);
  58. /// An unsigned value is read. This will only be triggered if the
  59. /// value cannot fit into a signed int.
  60. #if defined(POCO_HAVE_INT64)
  61. void value(Int64 v);
  62. /// A 64-bit integer value is read; it will be written to the output.
  63. void value(UInt64 v);
  64. /// An unsigned 64-bit integer value is read; it will be written to the output.
  65. #endif
  66. void value(const std::string& value);
  67. /// A string value is read; it will be formatted and written to the output.
  68. void value(double d);
  69. /// A double value is read; it will be written to the output.
  70. void value(bool b);
  71. /// A boolean value is read; it will be written to the output.
  72. void comma();
  73. /// A comma is read; it will be written to the output as "true" or "false".
  74. void setIndent(unsigned indent);
  75. /// Sets indentation.
  76. private:
  77. const char* endLine() const;
  78. unsigned indent();
  79. bool printFlat() const;
  80. void arrayValue();
  81. bool array() const;
  82. std::ostream& _out;
  83. unsigned _indent;
  84. std::string _tab;
  85. int _array;
  86. bool _objStart;
  87. int _options;
  88. };
  89. //
  90. // inlines
  91. //
  92. inline void PrintHandler::setIndent(unsigned indent)
  93. {
  94. _indent = indent;
  95. }
  96. inline bool PrintHandler::array() const
  97. {
  98. return _array > 0;
  99. }
  100. } } // namespace Poco::JSON
  101. #endif // JSON_PrintHandler_INCLUDED