JSON or JavaScript Object Notation is a lightweight data-interchange format which is an alternative to XML. It is language and platform independent. Let’s check out best seven Java JSON libraries for you and in the process we’ll find out which is faster.
Jackson is a “multi-purpose Java library for processing JSON who aims to be the best possible combination of fast, correct, lightweight and ergonomic for developers”. Jackson offers three methods for processing JSON format: Streaming API or incremental parsing/generation, Tree model and Data binding. As we are focusing on converting Java object to and from JSON, we need the third method. But first of all you need to download Jackson from http://repo1.maven.org/maven2/com/fasterxml/jackson/.
Now to obtain a JSON complex object from Java POJO’s, we need to follow the below mentioned method:
The result is like the following:
Now, convert the POJO’s to JSON and write it to a file for which an ObjectMapper is used:
Then, we will use this ObjectMapper to write the values directly to a file:
With Jackson, you can convert Java objects to JSON format and save it to a file and backwards too.
2. Google-Gson Library:
Gson is a Java library which allows converting Java objects into their JSON representation and JSON strings to an equivalent Java object without any Java annotation. Gson needs only one .jar, the gson-2.2.4.jar, and it can be found at http://code.google.com/p/google-gson/downloads/list. Look at an example of Gson.
Then to read it back as a Java object do this:
3. JSON-lib Library:
JSON-lib library is a Java library which is capable of transforming beans, maps, collections, java arrays and XML to JSON and back again to beans and DynaBeans. JSON-lib is downloadable at http://sourceforge.net/projects/json-lib/files/
Now write your Java object to file. The POJO’s are same like the Jackson example:
Then you can get the Java object back:
4. Flexjson Library:
Flexjson is a lightweight library which allows serializing and deserializing Java objects into and from JSON format. Flexjson is downloadable from http://sourceforge.net/projects/flexjson/files/. The first step is to have an instance of JSONSerializer and then use that instance to write the object to file:
5. Json-io Library:
This library is capable of JSON processing and it is downloadable from the Maven Central Repository and does not have other dependencies. We will use Json-io JsonWriter class in order to write the POJO’s to file:
To read the JSON file back to POJO’s:
6. Genson Library:
Genson is extensible, scalable and easy to use open-source Java library. It allows converting Java objects to JSON and vice-versa. It is downloadable from http://code.google.com/p/genson/downloads/list and does not have other dependencies. We will write the Java object to file:
Then read it back to a Java object:
7. JSONiJ Library:
This is the last Java JSON binding library and it is JSON parser capable of converting Java objects into their JSON representation and JSON strings to an equivalent Java object. It is downloadable from https://bitbucket.org/jmarsden/jsonij/downloads and does not have other dependencies.
Write a Java object to a JSON file using this:
Then read it back thus: