Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct set of dependencies for using Jackson mapper

Tags:

java

json

jackson

I am new to Jackson and I was writing some code for practice. I found out the the new version of Jackson library can be found on Fasterxml: Jackson, so I added the below dependencies to my Maven pom file:

<dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-annotations</artifactId>     <version>2.2.2</version> </dependency> <dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-core</artifactId>     <version>2.2.2</version> </dependency> <dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-databind</artifactId>     <version>2.2.2</version> </dependency> 

I was expecting that I can use the ObjectMapper directly, however after spending a lot of time I found out that to use the ObjectMapper I have to add the old libraries below:

<dependency>     <groupId>org.codehaus.jackson</groupId>     <artifactId>jackson-mapper-asl</artifactId>     <version>1.9.2</version> </dependency> <dependency>     <groupId>org.codehaus.jackson</groupId>     <artifactId>jackson-core-asl</artifactId>     <version>1.9.2</version> </dependency> 

I am a bit confused. Could someone please tell me why is that?

like image 439
Hossein Avatar asked Aug 25 '13 13:08

Hossein


People also ask

What is Jackson dependency?

The Jackson library is composed of three components: Jackson Databind, Core, and Annotation. Jackson Databind has internal dependencies on Jackson Core and Annotation. Therefore, adding Jackson Databind to your Maven POM dependency list will include the other dependencies as well.

What is the use of Jackson Databind dependency?

Data Binding API is used to convert JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations. It is of two type. Simple Data Binding - Converts JSON to and from Java Maps, Lists, Strings, Numbers, Booleans and null objects.

What is data Mapper for Jackson?

Data Mapper for Jackson Data Mapper package is a high-performance data binding package built on Jackson JSON processor. Licenses. The Apache Software License, Version 2.0.

Should Jackson object mapper be a singleton?

ObjectMapper is a completely thread-safe service class, it is meant to be used as singleton across the lifetime of the application.


2 Answers

<properties>   <!-- Use the latest version whenever possible. -->   <jackson.version>2.4.4</jackson.version> </properties> <dependencies>    <dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-databind</artifactId>     <version>${jackson.version}</version>   </dependency> </dependencies> 

you have a ObjectMapper (from Jackson Databind package) handy. if so, you can do:

JsonFactory factory = objectMapper.getFactory(); 

Source: https://github.com/FasterXML/jackson-core

So, the 3 "fasterxml" dependencies which you already have in u'r pom are enough for ObjectMapper as it includes jackson-databind.

like image 155
ASD Avatar answered Oct 21 '22 10:10

ASD


No, you can simply use com.fasterxml.jackson.databind.ObjectMapper. Most likely you forgot to fix your import-statements, delete all references to codehaus and you're golden.

like image 27
specializt Avatar answered Oct 21 '22 10:10

specializt