klaxon icon indicating copy to clipboard operation
klaxon copied to clipboard

PathMatcher won't call onMatch with simple array of strings

Open guidimarchi opened this issue 4 years ago • 2 comments

I have the following code and I would expect the regex to match on $.library.books[], but it does not. It only works if the value for "books" field is either an object or a single value.

Am I wrong on my expectation or is there another way to handle this?

fun test() {
        val pathMatcher = object : PathMatcher {
            override fun pathMatches(path: String) = Pattern.matches(".*library.*books", path)
            override fun onMatch(path: String, value: Any) = println("Adding $path = $value")
        }
        val json = """{"library": {"books":["Herman Melville"]}}"""
        
        // this works
        //val json = """{"library": {"books":"Herman Melville"}}"""

        Klaxon().pathMatcher(pathMatcher)
                .parseJsonObject(StringReader(json))
    }```

guidimarchi avatar May 14 '20 15:05 guidimarchi

Confusing, but at least nicely explained under https://www.baeldung.com/kotlin-json-klaxson#json-path-query-api

holgerbrandl avatar May 21 '20 07:05 holgerbrandl

The thing is that Klaxon is only "leaves" PROPERTIES only, as expected from line: https://github.com/cbeust/klaxon/blob/621fd051d17cbf5bd5618da6db68dde51cf3f42b/klaxon/src/main/kotlin/com/beust/klaxon/World.kt#L95

I don't know if this is was intended, but I expected PathMatchers to be called on OBJECTS and leaves "values" (for example in a json array). Even NULL properties would be interesting to be analyzed in pathMatchers, IDK why this line is even there.

apierri avatar Jul 03 '20 16:07 apierri