DEV Community

Zaccheaus Amenya
Zaccheaus Amenya

Posted on • Edited on

Gson, Moshi, Jackson

Retrofit is generally regarded as the lone constant in the network layer of every program. On the other hand, JSON Parser is used to deserialize responses into data objects or serialize information objects into requests.

Gson

The most widely used of the lot is Google's own JSON Parser package for Android (sitting at 18.5k Github stars, vs 5.9k and 6.5k). Due to the introduction of its 1.0 version in 2008, it is also the oldest.

We can transform Java objects into JSON representations using the Gson Java package. Additionally, We can use it to translate a JSON string into a corresponding Java object.

Why ought you use this library?

  • Gson is a standardized library run by Google that adheres to specific standards.
  • Efficient This Java standard library addition is dependable, quick, and efficient.
  • Enhanced optimization has been applied to the library.
  • Support for Generics It offers a lot of support for generics.
  • Supports large inheritance hierarchies and complicated objects with complex inner classes.

Features of Gson

  • Easy to use − Gson API provides a high-level facade to simplify commonly used use-cases.
  • No need to create mapping − Gson API provides default mapping for most of the objects to be serialized.
  • Performance − Gson is relatively fast and is of low memory footprint. It is suitable for large object graphs or systems.
  • Clean JSON − Gson creates a neat and compact JSON result that is easy to read.
  • No Dependency − Gson library does not require any other library besides JDK. -Open Source − Gson library is open-source; it is freely available.

Jackson

Gson's longest rival is trying to outperform it with a quicker and more feature-rich parser. Fun fact: Its 1.0 version in 2009 went by the codename "Hazelnut," which was chosen because many people choke on nuts.
It is a prevalent library to map JSON responses to the POJO or Model classes in Android. The Jackson parser has a better parsing speed as compared to other popular parsing libraries like JSONP, GSON,
It contains a built-in Object Mapper class that analyzes JSON files and deserializes them into custom Java objects. It facilitates the production of JSON from Java objects.

Jackson is a JSON API for JavaJava comprising:

  • Jackson Core(Streaming API) – low-level streaming API is defined and contains JSON-specific implementations
  • Jackson Annotations – includes the standard Jackson annotations.
  • Jackson Databind – provides support for data-binding (and object serialization) on streaming packages; it is dependent on the streaming and annotations packages.

Some of the most notable features of Jackson for Java include:

  • The capacity to annotate fields to map them to particular JSON keys. You can use Jackson to annotate fields in Java objects so that they correspond to specific JSON document keys. Working with complex JSON documents is now considerably simpler as a result. For instance, We can use the @JsonProperty("name") annotation to access a field in a Java object that is mapped to a "name" key in a JSON document.
  • POJOs (Plain Old Java Objects) and JAXB beans (Java Architecture for XML Binding): Jackson is compatible with both JAXB beans and POJOs. With no boilerplate code needed, you can now serialize and deserialize objects.
  • Various modules offer additional functionality: Jackson includes several modules that provide additional functionality. These modules all support the XML, YAML, and CSV formats.

Moshi

The most recent of the three parsers was initially made available in 2015. It was created by Square, the same company that made Retrofit, and some Gson team members are involved.
When compared to GSON, the fact that Kotlin was considered when writing it shines. It is considerably simpler to use and create tests for a polymorphic adapter or a primary Type adapter. This allows us to enhance our projects' code quality further.

Will there ever be circumstances where one is preferable to the other? Maybe. Can we gather them all together to decide on an objective best? We'll soon find out, though.

Moshi offers a more condensed API than libraries like Jackson or Gson without sacrificing performance. As a result, we can design more testable code, making it simpler to integrate into our apps. In some circumstances, such as developing for Android, a lesser reliance may be crucial.

Standard Java types are already supported by Moshi, which accurately converts to and from JSON. This includes:

  • All primitives – int, float, char, etc.
  • All the JavaJava boxed equivalents – Integer, Float, Character, etc.
  • String
  • Enums
  • Arrays of these types
  • Standard Java collections of these types – List, Set, Map

In addition, Moshi will automatically transform any random Java bean into a JSON object, with the values converted per the same guidelines as any other kind. This indicates that Java beans are successfully serialized as profoundly as necessary within Java beans.
We then get access to specific additional conversion rules thanks to the Moshi-adapters dependency, including:

An Enums adapter is a bit more robust, enabling a fallback value when reading an uncertain value from JSON, a Java adapter. util
supporting the RFC-3339 format for dates

Top comments (0)