loading...

Why Jackson is Better Than Gson

neelkamath profile image Neel Kamath ・2 min read

Gson has over 17k stars on GitHub despite not providing even a fraction of Jackson's API. People often think that Gson is easier because it has more stars, is made by Google, and only has a single repo. Even I initially used to use Gson!

However, this is not the case. Here are the reasons why Jackson is better.

  • Faster.

  • Better language support. They have extensions for Scala, Kotlin, etc. For example, if you're using Gson in Kotlin, you'd have to write ugly type tokens; but in Jackson for Kotlin, you can deserialize collections just as easily as a primitive.

  • Extremely importantly, unlike Gson which bypasses the type checker and fills nulls for POJO fields missing from the JSON, Jackson gives you a helpful error message when a field which shouldn't be null is missing.

  • Built-in serializers/deserializers. Unlike Gson which only offers rudimentary serialization options for important types such as dates, Jackson provides easily pluggable modules.

  • Object mapping. There are three steps to serializing/deserializing JSON: JSON string <--> HashMap <--> POJO. A lot of times, you only need to convert between JSON strings and POJOs. But it isn't unusual to convert between a HashMap and a POJO (e.g., a GraphQL engine providing a HashMap to the resolver; an HTTP API endpoint accepting application/ x-www-form-urlencoded, which is parsed as a HashMap). Gson only provides functions to convert between the JSON string and POJO. This makes converting JSON from a HashMap to a POJO require an additional step.

  • It supports not only JSON, but XML, Protobuf, etc.

  • Every major library uses Jackson, and hence you already get Jackson bundled.

  • More annotations.

So not only is Jackson more powerful, but it's easier and faster!

Posted on by:

neelkamath profile

Neel Kamath

@neelkamath

Undergraduate who likes writing servers in Kotlin

Discussion

markdown guide