| 1 | // |
| 2 | // PrintHandler.cpp |
| 3 | // |
| 4 | // Library: JSON |
| 5 | // Package: JSON |
| 6 | // Module: PrintHandler |
| 7 | // |
| 8 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/JSON/PrintHandler.h" |
| 16 | #include "Poco/JSON/Stringifier.h" |
| 17 | #include <iostream> |
| 18 | |
| 19 | |
| 20 | namespace Poco { |
| 21 | namespace JSON { |
| 22 | |
| 23 | |
| 24 | PrintHandler::PrintHandler(unsigned indent, int options): |
| 25 | _out(std::cout), |
| 26 | _indent(indent), |
| 27 | _array(0), |
| 28 | _objStart(true), |
| 29 | _options(options) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | |
| 34 | PrintHandler::PrintHandler(std::ostream& out, unsigned indent, int options): |
| 35 | _out(out), |
| 36 | _indent(indent), |
| 37 | _array(0), |
| 38 | _objStart(true), |
| 39 | _options(options) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | |
| 44 | PrintHandler::~PrintHandler() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void PrintHandler::reset() |
| 50 | { |
| 51 | _out.flush(); |
| 52 | _tab = "" ; |
| 53 | _array = 0; |
| 54 | _objStart = true; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | const char* PrintHandler::endLine() const |
| 59 | { |
| 60 | if (!printFlat()) return "\n" ; |
| 61 | else return "" ; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | bool PrintHandler::printFlat() const |
| 66 | { |
| 67 | return _indent == JSON_PRINT_FLAT; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | unsigned PrintHandler::indent() |
| 72 | { |
| 73 | if (!printFlat()) return _indent; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void PrintHandler::startObject() |
| 80 | { |
| 81 | arrayValue(); |
| 82 | _out << '{'; |
| 83 | _out << endLine(); |
| 84 | _tab.append(indent(), ' '); |
| 85 | _objStart = true; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void PrintHandler::endObject() |
| 90 | { |
| 91 | if (_tab.length() >= indent()) |
| 92 | _tab.erase(_tab.length() - indent()); |
| 93 | |
| 94 | _out << endLine() << _tab << '}'; |
| 95 | _objStart = false; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void PrintHandler::startArray() |
| 100 | { |
| 101 | arrayValue(); |
| 102 | _out << '[' << endLine(); |
| 103 | _tab.append(indent(), ' '); |
| 104 | ++_array; |
| 105 | _objStart = true; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void PrintHandler::endArray() |
| 110 | { |
| 111 | _tab.erase(_tab.length() - indent()); |
| 112 | _out << endLine() << _tab << ']'; |
| 113 | --_array; |
| 114 | poco_assert (_array >= 0); |
| 115 | _objStart = false; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void PrintHandler::key(const std::string& k) |
| 120 | { |
| 121 | if (!_objStart) comma(); |
| 122 | |
| 123 | _objStart = true; |
| 124 | |
| 125 | _out << _tab; |
| 126 | Stringifier::formatString(k, _out, _options); |
| 127 | if (!printFlat()) _out << ' '; |
| 128 | _out << ':'; |
| 129 | if (!printFlat()) _out << ' '; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void PrintHandler::null() |
| 134 | { |
| 135 | arrayValue(); |
| 136 | _out << "null" ; |
| 137 | _objStart = false; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void PrintHandler::value(int v) |
| 142 | { |
| 143 | arrayValue(); |
| 144 | _out << v; |
| 145 | _objStart = false; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void PrintHandler::value(unsigned v) |
| 150 | { |
| 151 | arrayValue(); |
| 152 | _out << v; |
| 153 | _objStart = false; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | #if defined(POCO_HAVE_INT64) |
| 158 | void PrintHandler::value(Int64 v) |
| 159 | { |
| 160 | arrayValue(); |
| 161 | _out << v; |
| 162 | _objStart = false; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void PrintHandler::value(UInt64 v) |
| 167 | { |
| 168 | arrayValue(); |
| 169 | _out << v; |
| 170 | _objStart = false; |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | |
| 175 | void PrintHandler::value(const std::string& value) |
| 176 | { |
| 177 | arrayValue(); |
| 178 | Stringifier::formatString(value, _out, _options); |
| 179 | _objStart = false; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | void PrintHandler::value(double d) |
| 184 | { |
| 185 | arrayValue(); |
| 186 | _out << d; |
| 187 | _objStart = false; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void PrintHandler::value(bool b) |
| 192 | { |
| 193 | arrayValue(); |
| 194 | _out << b; |
| 195 | _objStart = false; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | void PrintHandler::comma() |
| 200 | { |
| 201 | _out << ',' << endLine(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void PrintHandler::arrayValue() |
| 206 | { |
| 207 | if (!_objStart) comma(); |
| 208 | if (array()) |
| 209 | { |
| 210 | _out << _tab; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | } } // namespace Poco::JSON |
| 216 | |