vert-x3 / issues

Apache License 2.0
37 stars 7 forks source link

JsonObject/JsonArray need compound query syntax #449

Open lukehutch opened 5 years ago

lukehutch commented 5 years ago

Currently JsonObject methods like JsonObject::getBoolean take only a field name, and look up the field directly in the embedded Map instance. It would be nice if JsonObject supported syntax like outerObject.innerObject. This could be implemented recursively, removing one path segment at a time, with . as a delimiter.

Probably there should also be support for JsonArray indexing, e.g. outerObject.innerArray[0].innermostObject.

These methods would stop and return null if they fail at any point along the path.

These changes would bring the JsonObject and JsonArray query syntax into line with MongoDB's find method.

vietj commented 5 years ago

we currently have a PR for json pointers, I believe this could work too

slinkydeveloper commented 5 years ago

@lukehutch look at https://github.com/eclipse-vertx/vert.x/pull/2898 and feel free to comment

lukehutch commented 5 years ago

@vietj @slinkydeveloper Thanks, that's exactly the sort of thing I was hoping for. Although I have to say that the escaping ~0 and ~1 for ~ and / respectively are super weird.

I was going to suggest using the query logic in jq:

https://stedolan.github.io/jq/

slinkydeveloper commented 5 years ago

@lukehutch I know but that's how the rfc6901 was designed :smile: We decided to adopt it more than others because we don't want any complex query system inside vertx-core and we need it for $ref resolution in new packages we are developing for vertx 4. There are good json querying libraries out there that can be plugged to support vert.x json structures (e.g. https://github.com/burtcorp/jmespath-java), but they are out of scope of vertx-stack for now

lukehutch commented 5 years ago

@slinkydeveloper Since a jmespath-jackson and jmespath-gson backends already exist, maybe a jmespath-vertx backend could be written to plug into the vertx JsonObject / JsonArray classes. (I haven't looked at jmespath closely enough to know if that even makes sense though :-) )

Really there just needs to be a quick and simple query syntax that doesn't require the user to write custom json path-following code from scratch every time. JsonPointer seems to support that fine. However, see my comments in that PR that you linked.

lukehutch commented 5 years ago

I just submitted https://github.com/eclipse-vertx/vert.x/pull/2931 with a simple query API for JsonObject / JsonArray.