- 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)
throw InvalidAccessException("Can not convert empty value.");
if (typeid(T) == _pHolder->type()) return extract<T>();
T result;
_pHolder->convert(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>
const T& extract() const
/// Returns a const reference to the actual value.
@ -210,8 +217,6 @@ public:
typeid(T).name()));
}
template <typename T>
Var& operator = (const T& other)
/// Assignment operator for assigning POD to Var
@ -558,6 +563,7 @@ private:
///
/// Var members
///
inline void Var::swap(Var& ptr)
{
std::swap(_pHolder, ptr._pHolder);

View File

@ -263,7 +263,7 @@ private:
T n = 0;
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))
return false;
@ -272,6 +272,8 @@ private:
n = n * base + *it - '0';
else if (*it >= 'A' && *it <= 'F')
n = n * base + *it - 'A' + 10;
else if (*it >= 'a' && *it <= 'f')
n = n * base + *it - 'a' + 10;
}
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 tmp(other);

View File

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

View File

@ -73,7 +73,10 @@ void NumberParserTest::testParse()
assert(NumberParser::parse("-123") == -123);
assert(NumberParser::parseUnsigned("123") == 123);
assert(NumberParser::parseHex("12AB") == 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("00") == 0);
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/Dynamic/Var.h"
namespace Poco
{
namespace JSON
{
namespace Poco {
namespace JSON {
class Object;
@ -58,37 +56,28 @@ class JSON_API Array
{
public:
typedef std::vector<DynamicAny> ValueVector;
typedef std::vector<Dynamic::Var> ValueVector;
typedef SharedPtr<Array> Ptr;
Array();
/// Default constructor
Array(const Array& copy);
/// Copy Constructor
virtual ~Array();
/// Destructor
ValueVector::const_iterator begin() const;
/// Returns iterator
ValueVector::const_iterator end() const;
/// Returns iterator
DynamicAny get(unsigned int index) const;
Dynamic::Var 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.
@ -101,11 +90,10 @@ public:
/// exceptions for invalid values.
/// Note: This will not work for an array or an object.
{
DynamicAny value = get(index);
Dynamic::Var value = get(index);
return value.convert<T>();
}
SharedPtr<Object> getObject(unsigned int index) const;
/// Retrieves an object. When the element is not
/// an object or doesn't exist, an empty SharedPtr is returned.
@ -113,20 +101,16 @@ public:
unsigned int size() const;
/// Returns the size of the array
bool isArray(unsigned int index) const;
/// Returns true when the element is an array
bool isNull(unsigned int index) const;
/// Returns true when the element is null or
/// when the element doesn't exist.
bool isObject(unsigned int index) const;
/// Returns true when the element is an object
template<typename T>
T optElement(unsigned int index, const T& def) const
/// Returns the element at the given index. When
@ -149,24 +133,20 @@ public:
return value;
}
void add(const DynamicAny& value)
void add(const Dynamic::Var& 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;
};
@ -176,28 +156,32 @@ 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);
Dynamic::Var 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];
Dynamic::Var value = _values[index];
return value.isEmpty();
}
return true;
@ -211,11 +195,9 @@ inline void Array::remove(unsigned int index)
}} // Namespace Poco::JSON
namespace Poco
{
namespace Dynamic
{
namespace Poco {
namespace Dynamic {
template <>
class VarHolderImpl<JSON::Array::Ptr>: public VarHolder
@ -355,8 +337,7 @@ private:
JSON::Array::Ptr _val;
};
} // Namespace Dynamic
} // Namespace Poco
}} // namespace Poco::JSON
#endif // JSON_Array_INCLUDED

View File

@ -40,13 +40,12 @@
#include "Poco/JSON/Handler.h"
#include <stack>
namespace Poco
{
namespace JSON
{
namespace Poco {
namespace JSON {
class JSON_API DefaultHandler : public Handler
/// Provides a default handler for the JSON parser.
@ -55,82 +54,60 @@ class JSON_API DefaultHandler : public Handler
{
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;
Dynamic::Var 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
#if defined(POCO_HAVE_INT64)
virtual void value(Int64 v);
/// A 64-bit integer value is read
#endif
virtual void value(const std::string& s);
/// A string value is read.
virtual void value(double d);
/// A double value is read
virtual void value(bool b);
/// A boolean value is read
virtual void null();
/// A null value is read
private:
void setValue(const Poco::Dynamic::Var& value);
void setValue(const Poco::DynamicAny& value);
std::stack<DynamicAny> _stack;
std::string _key;
DynamicAny _result;
std::stack<Dynamic::Var> _stack;
std::string _key;
Dynamic::Var _result;
};
inline DynamicAny DefaultHandler::result() const
inline Dynamic::Var DefaultHandler::result() const
{
return _result;
}
@ -141,6 +118,7 @@ inline void DefaultHandler::value(int v)
setValue(v);
}
#if defined(POCO_HAVE_INT64)
inline void DefaultHandler::value(Int64 v)
{
@ -169,11 +147,11 @@ inline void DefaultHandler::value(bool b)
inline void DefaultHandler::null()
{
Poco::DynamicAny empty;
Poco::Dynamic::Var empty;
setValue(empty);
}
}} // Namespace Poco::JSON
}} // namespace Poco::JSON
#endif // JSON_DefaultHandler_INCLUDED

View File

@ -39,65 +39,51 @@
#define JSON_Handler_INCLUDED
#include "Poco/DynamicAny.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h"
namespace Poco
{
namespace JSON
{
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
#if defined(POCO_HAVE_INT64)
virtual void value(Int64 v) = 0;
/// A 64-bit integer value is read
#endif
virtual void value(const std::string& value) = 0;
/// A string value is read.
virtual void value(double d) = 0;
/// A double value is read
virtual void value(bool b) = 0;
/// A boolean value is read
protected:
virtual ~Handler();
@ -107,6 +93,6 @@ private:
};
}} // Namespace Poco::JSON
}} // namespace Poco::JSON
#endif // JSON_Handler_INCLUDED

View File

@ -54,16 +54,16 @@
// 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
#if defined(JSON_EXPORTS)
#define JSON_API __declspec(dllexport)
#else
#define JSON_API __declspec(dllimport)
#endif
#endif
#if !defined(JSON_API)
#define JSON_API
#define JSON_API
#endif

View File

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

View File

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

View File

@ -42,7 +42,7 @@
#include <istream>
#include <sstream>
#include "Poco/DynamicAny.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/StreamTokenizer.h"
#include "Poco/JSON/JSON.h"
@ -50,10 +50,9 @@
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Handler.h"
namespace Poco
{
namespace JSON
{
namespace Poco {
namespace JSON {
class JSON_API Parser
/// A class for passing JSON strings or streams
@ -63,33 +62,26 @@ 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
@ -97,23 +89,17 @@ private:
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;
Handler* _handler;
};
@ -135,6 +121,8 @@ inline Handler* Parser::getHandler()
return _handler;
}
}} // Namespace Poco::JSON
}} // namespace Poco::JSON
#endif // JSON_JSONParser_INCLUDED

View File

@ -44,40 +44,34 @@
#include "Poco/JSON/Array.h"
namespace Poco
{
namespace JSON
{
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);
Query(const Dynamic::Var& 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;
Dynamic::Var find(const std::string& path) const;
/// Searches a value
/// For example: "person.children[0].name" will return the
/// the name of the first child. When the value can't be found
/// an empty value is returned.
template<typename T>
T findValue(const std::string& path, const T& def) const
/// Searches for a value will convert it to the given type.
@ -85,16 +79,14 @@ public:
/// the default value will be returned.
{
T result = def;
DynamicAny value = find(path);
Dynamic::Var value = find(path);
if ( ! value.isEmpty() )
{
try
{
result = value.convert<T>();
}
catch(...)
{
}
catch(...) { }
}
return result;
}
@ -108,11 +100,11 @@ public:
}
private:
DynamicAny _source;
Dynamic::Var _source;
};
}} // Namespace JSON
}} // namespace Poco::JSON
#endif // JSON_JSONQuery_INCLUDED

View File

@ -41,24 +41,24 @@
#include <iostream>
#include "Poco/DynamicAny.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h"
namespace Poco
{
namespace JSON
{
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);
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.
/// When indent is 0, the String will be created as small as possible.
};
}} // Namespace Poco::JSON
}} // namespace Poco::JSON
#endif // JSON_JSONStringifier_INCLUDED

View File

