ObjectMapper๋ ์ผ๋ฐ์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ๋ด ๊ฐ์ฒด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค, XML, JSON ๋ฌธ์์ ์ ์ฅ๋ ๋ฐ์ดํฐ๊ฐ์ ์๋ก ๋ค๋ฅธ ํ์ ๋๋ ํํ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค. ObjectMapper๋ ์๋ก ๋ค๋ฅธ๋ฐฉ์์ผ๋ก ํํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๋ ํ๋ก์ธ์ค์ ๋๋ค. ์ฌ์ฉ์ค์ธ ์ ํ๋ฆฌ์ผ์ด์ ์ ํธ๋ฆฌํ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์กฐ์ํ๊ณ ๊ตํํ ์ ์์ต๋๋ค.
๊ฐ์ฑ ๋งคํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ธ์ด๋ณ๋ก ๋ง์ต๋๋ค. Java์์๋ Jackson์ด ๋ํ์ ์ผ๋ก ๊ฐ์ฒด ๋งคํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด๋ก ๋๋ ๊ฐ์ฒด์์ ๋ณํํ๊ธฐ ์ํ ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ธ API๋ฅผ ์ ๊ณตํ์ฌ ๊ฐ๋ฐ์๊ฐ ์์ฉ ํ๋ก๊ทธ๋จ์์ ๋ฐ์ดํฐ ์์ ์ ๋ ์ฝ๊ฒ ์ํํ ์ ์๋๋ก ํฉ๋๋ค.
์๋ก ๋ค๋ฅธ ํ์์ ๋ฐ์ดํฐ๋ฅผ ๊ตํํ๋ ๊ฒ์๋ ๋๋ถ๋ถ ์ฌ์ฉ์ด ๋ฉ๋๋ค. ๋งคํ ๊ธฐ๋ฅ ๋ฟ๋ง์ด ์๋๋ผ ๋ฐ์ดํฐ๋ฅผ ์กฐ์ ํ ์๋ ์์ด ์ ์ฉํฉ๋๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.2'
Dto
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
class Car {
private String name;
private String carNumber;
@JsonProperty("TYPE")
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getcarNumber() {
return carNumber;
}
public void setcarNumber(String carNumber) {
this.carNumber = carNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", carNumber='" + carNumber + '\'' +
", type='" + type + '\'' +
'}';
}
}
public class User {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", cars=" + cars +
'}';
}
}
Main
public class Main {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
User user = new User();
user.setName("Chan");
user.setAge(27);
Car car1 = new Car();
car1.setName("K3");
car1.setcarNumber("11๋ 1111");
car1.setType("sedan");
Car car2 = new Car();
car2.setName("Q5");
car2.setcarNumber("22๋ 2222");
car2.setType("SUV");
List<Car> carList = Arrays.asList(car1, car2);
user.setCars(carList);
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
System.out.println("-------------");
JsonNode jsonNode = objectMapper.readTree(json);
String _name = jsonNode.get("name").asText();
int _age = jsonNode.get("age").asInt();
System.out.println("name : " + _name);
System.out.println("age : " + _age);
JsonNode cars = jsonNode.get("cars");
ArrayNode arrayNode = (ArrayNode) cars;
List<Car> _cars = objectMapper.convertValue(arrayNode, new TypeReference<List<Car>>() {
});
System.out.println(_cars);
System.out.println("-------------");
ObjectNode objectNode = (ObjectNode) jsonNode;
objectNode.put("name", "KIM");
System.out.println(objectNode.toPrettyString());
}
}
์ฒซ๋ฒ์งธ ์ถ๋ ฅ์ ObjectMapper์ writeValueAsString๋ฉ์๋๋ฅผ ํ์ฉํ์ฌ jsonํํ๋ก ์ถ๋ ฅํ์์ต๋๋ค.
๋๋ฒ์งธ ์ถ๋ ฅ์ JsonNode๋ฅผ ํ์ฉํ์ฌ ๊ธฐ์กด์ ๋ง๋ json๋ฅผ JsonNode ํํ๋ก ๋ง๋ค์ด ๊ฐ๊ฐ์ key๊ฐ์ผ๋ก value๋ฅผ ์ถ๋ ฅํ์์ต๋๋ค.
์ธ๋ฒ์งธ ์ถ๋ ฅ์ ๊ธฐ์กด์ ๋ง๋ jsonNode๋ฅผ ObjectNode๋ก ๋งคํํ์ฌ ๊ฐ์ ๋ณ๊ฒฝํ๊ณ ์ถ๋ ฅํ์์ต๋๋ค.