- VS2012: build/tests

- JSON: style and exceptions catch by ref fix
- NumberParser: case-insensitive hex parsing
- Dynamic::Var: const std::string& cast operator specialization (coaxing std::string ctor)
- LocalDateTime: strftime fix (buffer too small for full tz string, use %Z instead of %z - makes no difference on windows but is more portable)
- small buildwin.cmd vsvars32.bat call fix
This commit is contained in:
Aleksandar Fabijanic 2012-09-24 03:51:40 +00:00
parent a6cc973fe3
commit d8397b9153
30 changed files with 3715 additions and 3739 deletions

View File

@ -156,6 +156,8 @@ public:
if (!_pHolder) if (!_pHolder)
throw InvalidAccessException("Can not convert empty value."); throw InvalidAccessException("Can not convert empty value.");
if (typeid(T) == _pHolder->type()) return extract<T>();
T result; T result;
_pHolder->convert(result); _pHolder->convert(result);
return result; return result;
@ -188,6 +190,11 @@ public:
} }
} }
operator const std::string & ();
/// Specialization of the cast operator for const std::string reference.
/// The main reason for this specialization is to help compilers
/// with construction/assignment of Var to std::string.
template <typename T> template <typename T>
const T& extract() const const T& extract() const
/// Returns a const reference to the actual value. /// Returns a const reference to the actual value.
@ -210,8 +217,6 @@ public:
typeid(T).name())); typeid(T).name()));
} }
template <typename T> template <typename T>
Var& operator = (const T& other) Var& operator = (const T& other)
/// Assignment operator for assigning POD to Var /// Assignment operator for assigning POD to Var
@ -558,6 +563,7 @@ private:
/// ///
/// Var members /// Var members
/// ///
inline void Var::swap(Var& ptr) inline void Var::swap(Var& ptr)
{ {
std::swap(_pHolder, ptr._pHolder); std::swap(_pHolder, ptr._pHolder);

View File

@ -263,7 +263,7 @@ private:
T n = 0; T n = 0;
for (; it != end; ++it) for (; it != end; ++it)
{ {
if ((*it >= '0' && *it <= '9') || (*it >= 'A' && *it <= 'F')) if ((*it >= '0' && *it <= '9') || (*it >= 'A' && *it <= 'F') || (*it >= 'a' && *it <= 'f'))
{ {
if (n > (std::numeric_limits<T>::max() / base)) if (n > (std::numeric_limits<T>::max() / base))
return false; return false;
@ -272,6 +272,8 @@ private:
n = n * base + *it - '0'; n = n * base + *it - '0';
else if (*it >= 'A' && *it <= 'F') else if (*it >= 'A' && *it <= 'F')
n = n * base + *it - 'A' + 10; n = n * base + *it - 'A' + 10;
else if (*it >= 'a' && *it <= 'f')
n = n * base + *it - 'a' + 10;
} }
else break; else break;
} }

View File

@ -69,6 +69,15 @@ Var::~Var()
} }
Var::operator const std::string & ()
{
if (typeid(std::string) != _pHolder->type())
*this = this->convert<std::string>();
return extract<std::string>();
}
Var& Var::operator = (const Var& other) Var& Var::operator = (const Var& other)
{ {
Var tmp(other); Var tmp(other);

View File

@ -396,23 +396,22 @@ void LocalDateTimeTest::testTimezone()
if (then.tm_isdst >= 0) if (then.tm_isdst >= 0)
{ {
std::string tzNow, tzThen; std::string tzNow, tzThen;
char tzBuf[12]; char tzBuf[48] = {0};
int iterations = 0; if (0 == std::strftime(&tzBuf[0], sizeof(tzBuf), "%Z", &then))
std::strftime(&tzBuf[0], sizeof(tzBuf), "%z", &then); fail ("Insufficient character array length.");
tzNow = tzThen = tzBuf; tzNow = tzThen = tzBuf;
int iterations = 0;
while (iterations < 14) while (iterations < 14)
{ {
// Add one month until the timezone changes or we roll // Add one month until the timezone changes or we roll
// over 13 months. // over 13 months.
t += tINCREMENT; t += tINCREMENT;
then = *std::localtime(&t); then = *std::localtime(&t);
std::strftime(&tzBuf[0], sizeof(tzBuf), "%z", &then); std::strftime(&tzBuf[0], sizeof(tzBuf), "%Z", &then);
tzThen = tzBuf; tzThen = tzBuf;
foundDST = (tzNow == tzThen); foundDST = (tzNow == tzThen);
if (foundDST) if (foundDST) break;
{
break;
}
++iterations; ++iterations;
} }
if (foundDST) if (foundDST)

View File

@ -73,7 +73,10 @@ void NumberParserTest::testParse()
assert(NumberParser::parse("-123") == -123); assert(NumberParser::parse("-123") == -123);
assert(NumberParser::parseUnsigned("123") == 123); assert(NumberParser::parseUnsigned("123") == 123);
assert(NumberParser::parseHex("12AB") == 0x12ab); assert(NumberParser::parseHex("12AB") == 0x12ab);
assert(NumberParser::parseHex("0X12AB") == 0x12ab);
assert(NumberParser::parseHex("0x12AB") == 0x12ab); assert(NumberParser::parseHex("0x12AB") == 0x12ab);
assert(NumberParser::parseHex("0x12aB") == 0x12ab);
assert(NumberParser::parseHex("0X98Fe") == 0x98fe);
assert(NumberParser::parseHex("0x0") == 0); assert(NumberParser::parseHex("0x0") == 0);
assert(NumberParser::parseHex("00") == 0); assert(NumberParser::parseHex("00") == 0);
assert(NumberParser::parseOct("123") == 0123); assert(NumberParser::parseOct("123") == 0123);

File diff suppressed because it is too large Load Diff

View File

@ -46,10 +46,8 @@
#include "Poco/SharedPtr.h" #include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h" #include "Poco/Dynamic/Var.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
class Object; class Object;
@ -58,37 +56,28 @@ class JSON_API Array
{ {
public: public:
typedef std::vector<DynamicAny> ValueVector; typedef std::vector<Dynamic::Var> ValueVector;
typedef SharedPtr<Array> Ptr; typedef SharedPtr<Array> Ptr;
Array(); Array();
/// Default constructor /// Default constructor
Array(const Array& copy); Array(const Array& copy);
/// Copy Constructor /// Copy Constructor
virtual ~Array(); virtual ~Array();
/// Destructor /// Destructor
ValueVector::const_iterator begin() const; ValueVector::const_iterator begin() const;
/// Returns iterator /// Returns iterator
ValueVector::const_iterator end() const; ValueVector::const_iterator end() const;
/// Returns iterator /// Returns iterator
Dynamic::Var get(unsigned int index) const;
DynamicAny get(unsigned int index) const;
/// Retrieves an element. Will return an empty value /// Retrieves an element. Will return an empty value
/// when the element doesn't exist. /// when the element doesn't exist.
Array::Ptr getArray(unsigned int index) const; Array::Ptr getArray(unsigned int index) const;
/// Retrieves an array. When the element is not /// Retrieves an array. When the element is not
/// an array or doesn't exist, an empty SharedPtr is returned. /// an array or doesn't exist, an empty SharedPtr is returned.
@ -101,11 +90,10 @@ public:
/// exceptions for invalid values. /// exceptions for invalid values.
/// Note: This will not work for an array or an object. /// Note: This will not work for an array or an object.
{ {
DynamicAny value = get(index); Dynamic::Var value = get(index);
return value.convert<T>(); return value.convert<T>();
} }
SharedPtr<Object> getObject(unsigned int index) const; SharedPtr<Object> getObject(unsigned int index) const;
/// Retrieves an object. When the element is not /// Retrieves an object. When the element is not
/// an object or doesn't exist, an empty SharedPtr is returned. /// an object or doesn't exist, an empty SharedPtr is returned.
@ -113,20 +101,16 @@ public:
unsigned int size() const; unsigned int size() const;
/// Returns the size of the array /// Returns the size of the array
bool isArray(unsigned int index) const; bool isArray(unsigned int index) const;
/// Returns true when the element is an array /// Returns true when the element is an array
bool isNull(unsigned int index) const; bool isNull(unsigned int index) const;
/// Returns true when the element is null or /// Returns true when the element is null or
/// when the element doesn't exist. /// when the element doesn't exist.
bool isObject(unsigned int index) const; bool isObject(unsigned int index) const;
/// Returns true when the element is an object /// Returns true when the element is an object
template<typename T> template<typename T>
T optElement(unsigned int index, const T& def) const T optElement(unsigned int index, const T& def) const
/// Returns the element at the given index. When /// Returns the element at the given index. When
@ -149,24 +133,20 @@ public:
return value; return value;
} }
void add(const Dynamic::Var& value)
void add(const DynamicAny& value)
/// Add the given value to the array /// Add the given value to the array
{ {
_values.push_back(value); _values.push_back(value);
} }
void stringify(std::ostream& out, unsigned int indent) const; void stringify(std::ostream& out, unsigned int indent) const;
/// Prints the array to out. When indent is 0, the array /// Prints the array to out. When indent is 0, the array
/// will be printed on one line without indentation. /// will be printed on one line without indentation.
void remove(unsigned int index); void remove(unsigned int index);
/// Removes the element on the given index. /// Removes the element on the given index.
private: private:
ValueVector _values; ValueVector _values;
}; };
@ -176,28 +156,32 @@ inline Array::ValueVector::const_iterator Array::begin() const
return _values.begin(); return _values.begin();
} }
inline Array::ValueVector::const_iterator Array::end() const inline Array::ValueVector::const_iterator Array::end() const
{ {
return _values.end(); return _values.end();
} }
inline unsigned int Array::size() const inline unsigned int Array::size() const
{ {
return _values.size(); return _values.size();
} }
inline bool Array::isArray(unsigned int index) const inline bool Array::isArray(unsigned int index) const
{ {
DynamicAny value = get(index); Dynamic::Var value = get(index);
return value.type() == typeid(Array::Ptr); return value.type() == typeid(Array::Ptr);
} }
inline bool Array::isNull(unsigned int index) const inline bool Array::isNull(unsigned int index) const
{ {
if ( index < _values.size() ) if ( index < _values.size() )
{ {
DynamicAny value = _values[index]; Dynamic::Var value = _values[index];
return value.isEmpty(); return value.isEmpty();
} }
return true; return true;
@ -211,11 +195,9 @@ inline void Array::remove(unsigned int index)
}} // Namespace Poco::JSON }} // Namespace Poco::JSON
namespace Poco
{
namespace Dynamic namespace Poco {
{ namespace Dynamic {
template <> template <>
class VarHolderImpl<JSON::Array::Ptr>: public VarHolder class VarHolderImpl<JSON::Array::Ptr>: public VarHolder
@ -355,8 +337,7 @@ private:
JSON::Array::Ptr _val; JSON::Array::Ptr _val;
}; };
} // Namespace Dynamic }} // namespace Poco::JSON
} // Namespace Poco
#endif // JSON_Array_INCLUDED #endif // JSON_Array_INCLUDED

