Notice
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

DeFacto-Standard IT

Object Serialization (JSON) (2) - jackson-databind# ObjectMapper Class 본문

SpringFramework/References

Object Serialization (JSON) (2) - jackson-databind# ObjectMapper Class

defacto standard 2018. 5. 3. 22:44

jackson-databind 라이브러리는 REST API를 구현하는데 자주 이용된다.


그 외에 json포맷으로 바꾸는데 여러가지 라이브러리가 쓰인다. gson, json, jackson, net.sf 등의 라이브러리가 있다.


이 라이브러리에는 ObjectMapper라는 클래스가 있는데, Object to JSON / JSON to Object를 모두 지원한다.


여기서는 Object Serialization에 사용되는 writeValueAsString()을 알아볼 것이다.


ObjectMapper# writeValueAsString()은 인자로 객체를 넘겨주기만 하면 해당 객체를 JSON 포맷을 String Type으로 변환하여 리턴한다.


- ObjectMapper# writeValueAsString(Object object)

public class Main {
public static void main(String args[]) throws JsonProcessingException {
Person p = new Person();

p.setAge(10);
p.setGrade('A');
p.setName("ycp");
p.setSubName("YCPark");

ObjectMapper mapper = new ObjectMapper();

String result = mapper.writeValueAsString(p);
System.out.println(result.toString());
}
}


실행 결과는 다음과 같다.

Comments