Ver código fonte

jackson json 수정 및 xml 추가

tobby48 7 anos atrás
pai
commit
fd70305398

+ 7
- 0
pom.xml Ver arquivo

@@ -100,6 +100,13 @@
100 100
 			<artifactId>jackson-databind</artifactId>
101 101
 			<version>${jackson.version}</version>
102 102
 		</dependency>
103
+		<!-- 파일 포맷 XML -->
104
+		<dependency>
105
+			<groupId>com.fasterxml.jackson.dataformat</groupId>
106
+			<artifactId>jackson-dataformat-xml</artifactId>
107
+			<version>${jackson.version}</version>
108
+		</dependency>
109
+		<!-- 파일 포맷 JSON -->
103 110
 		<dependency>
104 111
 			<groupId>com.google.code.gson</groupId>
105 112
 			<artifactId>gson</artifactId>

+ 27
- 9
src/main/java/kr/co/swh/lecture/opensource/jackson/JacksonMain.java Ver arquivo

@@ -1,15 +1,18 @@
1 1
 package kr.co.swh.lecture.opensource.jackson;
2 2
 
3 3
 import java.io.InputStream;
4
+import java.util.ArrayList;
5
+import java.util.List;
4 6
 
7
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
5 8
 import com.fasterxml.jackson.databind.ObjectMapper;
6 9
 
7 10
 /**
8 11
  * <pre>
9 12
  * kr.co.swh.lecture.opensource.jackson
10
- * JacksonMain.java
13
+ * JacksonJsonMain.java
11 14
  *
12
- * 설명 :Jackson 예제
15
+ * 설명 :Jackson Json 예제
13 16
  * </pre>
14 17
  * 
15 18
  * @since : 2018. 1. 29.
@@ -24,25 +27,40 @@ public class JacksonMain {
24 27
 	@SuppressWarnings("static-access")
25 28
 	public JacksonMain(){
26 29
 		is = Thread.currentThread().getContextClassLoader().getSystemResourceAsStream("json.json");
30
+		mapper.setSerializationInclusion(Include.NON_NULL);		//	Serialize 시에 무시, Deserialize 시에 초기값
27 31
 	}
28 32
 	
29
-	public void print() throws Exception{
33
+	public void deserialize() throws Exception{
30 34
 		StudentList value = mapper.readValue(is, StudentList.class);
31 35
 		System.out.println(value);
32 36
 	}
33 37
 	
34
-	public void write() throws Exception{
38
+	public void serialize() throws Exception{
39
+		StudentList list = new StudentList();
40
+		List<Student> studentList = new ArrayList<Student>();
41
+		
35 42
 		Student s1 = new Student();
36 43
 		s1.setName("돌쌤");
37 44
 		s1.setAge(18);
38
-		System.out.println(mapper.writeValueAsString(s1));
45
+		List<String> addressList = new ArrayList<String>();
46
+		addressList.add("안흥");
47
+		addressList.add("증포");
48
+		s1.setAddresses(addressList);
49
+		studentList.add(s1);
50
+		
51
+		Student s2 = new Student();
52
+		s2.setName("하마쌤");
53
+		s2.setAge(28);
54
+		studentList.add(s2);
55
+		
56
+		list.setStudents(studentList);
57
+		System.out.println(mapper.writeValueAsString(list));
39 58
 	}
40 59
 	
41 60
 	public static void main(String[] args) throws Exception  {
42 61
 		// TODO Auto-generated method stub
43 62
 		JacksonMain jackson = new JacksonMain();
44
-		jackson.print();
45
-		jackson.write();
63
+		jackson.deserialize();
64
+		jackson.serialize();
46 65
 	}
47
-
48
-}
66
+}

+ 2
- 2
src/main/java/kr/co/swh/lecture/opensource/jackson/Student.java Ver arquivo

@@ -20,5 +20,5 @@ import lombok.Data;
20 20
 public class Student {
21 21
 	private String name;
22 22
 	private int age;
23
-	private List<String> address;
24
-}
23
+	private List<String> addresses;
24
+}

+ 3
- 1
src/main/java/kr/co/swh/lecture/opensource/jackson/StudentList.java Ver arquivo

@@ -19,4 +19,6 @@ import lombok.Data;
19 19
 @Data
20 20
 public class StudentList {
21 21
 	private List<Student> students;
22
-}
22
+	private String academyName;
23
+	private String location;
24
+}

+ 19
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/architecture/IService.java Ver arquivo

@@ -0,0 +1,19 @@
1
+package kr.co.swh.lecture.opensource.jackson.architecture; 
2
+
3
+
4
+/**
5
+ * <pre>
6
+ * kr.co.swh.lecture.opensource.jackson.architecture 
7
+ * IService.java
8
+ *
9
+ * 설명 :
10
+ * </pre>
11
+ * 
12
+ * @since : 2019. 3. 29.
13
+ * @author : tobby48
14
+ * @version : v1.0
15
+ */
16
+public interface IService {
17
+	public void deserialize() throws Exception;
18
+	public void serialize() throws Exception;
19
+}

+ 70
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/architecture/Jackson.java Ver arquivo

@@ -0,0 +1,70 @@
1
+package kr.co.swh.lecture.opensource.jackson.architecture;
2
+
3
+import java.io.InputStream;
4
+import java.util.ArrayList;
5
+import java.util.List;
6
+
7
+import com.fasterxml.jackson.databind.ObjectMapper;
8
+import com.fasterxml.jackson.databind.SerializationFeature;
9
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
10
+
11
+/**
12
+ * <pre>
13
+ * kr.co.swh.lecture.opensource.jackson.architecture
14
+ * Jackson.java
15
+ *
16
+ * 설명 :Jackson Json 예제
17
+ * </pre>
18
+ * 
19
+ * @since : 2018. 1. 29.
20
+ * @author : tobby48
21
+ * @version : v1.0
22
+ */
23
+public class Jackson implements IService{
24
+	
25
+	private ObjectMapper mapper;
26
+	private InputStream is;
27
+	
28
+	@SuppressWarnings("static-access")
29
+	public Jackson(String fileName){
30
+		String suffixName = fileName.substring(fileName.length()-4, fileName.length());
31
+		if(suffixName.equalsIgnoreCase("json")) {
32
+			mapper = new ObjectMapper();
33
+		}else {
34
+			mapper = new XmlMapper();
35
+			mapper.enable(SerializationFeature.INDENT_OUTPUT);
36
+		}
37
+		is = Thread.currentThread().getContextClassLoader().getSystemResourceAsStream(fileName);
38
+	}
39
+	
40
+	@Override
41
+	public void deserialize() throws Exception {
42
+		// TODO Auto-generated method stub
43
+		StudentList value = mapper.readValue(is, StudentList.class);
44
+		System.out.println(value);
45
+	}
46
+	
47
+	@Override
48
+	public void serialize() throws Exception {
49
+		// TODO Auto-generated method stub
50
+		StudentList list = new StudentList();
51
+		List<Student> studentList = new ArrayList<Student>();
52
+		
53
+		Student s1 = new Student();
54
+		s1.setN("돌쌤");
55
+		s1.setAge(18);
56
+		List<String> addressList = new ArrayList<String>();
57
+		addressList.add("안흥");
58
+		addressList.add("증포");
59
+		s1.setAddresses(addressList);
60
+		studentList.add(s1);
61
+		
62
+		Student s2 = new Student();
63
+		s2.setN("하마쌤");
64
+		s2.setAge(28);
65
+		studentList.add(s2);
66
+		
67
+		list.setStudent(studentList);
68
+		System.out.println(mapper.writeValueAsString(list));
69
+	}
70
+}

