# ObjectMapper, JSON 다루기
## 미션
ObjectMapper를 활용하여, JSON과 DTO간 변환을 훈련하시오.
![홍팍-스프링-부트-입문--미션](http://drive.google.com/thumbnail?export=view&sz=w960&id=1ePBspZ2e6U3_CAQ6ufmUSYf8pvxKGy0g)
## 01:33 디버깅 AOP 적용 - @Pointcut
#### ../aop/DebuggingAspect
```
package com.example.firstproject.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class DebuggingAspect {
@Pointcut("execution(* com.example.firstproject.api.*.*(..))")
private void cut() {}
...
}
```
## 03:42 객체를 JSON으로 - writeValueAsString(), @Getter
#### ../objectmapper/Burger
```
package com.example.firstproject.objectmapper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
public class Burger {
private String name;
private int price;
private List<String> ingredients;
}
```
#### ../test/.../objectmapper/BurgerTest
```
package com.example.firstproject.objectmapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BurgerTest {
@Test
public void 자바_객체를_JSON으로_변환() throws JsonProcessingException {
// 준비
ObjectMapper objectMapper = new ObjectMapper();
List<String> ingredients = Arrays.asList("통새우 패티", "순쇠고기 패티", "토마토", "스파이시 어니언 소스");
Burger burger = new Burger("맥도날드 슈비버거", 5500, ingredients);
// 수행
String json = objectMapper.writeValueAsString(burger);
// 예상
String expected = "{\"name\":\"맥도날드 슈비버거\",\"price\":5500,\"ingredients\":[\"통새우 패티\",\"순쇠고기 패티\",\"토마토\",\"스파이시 어니언 소스\"]}";
// 검증
assertEquals(expected, json);
System.out.println(json);
}
}
```
## 10:34 JsonNode 활용하기 - readTree(), JsonNode, toPrettyString()
#### ../test/.../objectmapper/BurgerTest
```
package com.example.firstproject.objectmapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BurgerTest {
@Test
public void 자바_객체를_JSON으로_변환() throws JsonProcessingException {
// 준비
ObjectMapper objectMapper = new ObjectMapper();
List<String> ingredients = Arrays.asList("통새우 패티", "순쇠고기 패티", "토마토", "스파이시 어니언 소스");
Burger burger = new Burger("맥도날드 슈비버거", 5500, ingredients);
// 수행
String json = objectMapper.writeValueAsString(burger);
// 예상
String expected = "{\"name\":\"맥도날드 슈비버거\",\"price\":5500,\"ingredients\":[\"통새우 패티\",\"순쇠고기 패티\",\"토마토\",\"스파이시 어니언 소스\"]}";
// 검증
assertEquals(expected, json);
JsonNode jsonNode = objectMapper.readTree(json);
System.out.println(jsonNode.toPrettyString());
}
}
```
## 11:38 JSON을 객체로 - readValue(), @NoArgsConstructor
#### ../test/.../objectmapper/BurgerTest
```
package com.example.firstproject.objectmapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BurgerTest {
...
@Test
public void JSON을_자바_객체로_변환() throws JsonProcessingException {
// 준비
ObjectMapper objectMapper = new ObjectMapper();
String json = "{\"name\":\"맥도날드 슈비버거\",\"price\":5500,\"ingredients\":[\"통새우 패티\",\"순쇠고기 패티\",\"토마토\",\"스파이시 어니언 소스\"]}";
// 수행
Burger burger = objectMapper.readValue(json, Burger.class);
// 예상
List<String> ingredients = Arrays.asList("통새우 패티", "순쇠고기 패티", "토마토", "스파이시 어니언 소스");
Burger expected = new Burger("맥도날드 슈비버거", 5500, ingredients);
// 검증
assertEquals(expected.toString(), burger.toString());
JsonNode jsonNode = objectMapper.readTree(json);
System.out.println(jsonNode.toPrettyString());
System.out.println(burger.toString());
}
}
```
## 16:00 JSON 직접 만들기 - createObjectNode(), ObjectNode, ArrayNode
#### ../test/.../objectmapper/BurgerTest
```
package com.example.firstproject.objectmapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BurgerTest {
...
@Test
public void JSON을_자바_객체로_변환() throws JsonProcessingException {
// 준비
ObjectMapper objectMapper = new ObjectMapper();
/*
{
"name" : "맥도날드 슈비버거",
"price" : 5500,
"ingredients" : [ "통새우 패티", "순쇠고기 패티", "토마토", "스파이시 어니언 소스" ]
}
*/
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("name", "맥도날드 슈비버거");
objectNode.put("price", 5500);
ArrayNode arrayNode = objectMapper.createArrayNode();
arrayNode.add("통새우 패티");
arrayNode.add("순쇠고기 패티");
arrayNode.add("토마토");
arrayNode.add("스파이시 어니언 소스");
objectNode.set("ingredients", arrayNode);
String json = objectNode.toString();
// 수행
Burger burger = objectMapper.readValue(json, Burger.class);
// 예상
List<String> ingredients = Arrays.asList("통새우 패티", "순쇠고기 패티", "토마토", "스파이시 어니언 소스");
Burger expected = new Burger("맥도날드 슈비버거", 5500, ingredients);
// 검증
assertEquals(expected.toString(), burger.toString());
JsonNode jsonNode = objectMapper.readTree(json);
System.out.println(jsonNode.toPrettyString());
System.out.println(burger.toString());
}
}
```
## 🔥 구글링 훈련하기
- Java Jackson 사용법
- ObjectMapper 사용법