I'm using Release version 1.5.1 compiled with Visual Studio 2005.
If i query an Object, the result is not as expected. If i make the same with a pointer to the object the result is as expected. The problem can be reproduced with the following sample code.
Poco::JSON::Object::Ptr pObj = &obj;
//Query ok (Object::Ptr)
Poco::Dynamic::Var idQueryPointer = queryPointer.find("Id");
int nIdPointer = idQueryPointer;
//Query fails (Object)
Poco::Dynamic::Var idQueryObj = queryObj.find("Id");
int nIdObj = idQueryObj; //Throws BadCastException("Can not convert to Int32");
It's because Object::Ptr has Dynamic::Var specialization and Object does not. I really don't know enough about the internals of the library to say whether having Object (and maybe Array) VarHolder specializations would make sense and how difficult would it be implement. Franky Braem would be the one to answer those questions, I'll notify him.
It was a design decision to use SharedPtr for Objects and Arrays in the JSON module to avoid the call of a lot of copy constructors when storing/retrieving objects in a JSON structure.
I see that it is much better to use SharedPtr than objects, but I think in this case it would be clearer if the constructor for the query class only would supports pointers instead of Var
const Dynamic::Var & source
const Object::Ptr source
and even better would be, if the query class stumbles over an object that it would throw an exception or return an empty result instead of a wrong result. With wrong result i mean, that with the current solution a query on an object returns the original object.
But on the other side, it should not be too hard to change the Query class so that the Query class could also support Objects and not only pointers. But that’s just my two cents.
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
Fixed for 1.5.2