자바 9 양방향 REPL 도입 JShell이라는 명령줄 도구 . 이를 통해 Java 코드 조각을 실행하고 즉각적인 결과를 얻을 수 있습니다. 클래스 경로를 통해 JShell 세션에서 액세스할 수 있는 외부 클래스를 가져올 수 있습니다. Gson 라이브러리 Java 직렬화/역직렬화 입니다. Java 개체를으로 변환하기 위한 라이브러리 JSON 그 반대도 마찬가지입니다.
아래 코드 스니펫에서 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}"
아래 코드 스니펫에서 Employee 개체 및 결과를 표시합니다.
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.