Copy JSON from SandBox to trunk

This commit is contained in:
Franky Braem
2012-04-29 20:09:55 +00:00
parent 5a639074d9
commit 95c62c00df
121 changed files with 5085 additions and 0 deletions

View File

@@ -0,0 +1,358 @@
//
// Array.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Array
//
// Definition of the Array 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 JSON_Array_INCLUDED
#define JSON_Array_INCLUDED
#include <vector>
#include <sstream>
#include "Poco/JSON/JSON.h"
#include "Poco/SharedPtr.h"
#include "Poco/DynamicAny.h"
namespace Poco
{
namespace JSON
{
class Object;
class JSON_API Array
{
public:
typedef std::vector<DynamicAny> ValueVector;
typedef SharedPtr<Array> Ptr;
Array();
/// Default constructor
Array(const Array& copy);
/// Copy Constructor
virtual ~Array();
/// Destructor
ValueVector::const_iterator begin() const;
/// Returns iterator
ValueVector::const_iterator end() const;
/// Returns iterator
DynamicAny get(unsigned int index) const;
/// Retrieves an element. Will return an empty value
/// when the element doesn't exist.
Array::Ptr getArray(unsigned int index) const;
/// Retrieves an array. When the element is not
/// an array or doesn't exist, an empty SharedPtr is returned.
template<typename T>
T getElement(unsigned int index) const
/// Retrieves an element and tries to convert it to the
/// template type. The convert<T> method of
/// Dynamic is called which can also throw
/// exceptions for invalid values.
/// Note: This will not work for an array or an object.
{
DynamicAny value = get(index);
return value.convert<T>();
}
SharedPtr<Object> getObject(unsigned int index) const;
/// Retrieves an object. When the element is not
/// an object or doesn't exist, an empty SharedPtr is returned.
unsigned int size() const;
/// Returns the size of the array
bool isArray(unsigned int index) const;
/// Returns true when the element is an array
bool isNull(unsigned int index) const;
/// Returns true when the element is null or
/// when the element doesn't exist.
bool isObject(unsigned int index) const;
/// Returns true when the element is an object
template<typename T>
T optElement(unsigned int index, const T& def) const
/// Returns the element at the given index. When
/// the element is null, doesn't exist or can't
/// be converted to the given type, the default
/// value will be returned
{
T value = def;
if ( index < _values.size() )
{
try
{
value = _values[index].convert<T>();
}
catch(...)
{
// Default value is returned.
}
}
return value;
}
void add(const DynamicAny& value)
/// Add the given value to the array
{
_values.push_back(value);
}
void stringify(std::ostream& out, unsigned int indent) const;
/// Prints the array to out. When indent is 0, the array
/// will be printed on one line without indentation.
void remove(unsigned int index);
/// Removes the element on the given index.
private:
ValueVector _values;
};
inline Array::ValueVector::const_iterator Array::begin() const
{
return _values.begin();
}
inline Array::ValueVector::const_iterator Array::end() const
{
return _values.end();
}
inline unsigned int Array::size() const
{
return _values.size();
}
inline bool Array::isArray(unsigned int index) const
{
DynamicAny value = get(index);
return value.type() == typeid(Array::Ptr);
}
inline bool Array::isNull(unsigned int index) const
{
if ( index < _values.size() )
{
DynamicAny value = _values[index];
return value.isEmpty();
}
return true;
}
inline void Array::remove(unsigned int index)
{
_values.erase(_values.begin() + index);
}
}} // Namespace Poco::JSON
namespace Poco
{
template <>
class DynamicAnyHolderImpl<JSON::Array::Ptr>: public DynamicAnyHolder
{
public:
DynamicAnyHolderImpl(const JSON::Array::Ptr& val): _val(val)
{
}
~DynamicAnyHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Array::Ptr);
}
void convert(Int8&) const
{
throw BadCastException();
}
void convert(Int16&) const
{
throw BadCastException();
}
void convert(Int32&) const
{
throw BadCastException();
}
void convert(Int64&) const
{
throw BadCastException();
}
void convert(UInt8&) const
{
throw BadCastException();
}
void convert(UInt16&) const
{
throw BadCastException();
}
void convert(UInt32&) const
{
throw BadCastException();
}
void convert(UInt64&) const
{
throw BadCastException();
}
void convert(bool& value) const
{
value = !_val.isNull() && _val->size() > 0;
}
void convert(float&) const
{
throw BadCastException();
}
void convert(double&) const
{
throw BadCastException();
}
void convert(char&) const
{
throw BadCastException();
}
void convert(std::string& s) const
{
std::ostringstream oss;
_val->stringify(oss, 2);
s = oss.str();
}
void convert(DateTime& val) const
{
throw BadCastException();
}
void convert(LocalDateTime& ldt) const
{
throw BadCastException();
}
void convert(Timestamp& ts) const
{
throw BadCastException();
}
DynamicAnyHolder* clone() const
{
return new DynamicAnyHolderImpl(_val);
}
const JSON::Array::Ptr& value() const
{
return _val;
}
bool isArray() const
{
return false;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
JSON::Array::Ptr _val;
};
} // Namespace Poco
#endif // JSON_Array_INCLUDED

