fixed GH #99: JSON::Query an JSON::Object

- fixed GH #99: JSON::Query an JSON::Object
- swapped order of AnyCast(const Any&) and AnyCast(Any&) definitions
This commit is contained in:
Aleksandar Fabijanic
2013-06-07 23:15:02 -05:00
parent 1d2a95e110
commit 5e6ef1c14d
9 changed files with 447 additions and 48 deletions

View File

@@ -186,6 +186,9 @@ public:
static Poco::Dynamic::Array makeArray(const JSON::Array::Ptr& arr);
/// Utility function for creation of array.
void clear();
/// Clears the contents of the array.
private:
typedef SharedPtr<Poco::Dynamic::Array> ArrayPtr;
@@ -389,6 +392,145 @@ private:
};
template <>
class VarHolderImpl<JSON::Array>: public VarHolder
{
public:
VarHolderImpl(const JSON::Array& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Array);
}
void convert(Int8&) const
{
throw BadCastException();
}
void convert(Int16&) const
{
throw BadCastException();
}
void convert(Int32&) const
{
throw BadCastException();
}
void convert(Int64&) const
{
throw BadCastException();
}
void convert(UInt8&) const
{
throw BadCastException();
}
void convert(UInt16&) const
{
throw BadCastException();
}
void convert(UInt32&) const
{
throw BadCastException();
}
void convert(UInt64&) const
{
throw BadCastException();
}
void convert(bool& value) const
{
value = _val.size() > 0;
}
void convert(float&) const
{
throw BadCastException();
}
void convert(double&) const
{
throw BadCastException();
}
void convert(char&) const
{
throw BadCastException();
}
void convert(std::string& s) const
{
std::ostringstream oss;
_val.stringify(oss, 2);
s = oss.str();
}
void convert(DateTime& /*val*/) const
{
throw BadCastException("Cannot convert Array to DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
throw BadCastException("Cannot convert Array to LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
throw BadCastException("Cannot convert Array to Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const JSON::Array& value() const
{
return _val;
}
bool isArray() const
{
return false;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
JSON::Array _val;
};
}} // namespace Poco::JSON

View File

@@ -206,6 +206,10 @@ public:
operator const Poco::DynamicStruct& () const;
/// Cast operator to Poco::DynamiStruct.
void clear();
/// Clears the contents of the object. Insertion order
/// preservation property is left intact.
private:
template <typename C>
void doStringify(const C& container, std::ostream& out, unsigned int indent, int step) const
@@ -472,6 +476,148 @@ private:
};
template <>
class VarHolderImpl<JSON::Object>: public VarHolder
{
public:
VarHolderImpl(const JSON::Object& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(JSON::Object);
}
void convert(Int8&) const
{
throw BadCastException();
}
void convert(Int16&) const
{
throw BadCastException();
}
void convert(Int32&) const
{
throw BadCastException();
}
void convert(Int64&) const
{
throw BadCastException();
}
void convert(UInt8&) const
{
throw BadCastException();
}
void convert(UInt16&) const
{
throw BadCastException();
}
void convert(UInt32&) const
{
throw BadCastException();
}
void convert(UInt64&) const
{
throw BadCastException();
}
void convert(bool& value) const
{
value = _val.size() > 0;
}
void convert(float&) const
{
throw BadCastException();
}
void convert(double&) const
{
throw BadCastException();
}
void convert(char&) const
{
throw BadCastException();
}
void convert(std::string& s) const
{
std::ostringstream oss;
_val.stringify(oss, 2);
s = oss.str();
}
void convert(DateTime& /*val*/) const
{
//TODO: val = _val;
throw NotImplementedException("Conversion not implemented: JSON:Object => DateTime");
}
void convert(LocalDateTime& /*ldt*/) const
{
//TODO: ldt = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => LocalDateTime");
}
void convert(Timestamp& /*ts*/) const
{
//TODO: ts = _val.timestamp();
throw NotImplementedException("Conversion not implemented: JSON:Object => Timestamp");
}
VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
{
return cloneHolder(pVarHolder, _val);
}
const JSON::Object& value() const
{
return _val;
}
bool isArray() const
{
return false;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
private:
JSON::Object _val;
};
}} // namespace Poco::JSON

View File

@@ -53,18 +53,38 @@ class JSON_API Query
{
public:
Query(const Dynamic::Var& source);
/// Constructor. Pass the start object/array.
/// Constructor. Pass the start object/array or Ptr thereof.
/// Creating Query holding Ptr will typically result in faster
/// performance.
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.
/// Search for an object. When the object can't be found, a zero Ptr
/// is returned; otherwise, a shared pointer to internally held object
/// is returned.
/// If object (as opposed to a pointer to object) is held
/// internally, a shared pointer to new (heap-allocated) Object is
/// returned; this may be expensive operation.
Object& findObject(const std::string& path, Object& obj) const;
/// Search for an object. If object is found, it is assigned to the
/// Object through the reference passed in. When the object can't be
/// found, the provided Object is emptied and returned.
Array::Ptr findArray(const std::string& path) const;
/// Search for an array. When the array can't be found, an empty
/// SharedPtr is returned.
/// Search for an array. When the array can't be found, a zero Ptr
/// is returned; otherwise, a shared pointer to internally held array
/// is returned.
/// If array (as opposed to a pointer to array) is held
/// internally, a shared pointer to new (heap-allocated) Object is
/// returned; this may be expensive operation.
Array& findArray(const std::string& path, Array& obj) const;
/// Search for an array. If array is found, it is assigned to the
/// Object through the reference passed in. When the array can't be
/// found, the provided Object is emptied and returned.
Dynamic::Var find(const std::string& path) const;
/// Searches a value