jackson

jackson

JSON support from the excellent Jackson library.

exports

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-jackson</artifactId>
  <version>1.6.6</version>
</dependency>

usage

import org.jooby.json.Jackson;

{
  use(new Jackson());
 
  // sending
  get("/my-api", req -> new MyObject()); 

  // receiving a json body
  post("/my-api", req -> {
    MyObject obj = req.body(MyObject.class);
    return obj;
  });
}

views

Dynamic views are supported via org.jooby.json.JacksonView object:

{
  use(new Jackson());
  
  get("/public", req -> {
    Item item = ...
    return new JacksonView<>(Views.Public.class, item);
  });
  
  get("/internal", req -> {
    Item item = ...
    return new JacksonView<>(Views.Internal.class, item);
  });
}

advanced configuration

If you need a special setting or configuration for your ObjectMapper:

{
  use(new Jackson().doWith(mapper -> {
    // setup your custom object mapper
  });
}

or provide an ObjectMapper instance:

{
   ObjectMapper mapper = ....;
   use(new Jackson(mapper));
}

It is possible to wire Jackson modules too:

{
  use(new Jackson()
    .module(MyJacksonModuleWiredByGuice.class)
  );
}

This is useful when your MyJacksonModuleWiredByGuice module require some dependencies.