+ 26
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/architecture/JacksonMain.java Ver arquivo

@@ -0,0 +1,26 @@
1
+package kr.co.swh.lecture.opensource.jackson.architecture;
2
+
3
+/**
4
+ * <pre>
5
+ * kr.co.swh.lecture.opensource.jackson.architecture
6
+ * JacksonJsonMain.java
7
+ *
8
+ * 설명 :Jackson Json 예제
9
+ * </pre>
10
+ * 
11
+ * @since : 2018. 1. 29.
12
+ * @author : tobby48
13
+ * @version : v1.0
14
+ */
15
+public class JacksonMain {
16
+	
17
+	public static void main(String[] args) throws Exception  {
18
+		// TODO Auto-generated method stub
19
+		IService service = new Jackson("json.json");
20
+		
21
+		service = new Jackson("xml.xml");
22
+		service.deserialize();
23
+		service.serialize();
24
+	}
25
+
26
+}

+ 40
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/architecture/Student.java Ver arquivo

@@ -0,0 +1,40 @@
1
+package kr.co.swh.lecture.opensource.jackson.architecture;
2
+
3
+import java.util.List;
4
+
5
+import com.fasterxml.jackson.annotation.JsonIgnore;
6
+import com.fasterxml.jackson.annotation.JsonInclude;
7
+import com.fasterxml.jackson.annotation.JsonProperty;
8
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
9
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
10
+
11
+import lombok.Data;
12
+
13
+/**
14
+ * <pre>
15
+ * kr.co.swh.lecture.opensource.jackson.architecture 
16
+ * Student.java
17
+ *
18
+ * 설명 :Jackson 예제
19
+ * </pre>
20
+ * 
21
+ * @since : 2018. 1. 29.
22
+ * @author : tobby48
23
+ * @version : v1.0
24
+ */
25
+@Data
26
+@JacksonXmlRootElement(localName = "student")	//	루트 엘리먼트 이름
27
+public class Student {
28
+	//	Serialize 에는 멤버변수 'n'이 JSON과 XML의 'name'으로 설정
29
+	//	Deserialize 에는 JSON과 XML의 'name'에 대한 값이 멤버변수 'n'으로 설정
30
+	@JsonProperty("name")
31
+	@JacksonXmlProperty(localName = "name")
32
+	private String n;
33
+	
34
+	@JsonIgnore		//	Serialize 시에 무시, Deserialize 시에 초기값
35
+	private int age;
36
+	
37
+	@JsonProperty("addresses")
38
+	@JsonInclude(JsonInclude.Include.NON_NULL)	//	Null이면 무시
39
+	private List<String> addresses;
40
+}

+ 35
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/architecture/StudentList.java Ver arquivo

@@ -0,0 +1,35 @@
1
+package kr.co.swh.lecture.opensource.jackson.architecture;
2
+
3
+import java.util.List;
4
+
5
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
6
+import com.fasterxml.jackson.annotation.JsonProperty;
7
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
8
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
9
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
10
+
11
+import lombok.Data;
12
+
13
+/**
14
+ * <pre>
15
+ * kr.co.swh.lecture.opensource.jackson.architecture 
16
+ * StudentList.java
17
+ *
18
+ * 설명 :StudentList 예제
19
+ * </pre>
20
+ * 
21
+ * @since : 2018. 1. 29.
22
+ * @author : tobby48
23
+ * @version : v1.0
24
+ */
25
+@Data
26
+@JacksonXmlRootElement(localName = "students")
27
+@JsonIgnoreProperties({ "academyName", "location" })	//	Serializer/Deserialize 시 제외시킬 프로퍼티를 지정
28
+public class StudentList {
29
+	@JacksonXmlProperty		//	xml의 엘리먼트임을 명시, 엘리먼트 이름을 지정하지 않으면 멤버변수인 'student'로 지정
30
+    @JacksonXmlElementWrapper(useWrapping = false)	//	컬렉션에 요소가 없을 때 에러방지, 빈 엘리먼트 생성
31
+	@JsonProperty("students")
32
+	private List<Student> student;
33
+	private String academyName;
34
+	private String location;
35
+}