View File

@@ -0,0 +1,166 @@
//
// DefaultHandler.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: DefaultHandler
//
// Definition of the DefaultHandler 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 JSON_DefaultHandler_INCLUDED
#define JSON_DefaultHandler_INCLUDED
#include "Poco/JSON/Handler.h"
#include <stack>
namespace Poco
{
namespace JSON
{
class JSON_API DefaultHandler : public Handler
/// Provides a default handler for the JSON parser.
/// This handler will build up an object or array based
/// on the handlers called by the parser.
{
public:
DefaultHandler();
/// Default Constructor
virtual ~DefaultHandler();
/// Destructor
void startObject();
/// Handles a {, meaning a new object will be read
void endObject();
/// Handles a }, meaning the object is read
void startArray();
/// Handles a [, meaning a new array will be read
void endArray();
/// Handles a ], meaning the array is read
void key(const std::string& k);
/// A key is read
DynamicAny result() const;
/// Returns the result of the parser. Which is an object or an array.
virtual void value(int v);
/// An integer value is read
virtual void value(const std::string& s);
/// A string value is read.
virtual void value(double d);
/// A double value is read
virtual void value(bool b);
/// A boolean value is read
virtual void null();
/// A null value is read
private:
void setValue(const Poco::DynamicAny& value);
std::stack<DynamicAny> _stack;
std::string _key;
DynamicAny _result;
};
inline DynamicAny DefaultHandler::result() const
{
return _result;
}
inline void DefaultHandler::value(int v)
{
setValue(v);
}
inline void DefaultHandler::value(const std::string& s)
{
setValue(s);
}
inline void DefaultHandler::value(double d)
{
setValue(d);
}
inline void DefaultHandler::value(bool b)
{
setValue(b);
}
inline void DefaultHandler::null()
{
Poco::DynamicAny empty;
setValue(empty);
}
}} // Namespace Poco::JSON
#endif // JSON_DefaultHandler_INCLUDED

View File

@@ -0,0 +1,106 @@
//
// Handler.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Handler
//
// Definition of the Handler 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 JSON_Handler_INCLUDED
#define JSON_Handler_INCLUDED
#include "Poco/DynamicAny.h"
#include "Poco/JSON/JSON.h"
namespace Poco
{
namespace JSON
{
class JSON_API Handler
{
public:
virtual void startObject() = 0;
/// The parser has read a {, meaning a new object will be read
virtual void endObject() = 0;
/// The parser has read a }, meaning the object is read
virtual void startArray() = 0;
/// The parser has read a [, meaning a new array will be read
virtual void endArray() = 0;
/// The parser has read a ], meaning the array is read
virtual void key(const std::string& k) = 0;
/// A key of an object is read
virtual void null() = 0;
/// A null value is read
virtual void value(int v) = 0;
/// An integer value is read
virtual void value(const std::string& value) = 0;
/// A string value is read.
virtual void value(double d) = 0;
/// A double value is read
virtual void value(bool b) = 0;
/// A boolean value is read
protected:
virtual ~Handler();
/// Destructor
private:
};
}} // Namespace Poco::JSON
#endif // JSON_Handler_INCLUDED