View File

@ -40,13 +40,12 @@
#include "Poco/JSON/Handler.h" #include "Poco/JSON/Handler.h"
#include <stack> #include <stack>
namespace Poco
{ namespace Poco {
namespace JSON namespace JSON {
{
class JSON_API DefaultHandler : public Handler class JSON_API DefaultHandler : public Handler
/// Provides a default handler for the JSON parser. /// Provides a default handler for the JSON parser.
@ -55,82 +54,60 @@ class JSON_API DefaultHandler : public Handler
{ {
public: public:
DefaultHandler(); DefaultHandler();
/// Default Constructor /// Default Constructor
virtual ~DefaultHandler(); virtual ~DefaultHandler();
/// Destructor /// Destructor
void startObject(); void startObject();
/// Handles a {, meaning a new object will be read /// Handles a {, meaning a new object will be read
void endObject(); void endObject();
/// Handles a }, meaning the object is read /// Handles a }, meaning the object is read
void startArray(); void startArray();
/// Handles a [, meaning a new array will be read /// Handles a [, meaning a new array will be read
void endArray(); void endArray();
/// Handles a ], meaning the array is read /// Handles a ], meaning the array is read
void key(const std::string& k); void key(const std::string& k);
/// A key is read /// A key is read
Dynamic::Var result() const;
DynamicAny result() const;
/// Returns the result of the parser. Which is an object or an array. /// Returns the result of the parser. Which is an object or an array.
virtual void value(int v); virtual void value(int v);
/// An integer value is read /// An integer value is read
#if defined(POCO_HAVE_INT64) #if defined(POCO_HAVE_INT64)
virtual void value(Int64 v); virtual void value(Int64 v);
/// A 64-bit integer value is read /// A 64-bit integer value is read
#endif #endif
virtual void value(const std::string& s); virtual void value(const std::string& s);
/// A string value is read. /// A string value is read.
virtual void value(double d); virtual void value(double d);
/// A double value is read /// A double value is read
virtual void value(bool b); virtual void value(bool b);
/// A boolean value is read /// A boolean value is read
virtual void null(); virtual void null();
/// A null value is read /// A null value is read
private: private:
void setValue(const Poco::Dynamic::Var& value);
std::stack<Dynamic::Var> _stack;
void setValue(const Poco::DynamicAny& value); std::string _key;
Dynamic::Var _result;
std::stack<DynamicAny> _stack;
std::string _key;
DynamicAny _result;
}; };
inline DynamicAny DefaultHandler::result() const inline Dynamic::Var DefaultHandler::result() const
{ {
return _result; return _result;
} }
@ -141,6 +118,7 @@ inline void DefaultHandler::value(int v)
setValue(v); setValue(v);
} }
#if defined(POCO_HAVE_INT64) #if defined(POCO_HAVE_INT64)
inline void DefaultHandler::value(Int64 v) inline void DefaultHandler::value(Int64 v)
{ {
@ -169,11 +147,11 @@ inline void DefaultHandler::value(bool b)
inline void DefaultHandler::null() inline void DefaultHandler::null()
{ {
Poco::DynamicAny empty; Poco::Dynamic::Var empty;
setValue(empty); setValue(empty);
} }
}} // Namespace Poco::JSON }} // namespace Poco::JSON
#endif // JSON_DefaultHandler_INCLUDED #endif // JSON_DefaultHandler_INCLUDED

View File

@ -39,65 +39,51 @@
#define JSON_Handler_INCLUDED #define JSON_Handler_INCLUDED
#include "Poco/DynamicAny.h" #include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h" #include "Poco/JSON/JSON.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
class JSON_API Handler class JSON_API Handler
{ {
public: public:
virtual void startObject() = 0; virtual void startObject() = 0;
/// The parser has read a {, meaning a new object will be read /// The parser has read a {, meaning a new object will be read
virtual void endObject() = 0; virtual void endObject() = 0;
/// The parser has read a }, meaning the object is read /// The parser has read a }, meaning the object is read
virtual void startArray() = 0; virtual void startArray() = 0;
/// The parser has read a [, meaning a new array will be read /// The parser has read a [, meaning a new array will be read
virtual void endArray() = 0; virtual void endArray() = 0;
/// The parser has read a ], meaning the array is read /// The parser has read a ], meaning the array is read
virtual void key(const std::string& k) = 0; virtual void key(const std::string& k) = 0;
/// A key of an object is read /// A key of an object is read
virtual void null() = 0; virtual void null() = 0;
/// A null value is read /// A null value is read
virtual void value(int v) = 0; virtual void value(int v) = 0;
/// An integer value is read /// An integer value is read
#if defined(POCO_HAVE_INT64) #if defined(POCO_HAVE_INT64)
virtual void value(Int64 v) = 0; virtual void value(Int64 v) = 0;
/// A 64-bit integer value is read /// A 64-bit integer value is read
#endif #endif
virtual void value(const std::string& value) = 0; virtual void value(const std::string& value) = 0;
/// A string value is read. /// A string value is read.
virtual void value(double d) = 0; virtual void value(double d) = 0;
/// A double value is read /// A double value is read
virtual void value(bool b) = 0; virtual void value(bool b) = 0;
/// A boolean value is read /// A boolean value is read
protected: protected:
virtual ~Handler(); virtual ~Handler();
@ -107,6 +93,6 @@ private:
}; };
}} // Namespace Poco::JSON }} // namespace Poco::JSON
#endif // JSON_Handler_INCLUDED #endif // JSON_Handler_INCLUDED

View File

@ -54,16 +54,16 @@
// defined with this macro as being exported. // defined with this macro as being exported.
// //
#if defined(_WIN32) && defined(POCO_DLL) #if defined(_WIN32) && defined(POCO_DLL)
#if defined(JSON_EXPORTS) #if defined(JSON_EXPORTS)
#define JSON_API __declspec(dllexport) #define JSON_API __declspec(dllexport)
#else #else
#define JSON_API __declspec(dllimport) #define JSON_API __declspec(dllimport)
#endif #endif
#endif #endif
#if !defined(JSON_API) #if !defined(JSON_API)
#define JSON_API #define JSON_API
#endif #endif

