2012-04-29 20:09:55 +00:00
|
|
|
//
|
|
|
|
// Object.cpp
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
//
|
|
|
|
// Library: JSON
|
|
|
|
// Package: JSON
|
|
|
|
// Module: Object
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-29 20:09:55 +00:00
|
|
|
//
|
|
|
|
|
2012-10-15 12:27:56 +00:00
|
|
|
|
2012-04-29 20:09:55 +00:00
|
|
|
#include "Poco/JSON/Object.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
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
|
|
|
Object::Object(bool preserveInsertionOrder): _preserveInsOrder(preserveInsertionOrder)
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-24 03:51:40 +00:00
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
Object::Object(const Object& copy) : _values(copy._values),
|
|
|
|
_keys(copy._keys),
|
2013-05-27 21:00:14 -05:00
|
|
|
_preserveInsOrder(copy._preserveInsOrder),
|
|
|
|
_pStruct(0)
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-24 03:51:40 +00:00
|
|
|
|
2012-04-29 20:09:55 +00:00
|
|
|
Object::~Object()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-24 03:51:40 +00:00
|
|
|
Var Object::get(const std::string& key) const
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
|
|
|
ValueMap::const_iterator it = _values.find(key);
|
2013-05-27 21:00:14 -05:00
|
|
|
if (it != _values.end())
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
2013-05-27 21:00:14 -05:00
|
|
|
return it->second;
|
2012-04-29 20:09:55 +00:00
|
|
|
}
|
2013-05-27 21:00:14 -05:00
|
|
|
|
|
|
|
return Var();
|
2012-04-29 20:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Array::Ptr Object::getArray(const std::string& key) const
|
|
|
|
{
|
2013-05-27 21:00:14 -05:00
|
|
|
ValueMap::const_iterator it = _values.find(key);
|
|
|
|
if ((it != _values.end()) && (it->second.type() == typeid(Array::Ptr)))
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
2013-05-27 21:00:14 -05:00
|
|
|
return it->second.extract<Array::Ptr>();
|
2012-04-29 20:09:55 +00:00
|
|
|
}
|
2013-05-27 21:00:14 -05:00
|
|
|
|
|
|
|
return 0;
|
2012-04-29 20:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Object::Ptr Object::getObject(const std::string& key) const
|
|
|
|
{
|
2013-05-27 21:00:14 -05:00
|
|
|
ValueMap::const_iterator it = _values.find(key);
|
|
|
|
if ((it != _values.end()) && (it->second.type() == typeid(Object::Ptr)))
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
2013-05-27 21:00:14 -05:00
|
|
|
return it->second.extract<Object::Ptr>();
|
2012-04-29 20:09:55 +00:00
|
|
|
}
|
2013-05-27 21:00:14 -05:00
|
|
|
|
|
|
|
return 0;
|
2012-04-29 20:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Object::getNames(std::vector<std::string>& names) const
|
|
|
|
{
|
|
|
|
names.clear();
|
|
|
|
for(ValueMap::const_iterator it = _values.begin(); it != _values.end(); ++it)
|
|
|
|
{
|
|
|
|
names.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void Object::stringify(std::ostream& out, unsigned int indent, int step) const
|
2012-04-29 20:09:55 +00:00
|
|
|
{
|
2014-01-23 16:22:38 -05:00
|
|
|
if (step < 0) step = indent;
|
2012-04-29 20:09:55 +00:00
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
if(!_preserveInsOrder)
|
|
|
|
doStringify(_values, out, indent, step);
|
|
|
|
else
|
|
|
|
doStringify(_keys, out, indent, step);
|
|
|
|
}
|
2013-03-06 21:21:40 -06:00
|
|
|
|
2012-04-29 20:09:55 +00:00
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
const std::string& Object::getKey(KeyPtrList::const_iterator& iter) const
|
|
|
|
{
|
|
|
|
ValueMap::const_iterator it = _values.begin();
|
|
|
|
ValueMap::const_iterator end = _values.end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
{
|
2015-06-09 22:41:38 -05:00
|
|
|
if (it->first == **iter) return it->first;
|
2013-03-16 11:33:27 -05:00
|
|
|
}
|
2012-04-29 20:09:55 +00:00
|
|
|
|
2015-06-09 22:41:38 -05:00
|
|
|
throw NotFoundException(**iter);
|
2013-03-16 11:33:27 -05:00
|
|
|
}
|
2012-04-29 20:09:55 +00:00
|
|
|
|
|
|
|
|
2013-03-16 11:33:27 -05:00
|
|
|
void Object::set(const std::string& key, const Dynamic::Var& value)
|
|
|
|
{
|
2015-06-09 22:41:38 -05:00
|
|
|
std::pair<ValueMap::iterator, bool> ret = _values.insert(ValueMap::value_type(key, value));
|
2015-09-12 13:19:28 -05:00
|
|
|
if (!ret.second) ret.first->second = value;
|
2013-03-16 11:33:27 -05:00
|
|
|
if (_preserveInsOrder)
|
|
|
|
{
|
|
|
|
KeyPtrList::iterator it = _keys.begin();
|
|
|
|
KeyPtrList::iterator end = _keys.end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
{
|
|
|
|
if (key == **it) return;
|
|
|
|
}
|
2015-06-09 22:41:38 -05:00
|
|
|
_keys.push_back(&ret.first->first);
|
2012-04-29 20:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 12:27:56 +00:00
|
|
|
|
2013-05-27 21:00:14 -05:00
|
|
|
Poco::DynamicStruct Object::makeStruct(const Object::Ptr& obj)
|
|
|
|
{
|
|
|
|
Poco::DynamicStruct ds;
|
|
|
|
|
|
|
|
ConstIterator it = obj->begin();
|
|
|
|
ConstIterator end = obj->end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
{
|
|
|
|
if (obj->isObject(it))
|
|
|
|
{
|
|
|
|
Object::Ptr pObj = obj->getObject(it->first);
|
|
|
|
DynamicStruct str = makeStruct(pObj);
|
|
|
|
ds.insert(it->first, str);
|
|
|
|
}
|
|
|
|
else if (obj->isArray(it))
|
|
|
|
{
|
|
|
|
Array::Ptr pArr = obj->getArray(it->first);
|
|
|
|
std::vector<Poco::Dynamic::Var> v = Poco::JSON::Array::makeArray(pArr);
|
|
|
|
ds.insert(it->first, v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ds.insert(it->first, it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Object::operator const Poco::DynamicStruct& () const
|
|
|
|
{
|
|
|
|
if (!_pStruct)
|
|
|
|
{
|
|
|
|
ValueMap::const_iterator it = _values.begin();
|
|
|
|
ValueMap::const_iterator end = _values.end();
|
|
|
|
_pStruct = new Poco::DynamicStruct;
|
|
|
|
for (; it != end; ++it)
|
|
|
|
{
|
|
|
|
if (isObject(it))
|
|
|
|
{
|
|
|
|
_pStruct->insert(it->first, makeStruct(getObject(it->first)));
|
|
|
|
}
|
|
|
|
else if (isArray(it))
|
|
|
|
{
|
|
|
|
_pStruct->insert(it->first, Poco::JSON::Array::makeArray(getArray(it->first)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_pStruct->insert(it->first, it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *_pStruct;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 23:15:02 -05:00
|
|
|
void Object::clear()
|
|
|
|
{
|
|
|
|
_values.clear();
|
|
|
|
_keys.clear();
|
|
|
|
_pStruct = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-15 12:27:56 +00:00
|
|
|
} } // namespace Poco::JSON
|