View File

@@ -0,0 +1,80 @@
//
// JSON.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: JSON
//
// Basic definitions for the Poco Util library.
// This file must be the first file included by every other Util
// header file.
//
// 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 JSON_JSON_INCLUDED
#define JSON_JSON_INCLUDED
#include "Poco/Foundation.h"
//
// The following block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the Util_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// Util_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(JSON_EXPORTS)
#define JSON_API __declspec(dllexport)
#else
#define JSON_API __declspec(dllimport)
#endif
#endif
#if !defined(JSON_API)
#define JSON_API
#endif
//
// Automatically link JSON library.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(JSON_EXPORTS)
#pragma comment(lib, "PocoJSON" POCO_LIB_SUFFIX)
#endif
#endif
#endif // JSON_JSON_INCLUDED

View File

@@ -0,0 +1,55 @@
//
// JSONException.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: JSONException
//
// Definition of the JSONException 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 JSON_JSONException_INCLUDED
#define JSON_JSONException_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/Exception.h"
namespace Poco
{
namespace JSON
{
POCO_DECLARE_EXCEPTION(JSON_API, JSONException, Poco::Exception)
}} // Namespace Poco::JSON
#endif //JSON_JSONException_INCLUDED

View File

@@ -0,0 +1,359 @@
//
// Object.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Object
//
// Definition of the Object 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 JSON_Object_INCLUDED
#define JSON_Object_INCLUDED
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include "Poco/SharedPtr.h"
#include "Poco/DynamicAny.h"
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Array.h"
namespace Poco
{
namespace JSON
{
class JSON_API Object
/// Represents a JSON object
{
public:
typedef SharedPtr<Object> Ptr;
Object();
/// Default constructor
Object(const Object& copy);
/// Copy constructor
virtual ~Object();
/// Destructor
DynamicAny get(const std::string& key) const;
/// Retrieves a property. An empty value is
/// returned when the property doesn't exist.
Array::Ptr getArray(const std::string& key) const;
/// Returns a SharedPtr to an array when the property
/// is an array. An empty SharedPtr is returned when
/// the element doesn't exist or is not an array.
Object::Ptr getObject(const std::string& key) const;
/// Returns a SharedPtr to an object when the property
/// is an object. An empty SharedPtr is returned when
/// the property doesn't exist or is not an object
template<typename T>
T getValue(const std::string& key) const
/// Retrieves the property with the given name and will
/// try to convert the value to the given template type.
/// The convert<T> method of Dynamic is called
/// which can also throw exceptions for invalid values.
/// Note: This will not work for an array or an object.
{
DynamicAny value = get(key);
return value.convert<T>();
}
void getNames(std::vector<std::string>& names) const;
/// Returns all property names
bool has(const std::string& key) const;
/// Returns true when the given property exists
bool isArray(const std::string& key) const;
/// Returns true when the given property contains an array
bool isNull(const std::string& key) const;
/// Returns true when the given property contains a null value
bool isObject(const std::string& key) const;
/// Returns true when the given property contains an object
template<typename T>
T optValue(const std::string& key, const T& def) const
/// Returns the value of a property when the property exists
/// and can be converted to the given type. Otherwise
/// def will be returned.
{
T value = def;
ValueMap::const_iterator it = _values.find(key);
if ( it != _values.end()
&& ! it->second.isEmpty() )
{
try
{
value = it->second.convert<T>();
}
catch(...)
{
// The default value will be returned
}
}
return value;
}
unsigned int size() const;
/// Returns the number of properties
void set(const std::string& key, const DynamicAny& value);
/// Sets a new value
void stringify(std::ostream& out, unsigned int indent = 0) const;
/// Prints the object to out. When indent is 0, the object
/// will be printed on one line without indentation.
void remove(const std::string& key);
/// Removes the property with the given key
private:
typedef std::map<std::string, DynamicAny> ValueMap;
ValueMap _values;
};
inline bool Object::has(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
return it != _values.end();
}
inline bool Object::isArray(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
return it != _values.end() || it->second.type() == typeid(Array::Ptr);
}
inline bool Object::isNull(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
return it == _values.end() || it->second.isEmpty();
}
inline bool Object::isObject(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
return it != _values.end() || it->second.type() == typeid(Object::Ptr);
}
inline void Object::set(const std::string& key, const DynamicAny& value)
{
_values[key] = value;
}
inline unsigned int Object::size() const
{
return _values.size();
}
inline void Object::remove(const std::string& key)
{
_values.erase(key);
}
}} // Namespace Poco::JSON
namespace Poco
{
template <>
class DynamicAnyHolderImpl<JSON::Object::Ptr>: public DynamicAnyHolder
{
public:
DynamicAnyHolderImpl(const JSON::Object::Ptr& val): _val(val)
{
}
~DynamicAnyHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Object::Ptr);
}
void convert(Int8&) const
{
throw BadCastException();
}
void convert(Int16&) const
{
throw BadCastException();
}
void convert(Int32&) const
{
throw BadCastException();
}
void convert(Int64&) const
{
throw BadCastException();
}
void convert(UInt8&) const
{
throw BadCastException();
}
void convert(UInt16&) const
{
throw BadCastException();
}
void convert(UInt32&) const
{
throw BadCastException();
}
void convert(UInt64&) const
{
throw BadCastException();
}
void convert(bool& value) const
{
value = !_val.isNull() && _val->size() > 0;
}
void convert(float&) const
{
throw BadCastException();
}
void convert(double&) const
{
throw BadCastException();
}
void convert(char&) const
{
throw BadCastException();
}
void convert(std::string& s) const
{
std::ostringstream oss;
_val->stringify(oss, 2);
s = oss.str();
}
void convert(DateTime& val) const
{
//TODO: val = _val;
}
void convert(LocalDateTime& ldt) const
{
//TODO: ldt = _val.timestamp();
}
void convert(Timestamp& ts) const
{
//TODO: ts = _val.timestamp();
}
DynamicAnyHolder* clone() const
{
return new DynamicAnyHolderImpl(_val);
}
const JSON::Object::Ptr& value() const
{
return _val;
}
bool isArray() const
{
return false;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
JSON::Object::Ptr _val;
};
}
#endif // JSON_Object_INCLUDED

