0

Recently I wanted find a POJO to JSON mapper and finally ended up using Jackson because it is recommended in most places, but when I just did a small test I found out that to serialize java object which is even having 2 attributes it takes average time of 450ms

but if I do the mapping manually it takes only less than 10ms , below is my simple code for calculating the time.

public static void main(String[] args) throws Exception{    
    long time = new Date().getTime();
    User user = new User();
    user.set_id("100");
    user.setName("testuser");

    ObjectMapper mapper = new ObjectMapper();
    StringWriter write = new StringWriter();
    mapper.writeValue(write, user);

    long endtime = new Date().getTime();
    System.out.println("Spent Time : "+(endtime-time));
}

So my question is should I still use Jackon? is any other benefits that I have not understood. for your note project that I'm currently working is having spring and mongodb and our task is to develop REST Service, and we don't want a particular request to hang unwanted time.

Imal Hasaranga Perera
  • 9,683
  • 3
  • 51
  • 41
  • If you don't know what Jackson is for in your project and have no need in it - simply don't use it. You can always switch back when you need some specific features – Konstantin V. Salikhov Jul 17 '14 at 07:58
  • Have you had a look at Gson? It's VERY easy to get going with. It's a pretty fast library too! http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ – Ben Dale Jul 17 '14 at 07:59
  • I don't think that `Date` is reliable for measuring the elapsed time. See [this answer](http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java#answer-1776053) – BackSlash Jul 17 '14 at 08:00
  • @KonstantinV.Salikhov No i understand the role of jackon in my project, I need some kind of a pojo to json mapper my problem is should i go with jackon or should I stick to manual pojo to json mapping – Imal Hasaranga Perera Jul 17 '14 at 08:02
  • @BenjaminDale with the performance vise Jackson is ahead of Gson that is why I went with the Jackon at first – Imal Hasaranga Perera Jul 17 '14 at 08:03
  • @BackSlash I just used System.nanoTime() but ended up with the same result but one thing I observed and could not state is that, for creating the first JSON it takes 450 average but if I put this in a loop and create 1000 iterations then the execution time is 782 which means 0.782 for a one request, but can we take it to account because, after an each request to a server there may be a interval time and that period garbage collector might remove the junk and because of this next request might be take 450 again, am I wrong in the assumption ? – Imal Hasaranga Perera Jul 17 '14 at 08:12
  • 3
    This isn't a fair benchmark as you're including the time taken to create the ObjectMapper. Normally with Jackson you would create one ObjectMapper up front (which is the time- consuming bit) and then use the same mapper repeatedly to do conversions. If you want more reliable numbers then create the mapper before you start timing, then use it to do a few hundred or thousand conversions and calculate the average time. – Ian Roberts Jul 17 '14 at 08:13
  • Take a look at this document: http://wiki.fasterxml.com/JacksonBestPracticesPerformance – Alexey Gavrilov Jul 17 '14 at 08:14
  • @IanRoberts thank you very much I'm missing some reading about Jackson. – Imal Hasaranga Perera Jul 17 '14 at 08:18
  • @AlexeyGavrilov thank you for your link, I surely need more reading. – Imal Hasaranga Perera Jul 17 '14 at 08:19

1 Answers1

2

This benchmark result shows Jackson is pretty slow when creating an instance. To use Jackson library efficiently, you have to reuse a single instance of ObjectMapper repeatedly.

If you instantiate a class of JSON library every time when it is used, I recommend to use Gson as the benchmark result shows.

Heejin
  • 4,463
  • 3
  • 26
  • 30
  • The post you are referring is misleading. The ObjectMapper is designed to be shared across invocations: http://wiki.fasterxml.com/JacksonBestPracticesPerformance – Alexey Gavrilov Jul 17 '14 at 08:20
  • @AlexeyGavrilov I don't think it is misleading. The post says `If your project will be creating an instance of the JSON library on the fly, it would be better to use GSON. However, if you have an instance of the library already created, Jackson proves to be faster. As my project is using Spring, it is beneficial to use Jackson, and instantiate the ObjectMapper in the Spring context.` – Heejin Jul 17 '14 at 08:21
  • 1
    Basically, what you are saying in your answer is that if you don't know how to use Jackson properly then use another library. – Alexey Gavrilov Jul 17 '14 at 08:23
  • @AlexeyGavrilov Yes, you're right because the running time deviation of `with instantiation` and `without instantiation` is smaller if use Gson. – Heejin Jul 17 '14 at 08:26