ObjectMapper

์ด์ข…์ฐฌยท2023๋…„ 2์›” 12์ผ
0
post-custom-banner

๐Ÿ“– ObjectMapper?

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๋กœ ๋งคํ•‘ํ•˜์—ฌ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์ถœ๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ… ์š”์•ฝ

  • ObjectMapper๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์•Œ๋งž๊ฒŒ ๋ณ€ํ™˜ํ•ด์ฃผ์–ด ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  • ๋งคํ•‘๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ ์ˆ˜์ •๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
profile
์™œ? ๋ผ๋Š” ์งˆ๋ฌธ์ด ์‚ฌ๋ผ์งˆ ๋•Œ๊นŒ์ง€
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€