View File

@@ -0,0 +1,140 @@
//
// Parser.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Parser
//
// Definition of the Parser 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 JSON_JSONParser_INCLUDED
#define JSON_JSONParser_INCLUDED
#include <istream>
#include <sstream>
#include "Poco/DynamicAny.h"
#include "Poco/StreamTokenizer.h"
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Handler.h"
namespace Poco
{
namespace JSON
{
class JSON_API Parser
/// A class for passing JSON strings or streams
{
public:
Parser();
/// Constructor
virtual ~Parser();
/// Destructor
void parse(const std::string& source);
/// Parses a string
void parse(std::istream& in);
/// Parses a JSON from the input stream
void setHandler(Handler* handler);
/// Set the handler
Handler* getHandler();
/// Returns the handler
private:
const Token* nextToken();
/// Returns the next token
void readObject();
/// Starts reading an object
void readArray();
/// Starts reading an array
bool readRow(bool firstCall = false);
/// Reads a property value pair. Returns true when a next row is expected.
void readValue(const Token* token);
/// Read a value from the token
bool readElements(bool firstCall = false);
/// Read all elements of an array
StreamTokenizer _tokenizer;
Handler* _handler;
};
inline void Parser::parse(const std::string& source)
{
std::istringstream is(source);
parse(is);
}
inline void Parser::setHandler(Handler* handler)
{
_handler = handler;
}
inline Handler* Parser::getHandler()
{
return _handler;
}
}} // Namespace Poco::JSON
#endif // JSON_JSONParser_INCLUDED

