ObjectMapper ๋ž€ ?

JangUTยท2025๋…„ 3์›” 10์ผ

๐Ÿ“Œ ObjectMapper ๋ž€?

  • Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ObjectMapper๋ž€, Java ๊ฐ์ฒด๋ฅผ JSON์œผ๋กœ ๋˜๋Š” ๋ฐ˜๋Œ€๋กœ JSON์„ java ๊ฐ์ฒด๋กœ ์—ญ์ง๋ ฌํ™”ํ•˜๋Š” ๋„๊ตฌ์ด๋‹ค.
  • Spring boot์—์„œ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ REST API ๊ฐœ๋ฐœ ์‹œ JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ง๋ ฌํ™”ํ•œ๊ฑฐ๋‚˜ ์—ญ์ง๋ ฌํ™” ํ•˜๋Š” ๋ฐ ๋งŽ์ด ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์ง๋ ฌํ™”์™€ ์—ญ์ง๋ ฌํ™”

1. ์ง๋ ฌํ™”

  • ์ •์˜ : ๊ฐ์ฒด๋ฅผ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ(ํŒŒ์ผ, JSON, XML ๋“ฑ)์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.
    ex. java ๊ฐ์ฒด -> JSON

  • ์šฉ๋„ :

    • ์˜๊ตฌ ์ €์žฅ : ๊ฐ์ฒด๋ฅผ ํŒŒ์ผ์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜์—ฌ ๋‚˜์ค‘์— ๋ณต์›ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋„คํŠธ์›Œํฌ ์ „์†ก : ๊ฐ์ฒด๋ฅผ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์‹œ์Šคํ…œ์œผ๋กœ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค
    • ์บ์‹œ : ๊ฐ์ฒด๋ฅผ ์บ์‹œ์— ์ €์žฅํ•˜์—ฌ ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ์—ญ์ง๋ ฌํ™”

  • ์ •์˜ : ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ(ํŒŒ์ผ, JSON, XML ๋“ฑ)์„ -> ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ๋„คํŠธ์›Œํฌ ํ†ต์‹ , ํŒŒ์ผ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋กœ ๋ณต์›ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

  • ์šฉ๋„ :

    	- ๋ฐ์ดํ„ฐ ๋ณต์› : ํŒŒ์ผ์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
    • ๋„คํŠธ์›Œํฌ ๋ฐ์ดํ„ฐ ์ˆ˜์‹  : ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

-JSON๊ณผ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ
JSON ์ž์ฒด๋Š” ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์ด ์•„๋‹ˆ์ง€๋งŒ, JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ „์†กํ•  ๋•Œ๋Š” ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ ํ˜•ํƒœ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

๐Ÿ“Œ ์ง๋ ฌํ™” (Serialization) - Java Object to JSON

public class Student {
	
    private Long id;
	private String name;
    
    public Student() {}

    public Burger(Long id, String name) {
        this.id = id;
        this.name = name;
    }
   
   // Getter ๋ฐ Setter ๋ฉ”์„œ๋“œ


public class Main {
    public static void main(String[] args) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();

            Student student = new Student(1L, ๊น€์ผ๋ฐ˜);

            // student ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
            String prettyJson = objectMapper.writeWithDefaultPrettyPrinter()
            									.writeValueAsString(student);

            // JSON ๋ฌธ์ž์—ด ์ถœ๋ ฅ
            System.out.println(jsonString);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
์ถœ๋ ฅ : 

{
	"id":1,
	"name":"๊น€์ผ๋ฐ˜"
}
  • objectMapper ์˜ writeValueAsString() ๋ฉ”์„œ๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด -> JSON ํ˜•ํƒœ๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ ์—ญ์ง๋ ฌํ™” (Deserialization) - JSON to Java Object

public class Student {
	
    private Long id;
	private String name;
    
    public Student() {}

    public Burger(Long id, String name) {
        this.id = id;
        this.name = name;
    }
    
   // Getter ๋ฐ Setter ๋ฉ”์„œ๋“œ
   
   
    @Override
    public String toString() {
        return "Student{" +
                "id='" + id +"L"+ '\'' +
                ", name='" + name + '\'' +
                "}';
    }


public class Main {
    public static void main(String[] args) {
        try {
            // JSON ๋ฌธ์ž์—ด
            String jsonString = "{\"id\": 1,\"name\":\"๊น€์ผ๋ฐ˜\"}";

            // ObjectMapper ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
            ObjectMapper objectMapper = new ObjectMapper();

            // JSON ๋ฌธ์ž์—ด์„ Studednt ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
            Student student = objectMapper.readValue(jsonString, Student.class);

            // ๋ณ€ํ™˜๋œ ๊ฐ์ฒด ์ถœ๋ ฅ @Override ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
            System.out.println(student);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
์ถœ๋ ฅ :  Student{id='1L', name='๊น€์ผ๋ฐ˜'}
  • objectMapper ์˜ readValue() ๋ฉ”์„œ๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ ์ž ํ•˜๋Š” JSON -> Java ๊ฐ์ฒด๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“ŒCustom Serialization or Deserialization

  • ์ง๋ ฌํ™”๋ฅผ ์ž„์˜๋กœ ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ณดํ†ต ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” ํŠน์ • ํ•„๋“œ์˜ ๋ณ€ํ™˜ ๋ฐฉ์‹์„ ๋ณ€๊ฒฝ
  • ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ***** ๋กœ ๋ณ€ํ™˜
  • ํ•„๋“œ๋ช…์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์—ฐํ•˜๊ฒŒ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

1. Custom serializer - Object -> JSON


package com.example.study2.basic3.student;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class CustomStudentSerializer extends StdSerializer<Student> {

	public CustomStudentSerializer() {
		this(null);
	}

	public CustomStudentSerializer(Class<Student> t) {
		super(t);
	}

	@Override
	public void serialize(Student student, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws
		IOException {
		jsonGenerator.writeStartObject();
        jsonGenerator.writeNumberField("age", student.getId());
		jsonGenerator.writeStringField("teacher", student.getName());
		jsonGenerator.writeEndObject();
	}
}

package com.example.study2.basic3;

import com.example.study2.basic3.student.CustomStudentSerializer;
import com.example.study2.basic3.student.Student;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class CustomObToJSON {
	public static void main(String[] args) {

		try {
			ObjectMapper mapper = new ObjectMapper();
            
			SimpleModule module = new SimpleModule("CustomStudentSerializer",
				new Version(1, 0, 0, null, null, null));
                
			module.addSerializer(Student.class, new CustomStudentSerializer());
            
			mapper.registerModule(module);
            
			Student student = new Student(46L, "์„ ์ƒ๋‹˜");

			String studentJson = mapper.writeValueAsString(student);
			System.out.println(studentJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
์ถœ๋ ฅ : {
		  "age " : 46,
  		  "teacher " : "์„ ์ƒ๋‹˜"
       }
  • ์ปค์Šคํ…€ ์ง๋ ฌํ™”๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ธฐ์กด์˜ Student์˜ ํ•„๋“œ๋ช…์„ name -> teacher๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•˜๋‹ค
  • ์˜ˆ๋ฅผ ๋“ค๋ฉด JSON value์™€ java ๊ฐ์ฒด์˜ ํ•„๋“œ๋ช…์ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒฝ์šฐ ์ปค์Šคํ…€ ์ง๋ ฌํ™”๋ฅผ ์ด์šฉํ•œ๋‹ค.

2. custom Deserializer - JSON -> Object

package com.example.study2.basic3.student;

import java.io.IOException;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class CustomStudentDeserializer extends StdDeserializer<Student> {

	public CustomStudentDeserializer() {
		this(null);
	}

	public CustomStudentDeserializer(Class<?> vc) {
		super(vc);
	}

	@Override
	public Student deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws
		IOException,
		JacksonException {
		Student student = new Student();
		ObjectCodec codec = jsonParser.getCodec();
		JsonNode node = codec.readTree(jsonParser);

		try {
			String name = node.get("name").asText();
			Long id = node.get("id").asLong();
			student.updateName(id, name);
			return student;
		} catch (Exception e) {
			e.printStackTrace();
		}
		throw new RuntimeException();
	}

}
package com.example.study2.basic3.student;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class CustomJsonTpOb {
	public static void main(String[] args) {

		try {
			String json = "{\"id\" : 44555, \"name\" : \"๊น€์ผ๋ฐ˜\"}";
			
            ObjectMapper mapper = new ObjectMapper();
			
            SimpleModule module = new SimpleModule("CustomStudentDeserializer",
				new Version(1, 0, 0, null, null, null));
			
            module.addDeserializer(Student.class, new CustomStudentDeserializer());
			
            mapper.registerModule(module);
			
            Student student = mapper.readValue(json, Student.class);
			
            System.out.println(student);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
์ถœ๋ ฅ :  Student{id='44555L', name='๊น€์ผ๋ฐ˜'}
  • ์ปค์Šคํ…€ ์—ญ์ง๋ ฌํ™”๋ฅผ ํ†ตํ•ด์„œ ํŠน์ • ์กฐ๊ฑด๋งŒ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ผ๋ถ€๋งŒ ์—ญ์ง๋ ฌํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

์ฐธ๊ณ ๋ฌธ์„œ

profile
ํ‰๋ฒ”ํ•œ ๊ฐœ๋ฐœ์ž

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