Oct 292013
 
Article Java

JSON (JavaScript Object Notation) is a data interchange format widely used in many environments.

The JSON website includes links to libraries for the processing of this format in many languages, including PHP, Perl and Java. For Java, there are more than twenty links to different implementations of the JSON format processing functionality.

This post analyses the possibilities offered by google-gson (the JSON library offered by Google) , with some code samples written in java to demonstrate how to read and write files in JSON format.

Download and installation of the google-gson library

In the google-gson main page there is a link to download the google-gson installation archive “google-gson-2.2.4-release.zip” (558 KB).

The content of the zip file includes the source code and documentation, together with the jar file that contains the library itself:

As any other java library, this file needs to be added to the CLASSPATH library, or else needs to be explicitly referenced when a program that uses it is compiled and executed.

JSON format

The content of a JSON file may be one of:

  • a simple element (text string, number, boolean value or the null value). Text strings must be enclosed in double quotes, boolean values are “true” or “false”, and the null value is represented by the keyword “null”. Examples:
    • “John Doe”
    • 32
    • false
    • null
  • An ordered list (array) of elements, separated by commas, and enclosed in square brackets “[” and “]”. Each of the elements in the array can be a simple element, an array or a set of (key, value) pairs (as explained below). Examples:
    • [ “John”, 32 ]
    • [ “John”, 32, [ “Music”, “Cinema”], “New York”]

The first example above is an array of two simple elements, and the second example is an array of four elements, the thir element being an array of two simple elements.

  • A set of (key, value) pairs (this kind of structure is known as “hashtable” or “associative array”). The keys is a text string enclosed in double quotes, and de value can be a simple element, an array or a hastable. The key is separated from the value by a colon (:) character, and the whole element is enclosed in curly brackets “{” and “}”. Example:
    • { “boss”: “John”, “employees”: [ “Hellen”, “Louis”] }

In this example, the document is a hashtable with two keys “boss” and “employees”. The value of the first key is a simple element, whilst the value of the second key is an array.

Reading a JSON file in Java

We are going to write a sample program “read_json.java” to read a json file. The program uses the class com.google.gson.JsonParser. This class reads the file, and returns an object of type “com.google.gsonJsonElement”:

The “dumpJSONElement” method in our class “read_json” must determine the type of element received as argument ( one of simple element, hashtable or array), and process it accordingly.

If the element received as argument is a complex element  (array or hashtable), “dumpJSONElement” will call itself recursively:

The complete code of the dumpJSONElement method is:

The “read_json.java” file is compiled and executed with the commands:

and the output printed is:

Writing a JSON file

To convert a java object into a JSON string, the com.google.gson.Gson class provides the method “toJson”.

If the object to be written as JSON is of a class derived from the base “Collection” class “Collection” (ArrayList, Vector,…), toJson converts it into a JSON array.

If the object is of another type, toJson converts it into a set of (key,value) pairs, where the keys are the names of the attributes of the object, and the values are the values of those attributes.

Examples:

1. Simple types. Variables of type String, Integer, etc. are converted to JSON using the following sentences:

 

The code above prints to screen the output:

 

2. Collections

If the object to be converted to JSON if of class Collection or of a class derived from Collection:

In the example above, the result is a JSON array:

3. Objects

Finally, toJson accepts any type of object as input. Let’s define a class “MyObject” with some attributes of simple types “name”, “origin”, and “myString”, and complex attributes “myCollection” and “myVector”:

Next, in the main program, an instance of the MyObject class is created an converted into a JSON string:

and get the following result:

We can see that:

  • toJson has converted the object into a set of (key,value) pairs, where the keys are the names of the attributes.
  • The values of attributes of type Collection (MyCollection, MyVector) have been transformed int arrays.
  • The null value assigned to one of the elements in “myVector” appears in the resultin JSON array.
  • The attribute “myString”, whose value is also null, does not appear in the resulting JSON string.

References

Index of posts related to Java programming

 Posted by at 9:14 pm

 Leave a Reply

(required)

(required)