View File

@@ -0,0 +1,118 @@
//
// Query.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Query
//
// Definition of the Query 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 JSON_JSONQuery_INCLUDED
#define JSON_JSONQuery_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
namespace Poco
{
namespace JSON
{
class JSON_API Query
/// Class that can be used to search for a value in a JSON object or array.
{
public:
Query(const DynamicAny& source);
/// Constructor. Pass the start object/array.
virtual ~Query();
/// Destructor
Object::Ptr findObject(const std::string& path) const;
/// Search for an object. When the object can't be found, an empty
/// SharedPtr is returned.
Array::Ptr findArray(const std::string& path) const;
/// Search for an array. When the array can't be found, an empty
/// SharedPtr is returned.
DynamicAny find(const std::string& path) const;
/// Searches a value
/// For example: "person.children[0].name" will return the
/// the name of the first child. When the value can't be found
/// an empty value is returned.
template<typename T>
T findValue(const std::string& path, const T& def) const
/// Searches for a value will convert it to the given type.
/// When the value can't be found or has an invalid type
/// the default value will be returned.
{
T result = def;
DynamicAny value = find(path);
if ( ! value.isEmpty() )
{
try
{
result = value.convert<T>();
}
catch(...)
{
}
}
return result;
}
std::string findValue(const char* path, const char* def) const
/// Searches for a value will convert it to the given type.
/// When the value can't be found or has an invalid type
/// the default value will be returned.
{
return findValue<std::string>(path, def);
}
private:
DynamicAny _source;
};
}} // Namespace JSON
#endif // JSON_JSONQuery_INCLUDED

View File

@@ -0,0 +1,64 @@
//
// Stringifier.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Stringifier
//
// Definition of the Stringifier 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 JSON_JSONStringifier_INCLUDED
#define JSON_JSONStringifier_INCLUDED
#include <iostream>
#include "Poco/DynamicAny.h"
#include "Poco/JSON/JSON.h"
namespace Poco
{
namespace JSON
{
class JSON_API Stringifier
/// Helper class for creating a String from a JSON object or array
{
public:
static void stringify(const DynamicAny& any, std::ostream& out, unsigned int indent = 0);
/// Writes a String representation of the value to the output stream.
/// When indent is 0, the String will be created as small as possible.
};
}} // Namespace Poco::JSON
#endif // JSON_JSONStringifier_INCLUDED

View File

