
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we import a gson library in JShell in Java 9?n
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.
- Related Articles
- How can we implement a map in JShell in Java 9?
- How to import external libraries in JShell in Java 9?
- How can we avoid compilation errors in JShell in Java 9?
- How can we load a source code into JShell in Java 9?
- How can we customize the start of JShell in Java 9?
- How can we execute snippets by ID in JShell in Java 9?
- How can we use @Since annotation using Gson in Java?
- JShell in Java 9?
- How to format a date using the Gson library in Java?
- How to serialize a null field using Gson library in Java?
- How can we read & write a file using Gson streaming API in Java?
- How to debug JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to create a thread in JShell in Java 9?
- How to pretty print JSON using the Gson library in Java?

Advertisements