mirror of
https://github.com/pocoproject/poco.git
synced 2025-03-19 11:52:16 +01:00
221 lines
3.8 KiB
C++
221 lines
3.8 KiB
C++
//
|
|
// 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"
|
|
#include <iostream>
|
|
|
|
|
|
namespace Poco {
|
|
namespace JSON {
|
|
|
|
|
|
PrintHandler::PrintHandler(unsigned indent):
|
|
_out(std::cout),
|
|
_indent(indent),
|
|
_array(false),
|
|
_value(false)
|
|
{
|
|
}
|
|
|
|
|
|
PrintHandler::PrintHandler(std::ostream& out, unsigned indent):
|
|
_out(out),
|
|
_indent(indent),
|
|
_array(false),
|
|
_value(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;
|
|
_value = false;
|
|
}
|
|
|
|
|
|
void PrintHandler::endArray()
|
|
{
|
|
_tab.erase(_tab.length() - indent());
|
|
_out << endLine() << _tab << ']';
|
|
_array = false;
|
|
_value = false;
|
|
}
|
|
|
|
|
|
void PrintHandler::key(const std::string& k)
|
|
{
|
|
if (_value)
|
|
{
|
|
comma();
|
|
_value = false;
|
|
}
|
|
_out << _tab << '"' << k << '"';
|
|
if (!printFlat()) _out << ' ';
|
|
_out << ':';
|
|
if (!printFlat()) _out << ' ';
|
|
}
|
|
|
|
|
|
void PrintHandler::null()
|
|
{
|
|
arrayValue();
|
|
_out << "null";
|
|
_value = true;
|
|
}
|
|
|
|
|
|
void PrintHandler::value(int v)
|
|
{
|
|
arrayValue();
|
|
_out << v;
|
|
_value = true;
|
|
}
|
|
|
|
|
|
void PrintHandler::value(unsigned v)
|
|
{
|
|
arrayValue();
|
|
_out << v;
|
|
_value = true;
|
|
}
|
|
|
|
|
|
#if defined(POCO_HAVE_INT64)
|
|
void PrintHandler::value(Int64 v)
|
|
{
|
|
arrayValue();
|
|
_out << v;
|
|
_value = true;
|
|
}
|
|
|
|
|
|
void PrintHandler::value(UInt64 v)
|
|
{
|
|
arrayValue();
|
|
_out << v;
|
|
_value = true;
|
|
}
|
|
#endif
|
|
|
|
|
|
void PrintHandler::value(const std::string& value)
|
|
{
|
|
arrayValue();
|
|
Stringifier::formatString(value, _out);
|
|
_value = true;
|
|
}
|
|
|
|
|
|
|
|
void PrintHandler::value(double d)
|
|
{
|
|
arrayValue();
|
|
_out << d;
|
|
_value = true;
|
|
}
|
|
|
|
|
|
void PrintHandler::value(bool b)
|
|
{
|
|
arrayValue();
|
|
_out << b;
|
|
_value = true;
|
|
}
|
|
|
|
|
|
void PrintHandler::comma()
|
|
{
|
|
_out << ',' << endLine();
|
|
}
|
|
|
|
|
|
void PrintHandler::arrayValue()
|
|
{
|
|
if (_array)
|
|
{
|
|
if (_value) comma();
|
|
_out << _tab;
|
|
}
|
|
}
|
|
} } // namespace Poco::JSON
|