Skip to content

ZPath is for retrieving Nodes from structured data - like JsonPath, XPath and JMESPath, but simpler

License

Notifications You must be signed in to change notification settings

faceless2/zpath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZPath and ZTemplate

A Java implementation of ZPath / ZTemplates from https://zpath.me

ZPath API

import me.zpath.ZPath;
import java.util.List;

Object context = ...;
ZPath path = ZPath.compile("table/tr[td]");
List<Object> match = ZPath.evaluate(context).all();  // List of zero or more matches
Object match = ZPath.evaluate(context).first();      // The first match, or null if none

A ZPath can be compiled once and reused in multiple threads. context can be any type of object recognised by an EvalFactory registered with the implementation. The API ships with implementations for:

ZTemplate API

import me.zpath.ZTemplate;
import java.io.Reader;

Object context = ...;
Reader reader = ...;
ZTemplate template = ZTemplate.compile(reader);
reader = template.apply(context);

// Or to write to an Appendable
Appendable out = new StringBuilder();
template.apply(context, out);

A ZTemplate can be compiled once and reused in multiple threads. context can be any type of object accepted by ZPath.

By default the "include" functionality is not enabled, for security, but it's easy to add if required, either using a default implementation to load from the file, or implement me.zpath.Includer

import me.zpath.ZTemplate;
import me.zpath.Includer;
import me.zpath.Configuration;
import java.io.*;

Includer includer = Includer.getDefault(new File("dir"));
Configuration conf = new Configuration().setTemplateIncluder(includer);
Reader reader = new InputStreamReader(new FileInputStream("dir/template.zt"), "UTF-8");
ZTemplate template = ZTemplate.compile(reader, conf);

ZPath with BFO Json

This is a complete example, using the "multiline string" syntax from Java 15 and based on the "BFO Json" library from https://github.com/faceless2/json

import com.bfo.json.Json;
import me.zpath.ZPath;

public class Test{
 public static void main(String...args) {
  Json json = Json.read("""
  {
   "person": {
    "name": {
     "first": "John",
     "last": "Smith"
    },
    "age": 27,
    "books": [
     {
      "name": "A Clockwork Orange",
      "author": "Anthony Burgess"
     },
     {
      "name": "Wolf Hall",
      "author": "Hilary Mantel"
     },
     {
      "name": "The First Man",
      "author": "Albert Camus"
     }
    ]
   }
  }
  """);

  ZPath.compile("person/name/first").eval(json).first(); // "John"

  ZPath.compile("count(person/books/*)").eval(json).first(); // 3

  ZPath.compile("**/author").eval(json).all();
    // ["Anthony Burgess", "Hilary Mantel", "Albert Camus"]
 }
}

About

ZPath is for retrieving Nodes from structured data - like JsonPath, XPath and JMESPath, but simpler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages