Using the Jackson Maven Dependency

Jackson is an extremely fast library that will take a Plain Old Java Object (POJO) and serialize it to JavaScript Object Notation (or JSON) and vice versa.  Maven is great for handle dependencies.  Normally to use a library in Java you have to make sure the classes of a library is in your classpath.  Most classes are packaged in a jar library.  With Maven you can point to a repository and have it get the libraries for you.  These are very brief explanations. You can find more information about Maven here and Jackson here.

Now that the introductions are out of the way let’s get to it.  After searching the documentation for Jackson I didn’t see a tutorial on using the Jackson plug-in for Maven.  I knew it was simple to setup if you knew the Maven repository and artifact name.  I looked but couldn’t find the repository in the documentation.  I did a little digging and eventually found it out.  Below is an example and a few snippets to get you started.

Step 1:  Add the Repository

This change is made in your pom.xml file. If you don’t have a repositories tag in your pom.xml you will have to add one. Please take a look at the project in the zip file further down to see it in action.  The key here is that Jackson is in the Codehaus Maven Repository.  That is where I spent the most time figuring this out.

<repository>
  <id>codehaus</id>
  <url>http://repository.codehaus.org/org/codehaus</url>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
  <releases>
    <enabled>true</enabled>
  </releases>
</repository>

Step 2: Add the dependency

There are a total of six artifacts in Jackson 1.6.  I found a list on this page.  I only need one for the example in the test. In my pom.xml I added the following to my dependencies section.

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.6.4</version>
  <scope>compile</scope>
</dependency>

Step 3: Create and use a Mapper

In JacksonTest.java I show how to create and use the Mapper.  To serialize a basic object like a String, its three lines of code in this example.  Look at lines 35, 45, and 59.

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
private ObjectMapper mapper;
 
/**
* Create the test case
*
* @param testName name of the test case
*/
public JacksonTest(String testName) {
  super(testName);
 
  mapper = new ObjectMapper();
}
 
/**
* @return the suite of tests being tested
*/
public static Test suite() {
  return new TestSuite(JacksonTest.class);
}
 
/**
* Basic test to verify Jackson serialization
*/
public void test_HelloString() throws IOException {
  String jsonResult = mapper.writeValueAsString("hello");
 
  assertEquals(jsonResult, "\"hello\"");
}
...

Example Project

MavenJacksonPlugin.zip – Here is a Maven project that shows all of the components together.  I released the test under the Apache Software License.  Please abide by the terms of the license if you choose to use it.

More Information

JSON data binding performance: Jackson vs Google-gson vs BerliOS JSON Tools – A performance test that shows Jackson outperforming Google’s Gson.

Be Sociable, Share!