399

After searching through some existing libraries for JSON, I have finally ended up with these two:

  • Jackson
  • Google GSon

I am a bit partial towards GSON, but word on the net is that GSon suffers from a certain celestial performance issue (as of Sept 2009).

I am continuing my comparison; in the meantime, I'm looking for help to make up my mind.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • 3
    Also, for Android usage, latest performance benchmark I have seen is this: http://www.martinadamek.com/2011/02/04/json-parsers-performance-on-android-with-warmup-and-multiple-iterations/ – StaxMan Feb 15 '11 at 22:19
  • 1
    [Latest CowTalk performnce benchmark.](http://www.cowtowncoder.com/blog/archives/2011/01/entry_438.html#links) - January, 08, 2011 – Iogui Feb 24 '11 at 03:42
  • 8
    One quick note: anyone choosing GSon should make sure to use 2.1 -- its performance is finally measurably better than earlier versions. – StaxMan Feb 11 '12 at 21:07
  • 65
    With 74 up-votes as of now, this question obviously has some valuable answers. Good answers trump "not constructive" questions. Voting to reopen. – Nicholas Aug 22 '13 at 21:03
  • 1
    Jackson's documentation is getting ridiculously complex now . . . – dongshengcn Sep 17 '13 at 19:18
  • @dongshengcn perhaps so for more complex features, but basic usage is the same as it has always been. With all functionality covering polymporphic typing, handling of true object identity and so on, there is also need for describing complex handling. – StaxMan Mar 12 '15 at 23:03
  • Note for android users: gson 2.6.2: 1329 methods, jackson-databind 2.7.3: 10753 methods. Source: http://www.methodscount.com/ – Denys Kniazhev-Support Ukraine Apr 19 '16 at 12:30
  • Hi I just wanna share with u this link, maybe for new projects u can use it: http://jsoniter.com/ – Chris Sim Jan 16 '17 at 08:06
  • Comparison based on what? question is still too broad .. please to make it more in order to reopen the question again. – Maher Abuthraa Mar 19 '17 at 08:35
  • Question is too broad. But this feature comparison might help. http://itsallbinary.com/java-vs-groovy-scala-kotlin-code-comparison-of-jvm-languages/ . Mainly you have to go by features you need to choose right library amongst top json libraries. – Ravi K Jul 05 '19 at 21:55

5 Answers5

133

I did this research the last week and I ended up with the same 2 libraries. As I'm using Spring 3 (that adopts Jackson in its default Json view 'JacksonJsonView') it was more natural for me to do the same. The 2 lib are pretty much the same... at the end they simply map to a json file! :)

Anyway as you said Jackson has a + in performance and that's very important for me. The project is also quite active as you can see from their web page and that's a very good sign as well.

Hernán Eche
  • 6,529
  • 12
  • 51
  • 76
mickthompson
  • 5,442
  • 11
  • 47
  • 59
  • 2
    Also, Google GSon does not yet suppor circular references. Does Jackson handle them ? – Guido Mar 05 '10 at 08:12
  • 1
    Circular References support... that should be a primary feature but I'm not sure if it does support them, I've never encountered a circular reference so far (even if they should be quite common I think, especially in the model). Here's another benchmark that can highlight how fast Jackson is if compared to GSon. It looks 100x faster in Serialization/Deserialization http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking – mickthompson Mar 05 '10 at 08:45
  • 1
    Jackson does not handle circular references currently. If that is important, XStream does; not sure if any native json package does (flex-json perhaps?) – StaxMan Mar 12 '10 at 07:39
  • 13
    As of version 1.6, Jackson does support circular references. See [Handle bi-directional references using declarative methods](http://wiki.fasterxml.com/JacksonFeatureBiDirReferences) for reference. – Ophir Radnitz Apr 10 '11 at 20:41
  • Jackson has more security issue, accodring to fortify – TuGordoBello Apr 10 '19 at 23:43
  • Also need to notice that Jackson is using catchable exception, where Gson not. So if you are using Jackson, you should handle those exceptions in your code. For better and for worse... – Tamir Adler Feb 04 '22 at 12:03
88

Jackson and Gson are the most complete Java JSON packages regarding actual data binding support; many other packages only provide primitive Map/List (or equivalent tree model) binding. Both have complete support for generic types, as well, as enough configurability for many common use cases.

Since I am more familiar with Jackson, here are some aspects where I think Jackson has more complete support than Gson (apologies if I miss a Gson feature):

  • Extensive annotation support; including full inheritance, and advanced "mix-in" annotations (associate annotations with a class for cases where you can not directly add them)
  • Streaming (incremental) reading, writing, for ultra-high performance (or memory-limited) use cases; can mix with data binding (bind sub-trees) -- EDIT: latest versions of Gson also include streaming reader
  • Tree model (DOM-like access); can convert between various models (tree <-> java object <-> stream)
  • Can use any constructors (or static factory methods), not just default constructor
  • Field and getter/setter access (earlier gson versions only used fields, this may have changed)
  • Out-of-box JAX-RS support
  • Interoperability: can also use JAXB annotations, has support/work-arounds for common packages (joda, ibatis, cglib), JVM languages (groovy, clojure, scala)
  • Ability to force static (declared) type handling for output
  • Support for deserializing polymorphic types (Jackson 1.5) -- can serialize AND deserialize things like List correctly (with additional type information)
  • Integrated support for binary content (base64 to/from JSON Strings)
StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • 7
    Actually, this post -- http://www.cowtowncoder.com/blog/archives/2010/11/entry_434.html -- summarizes many of Jackson features that are not found in other packages. – StaxMan Mar 02 '11 at 23:02
  • 15
    I would consider not requiring annotations to be a feature of GSON, not a deficiency (which you have listed at least 3 times above). – orbfish Jan 01 '13 at 18:24
  • 9
    Neither Jackson nor Gson requires use of annotations. But having annotations as an option is a valuable feature in my opininion (esp. "mix-in annotations" which is additional processing option to allow associating external configuration). – StaxMan Jan 03 '13 at 18:43
  • 3
    Gson allows you to register an InstanceCreator to specify an alternate way to construct an instance instead of using a default constructor. – inder Mar 11 '14 at 19:17
42

Gson 1.6 now includes a low-level streaming API and a new parser which is actually faster than Jackson.

inder
  • 1,774
  • 1
  • 15
  • 15
  • I would be interested in seeing a measurement that backs this up. At least measurements at: http://wiki.fasterxml.com/JacksonInFiveMinutes still indicate that GSON is not competitive with other Java json packages. – StaxMan Jan 28 '11 at 19:46
  • 2
    We have micro-benchmarks available (checked into Gson subversion repository under trunk/metrics directory) that show that on simple object conversions, the low-level streaming API could be upto 10x faster. There are other benchmarks too (which I need to encourage the original author to publish) that this low-level API currently beats other libraries including Jackson. However, creating comprehensive and representative benchmarks is going to take some time and effort. – inder Feb 07 '11 at 13:12
  • 4
    One more data point: jvm-serializers (https://github.com/eishay/jvm-serializers) now has "gson/manual" test which uses GSON streaming api as alternative to data binding. Once author runs 'official' numbers, wiki can be updated. But from running this locally, I don't think it supports statements of being super fast. – StaxMan Jun 08 '11 at 00:01
  • 14
    (addition to above: official numbers were included -- streaming Gson is faster than databinding, but not up to Jackson performance level) – StaxMan May 03 '12 at 01:08
  • 11
    ... for anyone following up, Gson 2.1 eventually did come up with significant and measurable performance improvements. – StaxMan Mar 17 '14 at 19:52
14

Adding to other answers already given above. If case insensivity is of any importance to you, then use Jackson. Gson does not support case insensitivity for key names, while jackson does.

Here are two related links

(No) Case sensitivity support in Gson : GSON: How to get a case insensitive element from Json?

Case sensitivity support in Jackson https://gist.github.com/electrum/1260489

Community
  • 1
  • 1
snegi
  • 636
  • 1
  • 6
  • 22
  • The snippet for case sensitivity support in Jackson **does not work**. Or rather, it only works for top-level properties. Try nesting a property and you'll see your solution doesn't work. – Andres F. Oct 16 '15 at 19:08
  • 2
    Actually there is formal support for case-insensitive properties, `MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES`, added in Jackson 2.5. So no need to add custom code for that. – StaxMan Jan 07 '16 at 01:13
7

It seems that GSon don't support JAXB. By using JAXB annotated class to create or process the JSON message, I can share the same class to create the Restful Web Service interface by using spring MVC.

raymond.mh.ng
  • 71
  • 1
  • 1
  • If you already have annotations on your data classes for Hibernate, you don't want another set for JAXB anyway. – orbfish Jan 01 '13 at 18:25
  • 1
    It's nice to be able to add support for "foreign" annotations. Jackson has optional JAXB annotation module, as well as Hibernate module for couple of its annotations (for transiency, lazy loading). Maybe Gson could be extended to allow modular extensions as well. – StaxMan Mar 17 '14 at 19:54
  • 2
    Especially as JAXB is a standard! – maxxyme Oct 21 '16 at 15:41