1. JSON-B
JSON support using Jakarta JSON Binding (JSON-B) api and Yasson implementation.
1.1. Usage
1) Add the dependency:
Maven
Gradle
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-yasson</artifactId>
<version>3.5.5</version>
</dependency>
2) Install and encode/decode JSON
Java
Kotlin
import io.jooby.json.YassonModule;
{
install(new YassonModule()); (1)
get("/", ctx -> {
MyObject myObject = ...;
return myObject; (2)
});
post("/", ctx -> {
MyObject myObject = ctx.body(MyObject.class); (3)
...
});
}
1 | Install JSON-B Yasson Module |
2 | Use JSON-B to encode arbitrary object as JSON |
3 | Use JSON-B to decode JSON to Java object. Client must specify the Content-Type: application/json header |
1.2. Working with JSON-B
Access to default object mapper is available via require call:
Default object mapper
Java
Kotlin
import io.jooby.json.YassonModule;
{
install(new YassonModule());
Jsonb jsonb = require(Jsonb.class);
...
}
You can provide your own Jsonb
:
Custom ObjectMapper
Java
Kotlin
import io.jooby.json.YassonModule;
{
Jsonb jsonb = JsonbBuilder.create();
install(new YassonModule(jsonb));
}