2012-04-29 20:09:55 +00:00
|
|
|
//
|
2013-03-16 11:33:27 -05:00
|
|
|
// ParseHandler.cpp
|
2012-04-29 20:09:55 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
//
|
|
|
|
// Library: JSON
|
|
|
|
// Package: JSON
|
2013-03-16 11:33:27 -05:00
|
|
|
// Module: ParseHandler
|
2012-04-29 20:09:55 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2012-10-15 12:27:56 +00:00
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
#include "Poco/JSON/ParseHandler.h"
|
2012-04-29 20:09:55 +00:00
|
|
|
#include "Poco/JSON/Object.h"
|
|
|
|
|
2012-09-24 03:51:40 +00:00
|
|
|
|
|
|
|
using Poco::Dynamic::Var;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
namespace JSON {
|
|
|
|
|
2012-04-29 20:09:55 +00:00
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
ParseHandler::ParseHandler(bool preserveObjectOrder) : Handler(),
|
|
|
|
_preserveObjectOrder(preserveObjectOrder)
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-24 03:51:40 +00:00
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
ParseHandler::~ParseHandler()
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-27 21:00:14 -05:00
|
|
|
void ParseHandler::reset()
|
|
|
|
{
|
|
|
|
while (!_stack.empty()) _stack.pop();
|
|
|
|
_key = "";
|
|
|
|
_result.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void ParseHandler::startObject()
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
2013-03-16 11:33:27 -05:00
|
|
|
Object::Ptr newObj = new Object(_preserveObjectOrder);
|
2012-04-29 20:09:55 +00:00
|
|
|
|
|
|
|
if ( _stack.empty() ) // The first object
|
|
|
|
{
|
|
|
|
_result = newObj;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-24 03:51:40 +00:00
|
|
|
Var parent = _stack.top();
|
2012-04-29 20:09:55 +00:00
|
|
|
|
|
|
|
if ( parent.type() == typeid(Array::Ptr) )
|
|
|
|
{
|
|
|
|
Array::Ptr arr = parent.extract<Array::Ptr>();
|
|
|
|
arr->add(newObj);
|
|
|
|
}
|
|
|
|
else if ( parent.type() == typeid(Object::Ptr) )
|
|
|
|
{
|
|
|
|
poco_assert_dbg(!_key.empty());
|
|
|
|
Object::Ptr obj = parent.extract<Object::Ptr>();
|
|
|
|
obj->set(_key, newObj);
|
|
|
|
_key.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_stack.push(newObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void ParseHandler::endObject()
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
_stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void ParseHandler::startArray()
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
Array::Ptr newArr = new Array();
|
|
|
|
|
|
|
|
if ( _stack.empty() ) // The first array
|
|
|
|
{
|
|
|
|
_result = newArr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-24 03:51:40 +00:00
|
|
|
Var parent = _stack.top();
|
2012-04-29 20:09:55 +00:00
|
|
|
|
|
|
|
if ( parent.type() == typeid(Array::Ptr) )
|
|
|
|
{
|
|
|
|
Array::Ptr arr = parent.extract<Array::Ptr>();
|
|
|
|
arr->add(newArr);
|
|
|
|
}
|
|
|
|
else if ( parent.type() == typeid(Object::Ptr) )
|
|
|
|
{
|
|
|
|
poco_assert_dbg(!_key.empty());
|
|
|
|
Object::Ptr obj = parent.extract<Object::Ptr>();
|
|
|
|
obj->set(_key, newArr);
|
|
|
|
_key.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_stack.push(newArr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void ParseHandler::endArray()
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
_stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void ParseHandler::key(const std::string& k)
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
_key = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void ParseHandler::setValue(const Var& value)
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
2012-09-24 03:51:40 +00:00
|
|
|
Var parent = _stack.top();
|
2012-04-29 20:09:55 +00:00
|
|
|
|
|
|
|
if ( parent.type() == typeid(Array::Ptr) )
|
|
|
|
{
|
|
|
|
Array::Ptr arr = parent.extract<Array::Ptr>();
|
|
|
|
arr->add(value);
|
|
|
|
}
|
|
|
|
else if ( parent.type() == typeid(Object::Ptr) )
|
|
|
|
{
|
|
|
|
poco_assert_dbg(!_key.empty());
|
|
|
|
Object::Ptr obj = parent.extract<Object::Ptr>();
|
|
|
|
obj->set(_key, value);
|
|
|
|
_key.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-15 12:27:56 +00:00
|
|
|
} } // namespace Poco::JSON
|