Home > Java > javaTutorial > How to import the gson library in JShell in Java 9?

How to import the gson library in JShell in Java 9?

王林
Release: 2023-09-05 09:17:07
forward
1030 people have browsed it

在Java 9的JShell中如何导入gson库?

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template