// Do not edit directly!
// This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page
[id="extensions-jackson"]
= Jackson
:page-aliases: extensions/jackson.adoc
:linkattrs:
:cq-artifact-id: camel-quarkus-jackson
:cq-native-supported: true
:cq-status: Stable
:cq-status-deprecation: Stable
:cq-description: Marshal POJOs to JSON and back using Jackson
:cq-deprecated: false
:cq-jvm-since: 0.3.0
:cq-native-since: 0.3.0

ifeval::[{doc-show-badges} == true]
[.badges]
[.badge-key]##JVM since##[.badge-supported]##0.3.0## [.badge-key]##Native since##[.badge-supported]##0.3.0##
endif::[]

Marshal POJOs to JSON and back using Jackson

[id="extensions-jackson-whats-inside"]
== What's inside

* xref:{cq-camel-components}:dataformats:jackson2-dataformat.adoc[JSON Jackson data format]

Please refer to the above link for usage and configuration details.

[id="extensions-jackson-maven-coordinates"]
== Maven coordinates

https://{link-quarkus-code-generator}/?extension-search=camel-quarkus-jackson[Create a new project with this extension on {link-quarkus-code-generator}, window="_blank"]

Or add the coordinates to your existing project:

[source,xml]
----
<dependency>
    <groupId>org.apache.camel.quarkus</groupId>
    <artifactId>camel-quarkus-jackson</artifactId>
</dependency>
----
ifeval::[{doc-show-user-guide-link} == true]
Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
endif::[]

[id="extensions-jackson-usage"]
== Usage
[id="extensions-jackson-usage-configuring-the-jackson-objectmapper"]
=== Configuring the Jackson `ObjectMapper`

There are a few ways of configuring the `ObjectMapper` that the `JacksonDataFormat` uses. These are outlined below.

[id="extensions-jackson-usage-objectmapper-created-internally-by-jacksondataformat"]
==== `ObjectMapper` created internally by `JacksonDataFormat`

By default, `JacksonDataFormat` will create its own `ObjectMapper` and use the various configuration options on the `DataFormat`
to configure additional Jackson modules, pretty printing and other features.

[id="extensions-jackson-usage-custom-objectmapper-for-jacksondataformat"]
==== Custom `ObjectMapper` for `JacksonDataFormat`

You can pass a custom `ObjectMapper` instance to `JacksonDataFormat` as follows.

[source,java]
----
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;

public class Routes extends RouteBuilder {
    public void configure() {
        ObjectMapper mapper = new ObjectMapper();
        JacksonDataFormat dataFormat = new JacksonDataFormat();
        dataFormat.setObjectMapper(mapper);
        // Use the dataFormat instance in a route definition
        from("direct:my-direct").marshal(dataFormat)
    }
}
----

[id="extensions-jackson-usage-using-the-quarkus-jackson-objectmapper-with-jacksondataformat"]
==== Using the Quarkus Jackson `ObjectMapper` with `JacksonDataFormat`

The Quarkus Jackson extension exposes an `ObjectMapper` CDI bean which can be discovered by the `JacksonDataFormat`.

[source,java]
----
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;

public class Routes extends RouteBuilder {
    public void configure() {
        JacksonDataFormat dataFormat = new JacksonDataFormat();
        // Make JacksonDataFormat discover the Quarkus Jackson `ObjectMapper` from the Camel registry
        dataFormat.setAutoDiscoverObjectMapper(true);
        // Use the dataFormat instance in a route definition
        from("direct:my-direct").marshal(dataFormat)
    }
}
----

If you are using the JSON binding mode in the Camel REST DSL and want to use the Quarkus Jackson `ObjectMapper`, it can be achieved as follows.

[source,java]
----
import org.apache.camel.builder.RouteBuilder;

@ApplicationScoped
public class Routes extends RouteBuilder {
    public void configure() {
        restConfiguration().dataFormatProperty("autoDiscoverObjectMapper", "true");
        // REST definition follows...
    }
}
----

You can perform customizations on the Quarkus `ObjectMapper` with a `ObjectMapperCustomizer`.

[source,java]
----
import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.jackson.ObjectMapperCustomizer;

@Singleton
public class RegisterCustomModuleCustomizer implements ObjectMapperCustomizer {
    public void customize(ObjectMapper mapper) {
        mapper.registerModule(new CustomModule());
    }
}
----

It's also possible to `@Inject` the Quarkus `ObjectMapper` and pass it to the `JacksonDataFormat`.

[source,java]
----
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;

@ApplicationScoped
public class Routes extends RouteBuilder {
    @Inject
    ObjectMapper mapper;

    public void configure() {
        JacksonDataFormat dataFormat = new JacksonDataFormat();
        dataFormat.setObjectMapper(mapper);
        // Use the dataFormat instance in a route definition
        from("direct:my-direct").marshal(dataFormat)
    }
}
----

