How can we import a gson library in JShell in Java 9?


Java 9 introduced an interactive REPL command-line tool named JShell. It allows us to execute Java code snippets and get immediate results. We can import external classes that can be accessed from a JShell session through the classpath. The Gson library is a Java serialization/deserialization library intended for converting Java Objects into JSON and vice-versa.

In the below code snippet, we can set the classpath in JShell

jshell> /env --class-path C:\Users\User\gson.jar
| Setting new options and restoring state.


Once we have imported the gson library in JShell, able to see that library in the list.

jshell> import com.google.gson.*

jshell> /import
| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*
| import com.google.gson.*

jshell> Gson g = new GsonBuilder().setPrettyPrinting().create()
g ==> {serializeNulls:false,factories:[Factory[typeHier ... 78b9],instanceCreators:{}}


In the below code snippet, we have created an Employee class.

jshell> class Employee {
...>       private String firstName;
...>       private String lastName;
...>       private String designation;
...>       private String location;
...>       public Employee(String firstName, String lastName, String desigation, String location) {
...>          this.firstName = firstName;
...>          this.lastName = lastName;
...>          this.designation = designation;
...>          this.location = location;
...>       }
...>       public String getFirstName() {
...>          return firstName;
...>       }
...>       public String getLastName() {
...>          return lastName;
...>       }
...>       public String getJobDesignation() {
...>          return designation;
...>       }
...>       public String getLocation() {
...>          return location;
...>       }
...>       public String toString() {
...>          return "Name = " + firstName + ", " + lastName + " | " +
...>                 "Job designation = " + designation + " | " +
...>                 "location = " + location + ".";
...>       }
...>    }
| created class Employee

jshell> Employee e = new Employee("Jai", "Adithya", "Content Developer", "Hyderabad");
e ==> Name = Jai, Adithya | Job designation = Content D ... er | location = Hyderabad.

jshell> String empSerialized = g.toJson(e)
empSerialized ==> "{\n \"firstName\": \"Jai\",\n \"lastName\": \" ... ation\": \"Hyderabad\"\n}"


In the below code snippet, we can create an instance of an Employee object and display the result.

jshell> System.out.println(empSerialized)
{
   "firstName": "Jai",
   "lastName": "Adithya",
   "designation": "Content Developer",
   "location": "Hyderabad"
}
jshell> Employee e1 = g.fromJson(empSerialized, Employee.class)
e1 ==> Name = Jai, Adithya | Job designation = Content D ... er | location = Hyderabad.

Updated on: 02-Apr-2020

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements