|
@@ -8,10 +8,9 @@ import com.google.gson.GsonBuilder;
|
8
|
8
|
/**
|
9
|
9
|
* <pre>
|
10
|
10
|
* kr.co.swh.lecture.opensource.gson
|
11
|
|
- * Company.java
|
|
11
|
+ * GsonMain.java
|
12
|
12
|
*
|
13
|
13
|
* 설명 :GSON 예제
|
14
|
|
- * ref. http://1004lucifer.blogspot.kr/2015/04/javagson-gson-java-json.html
|
15
|
14
|
* </pre>
|
16
|
15
|
*
|
17
|
16
|
* @since : 2018. 1. 29.
|
|
@@ -20,30 +19,30 @@ import com.google.gson.GsonBuilder;
|
20
|
19
|
*/
|
21
|
20
|
public class GsonMain {
|
22
|
21
|
public static void main(String[] argv) {
|
23
|
|
- Company company = Company.getCompanyDummy();
|
24
|
|
-
|
25
|
|
- System.out.println("========= Object => Json ==========");
|
26
|
|
- String company2Json = new Gson().toJson(company);
|
27
|
|
- System.out.println(company2Json);
|
28
|
|
-
|
29
|
|
- System.out.println("========= Json => Object =========");
|
30
|
|
- Company json2Company = new Gson().fromJson(company2Json, Company.class);
|
31
|
|
- printCompanyObject(json2Company);
|
32
|
|
-
|
33
|
|
- System.out.println("========= Object => Json =========");
|
34
|
|
- String company2JsonIsNull = new GsonBuilder().serializeNulls().create().toJson(company);
|
35
|
|
- System.out.println(company2JsonIsNull);
|
36
|
|
-
|
37
|
|
- System.out.println("========= Json => Object =========");
|
38
|
|
- Company json2CompanyIsNull = new Gson().fromJson(company2Json, Company.class);
|
39
|
|
- printCompanyObject(json2CompanyIsNull);
|
|
22
|
+ Academy academy = Academy.getDummy();
|
|
23
|
+
|
|
24
|
+ System.out.println("1. Object -> Json");
|
|
25
|
+ String academy2Json = new Gson().toJson(academy);
|
|
26
|
+ System.out.println(academy2Json);
|
|
27
|
+
|
|
28
|
+ System.out.println("2. Object => Json");
|
|
29
|
+ String academy2JsonIsNull = new GsonBuilder().serializeNulls().create().toJson(academy);
|
|
30
|
+ System.out.println(academy2JsonIsNull);
|
|
31
|
+
|
|
32
|
+ System.out.println("3. Json => Object");
|
|
33
|
+ Academy json2Academy = new Gson().fromJson(academy2Json, Academy.class);
|
|
34
|
+ printAcademy(json2Academy);
|
|
35
|
+
|
|
36
|
+ System.out.println("4. Json => Object");
|
|
37
|
+ Academy json2AcademyIsNull = new Gson().fromJson(academy2JsonIsNull, Academy.class);
|
|
38
|
+ printAcademy(json2AcademyIsNull);
|
40
|
39
|
}
|
41
|
40
|
|
42
|
|
- private static void printCompanyObject(Company company) {
|
43
|
|
- List<Person> personList = company.getEmployees();
|
44
|
|
- System.out.println("userName: " + company.getName());
|
45
|
|
- for (Person person : personList) {
|
46
|
|
- System.out.println(person);
|
|
41
|
+ private static void printAcademy(Academy company) {
|
|
42
|
+ List<Student> studentList = company.getStudents();
|
|
43
|
+ System.out.println("학원명: " + company.getName());
|
|
44
|
+ for (Student s : studentList) {
|
|
45
|
+ System.out.println(s);
|
47
|
46
|
}
|
48
|
47
|
}
|
49
|
|
-}
|
|
48
|
+}
|