+ 65
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/json/JacksonMain.java Ver arquivo

@@ -0,0 +1,65 @@
1
+package kr.co.swh.lecture.opensource.jackson.json;
2
+
3
+import java.io.InputStream;
4
+import java.util.ArrayList;
5
+import java.util.List;
6
+
7
+import com.fasterxml.jackson.databind.ObjectMapper;
8
+
9
+
10
+/**
11
+ * <pre>
12
+ * kr.co.swh.lecture.opensource.jackson.json
13
+ * JacksonMain.java
14
+ *
15
+ * 설명 :Jackson Json 예제
16
+ * </pre>
17
+ * 
18
+ * @since : 2018. 1. 29.
19
+ * @author : tobby48
20
+ * @version : v1.0
21
+ */
22
+public class JacksonMain {
23
+	
24
+	private ObjectMapper mapper = new ObjectMapper();
25
+	private InputStream is;
26
+	
27
+	@SuppressWarnings("static-access")
28
+	public JacksonMain(){
29
+		is = Thread.currentThread().getContextClassLoader().getSystemResourceAsStream("json.json");
30
+	}
31
+	
32
+	public void deserialize() throws Exception{
33
+		StudentList value = mapper.readValue(is, StudentList.class);
34
+		System.out.println(value);
35
+	}
36
+	
37
+	public void serialize() throws Exception{
38
+		StudentList list = new StudentList();
39
+		List<Student> studentList = new ArrayList<Student>();
40
+		
41
+		Student s1 = new Student();
42
+		s1.setN("돌쌤");
43
+		s1.setAge(18);
44
+		List<String> addressList = new ArrayList<String>();
45
+		addressList.add("안흥");
46
+		addressList.add("증포");
47
+		s1.setAddresses(addressList);
48
+		studentList.add(s1);
49
+		
50
+		Student s2 = new Student();
51
+		s2.setN("하마쌤");
52
+		s2.setAge(28);
53
+		studentList.add(s2);
54
+		
55
+		list.setStudent(studentList);
56
+		System.out.println(mapper.writeValueAsString(list));
57
+	}
58
+	
59
+	public static void main(String[] args) throws Exception  {
60
+		// TODO Auto-generated method stub
61
+		JacksonMain jackson = new JacksonMain();
62
+		jackson.deserialize();
63
+		jackson.serialize();
64
+	}
65
+}

+ 36
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/json/Student.java Ver arquivo

@@ -0,0 +1,36 @@
1
+package kr.co.swh.lecture.opensource.jackson.json;
2
+
3
+import java.util.List;
4
+
5
+import com.fasterxml.jackson.annotation.JsonIgnore;
6
+import com.fasterxml.jackson.annotation.JsonInclude;
7
+import com.fasterxml.jackson.annotation.JsonProperty;
8
+
9
+import lombok.Data;
10
+
11
+/**
12
+ * <pre>
13
+ * kr.co.swh.lecture.opensource.jackson.json
14
+ * Student.java
15
+ *
16
+ * 설명 :Jackson 예제
17
+ * </pre>
18
+ * 
19
+ * @since : 2018. 1. 29.
20
+ * @author : tobby48
21
+ * @version : v1.0
22
+ */
23
+@Data
24
+public class Student {
25
+	//	Serialize 에는 멤버변수 'n'이 JSON의 'name'키로 설정
26
+	//	Deserialize 에는 'name'키에 대한 값이 멤버변수 'n'으로 설정
27
+	@JsonProperty("name")
28
+	private String n;
29
+	
30
+	@JsonIgnore		//	Serialize 시에 무시, Deserialize 시에 초기값
31
+	private int age;
32
+	
33
+	@JsonInclude(JsonInclude.Include.NON_NULL)	//	Null이면 무시
34
+	@JsonProperty("addresses")	//	멤버변수와 같으면 생략가능
35
+	private List<String> addresses;
36
+}

