klaxon
klaxon copied to clipboard
PathMatcher won't call onMatch with simple array of strings
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))
}```
Confusing, but at least nicely explained under https://www.baeldung.com/kotlin-json-klaxson#json-path-query-api
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.