@@ -0,0 +1,182 @@
//
// Template.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Template
//
// Definition of the Template 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 JSON_JSONTemplate_INCLUDED
#define JSON_JSONTemplate_INCLUDED
#include <sstream>
#include <stack>
#include "Poco/JSON/JSON.h"
#include "Poco/DynamicAny.h"
#include "Poco/SharedPtr.h"
#include "Poco/Path.h"
#include "Poco/Timestamp.h"
namespace Poco
{
namespace JSON
{
class MultiPart;
POCO_DECLARE_EXCEPTION(JSON_API, JSONTemplateException, Poco::Exception)
class JSON_API Template
/// Template is a template engine which uses JSON as input
/// for generating output. There are commands for
/// looping over JSON arrays, include other templates,
/// conditional output, ...
///
/// All text is send to the outputstream. A command is placed
/// between <? and ?>.
///
/// These are the available commands:
/// <? echo query ?>
/// The result of the query is send to the output stream
/// This command can also be written as <?= query ?>
/// <? if query ?> <? else ?> <? endif ?>
/// When the result of query is true, all the text between
/// if and else (or endif when there is no else) is send to the
/// output stream. When the result of query is false, all the text
/// between else and endif is send to the output stream. An empty
/// object, an empty array or a null value is considered as a false value.
/// For numbers a zero is false. An empty String is also false.
/// <? ifexist query ?> <? else ?> <? endif ?>
/// This can be used to check the existance of the value.
/// Use this for example when a zero value is ok (which returns false for <? if ?>.
/// <? for variable query ?> <? endfor ?>
/// The result of the query must be an array. For each element
/// in the array the text between for and endfor is send to the
/// output stream. The active element is stored in the variable.
/// <? include "filename" ?>
/// Includes a template. When the filename is relative it will try
/// to resolve the filename against the active template. When this
/// file doesn't exist, it can still be found when the JSONTemplateCache
/// is used.
///
/// A query is passed to Poco::JSON::Query to get the value.
{
public:
typedef SharedPtr<Template> Ptr;
Template();
/// Constructor
Template(const Path& templatePath);
/// Constructor. Creates a template from a file.
virtual ~Template();
/// Destructor
void parse();
/// Parse a template from a file
void parse(const std::string& source);
/// Parse a template from a String
void parse(std::istream& in);
/// Parse a template from a input stream
Timestamp parseTime() const;
/// Returns the time when the template was parsed
void render(const DynamicAny& data, std::ostream& out) const;
/// Renders the template and send the output to the stream
private:
std::string readText(std::istream& in);
std::string readWord(std::istream& in);
std::string readQuery(std::istream& in);
std::string readTemplateCommand(std::istream& in);
std::string readString(std::istream& in);
void readWhiteSpace(std::istream& in);
MultiPart* _parts;
std::stack<MultiPart*> _partStack;
MultiPart* _currentPart;
Path _templatePath;
Timestamp _parseTime;
};
inline void Template::parse(const std::string& source)
{
std::istringstream is(source);
parse(is);
}
inline Timestamp Template::parseTime() const
{
return _parseTime;
}
}} // Namespace Poco::JSON
#endif // JSON_JSONTemplate_INCLUDED

View File

@@ -0,0 +1,139 @@
//
// TemplateCache.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: TemplateCache
//
// Definition of the TemplateCache 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 JSON_JSONTemplateCache_INCLUDED
#define JSON_JSONTemplateCache_INCLUDED
#include <vector>
#include <map>
#include "Poco/Path.h"
#include "Poco/SharedPtr.h"
#include "Poco/Logger.h"
#include "Poco/JSON/Template.h"
namespace Poco
{
namespace JSON
{
class JSON_API TemplateCache
/// Use to cache parsed templates. Templates are
/// stored in a map with the full path as key.
/// When a template file has changed, the cache
/// will remove the old template from the cache
/// and load a new one.
{
public:
TemplateCache();
/// Constructor. The cache must be created
/// and not destroyed as long as it is used.
virtual ~TemplateCache();
/// Destructor
void addPath(const Path& path);
/// Add a path for resolving template paths.
/// The order of check is FIFO.
Template::Ptr getTemplate(const Path& path);
/// Returns a template from the cache.
/// When the template file is not yet loaded
/// or when the file has changed, the template
/// will be (re)loaded and parsed. A shared pointer
/// is returned, so it is safe to use this template
/// even when the template isn't stored anymore in
/// the cache.
static TemplateCache* instance();
/// Returns the only instance of this cache
void setLogger(Logger& logger);
/// Sets the logger for the cache.
private:
static TemplateCache* _instance;
std::vector<Path> _includePaths;
std::map<std::string, Template::Ptr> _cache;
Logger* _logger;
void setup();
Path resolvePath(const Path& path) const;
};
inline void TemplateCache::addPath(const Path& path)
{
_includePaths.push_back(path);
}
inline TemplateCache* TemplateCache::instance()
{
return _instance;
}
inline void TemplateCache::setLogger(Logger& logger)
{
_logger = &logger;
}
}} // Namespace Poco::JSON
#endif // JSON_JSONTemplateCache_INCLUDED