JSON::Query an JSON::Object #99

MarkusAdam opened this Issue Feb 20, 2013 · 4 comments


None yet

3 participants



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 obj;
Poco::JSON::Object::Ptr pObj = &obj;

pObj->set("Id", 22);

//Query ok (Object::Ptr)
Poco::JSON::Query queryPointer(pObj);
Poco::Dynamic::Var idQueryPointer = queryPointer.find("Id");
int nIdPointer = idQueryPointer;

//Query fails (Object)
Poco::JSON::Query queryObj(obj);
Poco::Dynamic::Var idQueryObj = queryObj.find("Id");
int nIdObj = idQueryObj;  //Throws BadCastException("Can not convert to Int32");
POCO C++ Libraries member

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.

POCO C++ Libraries member

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.

@aleks-f aleks-f was assigned Jun 8, 2013
@aleks-f aleks-f added a commit that referenced this issue Jun 8, 2013
@aleks-f aleks-f 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
POCO C++ Libraries member

Fixed for 1.5.2

@aleks-f aleks-f closed this Jun 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment