poco/JSON/src/PrintHandler.cpp

195 lines
3.5 KiB
C++
Raw Normal View History

//
// PrintHandler.cpp
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: PrintHandler
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/PrintHandler.h"
#include "Poco/JSON/Stringifier.h"
namespace Poco {
namespace JSON {
PrintHandler::PrintHandler(unsigned indent):
_out(std::cout),
_indent(indent),
_array(false)
{
}
PrintHandler::PrintHandler(std::ostream& out, unsigned indent):
_out(out),
_indent(indent),
_array(false)
{
}
PrintHandler::~PrintHandler()
{
}
const char* PrintHandler::endLine() const
{
if (!printFlat()) return "\n";
else return "";
}
bool PrintHandler::printFlat() const
{
return _indent == JSON_PRINT_FLAT;
}
unsigned PrintHandler::indent()
{
if (!printFlat()) return _indent;
return 0;
}
void PrintHandler::startObject()
{
_out << '{';
_out << endLine();
_tab.append(indent(), ' ');
}
void PrintHandler::endObject()
{
if( _tab.length() >= indent())
_tab.erase(_tab.length() - indent());
_out << endLine() << _tab << '}';
}
void PrintHandler::startArray()
{
_out << '[' << endLine();
_tab.append(indent(), ' ');
_array = true;
}
void PrintHandler::endArray()
{
_tab.erase(_tab.length() - indent());
_out << endLine() << _tab << ']';
_array = false;
}
void PrintHandler::key(const std::string& k)
{
_out << _tab << '"' << k << '"';
if (!printFlat()) _out << ' ';
_out << ':';
if (!printFlat()) _out << ' ';
}
void PrintHandler::null()
{
if (_array) _out << _tab;
_out << "null";
}
void PrintHandler::value(int v)
{
if (_array) _out << _tab;
_out << v;
}
void PrintHandler::value(unsigned v)
{
if (_array) _out << _tab;
_out << v;
}
#if defined(POCO_HAVE_INT64)
void PrintHandler::value(Int64 v)
{
if (_array) _out << _tab;
_out << v;
}
void PrintHandler::value(UInt64 v)
{
if (_array) _out << _tab;
_out << v;
}
#endif
void PrintHandler::value(const std::string& value)
{
if (_array) _out << _tab;
Stringifier::formatString(value, _out);
}
void PrintHandler::value(double d)
{
if (_array) _out << _tab;
_out << d;
}
void PrintHandler::value(bool b)
{
if (_array) _out << _tab;
_out << b;
}
void PrintHandler::comma()
{
_out << ',' << endLine();
}
} } // namespace Poco::JSON