View File

@ -43,13 +43,12 @@
#include "Poco/Exception.h" #include "Poco/Exception.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
POCO_DECLARE_EXCEPTION(JSON_API, JSONException, Poco::Exception) POCO_DECLARE_EXCEPTION(JSON_API, JSONException, Poco::Exception)
}} // Namespace Poco::JSON }} // namespace Poco::JSON
#endif //JSON_JSONException_INCLUDED #endif //JSON_JSONException_INCLUDED

View File

@ -50,10 +50,8 @@
#include "Poco/JSON/JSON.h" #include "Poco/JSON/JSON.h"
#include "Poco/JSON/Array.h" #include "Poco/JSON/Array.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
class JSON_API Object class JSON_API Object
@ -63,34 +61,29 @@ public:
typedef SharedPtr<Object> Ptr; typedef SharedPtr<Object> Ptr;
Object(); Object();
/// Default constructor /// Default constructor
Object(const Object& copy); Object(const Object& copy);
/// Copy constructor /// Copy constructor
virtual ~Object(); virtual ~Object();
/// Destructor /// Destructor
DynamicAny get(const std::string& key) const; Dynamic::Var get(const std::string& key) const;
/// Retrieves a property. An empty value is /// Retrieves a property. An empty value is
/// returned when the property doesn't exist. /// returned when the property doesn't exist.
Array::Ptr getArray(const std::string& key) const; Array::Ptr getArray(const std::string& key) const;
/// Returns a SharedPtr to an array when the property /// Returns a SharedPtr to an array when the property
/// is an array. An empty SharedPtr is returned when /// is an array. An empty SharedPtr is returned when
/// the element doesn't exist or is not an array. /// the element doesn't exist or is not an array.
Object::Ptr getObject(const std::string& key) const; Object::Ptr getObject(const std::string& key) const;
/// Returns a SharedPtr to an object when the property /// Returns a SharedPtr to an object when the property
/// is an object. An empty SharedPtr is returned when /// is an object. An empty SharedPtr is returned when
/// the property doesn't exist or is not an object /// the property doesn't exist or is not an object
template<typename T> template<typename T>
T getValue(const std::string& key) const T getValue(const std::string& key) const
/// Retrieves the property with the given name and will /// Retrieves the property with the given name and will
@ -99,30 +92,25 @@ public:
/// which can also throw exceptions for invalid values. /// which can also throw exceptions for invalid values.
/// Note: This will not work for an array or an object. /// Note: This will not work for an array or an object.
{ {
DynamicAny value = get(key); Dynamic::Var value = get(key);
return value.convert<T>(); return value.convert<T>();
} }
void getNames(std::vector<std::string>& names) const; void getNames(std::vector<std::string>& names) const;
/// Returns all property names /// Returns all property names
bool has(const std::string& key) const; bool has(const std::string& key) const;
/// Returns true when the given property exists /// Returns true when the given property exists
bool isArray(const std::string& key) const; bool isArray(const std::string& key) const;
/// Returns true when the given property contains an array /// Returns true when the given property contains an array
bool isNull(const std::string& key) const; bool isNull(const std::string& key) const;
/// Returns true when the given property contains a null value /// Returns true when the given property contains a null value
bool isObject(const std::string& key) const; bool isObject(const std::string& key) const;
/// Returns true when the given property contains an object /// Returns true when the given property contains an object
template<typename T> template<typename T>
T optValue(const std::string& key, const T& def) const T optValue(const std::string& key, const T& def) const
/// Returns the value of a property when the property exists /// Returns the value of a property when the property exists
@ -146,27 +134,21 @@ public:
return value; return value;
} }
unsigned int size() const; unsigned int size() const;
/// Returns the number of properties /// Returns the number of properties
void set(const std::string& key, const Dynamic::Var& value);
void set(const std::string& key, const DynamicAny& value);
/// Sets a new value /// Sets a new value
void stringify(std::ostream& out, unsigned int indent = 0) const; void stringify(std::ostream& out, unsigned int indent = 0) const;
/// Prints the object to out. When indent is 0, the object /// Prints the object to out. When indent is 0, the object
/// will be printed on one line without indentation. /// will be printed on one line without indentation.
void remove(const std::string& key); void remove(const std::string& key);
/// Removes the property with the given key /// Removes the property with the given key
private: private:
typedef std::map<std::string, Dynamic::Var> ValueMap;
typedef std::map<std::string, DynamicAny> ValueMap;
ValueMap _values; ValueMap _values;
}; };
@ -177,47 +159,52 @@ inline bool Object::has(const std::string& key) const
return it != _values.end(); return it != _values.end();
} }
inline bool Object::isArray(const std::string& key) const inline bool Object::isArray(const std::string& key) const
{ {
ValueMap::const_iterator it = _values.find(key); ValueMap::const_iterator it = _values.find(key);
return it != _values.end() || it->second.type() == typeid(Array::Ptr); return it != _values.end() || it->second.type() == typeid(Array::Ptr);
} }
inline bool Object::isNull(const std::string& key) const inline bool Object::isNull(const std::string& key) const
{ {
ValueMap::const_iterator it = _values.find(key); ValueMap::const_iterator it = _values.find(key);
return it == _values.end() || it->second.isEmpty(); return it == _values.end() || it->second.isEmpty();
} }
inline bool Object::isObject(const std::string& key) const inline bool Object::isObject(const std::string& key) const
{ {
ValueMap::const_iterator it = _values.find(key); ValueMap::const_iterator it = _values.find(key);
return it != _values.end() || it->second.type() == typeid(Object::Ptr); return it != _values.end() || it->second.type() == typeid(Object::Ptr);
} }
inline void Object::set(const std::string& key, const DynamicAny& value)
inline void Object::set(const std::string& key, const Dynamic::Var& value)
{ {
_values[key] = value; _values[key] = value;
} }
inline unsigned int Object::size() const inline unsigned int Object::size() const
{ {
return _values.size(); return _values.size();
} }
inline void Object::remove(const std::string& key) inline void Object::remove(const std::string& key)
{ {
_values.erase(key); _values.erase(key);
} }
}} // Namespace Poco::JSON }} // Namespace Poco::JSON
namespace Poco namespace Poco {
{ namespace Dynamic {
namespace Dynamic
{
template <> template <>
class VarHolderImpl<JSON::Object::Ptr>: public VarHolder class VarHolderImpl<JSON::Object::Ptr>: public VarHolder
@ -357,7 +344,7 @@ private:
JSON::Object::Ptr _val; JSON::Object::Ptr _val;
}; };
} }} // namespace Poco::JSON
}
#endif // JSON_Object_INCLUDED #endif // JSON_Object_INCLUDED

View File

@ -42,7 +42,7 @@
#include <istream> #include <istream>
#include <sstream> #include <sstream>
#include "Poco/DynamicAny.h" #include "Poco/Dynamic/Var.h"
#include "Poco/StreamTokenizer.h" #include "Poco/StreamTokenizer.h"
#include "Poco/JSON/JSON.h" #include "Poco/JSON/JSON.h"
@ -50,10 +50,9 @@
#include "Poco/JSON/Array.h" #include "Poco/JSON/Array.h"
#include "Poco/JSON/Handler.h" #include "Poco/JSON/Handler.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
class JSON_API Parser class JSON_API Parser
/// A class for passing JSON strings or streams /// A class for passing JSON strings or streams
@ -63,33 +62,26 @@ public:
Parser(); Parser();
/// Constructor /// Constructor
virtual ~Parser(); virtual ~Parser();
/// Destructor /// Destructor
void parse(const std::string& source); void parse(const std::string& source);
/// Parses a string /// Parses a string
void parse(std::istream& in); void parse(std::istream& in);
/// Parses a JSON from the input stream /// Parses a JSON from the input stream
void setHandler(Handler* handler); void setHandler(Handler* handler);
/// Set the handler /// Set the handler
Handler* getHandler(); Handler* getHandler();
/// Returns the handler /// Returns the handler
private: private:
const Token* nextToken(); const Token* nextToken();
/// Returns the next token /// Returns the next token
void readObject(); void readObject();
/// Starts reading an object /// Starts reading an object
@ -97,23 +89,17 @@ private:
void readArray(); void readArray();
/// Starts reading an array /// Starts reading an array
bool readRow(bool firstCall = false); bool readRow(bool firstCall = false);
/// Reads a property value pair. Returns true when a next row is expected. /// Reads a property value pair. Returns true when a next row is expected.
void readValue(const Token* token); void readValue(const Token* token);
/// Read a value from the token /// Read a value from the token
bool readElements(bool firstCall = false); bool readElements(bool firstCall = false);
/// Read all elements of an array /// Read all elements of an array
StreamTokenizer _tokenizer; StreamTokenizer _tokenizer;
Handler* _handler;
Handler* _handler;
}; };
@ -135,6 +121,8 @@ inline Handler* Parser::getHandler()
return _handler; return _handler;
} }
}} // Namespace Poco::JSON
}} // namespace Poco::JSON
#endif // JSON_JSONParser_INCLUDED #endif // JSON_JSONParser_INCLUDED

