Baratine on GitHub

JSON Serialization

Overview

By default, Baratine uses its own built-in JSON library to serialize and deserialize plain-old Java objects (POJOs) where needed.

Example

For the following application:

public class MyExample {
  @Get("/test")
  public void doTest(RequestWeb request)
  {
    request.ok(new MyObject());
  }

  public static void main(String[] args) throws Exception {
    Web.include(MyExample.class);

    Web.go(args);
  }
}

where MyObject is:

public class MyObject {
  private String a = "foo";
  private String b = "bar";
}

A GET request to /test would generate the following JSON:

{"a":"foo", "b":"bar"}

Using the Reader/Writer

To use the library directly:

@Inject
private JsonEngine jsonEngine;

// or getting it programmatically
JsonEngine jsonEngine = Services.current().injector().instance(JsonEngine.class);

// or creating it directly
JsonEngine jsonEngine = new JsonEngineDefault();

// writer usage
JsonWriter jsonWriter = jsonEngine.newWriter();
jsonWriter.init(writer);
jsonWriter.write(value);
jsonWriter.flush();

// reader usage
JsonReader jsonReader = jsonEngine.newReader();
jsonReader.init(reader);
MyObject my = jsonReader.readObject(MyObject.class);

Using Jackson

Instead of using Baratine’s JSON library, you may choose to use Jackson instead. To enable Jackson, set the following property:

Web.property("json.jackson.enabled", "true");

or in a YAML file passed to Web.go(new String[] {"--conf", "json.yaml"}):

json.jackson.enabled : true

Then include the Jackson dependency:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.7.4</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.7.4</version>
</dependency>

Finally, tell Baratine to search the classpath for Jackson:

Web.scanAutoConf();

Whereas Baratine will serialize any POJO, Jackson requires you to annotate your objects with @JsonView:

public class MyObject {
  @JsonView
  private String a = "foo";

  private String b = "bar";
}

For the object above (note that b isn’t annotated), Jackson would generate the following JSON:

{"a":"foo"}

Example

public static void main(String[] args) throws Exception {
  Web.scanAutoConf();
  Web.property("json.jackson.enabled", "true");
  ...
  Web.go(args);
}

Configuring Jackson

The configuration properties of Jackson’s SerializationFeature, DeserializationFeature, and MapperFeature are supported. Example:

json.jackson.WRITE_DATES_AS_TIMESTAMPS : false
json.jackson.DEFAULT_VIEW_INCLUSION    : false
...

See the class JsonJacksonConfig for the full set of config options and their default values.

Using a Custom ObjectMapper

You may provide your own ObjectMapper to Baratine. Simply create a bean that implements JacksonObjectMapperProvider:

public class MyObjectMapperProvider implements JacksonObjectMapperProvider {
  public ObjectMapper get() {
    ObjectMapper mapper = new ObjectMapper();
    ...
    return mapper;
  }

  @Bean
  public JacksonObjectMapperProvider getBean() {
    return this;
  }
}

Include your bean with:

Web.scanAutoConf();
Web.property("json.jackson.enabled", "true");
Web.include(MyObjectMapperProvider.class);
...
Web.go(args);