== Jackson

JSON support using https://github.com/FasterXML/jackson[Jackson 2] library.

=== Usage

1) Add the dependency:

[dependency, artifactId="jooby-jackson"]
.

2) Install and encode/decode JSON

.Java
[source, java, role="primary"]
----
import io.jooby.jackson.Jackson2Module;

{
  install(new Jackson2Module());                     <1>

  get("/", ctx -> {
    MyObject myObject = ...;
    return myObject;                                <2>
  });

  post("/", ctx -> {
    MyObject myObject = ctx.body(MyObject.class);   <3>
    ...
  });
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jackson.Jackson2Module;

{
  install(Jackson2Module())                          <1>

  get("/") {
    val myObject = ...;
    myObject                                        <2>
  }

  post("/") {
    val myObject = ctx.body<MyObject>()             <3>
    ...
  }
}
----

<1> Install Jackson
<2> Use Jackson to encode an arbitrary object as JSON
<3> Use Jackson to decode JSON to a Java object. Client must specify the `Content-Type: application/json` header

=== Working with ObjectMapper

Access to default object mapper is available via require call:

.Default object mapper
[source, java, role="primary"]
----
import io.jooby.jackson.Jackson2Module;

{
  install(new Jackson2Module());

  ObjectMapper mapper = require(ObjectMapper.class);
  
  ...
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jackson.Jackson2Module

{
  install(Jackson2Module())

  val mapper = require<ObjectMapper>()
}
----

You can provide your own `ObjectMapper`:

.Custom ObjectMapper
[source, java, role="primary"]
----
import io.jooby.jackson.Jackson2Module;

{
  ObjectMapper mapper = new ObjectMapper();

  install(new Jackson2Module(mapper));
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jackson.Jackson2Module

{
  val mapper = ObjectMapper()

  install(Jackson2Module(mapper))
}
----

This allows to configure Jackson2Module for doing `xml` processing:

.XmlMapper
[source, java, role="primary"]
----
import io.jooby.jackson.Jackson2Module;

{
  install(new Jackson2Module(new XmlMapper()));
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jackson.Jackson2Module

{
  install(Jackson2Module(XmlMapper()))
}
----

If you want `json` and `xml` processing then install twice:

.XmlMapper
[source, java, role="primary"]
----
import io.jooby.jackson.Jackson2Module;

{
  install(new Jackson2Module(new ObjectMapper()));
  install(new Jackson2Module(new XmlMapper()));
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jackson.Jackson2Module

{
  install(Jackson2Module(ObjectMapper()))
  install(Jackson2Module(XmlMapper()))
}
----

=== Provisioning Jackson Modules

Jackson module can be provided by a link:{uiVersion}/#ecosystem-services-and-the-registry-dependency-injection-di-bridge[dependency injection] framework.

.Provisioning Modules
[source, java, role="primary"]
----
import io.jooby.jackson.Jackson2Module;

{
  install(new Jackson2Module().module(MyModule.class);
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.jackson.Jackson2Module

{
  install(Jackson2Module().module(MyModule::class.java)
}
----

At startup time Jooby ask dependency injection framework to provide a `MyModule` instance.
