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>2.8.4</version>
</dependency>

2) Install and encode/decode JSON

Java
Kotlin
import io.jooby.json.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 Jackson to encode arbitrary object as JSON
3 Use Jackson 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.json.GsonModule;

{
  install(new GsonModule());

  Gson gson = require(Gson.class);

  ...
}

You can provide your own Gson:

Custom ObjectMapper
Java
Kotlin
import io.jooby.json.GsonModule;

{
  Gson gson = new GsonBuilder().create();

  install(new GsonModule(gson));
}