Home >Java >javaTutorial >How to import the gson library in JShell in Java 9?
Java 9 Introduces an interactive REPL command line tool named JShell. It allows us to execute Java code snippets and get results instantly. We can import external classes that can be accessed from the JShell session, via the classpath. Gson Library is a Java serialization/deserialization library for converting Java objects to JSON and vice versa.
In the code snippet below, we can set the classpath in JShell
<strong>jshell> /env --class-path C:\Users\User\gson.jar | Setting new options and restoring state.</strong>
Once we have imported it in JShell gson Library , you can see the library in the list.
<strong>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:{}}</strong>
In the following code snippet, we have created an Employee class.
<strong>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}"</strong>
In the following code snippet, we can create an instance of an Employee object and display the result.
<strong>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.</strong>
The above is the detailed content of How to import the gson library in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!