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
20namespace Poco {
21namespace JSON {
22
23
24PrintHandler::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
34PrintHandler::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
44PrintHandler::~PrintHandler()
45{
46}
47
48
49void PrintHandler::reset()
50{
51 _out.flush();
52 _tab = "";
53 _array = 0;
54 _objStart = true;
55}
56
57
58const char* PrintHandler::endLine() const
59{
60 if (!printFlat()) return "\n";
61 else return "";
62}
63
64
65bool PrintHandler::printFlat() const
66{
67 return _indent == JSON_PRINT_FLAT;
68}
69
70
71unsigned PrintHandler::indent()
72{
73 if (!printFlat()) return _indent;
74
75 return 0;
76}
77
78
79void PrintHandler::startObject()
80{
81 arrayValue();
82 _out << '{';
83 _out << endLine();
84 _tab.append(indent(), ' ');
85 _objStart = true;
86}
87
88
89void 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
99void PrintHandler::startArray()
100{
101 arrayValue();
102 _out << '[' << endLine();
103 _tab.append(indent(), ' ');
104 ++_array;
105 _objStart = true;
106}
107
108
109void 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
119void 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
133void PrintHandler::null()
134{
135 arrayValue();
136 _out << "null";
137 _objStart = false;
138}
139
140
141void PrintHandler::value(int v)
142{
143 arrayValue();
144 _out << v;
145 _objStart = false;
146}
147
148
149void PrintHandler::value(unsigned v)
150{
151 arrayValue();
152 _out << v;
153 _objStart = false;
154}
155
156
157#if defined(POCO_HAVE_INT64)
158void PrintHandler::value(Int64 v)
159{
160 arrayValue();
161 _out << v;
162 _objStart = false;
163}
164
165
166void PrintHandler::value(UInt64 v)
167{
168 arrayValue();
169 _out << v;
170 _objStart = false;
171}
172#endif
173
174
175void PrintHandler::value(const std::string& value)
176{
177 arrayValue();
178 Stringifier::formatString(value, _out, _options);
179 _objStart = false;
180}
181
182
183void PrintHandler::value(double d)
184{
185 arrayValue();
186 _out << d;
187 _objStart = false;
188}
189
190
191void PrintHandler::value(bool b)
192{
193 arrayValue();
194 _out << b;
195 _objStart = false;
196}
197
198
199void PrintHandler::comma()
200{
201 _out << ',' << endLine();
202}
203
204
205void PrintHandler::arrayValue()
206{
207 if (!_objStart) comma();
208 if (array())
209 {
210 _out << _tab;
211 }
212}
213
214
215} } // namespace Poco::JSON
216