Reputation: 652
I am trying to find a way to use JSON-PATH with SnakeYaml in order to select node and to still get a way to find the line number inside the yaml source file.
For example the path $.info.email
will tell me that it's line 16
(for example) of the source file, read by snakeyaml.
I am able to navigate inside the nodes given by Snakeyaml (ScalarNode
, MappedNode
, etc.) and Snakeyaml offers a way (Mark
) to get the line number of a node.
So I need a way to be able to use a json-path expression in roder to point inside the node tree of Snakeyaml.
I thought I can use JsonProvider
and a MappingProvider
from json-path, but things are not very clear to me and the Javadoc is quite silent about this topic.
Any advice?
Upvotes: 0
Views: 608
Reputation: 21
A possible solution could be the following:
public Object getByPath(InputStream io, String path) throws Exception {
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.load(io);
String json = new ObjectMapper().writeValueAsString(map);
Object value = JsonPath.read(json, path);
return value;
}
Basically the idea is to use SnakeYaml to read the file as a map, and ObjectMapper to transform the map into a json. Then it can be read with json-path. I hope it can be useful.
Upvotes: 0