+ 29
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/json/StudentList.java Ver arquivo

@@ -0,0 +1,29 @@
1
+package kr.co.swh.lecture.opensource.jackson.json;
2
+
3
+import java.util.List;
4
+
5
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
6
+import com.fasterxml.jackson.annotation.JsonProperty;
7
+
8
+import lombok.Data;
9
+
10
+/**
11
+ * <pre>
12
+ * kr.co.swh.lecture.opensource.jackson.json
13
+ * StudentList.java
14
+ *
15
+ * 설명 :StudentList 예제
16
+ * </pre>
17
+ * 
18
+ * @since : 2018. 1. 29.
19
+ * @author : tobby48
20
+ * @version : v1.0
21
+ */
22
+@Data
23
+@JsonIgnoreProperties({ "academyName", "location" })	//	Serialize 시에 제외, Deserialize 시에 초기값
24
+public class StudentList {
25
+	@JsonProperty("students")
26
+	private List<Student> student;
27
+	private String academyName;
28
+	private String location;
29
+}

+ 75
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/xml/JacksonMain.java Ver arquivo

@@ -0,0 +1,75 @@
1
+package kr.co.swh.lecture.opensource.jackson.xml;
2
+
3
+import java.io.InputStream;
4
+import java.util.ArrayList;
5
+import java.util.List;
6
+
7
+import com.fasterxml.jackson.databind.ObjectMapper;
8
+import com.fasterxml.jackson.databind.SerializationFeature;
9
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
10
+
11
+/**
12
+ * <pre>
13
+ * kr.co.swh.lecture.opensource.jackson.xml
14
+ * JacksonXmlMain.java
15
+ *
16
+ * 설명 :Jackson 예제
17
+ * </pre>
18
+ * 
19
+ * @since : 2018. 1. 29.
20
+ * @author : tobby48
21
+ * @version : v1.0
22
+ */
23
+public class JacksonMain {
24
+	
25
+	private ObjectMapper mapper = new XmlMapper();
26
+	private InputStream is;
27
+	
28
+	@SuppressWarnings("static-access")
29
+	public JacksonMain(){
30
+		mapper.enable(SerializationFeature.INDENT_OUTPUT);
31
+		/**
32
+		 * 	SerializationFeature.INDENT_OUTPUT 옵션은,
33
+		 * <Student>
34
+  				<name>돌쌤</name>
35
+  				<age>18</age>
36
+			</Student>
37
+			형태로 출력
38
+		 * */
39
+		is = Thread.currentThread().getContextClassLoader().getSystemResourceAsStream("xml.xml");
40
+	}
41
+	
42
+	public void deserialize() throws Exception{
43
+		StudentList value = mapper.readValue(is, StudentList.class);
44
+		System.out.println(value);
45
+	}
46
+	
47
+	public void serialize() throws Exception{
48
+		StudentList list = new StudentList();
49
+		List<Student> studentList = new ArrayList<Student>();
50
+		
51
+		Student s1 = new Student();
52
+		s1.setN("돌쌤");
53
+		s1.setAge(18);
54
+		List<String> addressList = new ArrayList<String>();
55
+		addressList.add("안흥");
56
+		addressList.add("증포");
57
+		s1.setAddresses(addressList);
58
+		studentList.add(s1);
59
+		
60
+		Student s2 = new Student();
61
+		s2.setN("하마쌤");
62
+		s2.setAge(28);
63
+		studentList.add(s2);
64
+		
65
+		list.setStudent(studentList);
66
+		System.out.println(mapper.writeValueAsString(list));
67
+	}
68
+	
69
+	public static void main(String[] args) throws Exception  {
70
+		// TODO Auto-generated method stub
71
+		JacksonMain jackson = new JacksonMain();
72
+		jackson.deserialize();
73
+		jackson.serialize();
74
+	}
75
+}

