์ ์ : ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํธ ์คํธ๋ฆผ(ํ์ผ, JSON, XML ๋ฑ)์ผ๋ก ๋ณํํ๋ ๊ณผ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ฑฐ๋ ์ ์กํ ๋ ์ฌ์ฉ๋๋ค.
ex. java ๊ฐ์ฒด -> JSON
์ฉ๋ :
์ ์ : ๋ฐ์ดํธ ์คํธ๋ฆผ(ํ์ผ, JSON, XML ๋ฑ)์ -> ๊ฐ์ฒด๋ก ๋ณํํ๋ ๊ณผ์ ์ผ๋ก ๋คํธ์ํฌ ํต์ , ํ์ผ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฐ์ฒด๋ก ๋ณต์ํ ๋ ์ฌ์ฉ๋๋ค.
์ฉ๋ :
- ๋ฐ์ดํฐ ๋ณต์ : ํ์ผ์ด๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋ ๊ฐ์ฒด๋ฅผ ๋ค์ ๋ฉ๋ชจ๋ฆฌ๋ก ๋ถ๋ฌ์ฌ ์ ์๋ค.
-JSON๊ณผ ๋ฐ์ดํธ ์คํธ๋ฆผ
JSON ์์ฒด๋ ๋ฐ์ดํธ ์คํธ๋ฆผ์ด ์๋์ง๋ง, 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":"๊น์ผ๋ฐ"
}
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='๊น์ผ๋ฐ'}
***** ๋ก ๋ณํ
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 " : "์ ์๋"
}
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='๊น์ผ๋ฐ'}