From 95c62c00df26d94f8ef4891b72e8ccbf8abecabb Mon Sep 17 00:00:00 2001 From: Franky Braem Date: Sun, 29 Apr 2012 20:09:55 +0000 Subject: [PATCH] Copy JSON from SandBox to trunk --- JSON/Makefile | 21 + JSON/include/Poco/JSON/Array.h | 358 ++++++++ JSON/include/Poco/JSON/DefaultHandler.h | 166 ++++ JSON/include/Poco/JSON/Handler.h | 106 +++ JSON/include/Poco/JSON/JSON.h | 80 ++ JSON/include/Poco/JSON/JSONException.h | 55 ++ JSON/include/Poco/JSON/Object.h | 359 ++++++++ JSON/include/Poco/JSON/Parser.h | 140 +++ JSON/include/Poco/JSON/Query.h | 118 +++ JSON/include/Poco/JSON/Stringifier.h | 64 ++ JSON/include/Poco/JSON/Template.h | 182 ++++ JSON/include/Poco/JSON/TemplateCache.h | 139 +++ JSON/src/Array.cpp | 147 ++++ JSON/src/DefaultHandler.cpp | 150 ++++ JSON/src/Handler.cpp | 48 ++ JSON/src/JSONException.cpp | 44 + JSON/src/Object.cpp | 155 ++++ JSON/src/Parser.cpp | 678 +++++++++++++++ JSON/src/Query.cpp | 141 +++ JSON/src/Stringifier.cpp | 114 +++ JSON/src/Template.cpp | 736 ++++++++++++++++ JSON/src/TemplateCache.cpp | 172 ++++ JSON/testsuite/src/JSONTest.cpp | 807 ++++++++++++++++++ .../encoded-surrogate-half/input | 1 + .../invalid-utf-8-after-backslash/input | 1 + .../invalid-utf-8-in-array/input | 1 + .../invalid-utf-8-in-bigger-int/input | 1 + .../invalid-utf-8-in-escape/input | 1 + .../invalid-utf-8-in-exponent/input | 1 + .../invalid-utf-8-in-identifier/input | 1 + .../invalid-utf-8-in-int/input | 1 + .../invalid-utf-8-in-real-after-e/input | 1 + .../invalid-utf-8-in-string/input | 1 + .../invalid-unicode/lone-invalid-utf-8/input | 1 + .../lone-utf-8-continuation-byte/input | 1 + .../not-in-unicode-range/input | 1 + .../overlong-3-byte-encoding/input | 1 + .../overlong-4-byte-encoding/input | 1 + .../overlong-ascii-encoding/input | 1 + .../invalid-unicode/restricted-utf-8/input | 1 + .../invalid-unicode/truncated-utf-8/input | 1 + .../testfiles/invalid/apostrophe/input | 1 + .../invalid/ascii-unicode-identifier/input | 1 + .../testfiles/invalid/brace-comma/input | 1 + .../testfiles/invalid/bracket-comma/input | 1 + .../testfiles/invalid/bracket-one-comma/input | 1 + JSON/testsuite/testfiles/invalid/empty/input | 0 .../invalid/escaped-null-byte-in-string/input | 1 + .../invalid/extra-comma-in-array/input | 1 + .../extra-comma-in-multiline-array/input | 6 + .../invalid/garbage-after-newline/input | 2 + .../invalid/garbage-at-the-end/input | 1 + .../invalid/integer-starting-with-zero/input | 1 + .../testfiles/invalid/invalid-escape/input | 1 + .../invalid/invalid-identifier/input | 1 + .../invalid/invalid-negative-integer/input | 1 + .../invalid/invalid-negative-real/input | 1 + .../invalid/invalid-second-surrogate/input | 1 + .../testfiles/invalid/lone-open-brace/input | 1 + .../testfiles/invalid/lone-open-bracket/input | 1 + .../invalid/lone-second-surrogate/input | 1 + .../invalid/minus-sign-without-number/input | 1 + .../negative-integer-starting-with-zero/input | 1 + .../invalid/null-byte-in-string/input | Bin 0 -> 28 bytes .../invalid/null-byte-outside-string/input | Bin 0 -> 3 bytes JSON/testsuite/testfiles/invalid/null/input | 1 + .../invalid/object-apostrophes/input | 1 + .../invalid/object-garbage-at-end/input | 1 + .../object-in-unterminated-array/input | 1 + .../testfiles/invalid/object-no-colon/input | 1 + .../testfiles/invalid/object-no-value/input | 1 + .../invalid/object-unterminated-value/input | 1 + .../invalid/real-garbage-after-e/input | 1 + .../invalid/real-negative-overflow/input | 1 + .../invalid/real-positive-overflow/input | 1 + .../invalid/real-truncated-at-e/input | 1 + .../invalid/real-truncated-at-point/input | 1 + .../invalid/tab-character-in-string/input | 1 + .../invalid/too-big-negative-integer/input | 1 + .../invalid/too-big-positive-integer/input | 1 + .../invalid/truncated-unicode-surrogate/input | 1 + .../invalid/unicode-identifier/input | 1 + .../unterminated-array-and-object/input | 1 + .../invalid/unterminated-array/input | 1 + .../invalid/unterminated-empty-key/input | 1 + .../testfiles/invalid/unterminated-key/input | 1 + .../unterminated-object-and-array/input | 1 + .../invalid/unterminated-string/input | 1 + .../testfiles/valid/complex-array/input | 5 + .../testfiles/valid/empty-array/input | 1 + .../valid/empty-object-in-array/input | 1 + .../testfiles/valid/empty-object/input | 1 + .../testfiles/valid/empty-string/input | 1 + .../valid/escaped-utf-control-char/input | 1 + JSON/testsuite/testfiles/valid/false/input | 1 + .../testfiles/valid/negative-int/input | 1 + .../testfiles/valid/negative-one/input | 1 + .../testfiles/valid/negative-zero/input | 1 + JSON/testsuite/testfiles/valid/null/input | 1 + .../testfiles/valid/one-byte-utf-8/input | 1 + .../real-capital-e-negative-exponent/input | 1 + .../real-capital-e-positive-exponent/input | 1 + .../testfiles/valid/real-capital-e/input | 1 + .../testfiles/valid/real-exponent/input | 1 + .../valid/real-fraction-exponent/input | 1 + .../valid/real-negative-exponent/input | 1 + .../valid/real-positive-exponent/input | 1 + .../testfiles/valid/real-underflow/input | 1 + .../testfiles/valid/short-string/input | 1 + .../testfiles/valid/simple-ascii-string/input | 1 + .../testfiles/valid/simple-int-0/input | 1 + .../testfiles/valid/simple-int-1/input | 1 + .../testfiles/valid/simple-int-123/input | 1 + .../testfiles/valid/simple-object/input | 1 + .../testfiles/valid/simple-real/input | 1 + .../testfiles/valid/string-escapes/input | 1 + .../testfiles/valid/three-byte-utf-8/input | 1 + JSON/testsuite/testfiles/valid/true/input | 1 + .../testfiles/valid/two-byte-utf-8/input | 1 + .../testfiles/valid/utf-8-string/input | 1 + .../utf-surrogate-four-byte-encoding/input | 1 + 121 files changed, 5085 insertions(+) create mode 100644 JSON/Makefile create mode 100644 JSON/include/Poco/JSON/Array.h create mode 100644 JSON/include/Poco/JSON/DefaultHandler.h create mode 100644 JSON/include/Poco/JSON/Handler.h create mode 100644 JSON/include/Poco/JSON/JSON.h create mode 100644 JSON/include/Poco/JSON/JSONException.h create mode 100644 JSON/include/Poco/JSON/Object.h create mode 100644 JSON/include/Poco/JSON/Parser.h create mode 100644 JSON/include/Poco/JSON/Query.h create mode 100644 JSON/include/Poco/JSON/Stringifier.h create mode 100644 JSON/include/Poco/JSON/Template.h create mode 100644 JSON/include/Poco/JSON/TemplateCache.h create mode 100644 JSON/src/Array.cpp create mode 100644 JSON/src/DefaultHandler.cpp create mode 100644 JSON/src/Handler.cpp create mode 100644 JSON/src/JSONException.cpp create mode 100644 JSON/src/Object.cpp create mode 100644 JSON/src/Parser.cpp create mode 100644 JSON/src/Query.cpp create mode 100644 JSON/src/Stringifier.cpp create mode 100644 JSON/src/Template.cpp create mode 100644 JSON/src/TemplateCache.cpp create mode 100644 JSON/testsuite/src/JSONTest.cpp create mode 100644 JSON/testsuite/testfiles/invalid-unicode/encoded-surrogate-half/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-after-backslash/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-array/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-bigger-int/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-escape/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-exponent/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-identifier/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-int/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-real-after-e/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/invalid-utf-8-in-string/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/lone-invalid-utf-8/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/lone-utf-8-continuation-byte/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/not-in-unicode-range/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/overlong-3-byte-encoding/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/overlong-4-byte-encoding/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/overlong-ascii-encoding/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/restricted-utf-8/input create mode 100644 JSON/testsuite/testfiles/invalid-unicode/truncated-utf-8/input create mode 100644 JSON/testsuite/testfiles/invalid/apostrophe/input create mode 100644 JSON/testsuite/testfiles/invalid/ascii-unicode-identifier/input create mode 100644 JSON/testsuite/testfiles/invalid/brace-comma/input create mode 100644 JSON/testsuite/testfiles/invalid/bracket-comma/input create mode 100644 JSON/testsuite/testfiles/invalid/bracket-one-comma/input create mode 100644 JSON/testsuite/testfiles/invalid/empty/input create mode 100644 JSON/testsuite/testfiles/invalid/escaped-null-byte-in-string/input create mode 100644 JSON/testsuite/testfiles/invalid/extra-comma-in-array/input create mode 100644 JSON/testsuite/testfiles/invalid/extra-comma-in-multiline-array/input create mode 100644 JSON/testsuite/testfiles/invalid/garbage-after-newline/input create mode 100644 JSON/testsuite/testfiles/invalid/garbage-at-the-end/input create mode 100644 JSON/testsuite/testfiles/invalid/integer-starting-with-zero/input create mode 100644 JSON/testsuite/testfiles/invalid/invalid-escape/input create mode 100644 JSON/testsuite/testfiles/invalid/invalid-identifier/input create mode 100644 JSON/testsuite/testfiles/invalid/invalid-negative-integer/input create mode 100644 JSON/testsuite/testfiles/invalid/invalid-negative-real/input create mode 100644 JSON/testsuite/testfiles/invalid/invalid-second-surrogate/input create mode 100644 JSON/testsuite/testfiles/invalid/lone-open-brace/input create mode 100644 JSON/testsuite/testfiles/invalid/lone-open-bracket/input create mode 100644 JSON/testsuite/testfiles/invalid/lone-second-surrogate/input create mode 100644 JSON/testsuite/testfiles/invalid/minus-sign-without-number/input create mode 100644 JSON/testsuite/testfiles/invalid/negative-integer-starting-with-zero/input create mode 100644 JSON/testsuite/testfiles/invalid/null-byte-in-string/input create mode 100644 JSON/testsuite/testfiles/invalid/null-byte-outside-string/input create mode 100644 JSON/testsuite/testfiles/invalid/null/input create mode 100644 JSON/testsuite/testfiles/invalid/object-apostrophes/input create mode 100644 JSON/testsuite/testfiles/invalid/object-garbage-at-end/input create mode 100644 JSON/testsuite/testfiles/invalid/object-in-unterminated-array/input create mode 100644 JSON/testsuite/testfiles/invalid/object-no-colon/input create mode 100644 JSON/testsuite/testfiles/invalid/object-no-value/input create mode 100644 JSON/testsuite/testfiles/invalid/object-unterminated-value/input create mode 100644 JSON/testsuite/testfiles/invalid/real-garbage-after-e/input create mode 100644 JSON/testsuite/testfiles/invalid/real-negative-overflow/input create mode 100644 JSON/testsuite/testfiles/invalid/real-positive-overflow/input create mode 100644 JSON/testsuite/testfiles/invalid/real-truncated-at-e/input create mode 100644 JSON/testsuite/testfiles/invalid/real-truncated-at-point/input create mode 100644 JSON/testsuite/testfiles/invalid/tab-character-in-string/input create mode 100644 JSON/testsuite/testfiles/invalid/too-big-negative-integer/input create mode 100644 JSON/testsuite/testfiles/invalid/too-big-positive-integer/input create mode 100644 JSON/testsuite/testfiles/invalid/truncated-unicode-surrogate/input create mode 100644 JSON/testsuite/testfiles/invalid/unicode-identifier/input create mode 100644 JSON/testsuite/testfiles/invalid/unterminated-array-and-object/input create mode 100644 JSON/testsuite/testfiles/invalid/unterminated-array/input create mode 100644 JSON/testsuite/testfiles/invalid/unterminated-empty-key/input create mode 100644 JSON/testsuite/testfiles/invalid/unterminated-key/input create mode 100644 JSON/testsuite/testfiles/invalid/unterminated-object-and-array/input create mode 100644 JSON/testsuite/testfiles/invalid/unterminated-string/input create mode 100644 JSON/testsuite/testfiles/valid/complex-array/input create mode 100644 JSON/testsuite/testfiles/valid/empty-array/input create mode 100644 JSON/testsuite/testfiles/valid/empty-object-in-array/input create mode 100644 JSON/testsuite/testfiles/valid/empty-object/input create mode 100644 JSON/testsuite/testfiles/valid/empty-string/input create mode 100644 JSON/testsuite/testfiles/valid/escaped-utf-control-char/input create mode 100644 JSON/testsuite/testfiles/valid/false/input create mode 100644 JSON/testsuite/testfiles/valid/negative-int/input create mode 100644 JSON/testsuite/testfiles/valid/negative-one/input create mode 100644 JSON/testsuite/testfiles/valid/negative-zero/input create mode 100644 JSON/testsuite/testfiles/valid/null/input create mode 100644 JSON/testsuite/testfiles/valid/one-byte-utf-8/input create mode 100644 JSON/testsuite/testfiles/valid/real-capital-e-negative-exponent/input create mode 100644 JSON/testsuite/testfiles/valid/real-capital-e-positive-exponent/input create mode 100644 JSON/testsuite/testfiles/valid/real-capital-e/input create mode 100644 JSON/testsuite/testfiles/valid/real-exponent/input create mode 100644 JSON/testsuite/testfiles/valid/real-fraction-exponent/input create mode 100644 JSON/testsuite/testfiles/valid/real-negative-exponent/input create mode 100644 JSON/testsuite/testfiles/valid/real-positive-exponent/input create mode 100644 JSON/testsuite/testfiles/valid/real-underflow/input create mode 100644 JSON/testsuite/testfiles/valid/short-string/input create mode 100644 JSON/testsuite/testfiles/valid/simple-ascii-string/input create mode 100644 JSON/testsuite/testfiles/valid/simple-int-0/input create mode 100644 JSON/testsuite/testfiles/valid/simple-int-1/input create mode 100644 JSON/testsuite/testfiles/valid/simple-int-123/input create mode 100644 JSON/testsuite/testfiles/valid/simple-object/input create mode 100644 JSON/testsuite/testfiles/valid/simple-real/input create mode 100644 JSON/testsuite/testfiles/valid/string-escapes/input create mode 100644 JSON/testsuite/testfiles/valid/three-byte-utf-8/input create mode 100644 JSON/testsuite/testfiles/valid/true/input create mode 100644 JSON/testsuite/testfiles/valid/two-byte-utf-8/input create mode 100644 JSON/testsuite/testfiles/valid/utf-8-string/input create mode 100644 JSON/testsuite/testfiles/valid/utf-surrogate-four-byte-encoding/input diff --git a/JSON/Makefile b/JSON/Makefile new file mode 100644 index 000000000..d5bf6fe41 --- /dev/null +++ b/JSON/Makefile @@ -0,0 +1,21 @@ +# +# Makefile +# +# $Id$ +# +# Makefile for Poco JSON +# + +include $(POCO_BASE)/build/rules/global + +INCLUDE += -I $(POCO_BASE)/JSON/include/Poco/JSON + +objects = Array Object Parser Handler \ + Stringifier DefaultHandler Query JSONException \ + Template TemplateCache + +target = PocoJSON +target_version = $(LIBVERSION) +target_libs = PocoFoundation + +include $(POCO_BASE)/build/rules/lib diff --git a/JSON/include/Poco/JSON/Array.h b/JSON/include/Poco/JSON/Array.h new file mode 100644 index 000000000..e0f75b51e --- /dev/null +++ b/JSON/include/Poco/JSON/Array.h @@ -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 +#include + +#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 ValueVector; + + + typedef SharedPtr 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 + T getElement(unsigned int index) const + /// Retrieves an element and tries to convert it to the + /// template type. The convert 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(); + } + + + SharedPtr 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 + 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(); + } + 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: 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 diff --git a/JSON/include/Poco/JSON/DefaultHandler.h b/JSON/include/Poco/JSON/DefaultHandler.h new file mode 100644 index 000000000..7af26bcbb --- /dev/null +++ b/JSON/include/Poco/JSON/DefaultHandler.h @@ -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 + +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 _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 diff --git a/JSON/include/Poco/JSON/Handler.h b/JSON/include/Poco/JSON/Handler.h new file mode 100644 index 000000000..6b32b5855 --- /dev/null +++ b/JSON/include/Poco/JSON/Handler.h @@ -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 diff --git a/JSON/include/Poco/JSON/JSON.h b/JSON/include/Poco/JSON/JSON.h new file mode 100644 index 000000000..7b30bd792 --- /dev/null +++ b/JSON/include/Poco/JSON/JSON.h @@ -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 diff --git a/JSON/include/Poco/JSON/JSONException.h b/JSON/include/Poco/JSON/JSONException.h new file mode 100644 index 000000000..1dba2f088 --- /dev/null +++ b/JSON/include/Poco/JSON/JSONException.h @@ -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 diff --git a/JSON/include/Poco/JSON/Object.h b/JSON/include/Poco/JSON/Object.h new file mode 100644 index 000000000..9056d30bd --- /dev/null +++ b/JSON/include/Poco/JSON/Object.h @@ -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 +#include +#include +#include + +#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 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 + 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 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(); + } + + void getNames(std::vector& 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 + 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(); + } + 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 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: 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 diff --git a/JSON/include/Poco/JSON/Parser.h b/JSON/include/Poco/JSON/Parser.h new file mode 100644 index 000000000..e685010ee --- /dev/null +++ b/JSON/include/Poco/JSON/Parser.h @@ -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 +#include + +#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 diff --git a/JSON/include/Poco/JSON/Query.h b/JSON/include/Poco/JSON/Query.h new file mode 100644 index 000000000..48e7d4c67 --- /dev/null +++ b/JSON/include/Poco/JSON/Query.h @@ -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 + 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(); + } + 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(path, def); + } + +private: + + DynamicAny _source; + +}; + +}} // Namespace JSON + +#endif // JSON_JSONQuery_INCLUDED diff --git a/JSON/include/Poco/JSON/Stringifier.h b/JSON/include/Poco/JSON/Stringifier.h new file mode 100644 index 000000000..80af2bb7f --- /dev/null +++ b/JSON/include/Poco/JSON/Stringifier.h @@ -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 + +#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 diff --git a/JSON/include/Poco/JSON/Template.h b/JSON/include/Poco/JSON/Template.h new file mode 100644 index 000000000..3d9c57b62 --- /dev/null +++ b/JSON/include/Poco/JSON/Template.h @@ -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 +#include + +#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 . + /// + /// These are the available commands: + /// + /// The result of the query is send to the output stream + /// This command can also be written as + /// + /// 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. + /// + /// 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 . + /// + /// 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. + /// + /// 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