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