1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-16 15:17:13 +02:00

Major plugin refactor and cleanup.

Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
This commit is contained in:
Sandu Liviu Catalin
2021-01-30 08:51:39 +02:00
parent e0e34b4030
commit 4a6bfc086c
6219 changed files with 1209835 additions and 454916 deletions

View File

@ -0,0 +1,564 @@
//
// Array.h
//
// Library: JSON
// Package: JSON
// Module: Array
//
// Definition of the Array class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_Array_INCLUDED
#define JSON_Array_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include <vector>
#include <sstream>
namespace Poco {
namespace JSON {
class Object;
class JSON_API Array
/// Represents a JSON array. Array provides a representation
/// based on shared pointers and optimized for performance. It is possible to
/// convert Array to Poco::Dynamic::Array. Conversion requires copying and therefore
/// has performance penalty; the benefit is in improved syntax, eg:
///
/// // use pointers to avoid copying
/// using namespace Poco::JSON;
/// std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]";
/// Parser parser;
/// Var result = parser.parse(json);
/// Array::Ptr arr = result.extract<Array::Ptr>();
/// Object::Ptr object = arr->getObject(0); // object == {\"test\" : 0}
/// int i = object->getElement<int>("test"); // i == 0;
/// Object::Ptr subObject = *arr->getObject(1); // subObject == {\"test\" : 0}
/// Array subArr::Ptr = subObject->getArray("test1"); // subArr == [1, 2, 3]
/// i = result = subArr->get(0); // i == 1;
///
/// // copy/convert to Poco::Dynamic::Array
/// Poco::Dynamic::Array da = *arr;
/// i = da[0]["test"]; // i == 0
/// i = da[1]["test1"][1]; // i == 2
/// i = da[1]["test2"]; // i == 4
/// ----
{
public:
using ValueVec = std::vector<Dynamic::Var>;
using Iterator = std::vector<Dynamic::Var>::iterator;
using ConstIterator = std::vector<Dynamic::Var>::const_iterator;
using Ptr = SharedPtr<Array>;
Array(int options = 0);
/// Creates an empty Array.
///
/// If JSON_ESCAPE_UNICODE is specified, when the object is
/// stringified, all unicode characters will be escaped in the
/// resulting string.
Array(const Array& copy);
/// Creates an Array by copying another one.
Array(Array&& other) noexcept;
/// Move constructor
Array& operator = (const Array& other);
/// Assignment operator.
Array& operator = (Array&& other) noexcept;
/// Move assignment operator.
~Array();
/// Destroys the Array.
void setEscapeUnicode(bool escape = true);
/// Sets the flag for escaping unicode.
bool getEscapeUnicode() const;
/// Returns the flag for escaping unicode.
ValueVec::const_iterator begin() const;
/// Returns the begin iterator for values.
ValueVec::const_iterator end() const;
/// Returns the end iterator for values.
Dynamic::Var get(unsigned int index) const;
/// Retrieves the element at the given index.
/// 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.
{
Dynamic::Var 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.
std::size_t size() const;
/// Returns the size of the array.
bool isArray(unsigned int index) const;
/// Returns true when the element is an array.
bool isArray(const Dynamic::Var& value) const;
/// Returns true when the element is an array.
bool isArray(ConstIterator& value) 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.
bool isObject(const Dynamic::Var& value) const;
/// Returns true when the element is an object.
bool isObject(ConstIterator& value) 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;
}
Array& add(const Dynamic::Var& value);
/// Add the given value to the array
Array& set(unsigned int index, const Dynamic::Var& value);
/// Update the element on the given index to specified value
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
/// Prints the array to out. When indent has zero value,
/// the array will be printed without newline breaks and spaces between elements.
void remove(unsigned int index);
/// Removes the element on the given index.
operator const Poco::Dynamic::Array& () const;
/// Conversion operator to Dynamic::Array.
static Poco::Dynamic::Array makeArray(const JSON::Array::Ptr& arr);
/// Utility function for creation of array.
void clear();
/// Clears the contents of the array.
private:
void resetDynArray() const;
typedef SharedPtr<Poco::Dynamic::Array> ArrayPtr;
ValueVec _values;
mutable ArrayPtr _pArray;
mutable bool _modified;
// Note:
// The reason we have this flag here (rather than as argument to stringify())
// is because Array can be returned stringified from a Dynamic::Var:toString(),
// so it must know whether to escape unicode or not.
bool _escapeUnicode;
};
//
// inlines
//
inline void Array::setEscapeUnicode(bool escape)
{
_escapeUnicode = escape;
}
inline bool Array::getEscapeUnicode() const
{
return _escapeUnicode;
}
inline Array::ValueVec::const_iterator Array::begin() const
{
return _values.begin();
}
inline Array::ValueVec::const_iterator Array::end() const
{
return _values.end();
}
inline std::size_t Array::size() const
{
return static_cast<std::size_t>(_values.size());
}
inline bool Array::isArray(unsigned int index) const
{
Dynamic::Var value = get(index);
return isArray(value);
}
inline bool Array::isArray(const Dynamic::Var& value) const
{
return value.type() == typeid(Array::Ptr);
}
inline bool Array::isArray(ConstIterator& it) const
{
return it!= end() && isArray(*it);
}
inline Array& Array::add(const Dynamic::Var& value)
{
_values.push_back(value);
_modified = true;
return *this;
}
inline Array& Array::set(unsigned int index, const Dynamic::Var& value)
{
if (index >= _values.size()) _values.resize(index + 1);
_values[index] = value;
_modified = true;
return *this;
}
inline void Array::remove(unsigned int index)
{
_values.erase(_values.begin() + index);
}
} } // namespace Poco::JSON
namespace Poco {
namespace Dynamic {
template <>
class VarHolderImpl<JSON::Array::Ptr>: public VarHolder
{
public:
VarHolderImpl(const JSON::Array::Ptr& val): _val(val)
{
}
~VarHolderImpl()
{
}
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("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const JSON::Array::Ptr& value() const
{
return _val;
}
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;
};
template <>
class VarHolderImpl<JSON::Array>: public VarHolder
{
public:
VarHolderImpl(const JSON::Array& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Array);
}
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.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("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const JSON::Array& value() const
{
return _val;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
JSON::Array _val;
};
} } // namespace Poco::Dynamic
#endif // JSON_Array_INCLUDED

View File

@ -0,0 +1,106 @@
//
// Handler.h
//
// Library: JSON
// Package: JSON
// Module: Handler
//
// Definition of the Handler class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_Handler_INCLUDED
#define JSON_Handler_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/Dynamic/Struct.h"
namespace Poco {
namespace JSON {
class JSON_API Handler
/// Interface for handling parsing events generated by the JSON Parser.
///
/// An application can implement a subclass of Handler to implement
/// callback-based parsing of a JSON document, similar to how a SAX
/// parser would handle XML.
{
public:
using Ptr = SharedPtr<Handler>;
Handler();
/// Creates an empty Handler.
virtual ~Handler();
/// Destroys the Handler.
virtual void reset() = 0;
/// Resets the handler state.
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(unsigned v) = 0;
/// An unsigned value is read. This will only be triggered if the
/// value cannot fit into a signed int.
#if defined(POCO_HAVE_INT64)
virtual void value(Int64 v) = 0;
/// A 64-bit integer value is read.
virtual void value(UInt64 v) = 0;
/// An unsigned 64-bit integer value is read. This will only be
/// triggered if the value cannot fit into a signed 64-bit integer.
#endif
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.
virtual Poco::Dynamic::Var asVar() const;
/// Returns the result of the parser (an object, array or string),
/// empty Var if there is no result.
virtual Poco::DynamicStruct asStruct() const;
/// Returns the result of the parser (an object, array or string),
/// empty Var if there is no result.
};
} } // namespace Poco::JSON
#endif // JSON_Handler_INCLUDED

View File

@ -0,0 +1,62 @@
//
// JSON.h
//
// Library: JSON
// Package: JSON
// Module: JSON
//
// Basic definitions for the Poco JSON library.
// This file must be the first file included by every other JSON
// header file.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#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 JSON_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
// JSON_API functions as being imported from a DLL, whereas 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)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define JSON_API __attribute__ ((visibility ("default")))
#else
#define JSON_API
#endif
#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,35 @@
//
// JSONException.h
//
// Library: JSON
// Package: JSON
// Module: JSONException
//
// Definition of the JSONException class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#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,780 @@
//
// Object.h
//
// Library: JSON
// Package: JSON
// Module: Object
//
// Definition of the Object class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_Object_INCLUDED
#define JSON_Object_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Stringifier.h"
#include "Poco/JSONString.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/Dynamic/Struct.h"
#include "Poco/Nullable.h"
#include <map>
#include <vector>
#include <deque>
#include <iostream>
#include <sstream>
namespace Poco {
namespace JSON {
class JSON_API Object
/// Represents a JSON object. Object provides a representation based on
/// shared pointers and optimized for performance. It is possible to
/// convert Object to DynamicStruct. Conversion requires copying and therefore
/// has performance penalty; the benefit is in improved syntax, eg:
///
/// std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
/// Parser parser;
/// Var result = parser.parse(json);
///
/// // use pointers to avoid copying
/// Object::Ptr object = result.extract<Object::Ptr>();
/// Var test = object->get("test"); // holds { "property" : "value" }
/// Object::Ptr subObject = test.extract<Object::Ptr>();
/// test = subObject->get("property");
/// std::string val = test.toString(); // val holds "value"
///
/// // copy/convert to Poco::DynamicStruct
/// Poco::DynamicStruct ds = *object;
/// val = ds["test"]["property"]; // val holds "value"
///
{
public:
using Ptr = SharedPtr<Object>;
using ValueMap = std::map<std::string, Dynamic::Var>;
using ValueType = ValueMap::value_type;
using Iterator = ValueMap::iterator;
using ConstIterator = ValueMap::const_iterator;
using NameList = std::vector<std::string>;
explicit Object(int options = 0);
/// Creates an empty Object.
///
/// If JSON_PRESERVE_KEY_ORDER is specified, the object will
/// preserve the items insertion order. Otherwise, items will be
/// sorted by keys.
///
/// If JSON_ESCAPE_UNICODE is specified, when the object is
/// stringified, all unicode characters will be escaped in the
/// resulting string.
Object(const Object& copy);
/// Creates an Object by copying another one.
///
/// Struct is not copied to keep the operation as
/// efficient as possible (when needed, it will be generated upon request).
Object(Object&& other) noexcept;
/// Move constructor
~Object();
/// Destroys the Object.
Object &operator = (const Object &other);
// Assignment operator
Object &operator = (Object &&other) noexcept;
// Move asignment operator
void setEscapeUnicode(bool escape = true);
/// Sets the flag for escaping unicode.
bool getEscapeUnicode() const;
/// Returns the flag for escaping unicode.
Iterator begin();
/// Returns begin iterator for values.
ConstIterator begin() const;
/// Returns const begin iterator for values.
Iterator end();
/// Returns end iterator for values.
ConstIterator end() const;
/// Returns const end iterator for values.
Dynamic::Var 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 Var is called
/// which can also throw exceptions for invalid values.
/// Note: This will not work for an array or an object.
{
Dynamic::Var value = get(key);
return value.convert<T>();
}
template<typename T>
Poco::Nullable<T> getNullableValue(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 Var is called
/// which can also throw exceptions for invalid values.
/// Note: This will not work for an array or an object.
{
if (isNull(key))
return Poco::Nullable<T>();
Dynamic::Var value = get(key);
return value.convert<T>();
}
void getNames(NameList& names) const;
/// Fills the supplied vector with all property names.
NameList getNames() 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 isArray(ConstIterator& it) 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.
bool isObject(ConstIterator& it) 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;
}
std::size_t size() const;
/// Returns the number of properties.
Object& set(const std::string& key, const Dynamic::Var& value);
/// Sets a new value.
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
/// Prints the object to out stream.
///
/// When indent is 0, the object will be printed on a single
/// line without indentation.
void remove(const std::string& key);
/// Removes the property with the given key.
static Poco::DynamicStruct makeStruct(const Object::Ptr& obj);
/// Utility function for creation of struct.
static Poco::OrderedDynamicStruct makeOrderedStruct(const Object::Ptr& obj);
/// Utility function for creation of ordered struct.
operator const Poco::OrderedDynamicStruct& () const;
/// Cast operator to Poco::OrderedDynamiStruct.
operator const Poco::DynamicStruct& () const;
/// Cast operator to Poco::DynamiStruct.
void clear();
/// Clears the contents of the object.
///
/// Insertion order preservation property is left intact.
private:
typedef std::deque<ValueMap::const_iterator> KeyList;
typedef Poco::DynamicStruct::Ptr StructPtr;
typedef Poco::OrderedDynamicStruct::Ptr OrdStructPtr;
void syncKeys(const KeyList& keys);
template <typename T>
void resetDynStruct(T& pStruct) const
{
if (!pStruct)
pStruct = new typename T::Type;
else
pStruct->clear();
}
template <typename C>
void doStringify(const C& container, std::ostream& out, unsigned int indent, unsigned int step) const
{
int options = Poco::JSON_WRAP_STRINGS;
options |= _escapeUnicode ? Poco::JSON_ESCAPE_UNICODE : 0;
out << '{';
if (indent > 0) out << std::endl;
typename C::const_iterator it = container.begin();
typename C::const_iterator end = container.end();
for (; it != end;)
{
for (unsigned int i = 0; i < indent; i++) out << ' ';
Stringifier::stringify(getKey(it), out, indent, step, options);
out << ((indent > 0) ? " : " : ":");
Stringifier::stringify(getValue(it), out, indent + step, step, options);
if (++it != container.end()) out << ',';
if (step > 0) out << std::endl;
}
if (indent >= step) indent -= step;
for (unsigned int i = 0; i < indent; i++) out << ' ';
out << '}';
}
template <typename S>
static S makeStructImpl(const Object::Ptr& obj)
{
S ds;
if (obj->_preserveInsOrder)
{
KeyList::const_iterator it = obj->_keys.begin();
KeyList::const_iterator end = obj->_keys.end();
for (; it != end; ++it)
{
if (obj->isObject((*it)->first))
{
Object::Ptr pObj = obj->getObject((*it)->first);
S str = makeStructImpl<S>(pObj);
ds.insert((*it)->first, str);
}
else if (obj->isArray((*it)->first))
{
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);
}
}
else
{
ConstIterator it = obj->begin();
ConstIterator end = obj->end();
for (; it != end; ++it)
{
if (obj->isObject(it))
{
Object::Ptr pObj = obj->getObject(it->first);
S str = makeStructImpl<S>(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;
}
const std::string& getKey(ValueMap::const_iterator& it) const;
const Dynamic::Var& getValue(ValueMap::const_iterator& it) const;
const std::string& getKey(KeyList::const_iterator& it) const;
const Dynamic::Var& getValue(KeyList::const_iterator& it) const;
ValueMap _values;
KeyList _keys;
bool _preserveInsOrder;
// Note:
// The reason for this flag (rather than as argument to stringify()) is
// because Object can be returned stringified from Dynamic::Var::toString(),
// so it must know whether to escape unicode or not.
bool _escapeUnicode;
mutable StructPtr _pStruct;
mutable OrdStructPtr _pOrdStruct;
mutable bool _modified;
};
//
// inlines
//
inline void Object::setEscapeUnicode(bool escape)
{
_escapeUnicode = escape;
}
inline bool Object::getEscapeUnicode() const
{
return _escapeUnicode;
}
inline Object::Iterator Object::begin()
{
return _values.begin();
}
inline Object::ConstIterator Object::begin() const
{
return _values.begin();
}
inline Object::Iterator Object::end()
{
return _values.end();
}
inline Object::ConstIterator Object::end() const
{
return _values.end();
}
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 isArray(it);
}
inline bool Object::isArray(ConstIterator& it) const
{
return it != _values.end() && (it->second.type() == typeid(Array::Ptr) || it->second.type() == typeid(Array));
}
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 isObject(it);
}
inline bool Object::isObject(ConstIterator& it) const
{
return it != _values.end() && (it->second.type() == typeid(Object::Ptr) || it->second.type() == typeid(Object));
}
inline std::size_t Object::size() const
{
return static_cast<std::size_t>(_values.size());
}
inline void Object::remove(const std::string& key)
{
_values.erase(key);
if (_preserveInsOrder)
{
KeyList::iterator it = _keys.begin();
KeyList::iterator end = _keys.end();
for (; it != end; ++it)
{
if (key == (*it)->first)
{
_keys.erase(it);
break;
}
}
}
_modified = true;
}
inline const std::string& Object::getKey(ValueMap::const_iterator& it) const
{
return it->first;
}
inline const Dynamic::Var& Object::getValue(ValueMap::const_iterator& it) const
{
return it->second;
}
inline const Dynamic::Var& Object::getValue(KeyList::const_iterator& it) const
{
ValueMap::const_iterator itv = _values.find((*it)->first);
if (itv != _values.end())
return itv->second;
else
throw Poco::NotFoundException();
}
} } // namespace Poco::JSON
namespace Poco {
namespace Dynamic {
template <>
class VarHolderImpl<JSON::Object::Ptr>: public VarHolder
{
public:
VarHolderImpl(const JSON::Object::Ptr& val): _val(val)
{
}
~VarHolderImpl()
{
}
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;
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
//TODO: ldt = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
//TODO: ts = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _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;
};
template <>
class VarHolderImpl<JSON::Object>: public VarHolder
{
public:
VarHolderImpl(const JSON::Object& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Object);
}
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.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;
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
//TODO: ldt = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
//TODO: ts = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const JSON::Object& 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 _val;
};
} } // namespace Poco::Dynamic
#endif // JSON_Object_INCLUDED

View File

@ -0,0 +1,169 @@
//
// ParseHandler.h
//
// Library: JSON
// Package: JSON
// Module: ParseHandler
//
// Definition of the ParseHandler class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_ParseHandler_INCLUDED
#define JSON_ParseHandler_INCLUDED
#include "Poco/JSON/Handler.h"
#include <stack>
namespace Poco {
namespace JSON {
class JSON_API ParseHandler: public Handler
/// ParseHandler is the default handler for the JSON Parser.
///
/// This handler will construct an Object or Array based
/// on the handlers called by the Parser.
{
public:
ParseHandler(bool preserveObjectOrder = false);
/// Creates the ParseHandler.
///
/// If preserveObjectOrder is true, the order of properties
/// inside objects is preserved. Otherwise, items
/// will be sorted by keys.
virtual ~ParseHandler();
/// Destroys the ParseHandler.
virtual void reset();
/// Resets the handler state.
void startObject();
/// Handles a '{'; a new object is started.
void endObject();
/// Handles a '}'; the object is closed.
void startArray();
/// Handles a '['; a new array is started.
void endArray();
/// Handles a ']'; the array is closed.
void key(const std::string& k);
/// A key is read
Dynamic::Var asVar() const;
/// Returns the result of the parser (an object or an array).
virtual void value(int v);
/// An integer value is read
virtual void value(unsigned v);
/// An unsigned value is read. This will only be triggered if the
/// value cannot fit into a signed int.
#if defined(POCO_HAVE_INT64)
virtual void value(Int64 v);
/// A 64-bit integer value is read
virtual void value(UInt64 v);
/// An unsigned 64-bit integer value is read. This will only be
/// triggered if the value cannot fit into a signed 64-bit integer.
#endif
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::Dynamic::Var& value);
typedef std::stack<Dynamic::Var> Stack;
Stack _stack;
std::string _key;
Dynamic::Var _result;
bool _preserveObjectOrder;
};
//
// inlines
//
inline Dynamic::Var ParseHandler::asVar() const
{
return _result;
}
inline void ParseHandler::value(int v)
{
setValue(v);
}
inline void ParseHandler::value(unsigned v)
{
setValue(v);
}
#if defined(POCO_HAVE_INT64)
inline void ParseHandler::value(Int64 v)
{
setValue(v);
}
inline void ParseHandler::value(UInt64 v)
{
setValue(v);
}
#endif
inline void ParseHandler::value(const std::string& s)
{
setValue(s);
}
inline void ParseHandler::value(double d)
{
setValue(d);
}
inline void ParseHandler::value(bool b)
{
setValue(b);
}
inline void ParseHandler::null()
{
Poco::Dynamic::Var empty;
setValue(empty);
}
} } // namespace Poco::JSON
#endif // JSON_ParseHandler_INCLUDED

View File

@ -0,0 +1,203 @@
//
// Parser.h
//
// Library: JSON
// Package: JSON
// Module: Parser
//
// Definition of the Parser class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_JSONParser_INCLUDED
#define JSON_JSONParser_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/ParserImpl.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Dynamic/Var.h"
#include <string>
namespace Poco {
namespace JSON {
class JSON_API Parser: private ParserImpl
/// A parser for reading RFC 4627 compliant JSON from strings or streams.
///
/// Simple usage example:
///
/// std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }";
/// Parser parser;
/// Var result = parser.parse(json);
/// // ... use result (see next example)
/// parser.reset();
/// std::ostringstream ostr;
/// PrintHandler::Ptr pHandler = new PrintHandler(ostr);
/// parser.setHandler(pHandler);
/// parser.parse(json); // ostr.str() == json
/// ----
///
/// The result of parsing a valid JSON document will be either an Object
/// or an Array. Therefore the result of parse() is a Poco::Dynamic::Var
/// containing a Poco::SharedPtr to an Object or Array instance.
///
/// Example:
///
/// std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }";
/// Parser parser;
/// Var result = parser.parse(json);
/// Object::Ptr object = result.extract<Object::Ptr>();
/// std::string name = object.getValue<std::string>("name");
/// Array::Ptr children = object.getArray("children");
/// ----
{
public:
Parser(const Handler::Ptr& pHandler = new ParseHandler, std::size_t bufSize = JSON_PARSE_BUFFER_SIZE);
/// Creates JSON Parser, using the given Handler and buffer size.
virtual ~Parser();
/// Destroys JSON Parser.
void reset();
/// Resets the parser.
void setAllowComments(bool comments);
/// Allow or disallow comments. By default, comments are not allowed.
bool getAllowComments() const;
/// Returns true if comments are allowed, false otherwise.
///
/// By default, comments are not allowed.
void setAllowNullByte(bool nullByte);
/// Allow or disallow null byte in strings.
///
/// By default, null byte is allowed.
bool getAllowNullByte() const;
/// Returns true if null byte is allowed, false otherwise.
///
/// By default, null bytes are allowed.
void setDepth(std::size_t depth);
/// Sets the allowed JSON depth.
std::size_t getDepth() const;
/// Returns the allowed JSON depth.
Dynamic::Var parse(const std::string& json);
/// Parses JSON from a string.
Dynamic::Var parse(std::istream& in);
/// Parses JSON from an input stream.
void setHandler(const Handler::Ptr& pHandler);
/// Set the Handler.
const Handler::Ptr& getHandler();
/// Returns the Handler.
Dynamic::Var asVar() const;
/// Returns the result of parsing;
Dynamic::Var result() const;
/// Returns the result of parsing as Dynamic::Var;
private:
Parser(const Parser&);
Parser& operator = (const Parser&);
};
//
// inlines
//
inline void Parser::setAllowComments(bool comments)
{
setAllowCommentsImpl(comments);
}
inline bool Parser::getAllowComments() const
{
return getAllowCommentsImpl();
}
inline void Parser::setAllowNullByte(bool nullByte)
{
setAllowNullByteImpl(nullByte);
}
inline bool Parser::getAllowNullByte() const
{
return getAllowNullByteImpl();
}
inline void Parser::setDepth(std::size_t depth)
{
setDepthImpl(depth);
}
inline std::size_t Parser::getDepth() const
{
return getDepthImpl();
}
inline const Handler::Ptr& Parser::getHandler()
{
return getHandlerImpl();
}
inline Dynamic::Var Parser::result() const
{
return resultImpl();
}
inline Dynamic::Var Parser::asVar() const
{
return asVarImpl();
}
inline void Parser::reset()
{
resetImpl();
}
inline Dynamic::Var Parser::parse(const std::string& json)
{
return parseImpl(json);
}
inline Dynamic::Var Parser::parse(std::istream& in)
{
return parseImpl(in);
}
} } // namespace Poco::JSON
#endif // JSON_JSONParser_INCLUDED

View File

@ -0,0 +1,194 @@
//
// Parser.h
//
// Library: JSON
// Package: JSON
// Module: ParserImpl
//
// Definition of the ParserImpl class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_JSONParserImpl_INCLUDED
#define JSON_JSONParserImpl_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Dynamic/Var.h"
#include <string>
struct json_stream;
namespace Poco {
namespace JSON {
class JSON_API ParserImpl
{
protected:
static const std::size_t JSON_PARSE_BUFFER_SIZE = 4096;
static const std::size_t JSON_PARSER_STACK_SIZE = 128;
static const int JSON_UNLIMITED_DEPTH = -1;
ParserImpl(const Handler::Ptr& pHandler = new ParseHandler, std::size_t bufSize = JSON_PARSE_BUFFER_SIZE);
/// Creates JSON ParserImpl, using the given Handler and buffer size.
virtual ~ParserImpl();
/// Destroys JSON ParserImpl.
void resetImpl();
/// Resets the parser.
void setAllowCommentsImpl(bool comments);
/// Allow or disallow comments. By default, comments are not allowed.
bool getAllowCommentsImpl() const;
/// Returns true if comments are allowed, false otherwise.
///
/// By default, comments are not allowed.
void setAllowNullByteImpl(bool nullByte);
/// Allow or disallow null byte in strings.
///
/// By default, null byte is allowed.
bool getAllowNullByteImpl() const;
/// Returns true if null byte is allowed, false otherwise.
///
/// By default, null bytes are allowed.
void setDepthImpl(std::size_t depth);
/// Sets the allowed JSON depth.
std::size_t getDepthImpl() const;
/// Returns the allowed JSON depth.
Dynamic::Var parseImpl(const std::string& json);
/// Parses JSON from a string.
Dynamic::Var parseImpl(std::istream& in);
/// Parses JSON from an input stream.
void setHandlerImpl(const Handler::Ptr& pHandler);
/// Set the Handler.
const Handler::Ptr& getHandlerImpl();
/// Returns the Handler.
Dynamic::Var asVarImpl() const;
/// Returns the result of parsing;
Dynamic::Var resultImpl() const;
/// Returns the result of parsing as Dynamic::Var;
private:
ParserImpl(const ParserImpl&);
ParserImpl& operator =(const ParserImpl&);
void handleArray();
void handleObject();
void handle();
void handle(const std::string& json);
void stripComments(std::string& json);
bool checkError();
struct json_stream* _pJSON;
Handler::Ptr _pHandler;
int _depth;
char _decimalPoint;
bool _allowNullByte;
bool _allowComments;
};
//
// inlines
//
inline void ParserImpl::resetImpl()
{
// currently, json stream is opened and closed on every parse request
// (perhaps there is some optimization room?)
//json_reset(&_json);
if (_pHandler) _pHandler->reset();
}
inline void ParserImpl::setAllowCommentsImpl(bool comments)
{
_allowComments = comments;
}
inline bool ParserImpl::getAllowCommentsImpl() const
{
return _allowComments;
}
inline void ParserImpl::setAllowNullByteImpl(bool nullByte)
{
_allowNullByte = nullByte;
}
inline bool ParserImpl::getAllowNullByteImpl() const
{
return _allowNullByte;
}
inline void ParserImpl::setDepthImpl(std::size_t depth)
{
_depth = static_cast<int>(depth);
}
inline std::size_t ParserImpl::getDepthImpl() const
{
return static_cast<int>(_depth);
}
inline void ParserImpl::setHandlerImpl(const Handler::Ptr& pHandler)
{
_pHandler = pHandler;
}
inline const Handler::Ptr& ParserImpl::getHandlerImpl()
{
return _pHandler;
}
inline Dynamic::Var ParserImpl::resultImpl() const
{
return asVarImpl();
}
inline Dynamic::Var ParserImpl::asVarImpl() const
{
if (_pHandler) return _pHandler->asVar();
return Dynamic::Var();
}
} } // namespace Poco::JSON
#endif // JSON_JSONParserImpl_INCLUDED

View File

@ -0,0 +1,139 @@
//
// PrintHandler.h
//
// Library: JSON
// Package: JSON
// Module: PrintHandler
//
// Definition of the PrintHandler class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_PrintHandler_INCLUDED
#define JSON_PrintHandler_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Handler.h"
#include "Poco/JSONString.h"
namespace Poco {
namespace JSON {
class JSON_API PrintHandler: public Handler
/// PrintHandler formats and prints the JSON object
/// to either user-provided std::ostream or standard output.
/// If indent is zero, the output is a condensed JSON string,
/// otherwise, the proper indentation is applied to elements.
{
public:
using Ptr = SharedPtr<PrintHandler>;
static const unsigned JSON_PRINT_FLAT = 0;
PrintHandler(unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
/// Creates the PrintHandler.
PrintHandler(std::ostream& out, unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
/// Creates the PrintHandler.
~PrintHandler();
/// Destroys the PrintHandler.
void reset();
/// Resets the handler state.
void startObject();
/// The parser has read a '{'; a new object is started.
/// If indent is greater than zero, a newline will be appended.
void endObject();
/// The parser has read a '}'; the object is closed.
void startArray();
/// The parser has read a [; a new array will be started.
/// If indent is greater than zero, a newline will be appended.
void endArray();
/// The parser has read a ]; the array is closed.
void key(const std::string& k);
/// A key of an object is read; it will be written to the output,
/// followed by a ':'. If indent is greater than zero, the colon
/// is padded by a space before and after.
void null();
/// A null value is read; "null" will be written to the output.
void value(int v);
/// An integer value is read.
void value(unsigned v);
/// An unsigned value is read. This will only be triggered if the
/// value cannot fit into a signed int.
#if defined(POCO_HAVE_INT64)
void value(Int64 v);
/// A 64-bit integer value is read; it will be written to the output.
void value(UInt64 v);
/// An unsigned 64-bit integer value is read; it will be written to the output.
#endif
void value(const std::string& value);
/// A string value is read; it will be formatted and written to the output.
void value(double d);
/// A double value is read; it will be written to the output.
void value(bool b);
/// A boolean value is read; it will be written to the output.
void comma();
/// A comma is read; it will be written to the output as "true" or "false".
void setIndent(unsigned indent);
/// Sets indentation.
private:
const char* endLine() const;
unsigned indent();
bool printFlat() const;
void arrayValue();
bool array() const;
std::ostream& _out;
unsigned _indent;
std::string _tab;
int _array;
bool _objStart;
int _options;
};
//
// inlines
//
inline void PrintHandler::setIndent(unsigned indent)
{
_indent = indent;
}
inline bool PrintHandler::array() const
{
return _array > 0;
}
} } // namespace Poco::JSON
#endif // JSON_PrintHandler_INCLUDED

View File

@ -0,0 +1,125 @@
//
// Query.h
//
// Library: JSON
// Package: JSON
// Module: Query
//
// Definition of the Query class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#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 Dynamic::Var& source);
/// Creates a Query/
///
/// Source must be JSON Object, Array, Object::Ptr,
/// Array::Ptr or empty Var. Any other type will trigger throwing of
/// InvalidArgumentException.
///
/// Creating Query holding Ptr will typically result in faster
/// performance.
virtual ~Query();
/// Destroys the Query.
Object::Ptr findObject(const std::string& path) const;
/// Search for an object.
///
/// When the object can't be found, a zero Ptr is returned;
/// otherwise, a shared pointer to internally held object
/// is returned.
/// If object (as opposed to a pointer to object) is held
/// internally, a shared pointer to new (heap-allocated) Object is
/// returned; this may be expensive operation.
Object& findObject(const std::string& path, Object& obj) const;
/// Search for an object.
///
/// If object is found, it is assigned to the
/// Object through the reference passed in. When the object can't be
/// found, the provided Object is emptied and returned.
Array::Ptr findArray(const std::string& path) const;
/// Search for an array.
///
/// When the array can't be found, a zero Ptr is returned;
/// otherwise, a shared pointer to internally held array
/// is returned.
/// If array (as opposed to a pointer to array) is held
/// internally, a shared pointer to new (heap-allocated) Object is
/// returned; this may be expensive operation.
Array& findArray(const std::string& path, Array& obj) const;
/// Search for an array.
///
/// If array is found, it is assigned to the
/// Object through the reference passed in. When the array can't be
/// found, the provided Object is emptied and returned.
Dynamic::Var find(const std::string& path) const;
/// Searches a value.
///
/// 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;
Dynamic::Var 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:
Dynamic::Var _source;
};
} } // namespace Poco::JSON
#endif // JSON_JSONQuery_INCLUDED

View File

@ -0,0 +1,73 @@
//
// Stringifier.h
//
// Library: JSON
// Package: JSON
// Module: Stringifier
//
// Definition of the Stringifier class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_JSONStringifier_INCLUDED
#define JSON_JSONStringifier_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSONString.h"
#include "Poco/Dynamic/Var.h"
#include <ostream>
namespace Poco {
namespace JSON {
class JSON_API Stringifier
/// Helper class for creating a string from a JSON object or array.
{
public:
static void condense(const Dynamic::Var& any, std::ostream& out, int options = Poco::JSON_WRAP_STRINGS);
/// Writes a condensed string representation of the value to the output stream while preserving
/// the insertion order.
///
/// If JSON_ESCAPE_UNICODE is in options, all unicode characters will be escaped, otherwise
/// only the compulsory ones.
///
/// This is just a "shortcut" to stringify(any, out) with name indicating the function effect.
static void stringify(const Dynamic::Var& any, std::ostream& out,
unsigned int indent = 0, int step = -1, int options = Poco::JSON_WRAP_STRINGS);
/// Writes a string representation of the value to the output stream.
///
/// When indent is 0, the string will be created as small as possible.
/// Indentation is increased/decreased using number of spaces defined in step.
/// The default value -1 for step indicates that step will be equal to the
/// indent size.
///
/// If JSON_ESCAPE_UNICODE is in options, all unicode characters will be escaped, otherwise
/// only the compulsory ones.
static void formatString(const std::string& value, std::ostream& out, int options = Poco::JSON_WRAP_STRINGS);
/// Formats the JSON string and streams it into ostream.
///
/// If JSON_ESCAPE_UNICODE is in options, all unicode characters will be escaped, otherwise
/// only the compulsory ones.
};
inline void Stringifier::condense(const Dynamic::Var& any, std::ostream& out, int options)
{
stringify(any, out, 0, -1, options);
}
} } // namespace Poco::JSON
#endif // JSON_JSONStringifier_INCLUDED

View File

@ -0,0 +1,151 @@
//
// Template.h
//
// Library: JSON
// Package: JSON
// Module: Template
//
// Definition of the Template class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_JSONTemplate_INCLUDED
#define JSON_JSONTemplate_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/SharedPtr.h"
#include "Poco/Path.h"
#include "Poco/Timestamp.h"
#include <sstream>
#include <stack>
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, etc.
///
/// 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 existence 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:
using Ptr = SharedPtr<Template>;
Template();
/// Creates a Template.
Template(const Path& templatePath);
/// Creates a Template from the file with the given templatePath.
virtual ~Template();
/// Destroys the Template.
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 an input stream.
Timestamp parseTime() const;
/// Returns the time when the template was parsed.
void render(const Dynamic::Var& 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;
};
//
// inlines
//
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,105 @@
//
// TemplateCache.h
//
// Library: JSON
// Package: JSON
// Module: TemplateCache
//
// Definition of the TemplateCache class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef JSON_JSONTemplateCache_INCLUDED
#define JSON_JSONTemplateCache_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Template.h"
#include "Poco/Path.h"
#include "Poco/SharedPtr.h"
#include "Poco/Logger.h"
#include <vector>
#include <map>
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();
/// Creates an empty TemplateCache.
///
/// The cache must be created and not destroyed
/// as long as it is used.
virtual ~TemplateCache();
/// Destroys the TemplateCache.
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:
void setup();
Path resolvePath(const Path& path) const;
static TemplateCache* _pInstance;
std::vector<Path> _includePaths;
std::map<std::string, Template::Ptr> _cache;
Logger* _pLogger;
};
//
// inlines
//
inline void TemplateCache::addPath(const Path& path)
{
_includePaths.push_back(path);
}
inline TemplateCache* TemplateCache::instance()
{
return _pInstance;
}
inline void TemplateCache::setLogger(Logger& logger)
{
_pLogger = &logger;
}
} } // namespace Poco::JSON
#endif // JSON_JSONTemplateCache_INCLUDED