@ -43,21 +43,22 @@
#include <stack>
#include "Poco/JSON/JSON.h"
#include "Poco/DynamicAny.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/SharedPtr.h"
#include "Poco/Path.h"
#include "Poco/Timestamp.h"
namespace Poco
{
namespace JSON
{
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
@ -125,7 +126,7 @@ public:
/// 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
@ -177,6 +178,6 @@ inline Timestamp Template::parseTime() const
return _parseTime;
}
}} // Namespace Poco::JSON
}} // namespace Poco::JSON
#endif // JSON_JSONTemplate_INCLUDED

View File

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

View File

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

View File

@ -36,15 +36,19 @@
#include "Poco/JSON/DefaultHandler.h"
#include "Poco/JSON/Object.h"
namespace Poco
{
namespace JSON
{
using Poco::Dynamic::Var;
namespace Poco {
namespace JSON {
DefaultHandler::DefaultHandler() : Handler()
{
}
DefaultHandler::~DefaultHandler()
{
}
@ -60,7 +64,7 @@ void DefaultHandler::startObject()
}
else
{
DynamicAny parent = _stack.top();
Var parent = _stack.top();
if ( parent.type() == typeid(Array::Ptr) )
{
@ -96,7 +100,7 @@ void DefaultHandler::startArray()
}
else
{
DynamicAny parent = _stack.top();
Var parent = _stack.top();
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) )
{

View File

@ -40,10 +40,12 @@
#include <iostream>
#include <sstream>
namespace Poco
{
namespace JSON
{
using Poco::Dynamic::Var;
namespace Poco {
namespace JSON {
Object::Object()
@ -51,19 +53,21 @@ Object::Object()
}
Object::Object(const Object& copy) : _values(copy._values)
{
}
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);
if ( it != _values.end() )
@ -78,7 +82,7 @@ Array::Ptr Object::getArray(const std::string& key) const
{
Array::Ptr result;
DynamicAny value = get(key);
Var value = get(key);
if ( value.type() == typeid(Array::Ptr) )
{
result = value.extract<Array::Ptr>();
@ -91,7 +95,7 @@ Object::Ptr Object::getObject(const std::string& key) const
{
Object::Ptr result;
DynamicAny value = get(key);
Var value = get(key);
if ( value.type() == typeid(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"
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()
{
}
Object::Ptr Query::findObject(const std::string& path) const
{
Object::Ptr obj;
DynamicAny result = find(path);
Var result = find(path);
if ( result.type() == typeid(Object::Ptr) )
{
obj = result.extract<Object::Ptr>();
@ -66,10 +71,11 @@ Object::Ptr Query::findObject(const std::string& path) const
return obj;
}
Array::Ptr Query::findArray(const std::string& path) const
{
Array::Ptr arr;
DynamicAny result = find(path);
Var result = find(path);
if ( result.type() == typeid(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, ".");
for(StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
{
@ -138,4 +144,5 @@ DynamicAny Query::find(const std::string& path) const
return result;
}
}} // Namespace Poco::JSON

View File

@ -39,12 +39,15 @@
#include "Poco/JSON/Array.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) )
{
@ -111,4 +114,5 @@ void Stringifier::stringify(const DynamicAny& any, std::ostream& out, unsigned i
}
}
}} // Namespace Poco::JSON

View File

@ -38,13 +38,17 @@
#include "Poco/JSON/TemplateCache.h"
#include "Poco/JSON/Query.h"
namespace Poco
{
namespace JSON
{
using Poco::Dynamic::Var;
namespace Poco {
namespace JSON {
POCO_IMPLEMENT_EXCEPTION(JSONTemplateException, Exception, "Template Exception")
class Part
{
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;
};
class StringPart : public Part
{
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;
}
@ -103,6 +108,7 @@ private:
std::string _content;
};
class MultiPart : public Part
{
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)
{
@ -138,6 +144,7 @@ protected:
VectorParts _parts;
};
class EchoPart : public Part
{
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);
DynamicAny value = query.find(_query);
Var value = query.find(_query);
if ( ! value.isEmpty() )
{
@ -167,6 +174,7 @@ private:
std::string _query;
};
class LogicQuery
{
public:
@ -178,18 +186,18 @@ public:
{
}
virtual bool apply(const DynamicAny& data) const
virtual bool apply(const Var& data) const
{
bool logic = false;
Query query(data);
DynamicAny value = query.find(_queryString);
Var value = query.find(_queryString);
if ( ! value.isEmpty() ) // When empty, logic will be false
{
if ( value.isString() )
// 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>();
logic = ! s.empty();
@ -198,7 +206,7 @@ public:
{
// All other values, try to convert to bool
// 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>();
}
}
@ -210,6 +218,7 @@ protected:
std::string _queryString;
};
class LogicExistQuery : public LogicQuery
{
public:
@ -221,10 +230,10 @@ public:
{
}
virtual bool apply(const DynamicAny& data) const
virtual bool apply(const Var& data) const
{
Query query(data);
DynamicAny value = query.find(_queryString);
Var value = query.find(_queryString);
return !value.isEmpty();
}
@ -242,7 +251,7 @@ public:
{
}
virtual bool apply(const DynamicAny& data) const
virtual bool apply(const Var& data) const
{
return true;
}
@ -273,7 +282,7 @@ public:
_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;
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;
};
class LoopPart : public MultiPart
{
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);
@ -316,7 +326,7 @@ public:
{
for(int i = 0; i < array->size(); i++)
{
DynamicAny value = array->get(i);
Var value = array->get(i);
dataObject->set(_name, value);
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();
if ( cache == NULL )
@ -383,12 +393,14 @@ private:
};
Template::Template(const Path& templatePath)
: _parts(NULL)
, _templatePath(templatePath)
{
}
Template::Template()
: _parts(NULL)
{
@ -713,6 +725,7 @@ void Template::readWhiteSpace(std::istream& in)
}
}
std::string Template::readString(std::istream& in)
{
std::string str;
@ -728,9 +741,11 @@ std::string Template::readString(std::istream& in)
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);
}
}} // Namespace Poco::JSON

View File

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

View File

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

View File

@ -42,8 +42,12 @@
#define PDF_PDF_INCLUDED
#if defined(_MSC_VER) && !defined(POCO_MSVC_SECURE_WARNINGS) && (!defined(_CRT_SECURE_NO_WARNINGS) || !defined(_CRT_SECURE_NO_DEPRECATE))
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#endif
#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)
{
if (value.compare(0, 2, "0x") == 0)
return NumberParser::parseHex(value.substr(2));
if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex(value);
else
return NumberParser::parse(value);
}
@ -459,8 +459,8 @@ int AbstractConfiguration::parseInt(const std::string& value)
int AbstractConfiguration::parseUInt(const std::string& value)
{
if (value.compare(0, 2, "0x") == 0)
return NumberParser::parseHex(value.substr(2));
if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex(value);
else
return NumberParser::parseUnsigned(value);
}
@ -468,8 +468,8 @@ int AbstractConfiguration::parseUInt(const std::string& value)
Int64 AbstractConfiguration::parseInt64(const std::string& value)
{
if (value.compare(0, 2, "0x") == 0)
return NumberParser::parseHex64(value.substr(2));
if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex64(value);
else
return NumberParser::parse64(value);
}
@ -477,8 +477,8 @@ Int64 AbstractConfiguration::parseInt64(const std::string& value)
UInt64 AbstractConfiguration::parseUInt64(const std::string& value)
{
if (value.compare(0, 2, "0x") == 0)
return NumberParser::parseHex64(value.substr(2));
if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))
return NumberParser::parseHex64(value);
else
return NumberParser::parseUnsigned64(value);
}

View File

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

View File

@ -52,16 +52,16 @@ if "%1"=="" goto usage
set VS_VERSION=vs%1
if not defined VCINSTALLDIR (
if %1==71 (set VSENV="%VS71COMNTOOLS%vsvars32.bat") else (
if %1==80 (set VSENV="%VS80COMNTOOLS%vsvars32.bat") else (
if %1==90 (set VSENV="%VS90COMNTOOLS%vsvars32.bat") else (
if %1==100 (set VSENV="%VS100COMNTOOLS%vsvars32.bat")
if %1==71 (call "%VS71COMNTOOLS%vsvars32.bat") else (
if %1==80 (call "%VS80COMNTOOLS%vsvars32.bat") else (
if %1==90 (call "%VS90COMNTOOLS%vsvars32.bat") else (
if %1==100 (call "%VS100COMNTOOLS%vsvars32.bat")
))))
call %VSENV%
if not defined VSINSTALLDIR (
echo Error: No Visual C++ environment found.
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
)
)