Wednesday, October 18, 2017

Top 5 JSON libraries for a java developer should know


The JSON format is one of the most popular formats to transfer and exchange data in web. Almost all RESTful web services take JSON input and provide JSON output but unfortunately JDK doesn't have built-in support for one of the most common web standard like JSON. As a Java developer if you want to develop RESTful web service and produce JSON data or if you are developing a client to an existing RESTFul web services and want to consume JSON response, you don't need to be disappointed. Fortunately, there are so many open source libraries and API available for creating, parsing and processing JSON response in Java e.g. Jackson, Google GSon, json-simple etc.
Actually, there are numerous JSON libraries exists in Java but you don't need to learn all of them, learning just one of them e.g. Jackson should be enough, but, on the other hand, it's worth knowing what are some of the most popular JSON parsing library exists in your disposal. In this article, I am going to share 5 useful JSON libraries which I believe every Java JEE developer should be aware of.
If you are new to JSON, probably heard about it but doesn't know what is JSON there here is a brief introduction of JSON for you. The JSON is an acronym for JavaScript Object Notation, is a lightweight data-interchange format, an alternative to XML, but smaller, faster and easier to parse. Because JSON uses the JavaScript syntax for describing data objects, it is language and platform independent and many parsers and libraries have been developed over the years. You can also read Java XML and JSON to learn more about pros and cons of JSON over XML.

5 Useful JSON libraries in Java

Here is my list of most useful and essential JSON libraries for Java developers. Though I have used them some or other time or other I mostly prefer Jackson because it's a feature rich and I believe in consistency. It doesn't mean those other libraries are not useful, they also have their own strength e.g. Gson is much simpler to use as compared to Jackson and json-simple is a really light-weight library without any third party dependency.
As the purpose of this article is to make you, a Java developer aware of useful JSON library, I leave the decision of choosing the JSON library to yourself. Depending upon your need, you can choose any of them.

Jackson

Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers.
Jackson offers three methods for processing JSON format, each of them having it’s pros and cons:

  1. Streaming API or incremental parsing/generation: reads and writes JSON content as discrete events
  2. Tree model: provides a mutable in-memory tree representation of a JSON document
  3. Data binding: converts JSON to and from POJO’s
If you are only interested in converting Java object to and from JSON string then the third method is most appropriate for you.
Pros of Jackson is that It provides heaps of features, and looks to be a good tool for reading and writing JSON in a variety of ways, but same time its size becomes a disadvantage if your requirement is just to serialize and deserialize Java object to JSON String. 
In order to use Jackson, you can include following maven dependency or manually include jackson-core-2.3.1.jar, jackson-databind-2.3.1.jar, and jackson-annotations-2.3.1.jar in Classpath.

GSON

The second Java JSON binding library we will discuss is Gson, or if you prefer the full name, the google-gson library. Gson is a Java library capable of converting Java objects into their JSON representation and JSON strings to an equivalent Java object without the need for placing Java annotations in your classes.
Some of the salient features of Gson library are:

  1. Provides simple toJson() and fromJson methods to convert Java objects to JSON and vice-versa
  2. Supports arbitrarily complex objects
  3. It has extensive support of Java Generics
  4. Allow custom representation for objects
  5. Allow pre-existing unmodifiable objects to be converted to and from JSON

 json-simple 

The json-simple is one of the simplest JSON library, also lightweight. You can use this library to encode or decode JSON text. It's an open source lightweight library which is flexible and easy to be used by reusing Map and List interfaces from JDK. A good thing about this library that it has no external dependency and both source and binary are JDK 1.2 compatible.

Pros of Json-simple is that it is lightweight, just 12 classes and it provides support for Java IO readers and writers. You can take your decision better if you know about JSON format i.e.g how information is represented there. 
If you are looking for a simple lightweight Java library that reads and writes JSON and supports Streams, json-simple is probably a good match. It does what it says with just 12 classes, and works on legacy (1.4) JREs as well.
In order to use JSON-Simple API, you need to include maven dependency in your project's pom.xml file or alternatively, you can also include following JAR files in your classpath.

Flexjson

Flexjson is another lightweight library for serializing and deserializing Java objects into and from JSON format allowing both deep and shallow copies of objects. The depth to which an object is serialized can be controlled with Flexjson and thus making it similar to lazy-loading, allowing us to extract only the information we need. This is not the case since we want an entire object to be written to file, but it’s good to know that it can do that.
If you know that you are going to use only a small amount of data in your application and you wish to store or read it to and from JSON format, you should consider using Flexjson or Gson.

JSON-lib

JSON-lib is a Java library, based on the work by Douglas Crockford, capable of transforming beans, maps, collections, java arrays and XML to JSON and back again to beans and DynaBeans.
If you are going to use large amounts of data and wish to store or read it to and from JSON format, you should consider using Jackson or JSON-lib.
That's all about some of the useful JSON libraries in Java. You can use these libraries to parse JSON String and generate Java objects or create JSON String from your existing Java objects.
If you are dealing with Web services which return JSON response then these libraries are very important for you. You can choose between them depending upon your need e.g. if you need features and speed then Jackson is probably the best, if you need simplicity then Google Gson looks better to me and if you are concerned about third party dependencies then json-simple or Flexjson can be a good choice.

1 comment:

Java garbage collection

In this post , we ’ ll take a look at how garbage collection works , why it ’ s important in Java , and how it works in...