Skip to content

Latest commit

 

History

History
122 lines (89 loc) · 5.41 KB

README.md

File metadata and controls

122 lines (89 loc) · 5.41 KB

License

All Contributors

Quarkus - Jackson Jq

jq is a very popular JSON processor. Jackson JQ is an implementation of this processor in Java, and now you can integrate it in your Quarkus application!

⚠️ Version 2.x.x of this extension (main branch) supports Quarkus 3, and version 1.x.x (quarkus2 branch) supports Quarkus 2.

Getting Started

Simply add the dependency to your Quarkus project:

<dependency>
  <groupId>io.quarkiverse.jackson-jq</groupId>
  <artifactId>quarkus-jackson-jq</artifactId>
</dependency>

Then you can simply use the jackson-jq's Scope bean in your services. For example:

@Path("/jackson-jq")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class JacksonJqResource {

    @Inject
    Scope scope;

    @POST
    public List<JsonNode> parse(Document document) throws JsonQueryException {
        final JsonQuery query = JsonQuery.compile(document.expression, Versions.JQ_1_6);
        List<JsonNode> out = new ArrayList<>();
        query.apply(this.scope, document.document, out::add);
        return out;
    }
}

Jackson Jq Extra Module

By default, this extension already includes the extra module.

Adding Custom functions

The extension support adding custom functions to the jq engine, as example, to add a function that accept one or two parameters:

import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;

import io.quarkiverse.jackson.jq.JqFunction;

import net.thisptr.jackson.jq.Expression;
import net.thisptr.jackson.jq.Function;
import net.thisptr.jackson.jq.PathOutput;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.Version;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.path.Path;

@JqFunction({ "myFunction/1", "myFunction/2" })
public class MyFunction implements Function {
    @Override
    public void apply(Scope scope, List<Expression> args, JsonNode in, Path path, PathOutput output, Version version)
            throws JsonQueryException {
        
        // add the content of your function here
    }
}

The function will then be available to any jq expression i.e.

.items[] | myFunction("foo", "bar")

Considerations

Underneath, this extension uses jackson-jq, so the same limitations and capabilities of that library also applies here.

If you encounter any bugs or have any questions, please feel free to open an issue.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Ricardo Zanini
Ricardo Zanini

💻 🚧
Eiichi Sato
Eiichi Sato

👀
Luca Burgazzoli
Luca Burgazzoli

💻
Jiehong
Jiehong

💻
Helber Belmiro
Helber Belmiro

💻

This project follows the all-contributors specification. Contributions of any kind welcome!