From 8b7c37a8377e0c27066858dc264997d37bf92f97 Mon Sep 17 00:00:00 2001 From: Franky Braem Date: Thu, 3 May 2012 20:14:38 +0000 Subject: [PATCH] Add JSONConfiguration --- Util/include/Poco/Util/JSONConfiguration.h | 155 ++++++++++ Util/src/JSONConfiguration.cpp | 340 +++++++++++++++++++++ 2 files changed, 495 insertions(+) create mode 100644 Util/include/Poco/Util/JSONConfiguration.h create mode 100644 Util/src/JSONConfiguration.cpp diff --git a/Util/include/Poco/Util/JSONConfiguration.h b/Util/include/Poco/Util/JSONConfiguration.h new file mode 100644 index 000000000..51f611953 --- /dev/null +++ b/Util/include/Poco/Util/JSONConfiguration.h @@ -0,0 +1,155 @@ +// +// JSONConfiguration.h +// +// $Id$ +// +// Library: Util +// Package: Util +// Module: JSONConfiguration +// +// Definition of the JSONConfiguration class. +// +// 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. +// + + +#ifndef Util_JSONConfiguration_INCLUDED +#define Util_JSONConfiguration_INCLUDED + + +#include + +#include "Poco/Util/AbstractConfiguration.h" +#include "Poco/JSON/Object.h" + +namespace Poco { +namespace Util { + +class Util_API JSONConfiguration : public AbstractConfiguration + /// This configuration class extracts configuration properties + /// from a JSON object. An XPath-like syntax for property + /// names is supported to allow full access to the JSON object. + /// + /// Given the following JSON object as an example: + /// { + /// "config" : { + /// "prop1" : "value1", + /// "prop2" : 10, + /// "prop3" : [ + /// "element1", + /// "element2" + /// ], + /// "prop4" : { + /// "prop5" : false, + /// "prop6" : null + /// } + /// } + /// } + /// The following property names would be valid and would + /// yield the shown values: + /// + /// config.prop1 --> "value1" + /// config.prop3[1] --> "element2" + /// config.prop4.prop5 --> false +{ +public: + + JSONConfiguration(); + /// Creates an empty configuration + + + JSONConfiguration(const std::string& path); + /// Creates a configuration and loads the JSON structure from the given file + + + JSONConfiguration(std::istream& istr); + /// Creates a configuration and loads the JSON structure from the given stream + + + JSONConfiguration(const JSON::Object::Ptr& object); + /// Creates a configuration from the given JSON object + + + virtual ~JSONConfiguration(); + /// Destructor + + + void load(const std::string& path); + /// Loads the configuration from the given file + + + void load(std::istream& istr); + /// Loads the configuration from the given stream + + + void loadEmpty(const std::string& root); + /// Loads an empty object containing only a root object with the given name. + + + void save(std::ostream& ostr, unsigned int indent = 2) const; + /// Saves the configuration to the given stream + + + void setInt(const std::string& key, int value); + + + void setBool(const std::string& key, bool value); + + + void setDouble(const std::string& key, double value); + + + +protected: + + bool getRaw(const std::string & key, std::string & value) const; + + + void setRaw(const std::string& key, const std::string& value); + + + void enumerate(const std::string& key, Keys& range) const; + + +private: + + + JSON::Object::Ptr findStart(const std::string& key, std::string& lastPart); + + + void getIndexes(std::string& name, std::vector& indexes); + + + void setValue(const std::string& key, const Poco::DynamicAny& value); + + + JSON::Object::Ptr _object; +}; + +} } // namespace Poco::Util + + +#endif // Util_JSONConfiguration_INCLUDED diff --git a/Util/src/JSONConfiguration.cpp b/Util/src/JSONConfiguration.cpp new file mode 100644 index 000000000..4b989067a --- /dev/null +++ b/Util/src/JSONConfiguration.cpp @@ -0,0 +1,340 @@ +// +// JSONConfiguration.cpp +// +// $Id$ +// +// Library: Util +// Package: JSON +// Module: JSONConfiguration +// +// 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/FileStream.h" +#include "Poco/StringTokenizer.h" +#include "Poco/Util/JSONConfiguration.h" +#include "Poco/JSON/Parser.h" +#include "Poco/JSON/Query.h" +#include "Poco/JSON/DefaultHandler.h" +#include "Poco/RegularExpression.h" +#include "Poco/NumberParser.h" + +namespace Poco +{ +namespace Util +{ + +JSONConfiguration::JSONConfiguration() +{ +} + + +JSONConfiguration::JSONConfiguration(const std::string& path) +{ + load(path); +} + + +JSONConfiguration::JSONConfiguration(std::istream& istr) +{ + load(istr); +} + + +JSONConfiguration::JSONConfiguration(const JSON::Object::Ptr& object) : _object(object) +{ +} + + +JSONConfiguration::~JSONConfiguration() +{ +} + + +void JSONConfiguration::load(const std::string& path) +{ + Poco::FileInputStream fis(path); + load(fis); +} + + +void JSONConfiguration::load(std::istream& istr) +{ + JSON::Parser parser; + JSON::DefaultHandler handler; + parser.setHandler(&handler); + parser.parse(istr); + DynamicAny result = handler.result(); + if ( result.type() == typeid(JSON::Object::Ptr) ) + { + _object = result.extract(); + } +} + + +void JSONConfiguration::loadEmpty(const std::string& root) +{ + _object = new JSON::Object(); + JSON::Object::Ptr rootObject = new JSON::Object(); + _object->set(root, rootObject); +} + + +bool JSONConfiguration::getRaw(const std::string & key, std::string & value) const +{ + JSON::Query query(_object); + Poco::DynamicAny result = query.find(key); + if ( ! result.isEmpty() ) + { + value = result.convert(); + return true; + } + return false; +} + + +void JSONConfiguration::getIndexes(std::string& name, std::vector& indexes) +{ + indexes.clear(); + + RegularExpression::MatchVec matches; + int firstOffset = -1; + int offset = 0; + RegularExpression regex("\\[([0-9]+)\\]"); + while(regex.match(name, offset, matches) > 0 ) + { + if ( firstOffset == -1 ) + { + firstOffset = matches[0].offset; + } + std::string num = name.substr(matches[1].offset, matches[1].length); + indexes.push_back(NumberParser::parse(num)); + offset = matches[0].offset + matches[0].length; + } + + if ( firstOffset != -1 ) + { + name = name.substr(0, firstOffset); + } +} + +JSON::Object::Ptr JSONConfiguration::findStart(const std::string& key, std::string& lastPart) +{ + JSON::Object::Ptr currentObject = _object; + + StringTokenizer tokenizer(key, "."); + lastPart = tokenizer[tokenizer.count() - 1]; + + for(int i = 0; i < tokenizer.count() - 1; ++i) + { + std::vector indexes; + std::string name = tokenizer[i]; + getIndexes(name, indexes); + + DynamicAny result = currentObject->get(name); + + if ( result.isEmpty() ) // Not found + { + if ( indexes.empty() ) // We want an object, create it + { + JSON::Object::Ptr newObject = new JSON::Object(); + currentObject->set(name, newObject); + currentObject = newObject; + } + else // We need an array + { + JSON::Array::Ptr newArray; + JSON::Array::Ptr parentArray; + JSON::Array::Ptr topArray; + for(std::vector::iterator it = indexes.begin(); it != indexes.end(); ++it) + { + newArray = new JSON::Array(); + if ( topArray.isNull() ) + { + topArray = newArray; + } + + if ( ! parentArray.isNull() ) + { + parentArray->add(newArray); + } + + for(int i = 0; i <= *it - 1; ++i) + { + Poco::DynamicAny nullValue; + newArray->add(nullValue); + } + + parentArray = newArray; + } + + currentObject->set(name, topArray); + currentObject = new JSON::Object(); + newArray->add(currentObject); + } + } + else // We have a value + { + if ( indexes.empty() ) // We want an object + { + if ( result.type() == typeid(JSON::Object::Ptr) ) + { + currentObject = result.extract(); + } + else + { + // TODO: throw an error + } + } + else + { + if ( result.type() == typeid(JSON::Array::Ptr) ) + { + JSON::Array::Ptr arr = result.extract(); + + for(std::vector::iterator it = indexes.begin(); it != indexes.end() - 1; ++it) + { + JSON::Array::Ptr currentArray = arr; + arr = arr->getArray(*it); + if ( arr.isNull() ) + { + arr = new JSON::Array(); + currentArray->add(arr); + } + } + + result = arr->get(*indexes.rbegin()); + if ( result.isEmpty() ) // Index doesn't exist + { + JSON::Object::Ptr newObject = new JSON::Object(); + arr->add(newObject); + currentObject = newObject; + } + else // Index is available + { + if ( result.type() == typeid(JSON::Object::Ptr) ) + { + currentObject = result.extract(); + } + else + { + // TODO: throw an error + } + } + } + else + { + // TODO: throw an error + } + } + } + } + return currentObject; +} + +void JSONConfiguration::setValue(const std::string& key, const Poco::DynamicAny& value) +{ + std::string lastPart; + JSON::Object::Ptr parentObject = findStart(key, lastPart); + + std::vector indexes; + getIndexes(lastPart, indexes); + + if ( indexes.empty() ) // No Array + { + parentObject->set(lastPart, value); + } + else + { + DynamicAny result = parentObject->get(lastPart); + if ( result.isEmpty() ) + { + result = JSON::Array::Ptr(new JSON::Array()); + parentObject->set(lastPart, result); + } + else if ( result.type() != typeid(JSON::Array::Ptr) ) + { + //TODO: throw error + } + + JSON::Array::Ptr arr = result.extract(); + for(std::vector::iterator it = indexes.begin(); it != indexes.end() - 1; ++it) + { + JSON::Array::Ptr nextArray = arr->getArray(*it); + if ( nextArray.isNull() ) + { + for(int i = arr->size(); i <= *it; ++i) + { + Poco::DynamicAny nullValue; + arr->add(nullValue); + } + nextArray = new JSON::Array(); + arr->add(nextArray); + } + arr = nextArray; + } + arr->add(value); + } +} + + +void JSONConfiguration::setRaw(const std::string& key, const std::string& value) +{ + setValue(key, value); +} + +void JSONConfiguration::setInt(const std::string& key, int value) +{ + setValue(key, value); +} + +void JSONConfiguration::setBool(const std::string& key, bool value) +{ + setValue(key, value); +} + +void JSONConfiguration::setDouble(const std::string& key, double value) +{ + setValue(key, value); +} + +void JSONConfiguration::enumerate(const std::string& key, Keys& range) const +{ + JSON::Query query(_object); + Poco::DynamicAny result = query.find(key); + if ( result.type() == typeid(JSON::Object::Ptr) ) + { + JSON::Object::Ptr object = result.extract(); + object->getNames(range); + } +} + + +void JSONConfiguration::save(std::ostream& ostr, unsigned int indent) const +{ + _object->stringify(ostr, indent); +} + +}} // Namespace Poco::Util