== Jackson

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

=== Usage

1) Add the dependency:

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

2) Install and encode/decode JSON

.Java
[source, java, role="primary"]
----
import io.jooby.json.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.json.JacksonModule;

{
  install(JacksonModule())                          <1>

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

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

<1> Install Jackson
<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

=== Working with ObjectMapper

Access to default object mapper is available via require call:

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

{
  install(new JacksonModule());

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

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

{
  install(JacksonModule())

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

You can provide your own `ObjectMapper`:

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

{
  ObjectMapper mapper = new ObjectMapper();

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

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

{
  val mapper = ObjectMapper()

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

=== Provisioning Jackson Modules

Jackson module can be provided by a link:/#dependency-injection[dependency injection] framework.

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

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

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

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

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