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