Skip to content

JSON 'find' not returning empty result if object is expected but another value is found #267

@ThadThompson

Description

@ThadThompson

JSON "find" doesn't return an empty result if an object is expected but another value is found instead.

Motivating example:

When trying to use JSONConfiguration for logging configuration,

    LoggingConfigurator::configureLogger

calls

    pConfig->hasProperty("channel.class")

which in turn calls

bool JSONConfiguration::getRaw(const std::string & key, std::string & value) const
{
    JSON::Query query(_object);
    Poco::DynamicAny result = query.find(key);
    if ( ! result.isEmpty() )
    {
        value = result.convert<std::string>();
        return true;
    }
    return false;
}

which queries down and finds "channel" but the value is the string name of another channel, not an object.

Possible patch and test:

 JSON/src/Query.cpp              |    2 ++
 JSON/testsuite/src/JSONTest.cpp |    3 +++
 2 files changed, 5 insertions(+)

diff --git a/JSON/src/Query.cpp b/JSON/src/Query.cpp
index 7df1809..614d80c 100644
--- a/JSON/src/Query.cpp
+++ b/JSON/src/Query.cpp
@@ -162,6 +162,8 @@ Var Query::find(const std::string& path) const
                    Object o = result.extract<Object>();
                    result = o.get(name);
                }
+               else
+                    result.empty();
            }

            if (!result.isEmpty() && !indexes.empty())
diff --git a/JSON/testsuite/src/JSONTest.cpp b/JSON/testsuite/src/JSONTest.cpp
index a98bdd5..0311854 100644
--- a/JSON/testsuite/src/JSONTest.cpp
+++ b/JSON/testsuite/src/JSONTest.cpp
@@ -1072,6 +1072,9 @@ void JSONTest::testQuery()
    Object& rAddress = query.findObject("address", address);
    assert (rAddress.getValue<int>("number") == 123);

+    Var badAddr = query.find("address.street.anotherObject");
+    assert(badAddr.isEmpty());
+
    using Poco::JSON::Array;

    Array::Ptr pChildren = query.findArray("children");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions