웹기반 애플리케이션

웹기반 애플리케이션JSP 9일차 📃_직렬화·역직렬화·마샬링·언마샬링

비비펄 2023. 3. 6. 19:41

마샬링(marshalling)

한 객체의 메모리에서 표현방식을 저장 또는 전송에 적합한 다른 데이터 형식으로 변환하는 과정

언마샬링(unmarshalling)

전송된 데이터를 다시 원래의 객체 모양으로 복원하는 작업

직렬화(Serialization)

객체 데이터를 일련의 byte stream으로 변환하는 작업

반대로 일련의 byte stream을 본래의 객체 모양으로 복원하는 작업은 Deserialization(역직렬화)

 


예제 1단계

일단 마샬링과 직렬화에 쓰일 VO class를 만들어준다. 이때 Serializable인터페이스를 implements해주어야한다.

package kr.or.ddit.modeling;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class TestVO implements Serializable{
	private String prop1;
	private Integer prop2;
	
	//password는 공개되면 안되기때문에 @JsonIgnore와 transient처리를 해준다. 둘다 해야함.
	@JsonIgnore
	private transient String password;
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getProp1() {
		return prop1;
	}
	public void setProp1(String prop1) {
		this.prop1 = prop1;
	}
	public Integer getProp2() {
		return prop2;
	}
	public void setProp2(Integer prop2) {
		this.prop2 = prop2;
	}
	@Override
	public String toString() {
		return "TestVO [prop1=" + prop1 + ", prop2=" + prop2 + ", password=" + password + "]";
	}
	
	
}
예제 CASE 1
//직렬화
	private static void seriallizeCase1() {
		//직렬화과정
		TestVO testVO = new TestVO();
		testVO.setProp1("value1");
		testVO.setProp2(45);
		testVO.setPassword("비밀번호");
		
		File file = new File("D:/00.medias/test.dat");
		try(
		FileOutputStream fos = new FileOutputStream(file);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		){
			oos.writeObject(testVO); //직렬화 
		}catch(IOException e) {
			e.printStackTrace();
		}		
	}
	
	//역직렬화
	private static void deseriallizeCase1() {
		
		File file = new File("D:/00.medias/test.dat");
		
		try(
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
				
		){
			TestVO testVO = (TestVO)ois.readObject();
			System.out.println(testVO);
			System.out.println(testVO.getPassword());
		}catch(IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}		
	}
예제 CASE 2
	private static void unMarshallingCase2() {
		File file = new File("D:/00.medias/test.json");
		
		try(
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
		){
			String json = br.readLine();
			
			ObjectMapper objectMapper = new ObjectMapper();
			TestVO testVO = objectMapper.readValue(json, TestVO.class);
			System.out.println(testVO);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void marshallingCase2() {
		
		TestVO testVO = new TestVO();
		testVO.setProp1("value1");
		testVO.setProp2(45);
		testVO.setPassword("비밀번호");
		
		//마샬링
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			String json = objectMapper.writeValueAsString(testVO);
			System.out.println(json);
			
			File file = new File("D:/00.medias/test.json");
			try(
				FileWriter fw = new FileWriter(file);
				BufferedWriter bw = new BufferedWriter(fw);	
			){
				bw.write(json);
			}
//			TestVO testVO2 = objectMapper.readValue(json, TestVO.class);
//			System.out.println(testVO2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
예제 CASE 3
	private static void unmarshallingCase3() {
		
		File file = new File("D:/00.medias/test.json");
		
		
		try(
			FileInputStream fis = new FileInputStream(file);
		){
			ObjectMapper objectMapper = new ObjectMapper();
			TestVO testVO= objectMapper.readValue(fis, TestVO.class);
			System.out.println(testVO);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	private static void marshallingCase3() {
		
		TestVO testVO = new TestVO();
		testVO.setProp1("value1");
		testVO.setProp2(45);
		testVO.setPassword("비밀번호");
		
		File file = new File("D:/00.medias/test.json");
		try(
			FileOutputStream fos = new FileOutputStream(file);
		){
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.writeValue(fos, testVO);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
예제 CASE 4
	private static void unMarshallingCase4ToXML() {
		File file = new File("D:/00.medias/test3.xml");
		
		
		try(
			FileInputStream fis = new FileInputStream(file);
		){
			ObjectMapper objectMapper = new XmlMapper();
			TestVO testVO= objectMapper.readValue(fis, TestVO.class);
			System.out.println(testVO);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	private static void marshallingCase4ToXML() {
		TestVO testVO = new TestVO();
		testVO.setProp1("value1");
		testVO.setProp2(45);
		testVO.setPassword("비밀번호");
		
		File file = new File("D:/00.medias/test3.xml");
		try(
			FileOutputStream fos = new FileOutputStream(file);
		){
			ObjectMapper objectMapper = new XmlMapper();
			objectMapper.writeValue(fos, testVO);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		
	}