Java 9 แนะนำการโต้ตอบ REPL เครื่องมือบรรทัดคำสั่งชื่อ JShell . ช่วยให้เราสามารถรันโค้ด Java และรับผลลัพธ์ได้ทันที เราสามารถนำเข้าคลาสภายนอกที่สามารถเข้าถึงได้จากเซสชัน JShell ผ่าน classpath ห้องสมุด Gson เป็น Java การทำให้เป็นอนุกรม/ดีซีเรียลไลเซชัน ไลบรารี่สำหรับแปลง Java Objects เป็น JSON และในทางกลับกัน
ในโค้ดด้านล่าง เราสามารถตั้งค่า classpath ใน JShell
jshell> /env --class-path C:\Users\User\gson.jar | Setting new options and restoring state.
เมื่อเรานำเข้า gson . แล้ว ห้องสมุด ใน JShell สามารถเห็นไลบรารีนั้นในรายการ
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:{}}
ในข้อมูลโค้ดด้านล่าง เราได้สร้าง พนักงาน ชั้นเรียน
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}"
ในข้อมูลโค้ดด้านล่าง เราสามารถสร้างอินสแตนซ์ของ พนักงาน วัตถุและแสดงผล
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.