+ 31
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/xml/Student.java Ver arquivo

@@ -0,0 +1,31 @@
1
+package kr.co.swh.lecture.opensource.jackson.xml;
2
+
3
+import java.util.List;
4
+
5
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
6
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
7
+
8
+import lombok.Data;
9
+
10
+/**
11
+ * <pre>
12
+ * kr.co.swh.lecture.opensource.jackson.xml
13
+ * Student.java
14
+ *
15
+ * 설명 :Jackson 예제
16
+ * </pre>
17
+ * 
18
+ * @since : 2018. 1. 29.
19
+ * @author : tobby48
20
+ * @version : v1.0
21
+ */
22
+@Data
23
+@JacksonXmlRootElement(localName = "student")	//	루트 엘리먼트 이름
24
+public class Student {
25
+	//	Serialize 에는 멤버변수 'n'이 XML의 'name'엘리먼트로 설정
26
+	//	Deserialize 에는 'name'엘리먼트에 대한 값이 멤버변수 'n'으로 설정
27
+	@JacksonXmlProperty(localName = "name")
28
+	private String n;
29
+	private int age;
30
+	private List<String> addresses;
31
+}

+ 29
- 0
src/main/java/kr/co/swh/lecture/opensource/jackson/xml/StudentList.java Ver arquivo

@@ -0,0 +1,29 @@
1
+package kr.co.swh.lecture.opensource.jackson.xml;
2
+
3
+import java.util.List;
4
+
5
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
6
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
7
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
8
+
9
+import lombok.Data;
10
+
11
+/**
12
+ * <pre>
13
+ * kr.co.swh.lecture.opensource.jackson.xml
14
+ * StudentList.java
15
+ *
16
+ * 설명 :StudentList 예제
17
+ * </pre>
18
+ * 
19
+ * @since : 2018. 1. 29.
20
+ * @author : tobby48
21
+ * @version : v1.0
22
+ */
23
+@Data
24
+@JacksonXmlRootElement(localName = "students")	//	루트 엘리먼트 이름
25
+public class StudentList {
26
+	@JacksonXmlProperty		//	xml의 엘리먼트임을 명시, 엘리먼트 이름을 지정하지 않으면 멤버변수인 'student'로 지정
27
+    @JacksonXmlElementWrapper(useWrapping = false)	//	컬렉션에 요소가 없을 때 에러방지, 빈 엘리먼트 생성
28
+	private List<Student> student;
29
+}

+ 4
- 3
src/main/resources/json.json Ver arquivo

@@ -4,12 +4,13 @@
4 4
 	    {
5 5
 	        "name": "tobby48",
6 6
 			"age": 32,
7
-			"address": ["안흥동", "증포동"]
7
+			"addresses": ["안흥동", "증포동"]
8 8
 	    },
9 9
 	    {
10 10
 	        "name": "hama",
11 11
 			"age": 30,
12
-			"address": ["안흥동", "증포동"]
12
+			"addresses": ["백사면", "부발읍"]
13 13
 	    }
14
-    ]
14
+    ],
15
+    "academyName":"swhacademy"
15 16
 }

+ 19
- 0
src/main/resources/xml.xml Ver arquivo

@@ -0,0 +1,19 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<students>
3
+	<student>
4
+		<name>tobby48</name>
5
+		<age>32</age>
6
+		<addresses>
7
+			<address>안흥동</address>
8
+			<address>증포동</address>
9
+		</addresses>
10
+	</student>
11
+	<student>
12
+		<name>hama</name>
13
+		<age>30</age>
14
+		<addresses>
15
+			<address>백사면</address>
16
+			<address>부발읍</address>
17
+		</addresses>
18
+	</student>
19
+</students>