Skip to content

Inserting JSON::Object by value results in isObject being false #740

@aleks-f

Description

@aleks-f

Based on Stack Overflow post

void build_object (Poco::JSON::Object&result)
{
    Poco::JSON::Object inner;
    inner.set("some_number", 5);
    inner.set("some_string", "xyz");
    std::string key = "new_object";
    result.set(key, inner); // no error, but ...
    assert (result.isObject(key)); // fails
}

The reason is that Objects and Arrays are held as shared pointers internally and typeids in the above case do not match. The workaround is:

void build_object (Poco::JSON::Object&result)
{
    Poco::JSON::Object::Ptr inner = new Poco::JSON::Object;
    inner->set("some_number", 5);
    inner->set("some_string", "xyz");
    std::string key = "new_object";
    result.set(key, inner); // OK
    assert (result.isObject(key)); // OK
}

We should , however, shield against this unintuitive trap and also return true even when the value is passed in. Surprisingly, everything else works fine with value and stringify will prints a valid json. @fbraem can you please take a look at this?

EDIT: the problem is here and here. Same thing in Array and it should be checked that change does not break something.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions