== 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.JacksonModule;

{
  install(new JacksonModule());                     <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.JacksonModule;

{
  install(JacksonModule())                          <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.JacksonModule;

{
  install(new JacksonModule());

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

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

{
  install(JacksonModule())

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

You can provide your own `ObjectMapper`:

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

{
  ObjectMapper mapper = new ObjectMapper();

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

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

{
  val mapper = ObjectMapper()

  install(JacksonModule(mapper))
}
----

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

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

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

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

{
  install(JacksonModule(XmlMapper()))
}
----

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

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

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

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

{
  install(JacksonModule(ObjectMapper()))
  install(JacksonModule(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.JacksonModule;

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

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

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

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