Serialization and Deserialization
A file can be saved to the disk or sent over the network by just sending the file as it is, byte by byte, from the beginning (as source code, bytecode or binary code). That is not serialization. Serialization is the process of converting an object into a stream of bytes, for storage or transmission, still as the object. This is not the same as just reading the bytes from the beginning and sending or saving. The opposite of serialization is deserialization. Not mush serialization, as a process, is done with primitive objects on their own.
JSON stands for JavaScript Object Notation. JSON is a format for serialization. A Java object (defined) can be converted to a JSON representation (string) for transmission or saving. For reuse, the JSON representation is converted back to the Java object. Gson is a Java library used for conversion in either direction.
To serialize, use the toJson() method of the Gson object. To deserialize, use the fromJson() method of the Gson object. This article explains the basics of serialization of Java objects to JSON representation, with the toJson() method, and deserialization of JSON representation (string), to Java object, with the fromJson() method.
Article Content
Downloading and Setting Up the Gson Library
The Gson library comes as a JAR file. A library like Gson is referred to as a dependency. It is free to download. The rest of this section explains what the author did with his host Ubuntu OS computer. The reader can repeat or modify the approach.
He created a directory, called dependencies, in /home/user/ to have: /home/user/dependencies, where a user should be replaced by the user-name.
He downloaded the library file, gson-2.8.9.jar, from the hyperlink:
https://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/2.8.9/gson-2.8.9.jar
and saved it, as it is, in the dependencies directory.
Next, at the command prompt, he set up (entered) a class variable, as follows:
The Java program should have, as a minimum, the following:
public class TheClass {
public static void main(String[] args) {
Gsongson = new Gson();
/*rest of code */
}
}
The name of the source code file is TheClass.java. Note the name of the imported package, which is in the gson-2.8.9.jar file. The following command line was used to compile the program into byte code:
Note the switch, -classpath . There are two paths here, separated by a colon (no space around the colon). The first is the path to the main file, TheClass.java; and the second is the path to the library file, gson-2.8.9.jar.
The resulting bytecode is run with the following command line:
The switch and the two paths are still there, in their positions, for the same reasons. The bytecode should run successfully, everything being equal.
Primitive Java Objects
This section illustrates what value a primitive object will have after serialization, as JSON string, and what value it will have after deserialization. To use the toJson() and the fromJson() methods, the Gson object has to be created with a statement like:
where gson is the Gson object to be used with its methods: toJson() for serialization, and fromJson() for deserialization.
byte
Consider the following code within the main() method:
The output is 56. This code serializes and deserializes. Note the second argument from fromJson(), which is byte.class. Serialization of a type object becomes a JSON string, and deserialization should go back to the same type. That is why byte.class is present.
int
Consider the following code within the main() method:
The output is 0. Note the second argument from fromJson(), which is int.class.
double
Consider the following code within the main() method:
The output is 7.8. Note the second argument for fromJson(), which is double.class.
char
Consider the following code within the main() method:
The output is E. Note the second argument for fromJson(), which is char.class.
boolean
Consider the following code within the main() method:
The output is false. Note the second argument for fromJson(), which is boolean.class.
null
Consider the following code within the main() method:
The output is null. Note the second argument for fromJson(), which is String.class, for the kind of null.
Array
Array Literal
Consider the following code:
The output is:
After creating the Gson object, a Java double array is created. Next, the array literal is converted into a JSON string. Yes, though the code is concerned with an array here and not a primitive type, the toJson() method is still used, and correspondingly, fromJson() will still be used at the receiver. The JSON string array literal is:
This sting is what is fitted into the stream that is transmitted or saved locally. The fromJson() method converts the JSON array string literal to the Java array (literal) at the receiving end.
Consider the following code, which commences with a Java array of strings, where each string is an item on a reading table:
The output is:
After creating the Gson object, a Java string array is created. Next, the array literal is converted into a JSON string. The JSON string array literal is:
This sting is what is fitted into the stream that is transmitted or saved locally. The fromJson() method converts the JSON array string literal of strings back to the Java array (literal) at the receiving end. Note that the class type (String[]) is needed for backward conversion.
Sending Array Literal with Name of array
The problem with the above scheme is that, at the destination, the array is likely to be given another name for the reconstructed Java code. The array name can be sent, as a single word array, preceding the array of interest to solve this problem. The Java program will receive two arrays at the receiving end and interpret them appropriately.
Object
Object Content
Consider the following code:
It begins with the importation of the Gson package, and then there is the description of a class, called AClass. The class has four fields (properties) and one method. One of the values of the fields is null, and another does not have any value. A suitable code in the main() function for this class is:
The output consists of two lines, which is:
5
After creating the Gson object, another object, obj is instantiated from the class, AClass. Next, the array literal is converted into a JSON string. Yes, though the code is concerned with an instantiated object here and not a primitive type, the toJson() method is still used, and correspondingly, fromJson() will still be used at the receiver. The JSON instantiated (class) object content string is like this:
Note the delimiting braces instead of square brackets to differentiate them from JSON. It consists of key/value pairs. The key is separated from its value with a colon. The pairs are separated from one another by commas.
This should be fitted into the stream for transmission or saving locally. Actually, the JSON string for the object is:
The pair for the field with null value is omitted. The pair for the field with the name but without a value is also omitted. The method name and its definition is also omitted. This means that the class information has to be transmitted as well. This can be done with a preceding JSON array. Nothing has been transmitted in the code samples of this article, so the class information is still available to be used in fromJson() .
The fromJson() method converts the JSON instantiated object string back to a Java object at the receiving end. To have the same name for the object, the object’s name has to be transmitted (separately) at the receiving end. After the object has been recreated at the receiving end, the fields and methods can be accessed (called). In the above code, the method is called to result in 5.
Conclusion
JSON is a serialized format. Java objects can be serialized to JSON format for transmission to another computer or to save locally. At the other end, deserialization takes place to have the same object residing at the source. Deserialization does not take place when the object is saved. Not only arrays and instantiated objects can be serialized. Other objects such as maps and collections can be serialized and deserialized. The Java library that can be used for these processes is the Gson library. Its method, toJson() is used for serialization, and its other method, fromJson(), is used for deserialization.