View File

@ -44,40 +44,34 @@
#include "Poco/JSON/Array.h" #include "Poco/JSON/Array.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
class JSON_API Query class JSON_API Query
/// Class that can be used to search for a value in a JSON object or array. /// Class that can be used to search for a value in a JSON object or array.
{ {
public: public:
Query(const DynamicAny& source); Query(const Dynamic::Var& source);
/// Constructor. Pass the start object/array. /// Constructor. Pass the start object/array.
virtual ~Query(); virtual ~Query();
/// Destructor /// Destructor
Object::Ptr findObject(const std::string& path) const; Object::Ptr findObject(const std::string& path) const;
/// Search for an object. When the object can't be found, an empty /// Search for an object. When the object can't be found, an empty
/// SharedPtr is returned. /// SharedPtr is returned.
Array::Ptr findArray(const std::string& path) const; Array::Ptr findArray(const std::string& path) const;
/// Search for an array. When the array can't be found, an empty /// Search for an array. When the array can't be found, an empty
/// SharedPtr is returned. /// SharedPtr is returned.
Dynamic::Var find(const std::string& path) const;
DynamicAny find(const std::string& path) const;
/// Searches a value /// Searches a value
/// For example: "person.children[0].name" will return the /// For example: "person.children[0].name" will return the
/// the name of the first child. When the value can't be found /// the name of the first child. When the value can't be found
/// an empty value is returned. /// an empty value is returned.
template<typename T> template<typename T>
T findValue(const std::string& path, const T& def) const T findValue(const std::string& path, const T& def) const
/// Searches for a value will convert it to the given type. /// Searches for a value will convert it to the given type.
@ -85,16 +79,14 @@ public:
/// the default value will be returned. /// the default value will be returned.
{ {
T result = def; T result = def;
DynamicAny value = find(path); Dynamic::Var value = find(path);
if ( ! value.isEmpty() ) if ( ! value.isEmpty() )
{ {
try try
{ {
result = value.convert<T>(); result = value.convert<T>();
} }
catch(...) catch(...) { }
{
}
} }
return result; return result;
} }
@ -108,11 +100,11 @@ public:
} }
private: private:
Dynamic::Var _source;
DynamicAny _source;
}; };
}} // Namespace JSON
}} // namespace Poco::JSON
#endif // JSON_JSONQuery_INCLUDED #endif // JSON_JSONQuery_INCLUDED

View File

@ -41,24 +41,24 @@
#include <iostream> #include <iostream>
#include "Poco/DynamicAny.h" #include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h" #include "Poco/JSON/JSON.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
class JSON_API Stringifier class JSON_API Stringifier
/// Helper class for creating a String from a JSON object or array /// Helper class for creating a String from a JSON object or array
{ {
public: public:
static void stringify(const DynamicAny& any, std::ostream& out, unsigned int indent = 0); static void stringify(const Dynamic::Var& any, std::ostream& out, unsigned int indent = 0);
/// Writes a String representation of the value to the output stream. /// Writes a String representation of the value to the output stream.
/// When indent is 0, the String will be created as small as possible. /// When indent is 0, the String will be created as small as possible.
}; };
}} // Namespace Poco::JSON
}} // namespace Poco::JSON
#endif // JSON_JSONStringifier_INCLUDED #endif // JSON_JSONStringifier_INCLUDED

View File

@ -43,21 +43,22 @@
#include <stack> #include <stack>
#include "Poco/JSON/JSON.h" #include "Poco/JSON/JSON.h"
#include "Poco/DynamicAny.h" #include "Poco/Dynamic/Var.h"
#include "Poco/SharedPtr.h" #include "Poco/SharedPtr.h"
#include "Poco/Path.h" #include "Poco/Path.h"
#include "Poco/Timestamp.h" #include "Poco/Timestamp.h"
namespace Poco namespace Poco {
{ namespace JSON {
namespace JSON
{
class MultiPart; class MultiPart;
POCO_DECLARE_EXCEPTION(JSON_API, JSONTemplateException, Poco::Exception) POCO_DECLARE_EXCEPTION(JSON_API, JSONTemplateException, Poco::Exception)
class JSON_API Template class JSON_API Template
/// Template is a template engine which uses JSON as input /// Template is a template engine which uses JSON as input
/// for generating output. There are commands for /// for generating output. There are commands for
@ -125,7 +126,7 @@ public:
/// Returns the time when the template was parsed /// Returns the time when the template was parsed
void render(const DynamicAny& data, std::ostream& out) const; void render(const Dynamic::Var& data, std::ostream& out) const;
/// Renders the template and send the output to the stream /// Renders the template and send the output to the stream
@ -177,6 +178,6 @@ inline Timestamp Template::parseTime() const
return _parseTime; return _parseTime;
} }
}} // Namespace Poco::JSON }} // namespace Poco::JSON
#endif // JSON_JSONTemplate_INCLUDED #endif // JSON_JSONTemplate_INCLUDED

View File

@ -45,13 +45,12 @@
#include "Poco/Path.h" #include "Poco/Path.h"
#include "Poco/SharedPtr.h" #include "Poco/SharedPtr.h"
#include "Poco/Logger.h" #include "Poco/Logger.h"
#include "Poco/JSON/Template.h" #include "Poco/JSON/Template.h"
namespace Poco
{ namespace Poco {
namespace JSON namespace JSON {
{
class JSON_API TemplateCache class JSON_API TemplateCache
/// Use to cache parsed templates. Templates are /// Use to cache parsed templates. Templates are
@ -66,16 +65,13 @@ public:
/// Constructor. The cache must be created /// Constructor. The cache must be created
/// and not destroyed as long as it is used. /// and not destroyed as long as it is used.
virtual ~TemplateCache(); virtual ~TemplateCache();
/// Destructor /// Destructor
void addPath(const Path& path); void addPath(const Path& path);
/// Add a path for resolving template paths. /// Add a path for resolving template paths.
/// The order of check is FIFO. /// The order of check is FIFO.
Template::Ptr getTemplate(const Path& path); Template::Ptr getTemplate(const Path& path);
/// Returns a template from the cache. /// Returns a template from the cache.
/// When the template file is not yet loaded /// When the template file is not yet loaded
@ -85,32 +81,20 @@ public:
/// even when the template isn't stored anymore in /// even when the template isn't stored anymore in
/// the cache. /// the cache.
static TemplateCache* instance(); static TemplateCache* instance();
/// Returns the only instance of this cache /// Returns the only instance of this cache
void setLogger(Logger& logger); void setLogger(Logger& logger);
/// Sets the logger for the cache. /// Sets the logger for the cache.
private: private:
static TemplateCache* _instance; static TemplateCache* _instance;
std::vector<Path> _includePaths;
std::vector<Path> _includePaths;
std::map<std::string, Template::Ptr> _cache; std::map<std::string, Template::Ptr> _cache;
Logger* _logger;
Logger* _logger;
void setup(); void setup();
Path resolvePath(const Path& path) const; Path resolvePath(const Path& path) const;
}; };

View File

@ -37,45 +37,52 @@
#include "Poco/JSON/Object.h" #include "Poco/JSON/Object.h"
#include "Poco/JSON/Stringifier.h" #include "Poco/JSON/Stringifier.h"
namespace Poco
{ using Poco::Dynamic::Var;
namespace JSON
{
namespace Poco {
namespace JSON {
Array::Array() Array::Array()
{ {
} }
Array::Array(const Array& copy) : _values(copy._values) Array::Array(const Array& copy) : _values(copy._values)
{ {
} }
Array::~Array() Array::~Array()
{ {
} }
DynamicAny Array::get(unsigned int index) const
Var Array::get(unsigned int index) const
{ {
DynamicAny value; Var value;
try try
{ {
value = _values.at(index); value = _values.at(index);
} }
catch(std::out_of_range) catch(std::out_of_range&)
{ {
//Ignore, we return an empty value //Ignore, we return an empty value
} }
return value; return value;
} }
Array::Ptr Array::getArray(unsigned int index) const Array::Ptr Array::getArray(unsigned int index) const
{ {
Array::Ptr result; Array::Ptr result;
DynamicAny value = get(index); Var value = get(index);
if ( value.type() == typeid(Array::Ptr) ) if ( value.type() == typeid(Array::Ptr) )
{ {
result = value.extract<Array::Ptr>(); result = value.extract<Array::Ptr>();
@ -88,7 +95,7 @@ Object::Ptr Array::getObject(unsigned int index) const
{ {
Object::Ptr result; Object::Ptr result;
DynamicAny value = get(index); Var value = get(index);
if ( value.type() == typeid(Object::Ptr) ) if ( value.type() == typeid(Object::Ptr) )
{ {
result = value.extract<Object::Ptr>(); result = value.extract<Object::Ptr>();
@ -96,9 +103,10 @@ Object::Ptr Array::getObject(unsigned int index) const
return result; return result;
} }
bool Array::isObject(unsigned int index) const bool Array::isObject(unsigned int index) const
{ {
DynamicAny value = get(index); Var value = get(index);
return value.type() == typeid(Object::Ptr); return value.type() == typeid(Object::Ptr);
} }
@ -144,4 +152,5 @@ void Array::stringify(std::ostream& out, unsigned int indent) const
out << "]"; out << "]";
} }
}} // Namespace Poco::JSON }} // Namespace Poco::JSON

View File

@ -36,15 +36,19 @@
#include "Poco/JSON/DefaultHandler.h" #include "Poco/JSON/DefaultHandler.h"
#include "Poco/JSON/Object.h" #include "Poco/JSON/Object.h"
namespace Poco
{ using Poco::Dynamic::Var;
namespace JSON
{
namespace Poco {
namespace JSON {
DefaultHandler::DefaultHandler() : Handler() DefaultHandler::DefaultHandler() : Handler()
{ {
} }
DefaultHandler::~DefaultHandler() DefaultHandler::~DefaultHandler()
{ {
} }
@ -60,7 +64,7 @@ void DefaultHandler::startObject()
} }
else else
{ {
DynamicAny parent = _stack.top(); Var parent = _stack.top();
if ( parent.type() == typeid(Array::Ptr) ) if ( parent.type() == typeid(Array::Ptr) )
{ {
@ -96,7 +100,7 @@ void DefaultHandler::startArray()
} }
else else
{ {
DynamicAny parent = _stack.top(); Var parent = _stack.top();
if ( parent.type() == typeid(Array::Ptr) ) if ( parent.type() == typeid(Array::Ptr) )
{ {
@ -128,9 +132,9 @@ void DefaultHandler::key(const std::string& k)
} }
void DefaultHandler::setValue(const Poco::DynamicAny& value) void DefaultHandler::setValue(const Var& value)
{ {
DynamicAny parent = _stack.top(); Var parent = _stack.top();
if ( parent.type() == typeid(Array::Ptr) ) if ( parent.type() == typeid(Array::Ptr) )
{ {

View File

@ -40,10 +40,12 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
namespace Poco
{ using Poco::Dynamic::Var;
namespace JSON
{
namespace Poco {
namespace JSON {
Object::Object() Object::Object()
@ -51,19 +53,21 @@ Object::Object()
} }
Object::Object(const Object& copy) : _values(copy._values) Object::Object(const Object& copy) : _values(copy._values)
{ {
} }
Object::~Object() Object::~Object()
{ {
} }
DynamicAny Object::get(const std::string& key) const Var Object::get(const std::string& key) const
{ {
DynamicAny value; Var value;
ValueMap::const_iterator it = _values.find(key); ValueMap::const_iterator it = _values.find(key);
if ( it != _values.end() ) if ( it != _values.end() )
@ -78,7 +82,7 @@ Array::Ptr Object::getArray(const std::string& key) const
{ {
Array::Ptr result; Array::Ptr result;
DynamicAny value = get(key); Var value = get(key);
if ( value.type() == typeid(Array::Ptr) ) if ( value.type() == typeid(Array::Ptr) )
{ {
result = value.extract<Array::Ptr>(); result = value.extract<Array::Ptr>();
@ -91,7 +95,7 @@ Object::Ptr Object::getObject(const std::string& key) const
{ {
Object::Ptr result; Object::Ptr result;
DynamicAny value = get(key); Var value = get(key);
if ( value.type() == typeid(Object::Ptr) ) if ( value.type() == typeid(Object::Ptr) )
{ {
result = value.extract<Object::Ptr>(); result = value.extract<Object::Ptr>();

File diff suppressed because it is too large Load Diff

View File

@ -41,24 +41,29 @@
#include "Poco/JSON/Query.h" #include "Poco/JSON/Query.h"
namespace Poco
{
namespace JSON
{
Query::Query(const DynamicAny& source) : _source(source) using Poco::Dynamic::Var;
namespace Poco {
namespace JSON {
Query::Query(const Var& source) : _source(source)
{ {
} }
Query::~Query() Query::~Query()
{ {
} }
Object::Ptr Query::findObject(const std::string& path) const Object::Ptr Query::findObject(const std::string& path) const
{ {
Object::Ptr obj; Object::Ptr obj;
DynamicAny result = find(path); Var result = find(path);
if ( result.type() == typeid(Object::Ptr) ) if ( result.type() == typeid(Object::Ptr) )
{ {
obj = result.extract<Object::Ptr>(); obj = result.extract<Object::Ptr>();
@ -66,10 +71,11 @@ Object::Ptr Query::findObject(const std::string& path) const
return obj; return obj;
} }
Array::Ptr Query::findArray(const std::string& path) const Array::Ptr Query::findArray(const std::string& path) const
{ {
Array::Ptr arr; Array::Ptr arr;
DynamicAny result = find(path); Var result = find(path);
if ( result.type() == typeid(Array::Ptr) ) if ( result.type() == typeid(Array::Ptr) )
{ {
arr = result.extract<Array::Ptr>(); arr = result.extract<Array::Ptr>();
@ -78,9 +84,9 @@ Array::Ptr Query::findArray(const std::string& path) const
} }
DynamicAny Query::find(const std::string& path) const Var Query::find(const std::string& path) const
{ {
DynamicAny result = _source; Var result = _source;
StringTokenizer tokenizer(path, "."); StringTokenizer tokenizer(path, ".");
for(StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++) for(StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
{ {
@ -138,4 +144,5 @@ DynamicAny Query::find(const std::string& path) const
return result; return result;
} }
}} // Namespace Poco::JSON }} // Namespace Poco::JSON

View File

@ -39,12 +39,15 @@
#include "Poco/JSON/Array.h" #include "Poco/JSON/Array.h"
#include "Poco/JSON/Object.h" #include "Poco/JSON/Object.h"
namespace Poco
{
namespace JSON
{
void Stringifier::stringify(const DynamicAny& any, std::ostream& out, unsigned int indent) using Poco::Dynamic::Var;
namespace Poco {
namespace JSON {
void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent)
{ {
if ( any.type() == typeid(Object::Ptr) ) if ( any.type() == typeid(Object::Ptr) )
{ {
@ -111,4 +114,5 @@ void Stringifier::stringify(const DynamicAny& any, std::ostream& out, unsigned i
} }
} }
}} // Namespace Poco::JSON }} // Namespace Poco::JSON

View File

@ -38,13 +38,17 @@
#include "Poco/JSON/TemplateCache.h" #include "Poco/JSON/TemplateCache.h"
#include "Poco/JSON/Query.h" #include "Poco/JSON/Query.h"
namespace Poco
{ using Poco::Dynamic::Var;
namespace JSON
{
namespace Poco {
namespace JSON {
POCO_IMPLEMENT_EXCEPTION(JSONTemplateException, Exception, "Template Exception") POCO_IMPLEMENT_EXCEPTION(JSONTemplateException, Exception, "Template Exception")
class Part class Part
{ {
public: public:
@ -58,11 +62,12 @@ public:
{ {
} }
virtual void render(const DynamicAny& data, std::ostream& out) const = 0; virtual void render(const Var& data, std::ostream& out) const = 0;
typedef std::vector<SharedPtr<Part> > VectorParts; typedef std::vector<SharedPtr<Part> > VectorParts;
}; };
class StringPart : public Part class StringPart : public Part
{ {
public: public:
@ -81,7 +86,7 @@ public:
} }
void render(const DynamicAny& data, std::ostream& out) const void render(const Var& data, std::ostream& out) const
{ {
out << _content; out << _content;
} }
@ -103,6 +108,7 @@ private:
std::string _content; std::string _content;
}; };
class MultiPart : public Part class MultiPart : public Part
{ {
public: public:
@ -125,7 +131,7 @@ public:
} }
void render(const DynamicAny& data, std::ostream& out) const void render(const Var& data, std::ostream& out) const
{ {
for(VectorParts::const_iterator it = _parts.begin(); it != _parts.end(); ++it) for(VectorParts::const_iterator it = _parts.begin(); it != _parts.end(); ++it)
{ {
@ -138,6 +144,7 @@ protected:
VectorParts _parts; VectorParts _parts;
}; };
class EchoPart : public Part class EchoPart : public Part
{ {
public: public:
@ -151,10 +158,10 @@ public:
} }
void render(const DynamicAny& data, std::ostream& out) const void render(const Var& data, std::ostream& out) const
{ {
Query query(data); Query query(data);
DynamicAny value = query.find(_query); Var value = query.find(_query);
if ( ! value.isEmpty() ) if ( ! value.isEmpty() )
{ {
@ -167,6 +174,7 @@ private:
std::string _query; std::string _query;
}; };
class LogicQuery class LogicQuery
{ {
public: public:
@ -178,18 +186,18 @@ public:
{ {
} }
virtual bool apply(const DynamicAny& data) const virtual bool apply(const Var& data) const
{ {
bool logic = false; bool logic = false;
Query query(data); Query query(data);
DynamicAny value = query.find(_queryString); Var value = query.find(_queryString);
if ( ! value.isEmpty() ) // When empty, logic will be false if ( ! value.isEmpty() ) // When empty, logic will be false
{ {
if ( value.isString() ) if ( value.isString() )
// An empty string must result in false, otherwise true // An empty string must result in false, otherwise true
// Which is not the case when we convert to bool with DynamicAny // Which is not the case when we convert to bool with Var
{ {
std::string s = value.convert<std::string>(); std::string s = value.convert<std::string>();
logic = ! s.empty(); logic = ! s.empty();
@ -198,7 +206,7 @@ public:
{ {
// All other values, try to convert to bool // All other values, try to convert to bool
// An empty object or array will turn into false // An empty object or array will turn into false
// all other values depend on the convert<> in DynamicAny // all other values depend on the convert<> in Var
logic = value.convert<bool>(); logic = value.convert<bool>();
} }
} }
@ -210,6 +218,7 @@ protected:
std::string _queryString; std::string _queryString;
}; };
class LogicExistQuery : public LogicQuery class LogicExistQuery : public LogicQuery
{ {
public: public:
@ -221,10 +230,10 @@ public:
{ {
} }
virtual bool apply(const DynamicAny& data) const virtual bool apply(const Var& data) const
{ {
Query query(data); Query query(data);
DynamicAny value = query.find(_queryString); Var value = query.find(_queryString);
return !value.isEmpty(); return !value.isEmpty();
} }
@ -242,7 +251,7 @@ public:
{ {
} }
virtual bool apply(const DynamicAny& data) const virtual bool apply(const Var& data) const
{ {
return true; return true;
} }
@ -273,7 +282,7 @@ public:
_queries.push_back(new LogicElseQuery()); _queries.push_back(new LogicElseQuery());
} }
void render(const DynamicAny& data, std::ostream& out) const void render(const Var& data, std::ostream& out) const
{ {
int count = 0; int count = 0;
for(std::vector<SharedPtr<LogicQuery> >::const_iterator it = _queries.begin(); it != _queries.end(); ++it, ++count) for(std::vector<SharedPtr<LogicQuery> >::const_iterator it = _queries.begin(); it != _queries.end(); ++it, ++count)
@ -291,6 +300,7 @@ private:
std::vector<SharedPtr<LogicQuery> > _queries; std::vector<SharedPtr<LogicQuery> > _queries;
}; };
class LoopPart : public MultiPart class LoopPart : public MultiPart
{ {
public: public:
@ -304,7 +314,7 @@ public:
} }
void render(const DynamicAny& data, std::ostream& out) const void render(const Var& data, std::ostream& out) const
{ {
Query query(data); Query query(data);
@ -316,7 +326,7 @@ public:
{ {
for(int i = 0; i < array->size(); i++) for(int i = 0; i < array->size(); i++)
{ {
DynamicAny value = array->get(i); Var value = array->get(i);
dataObject->set(_name, value); dataObject->set(_name, value);
MultiPart::render(data, out); MultiPart::render(data, out);
} }
@ -361,7 +371,7 @@ public:
} }
void render(const DynamicAny& data, std::ostream& out) const void render(const Var& data, std::ostream& out) const
{ {
TemplateCache* cache = TemplateCache::instance(); TemplateCache* cache = TemplateCache::instance();
if ( cache == NULL ) if ( cache == NULL )
@ -383,12 +393,14 @@ private:
}; };
Template::Template(const Path& templatePath) Template::Template(const Path& templatePath)
: _parts(NULL) : _parts(NULL)
, _templatePath(templatePath) , _templatePath(templatePath)
{ {
} }
Template::Template() Template::Template()
: _parts(NULL) : _parts(NULL)
{ {
@ -713,6 +725,7 @@ void Template::readWhiteSpace(std::istream& in)
} }
} }
std::string Template::readString(std::istream& in) std::string Template::readString(std::istream& in)
{ {
std::string str; std::string str;
@ -728,9 +741,11 @@ std::string Template::readString(std::istream& in)
return str; return str;
} }
void Template::render(const DynamicAny& data, std::ostream& out) const
void Template::render(const Var& data, std::ostream& out) const
{ {
_parts->render(data, out); _parts->render(data, out);
} }
}} // Namespace Poco::JSON }} // Namespace Poco::JSON

View File

@ -94,7 +94,7 @@ Template::Ptr TemplateCache::getTemplate(const Path& path)
tpl->parse(); tpl->parse();
_cache[templatePathname] = tpl; _cache[templatePathname] = tpl;
} }
catch(JSONTemplateException jte) catch(JSONTemplateException& jte)
{ {
if ( _logger ) if ( _logger )
{ {
@ -128,7 +128,7 @@ Template::Ptr TemplateCache::getTemplate(const Path& path)
tpl->parse(); tpl->parse();
_cache[templatePathname] = tpl; _cache[templatePathname] = tpl;
} }
catch(JSONTemplateException jte) catch(JSONTemplateException& jte)
{ {
if ( _logger ) if ( _logger )
{ {

View File

@ -40,7 +40,6 @@
#include "Poco/JSON/JSONException.h" #include "Poco/JSON/JSONException.h"
#include "Poco/JSON/Stringifier.h" #include "Poco/JSON/Stringifier.h"
#include "Poco/JSON/DefaultHandler.h" #include "Poco/JSON/DefaultHandler.h"
//#include "Poco/Util/JSONConfiguration.h"
#include "Poco/JSON/Template.h" #include "Poco/JSON/Template.h"
#include "Poco/Path.h" #include "Poco/Path.h"
@ -51,6 +50,9 @@
#include <set> #include <set>
using namespace Poco::JSON;
using namespace Poco::Dynamic;
JSONTest::JSONTest(const std::string& name): CppUnit::TestCase("JSON") JSONTest::JSONTest(const std::string& name): CppUnit::TestCase("JSON")
{ {
@ -77,26 +79,26 @@ void JSONTest::tearDown()
void JSONTest::testNullProperty() void JSONTest::testNullProperty()
{ {
std::string json = "{ \"test\" : null }"; std::string json = "{ \"test\" : null }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
assert(object->isNull("test")); assert(object->isNull("test"));
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.isEmpty()); assert(test.isEmpty());
} }
@ -104,26 +106,26 @@ void JSONTest::testNullProperty()
void JSONTest::testTrueProperty() void JSONTest::testTrueProperty()
{ {
std::string json = "{ \"test\" : true }"; std::string json = "{ \"test\" : true }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.type() == typeid(bool)); assert(test.type() == typeid(bool));
bool value = test; bool value = test;
assert(value); assert(value);
@ -133,26 +135,26 @@ void JSONTest::testTrueProperty()
void JSONTest::testFalseProperty() void JSONTest::testFalseProperty()
{ {
std::string json = "{ \"test\" : false }"; std::string json = "{ \"test\" : false }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.type() == typeid(bool)); assert(test.type() == typeid(bool));
bool value = test; bool value = test;
assert(!value); assert(!value);
@ -162,26 +164,26 @@ void JSONTest::testFalseProperty()
void JSONTest::testNumberProperty() void JSONTest::testNumberProperty()
{ {
std::string json = "{ \"test\" : 1969 }"; std::string json = "{ \"test\" : 1969 }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.isInteger()); assert(test.isInteger());
int value = test; int value = test;
assert(value == 1969); assert(value == 1969);
@ -191,26 +193,26 @@ void JSONTest::testNumberProperty()
void JSONTest::testStringProperty() void JSONTest::testStringProperty()
{ {
std::string json = "{ \"test\" : \"value\" }"; std::string json = "{ \"test\" : \"value\" }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.isString()); assert(test.isString());
std::string value = test; std::string value = test;
assert(value.compare("value") == 0); assert(value.compare("value") == 0);
@ -220,25 +222,25 @@ void JSONTest::testStringProperty()
void JSONTest::testEmptyObject() void JSONTest::testEmptyObject()
{ {
std::string json = "{}"; std::string json = "{}";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
assert(object->size() == 0); assert(object->size() == 0);
} }
@ -246,26 +248,26 @@ void JSONTest::testEmptyObject()
void JSONTest::testDoubleProperty() void JSONTest::testDoubleProperty()
{ {
std::string json = "{ \"test\" : 123.45 }"; std::string json = "{ \"test\" : 123.45 }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.isNumeric()); assert(test.isNumeric());
double value = test; double value = test;
assert(value == 123.45); assert(value == 123.45);
@ -275,26 +277,26 @@ void JSONTest::testDoubleProperty()
void JSONTest::testDouble2Property() void JSONTest::testDouble2Property()
{ {
std::string json = "{ \"test\" : 12e34 }"; std::string json = "{ \"test\" : 12e34 }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.isNumeric()); assert(test.isNumeric());
double value = test; double value = test;
assert(value == 12e34); assert(value == 12e34);
@ -304,26 +306,26 @@ void JSONTest::testDouble2Property()
void JSONTest::testDouble3Property() void JSONTest::testDouble3Property()
{ {
std::string json = "{ \"test\" : 12e-34 }"; std::string json = "{ \"test\" : 12e-34 }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.isNumeric()); assert(test.isNumeric());
double value = test; double value = test;
assert(value == 12e-34); assert(value == 12e-34);
@ -333,28 +335,28 @@ void JSONTest::testDouble3Property()
void JSONTest::testObjectProperty() void JSONTest::testObjectProperty()
{ {
std::string json = "{ \"test\" : { \"property\" : \"value\" } }"; std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
Poco::DynamicAny test = object->get("test"); Var test = object->get("test");
assert(test.type() == typeid(Poco::JSON::Object::Ptr)); assert(test.type() == typeid(Object::Ptr));
object = test.extract<Poco::JSON::Object::Ptr>(); object = test.extract<Object::Ptr>();
test = object->get("property"); test = object->get("property");
assert(test.isString()); assert(test.isString());
@ -366,25 +368,25 @@ void JSONTest::testObjectProperty()
void JSONTest::testEmptyArray() void JSONTest::testEmptyArray()
{ {
std::string json = "[]"; std::string json = "[]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
assert(array->size() == 0); assert(array->size() == 0);
} }
@ -392,25 +394,25 @@ void JSONTest::testEmptyArray()
void JSONTest::testNestedArray() void JSONTest::testNestedArray()
{ {
std::string json = "[[[[]]]]"; std::string json = "[[[[]]]]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
assert(array->size() == 1); assert(array->size() == 1);
} }
@ -418,27 +420,27 @@ void JSONTest::testNestedArray()
void JSONTest::testNullElement() void JSONTest::testNullElement()
{ {
std::string json = "[ null ]"; std::string json = "[ null ]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
assert(array->isNull(0)); assert(array->isNull(0));
Poco::DynamicAny test = array->get(0); Var test = array->get(0);
assert(test.isEmpty()); assert(test.isEmpty());
} }
@ -446,26 +448,26 @@ void JSONTest::testNullElement()
void JSONTest::testTrueElement() void JSONTest::testTrueElement()
{ {
std::string json = "[ true ]"; std::string json = "[ true ]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
Poco::DynamicAny test = array->get(0); Var test = array->get(0);
assert(test.type() == typeid(bool)); assert(test.type() == typeid(bool));
bool value = test; bool value = test;
assert(value); assert(value);
@ -475,26 +477,26 @@ void JSONTest::testTrueElement()
void JSONTest::testFalseElement() void JSONTest::testFalseElement()
{ {
std::string json = "[ false ]"; std::string json = "[ false ]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
Poco::DynamicAny test = array->get(0); Var test = array->get(0);
assert(test.type() == typeid(bool)); assert(test.type() == typeid(bool));
bool value = test; bool value = test;
assert(!value); assert(!value);
@ -504,26 +506,26 @@ void JSONTest::testFalseElement()
void JSONTest::testNumberElement() void JSONTest::testNumberElement()
{ {
std::string json = "[ 1969 ]"; std::string json = "[ 1969 ]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
Poco::DynamicAny test = array->get(0); Var test = array->get(0);
assert(test.isInteger()); assert(test.isInteger());
int value = test; int value = test;
assert(value == 1969); assert(value == 1969);
@ -533,26 +535,26 @@ void JSONTest::testNumberElement()
void JSONTest::testStringElement() void JSONTest::testStringElement()
{ {
std::string json = "[ \"value\" ]"; std::string json = "[ \"value\" ]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
Poco::DynamicAny test = array->get(0); Var test = array->get(0);
assert(test.isString()); assert(test.isString());
std::string value = test; std::string value = test;
assert(value.compare("value") == 0); assert(value.compare("value") == 0);
@ -562,26 +564,26 @@ void JSONTest::testStringElement()
void JSONTest::testEmptyObjectElement() void JSONTest::testEmptyObjectElement()
{ {
std::string json = "[{}]"; std::string json = "[{}]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
Poco::JSON::Object::Ptr object = array->getObject(0); Object::Ptr object = array->getObject(0);
assert(object->size() == 0); assert(object->size() == 0);
} }
@ -589,26 +591,26 @@ void JSONTest::testEmptyObjectElement()
void JSONTest::testDoubleElement() void JSONTest::testDoubleElement()
{ {
std::string json = "[ 123.45 ]"; std::string json = "[ 123.45 ]";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Array::Ptr)); assert(result.type() == typeid(Array::Ptr));
Poco::JSON::Array::Ptr array = result.extract<Poco::JSON::Array::Ptr>(); Array::Ptr array = result.extract<Array::Ptr>();
Poco::DynamicAny test = array->get(0); Var test = array->get(0);
assert(test.isNumeric()); assert(test.isNumeric());
double value = test; double value = test;
assert(value == 123.45); assert(value == 123.45);
@ -618,25 +620,25 @@ void JSONTest::testDoubleElement()
void JSONTest::testOptValue() void JSONTest::testOptValue()
{ {
std::string json = "{ }"; std::string json = "{ }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); Object::Ptr object = result.extract<Object::Ptr>();
int n = object->optValue("test", 123); int n = object->optValue("test", 123);
assert(n == 123); assert(n == 123);
} }
@ -645,25 +647,25 @@ void JSONTest::testOptValue()
void JSONTest::testQuery() void JSONTest::testQuery()
{ {
std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }"; std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }";
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(json); parser.parse(json);
result = handler.result(); result = handler.result();
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
std::cout << jsone.message() << std::endl; std::cout << jsone.message() << std::endl;
assert(false); assert(false);
} }
assert(result.type() == typeid(Poco::JSON::Object::Ptr)); assert(result.type() == typeid(Object::Ptr));
Poco::JSON::Query query(result); Query query(result);
std::string firstChild = query.findValue("children[0]", ""); std::string firstChild = query.findValue("children[0]", "");
assert(firstChild.compare("Jonas") == 0); assert(firstChild.compare("Jonas") == 0);
@ -689,21 +691,28 @@ void JSONTest::testValidJanssonFiles()
Poco::FileInputStream fis(filePath.toString()); Poco::FileInputStream fis(filePath.toString());
std::cout << filePath.toString() << std::endl; std::cout << filePath.toString() << std::endl;
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(fis); parser.parse(fis);
result = handler.result(); result = handler.result();
std::cout << "Ok!" << std::endl; std::cout << "Ok!" << std::endl;
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException& jsone)
{ {
// We shouldn't get here. std::string err = jsone.displayText();
assert(false); std::cout << "Failed:" << err << std::endl;
fail (err);
}
catch(Poco::Exception& e)
{
std::string err = e.displayText();
std::cout << "Failed:" << err << std::endl;
fail (err);
} }
} }
} }
@ -730,28 +739,25 @@ void JSONTest::testInvalidJanssonFiles()
Poco::FileInputStream fis(filePath.toString()); Poco::FileInputStream fis(filePath.toString());
std::cout << filePath.toString() << std::endl; std::cout << filePath.toString() << std::endl;
Poco::JSON::Parser parser; Parser parser;
Poco::DynamicAny result; Var result;
try try
{ {
Poco::JSON::DefaultHandler handler; DefaultHandler handler;
parser.setHandler(&handler); parser.setHandler(&handler);
parser.parse(fis); parser.parse(fis);
result = handler.result(); result = handler.result();
// We shouldn't get here. // We shouldn't get here.
std::cout << "We didn't get an exception. This is the result: " << result.convert<std::string>() << std::endl; std::cout << "We didn't get an exception. This is the result: " << result.convert<std::string>() << std::endl;
assert(false); fail(result.convert<std::string>());
} }
catch(Poco::JSON::JSONException jsone) catch(JSONException&)
{ {
std::cout << "Ok! We got an exception " << jsone.message() << std::endl;
continue; continue;
}
catch(Poco::SyntaxException se)
{
std::cout << "Ok! We got an exception " << se.message() << std::endl;
} }
catch(Poco::SyntaxException&)
{ }
} }
} }
} }
@ -760,11 +766,11 @@ void JSONTest::testInvalidJanssonFiles()
void JSONTest::testTemplate() void JSONTest::testTemplate()
{ {
Poco::JSON::Template tpl; Template tpl;
tpl.parse("Hello world! From <?= person.name ?>\n<?if person.toOld ?>You're to old<?endif?>\n"); tpl.parse("Hello world! From <?= person.name ?>\n<?if person.toOld ?>You're to old<?endif?>\n");
Poco::JSON::Object::Ptr data = new Poco::JSON::Object(); Object::Ptr data = new Object();
Poco::JSON::Object::Ptr person = new Poco::JSON::Object(); Object::Ptr person = new Object();
data->set("person", person); data->set("person", person);
person->set("name", "Franky"); person->set("name", "Franky");
person->set("toOld", true); person->set("toOld", true);

View File

@ -42,8 +42,12 @@
#define PDF_PDF_INCLUDED #define PDF_PDF_INCLUDED
#if defined(_MSC_VER) && !defined(POCO_MSVC_SECURE_WARNINGS) && (!defined(_CRT_SECURE_NO_WARNINGS) || !defined(_CRT_SECURE_NO_DEPRECATE)) #if defined(_MSC_VER) && !defined(POCO_MSVC_SECURE_WARNINGS) && (!defined(_CRT_SECURE_NO_WARNINGS) || !defined(_CRT_SECURE_NO_DEPRECATE))
#define _CRT_SECURE_NO_WARNINGS #ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE #define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#endif #endif
#include "Poco/Foundation.h" #include "Poco/Foundation.h"

View File

@ -450,8 +450,8 @@ std::string AbstractConfiguration::uncheckedExpand(const std::string& value) con
int AbstractConfiguration::parseInt(const std::string& value) int AbstractConfiguration::parseInt(const std::string& value)
{ {
if (value.compare(0, 2, "0x") == 0) if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex(value.substr(2)); return NumberParser::parseHex(value);
else else
return NumberParser::parse(value); return NumberParser::parse(value);
} }
@ -459,8 +459,8 @@ int AbstractConfiguration::parseInt(const std::string& value)
int AbstractConfiguration::parseUInt(const std::string& value) int AbstractConfiguration::parseUInt(const std::string& value)
{ {
if (value.compare(0, 2, "0x") == 0) if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex(value.substr(2)); return NumberParser::parseHex(value);
else else
return NumberParser::parseUnsigned(value); return NumberParser::parseUnsigned(value);
} }
@ -468,8 +468,8 @@ int AbstractConfiguration::parseUInt(const std::string& value)
Int64 AbstractConfiguration::parseInt64(const std::string& value) Int64 AbstractConfiguration::parseInt64(const std::string& value)
{ {
if (value.compare(0, 2, "0x") == 0) if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex64(value.substr(2)); return NumberParser::parseHex64(value);
else else
return NumberParser::parse64(value); return NumberParser::parse64(value);
} }
@ -477,8 +477,8 @@ Int64 AbstractConfiguration::parseInt64(const std::string& value)
UInt64 AbstractConfiguration::parseUInt64(const std::string& value) UInt64 AbstractConfiguration::parseUInt64(const std::string& value)
{ {
if (value.compare(0, 2, "0x") == 0) if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex64(value.substr(2)); return NumberParser::parseHex64(value);
else else
return NumberParser::parseUnsigned64(value); return NumberParser::parseUnsigned64(value);
} }

View File

@ -1,56 +1,57 @@
// //
// JSONConfigurationTest.cpp // JSONConfigurationTest.cpp
// //
// $Id$ // $Id$
// //
// Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH. // Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // 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 // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // 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 // 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 // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // 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 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#include "JSONConfigurationTest.h" #include "JSONConfigurationTest.h"
#include "CppUnit/TestCaller.h" #include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h" #include "CppUnit/TestSuite.h"
#include "Poco/Util/JSONConfiguration.h" #include "Poco/Util/JSONConfiguration.h"
#include "Poco/JSON/JSONException.h" #include "Poco/JSON/JSONException.h"
using Poco::Util::JSONConfiguration; using Poco::Util::JSONConfiguration;
using Poco::Util::AbstractConfiguration; using Poco::Util::AbstractConfiguration;
using Poco::AutoPtr; using Poco::AutoPtr;
using Poco::NotImplementedException; using Poco::NotImplementedException;
using Poco::NotFoundException; using Poco::NotFoundException;
using Poco::JSON::JSONException; using Poco::JSON::JSONException;
JSONConfigurationTest::JSONConfigurationTest(const std::string& name) : AbstractConfigurationTest(name) JSONConfigurationTest::JSONConfigurationTest(const std::string& name) : AbstractConfigurationTest(name)
{ {
} }
JSONConfigurationTest::~JSONConfigurationTest() JSONConfigurationTest::~JSONConfigurationTest()
{ {
} }
void JSONConfigurationTest::testLoad() void JSONConfigurationTest::testLoad()
{ {
JSONConfiguration config; JSONConfiguration config;
@ -100,29 +101,29 @@ void JSONConfigurationTest::testLoad()
} }
} }
AbstractConfiguration* JSONConfigurationTest::allocConfiguration() const AbstractConfiguration* JSONConfigurationTest::allocConfiguration() const
{ {
return new JSONConfiguration; return new JSONConfiguration;
} }
void JSONConfigurationTest::setUp() void JSONConfigurationTest::setUp()
{ {
} }
void JSONConfigurationTest::tearDown() void JSONConfigurationTest::tearDown()
{ {
} }
CppUnit::Test* JSONConfigurationTest::suite() CppUnit::Test* JSONConfigurationTest::suite()
{ {
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONConfigurationTest"); CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONConfigurationTest");
AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest); AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest);
CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad); CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad);
return pSuite; return pSuite;
} }

View File

@ -52,16 +52,16 @@ if "%1"=="" goto usage
set VS_VERSION=vs%1 set VS_VERSION=vs%1
if not defined VCINSTALLDIR ( if not defined VCINSTALLDIR (
if %1==71 (set VSENV="%VS71COMNTOOLS%vsvars32.bat") else ( if %1==71 (call "%VS71COMNTOOLS%vsvars32.bat") else (
if %1==80 (set VSENV="%VS80COMNTOOLS%vsvars32.bat") else ( if %1==80 (call "%VS80COMNTOOLS%vsvars32.bat") else (
if %1==90 (set VSENV="%VS90COMNTOOLS%vsvars32.bat") else ( if %1==90 (call "%VS90COMNTOOLS%vsvars32.bat") else (
if %1==100 (set VSENV="%VS100COMNTOOLS%vsvars32.bat") if %1==100 (call "%VS100COMNTOOLS%vsvars32.bat")
)))) ))))
call %VSENV%
if not defined VSINSTALLDIR ( if not defined VSINSTALLDIR (
echo Error: No Visual C++ environment found. echo Error: No Visual C++ environment found.
echo Please run this script from a Visual Studio Command Prompt echo Please run this script from a Visual Studio Command Prompt
echo or run "%%VSnnCOMNTOOLS%%\vcvars32.bat" first. echo or run "%%VSnnCOMNTOOLS%%\vsvars32.bat" first.
goto :EOF goto :EOF
) )
) )