wanglingsong / JsonSurfer

A streaming JsonPath processor in Java
MIT License
292 stars 55 forks source link

Support script expressions #36

Closed aljim closed 6 years ago

aljim commented 6 years ago

The current JsonPath spec allows script expressions aka functions, are there any plans to support it?

$.books.size()

$.somestring.split(".")[0]

wanglingsong commented 6 years ago

Which JsonPath spec? Can you provide a sample json and the expected result of your example?

aljim commented 6 years ago

http://goessner.net/articles/JsonPath/

"Script expression evaluated by the underlying script engine"

For example the jayway implementation allows some functions, min, max, etc.

https://github.com/json-path/JsonPath#functions

Ideally this would need to be an addition to the antlr grammar to allow pluggable functions that could be defined in the JsonSurfer instance

aljim commented 6 years ago

{ "items": [1,2,3,-1] }

$.items.size() // 4 $.items.min() // -1 $.items.toString() // "[1,2,3,-1]"

wanglingsong commented 6 years ago

I think it would not be too difficult if the "functions" apply only to leaf level(as the last node), then the implementation should be similar to JsonPath filter. Any pull requests for this feature are welcome. I'd love to help, if you have any further question.

wanglingsong commented 6 years ago

Alternatively, you can use stream API of Java8 to achieve above "functions". Please refer to https://github.com/jsurfer/JsonSurfer#stream-support

wanglingsong commented 6 years ago

Examples added in document: https://github.com/jsurfer/JsonSurfer#java-8-streams-api-support