Jackson์ Java ๊ฐ์ฒด๋ฅผ ๋ค์ํ ํ ์คํธ ํ์์ผ๋ก ์ง๋ ฌํ/์ญ์ง๋ ฌํํ๋ ๋ฐ ๋๋ฆฌ ์ฌ์ฉ๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. ObjectMapper ํด๋์ค ๋ JSON ํ์์ผ๋ก ์์ ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ฃผ์ ๋ฐฉ๋ฒ์ ๋๋ค. ๋ค๋ฅธ ํ์์ ๊ฒฝ์ฐ ์์( XmlMapper , YAMLMapper ) ์ด ์์ต๋๋ค . ์์ ๋๋ถ์ ๋จ์ผ ์ธํฐํ์ด์ค๋ฅผ ํตํด ์ผ๊ด๋ ๋ฐฉ์์ผ๋ก ๋ชจ๋ ํ์์ผ๋ก ์์ ํ ์ ์์ต๋๋ค.
jar ํ์ผ ๋ค์ด๋ก๋
์์ ๋ฅผ ์ดํด๋ณด๊ธฐ ์ ์ Jackson jar ํ์ผ์ ๋ค์ด๋ก๋ํ์ฌ IntelliJ IDEA์ ํ๋ก์ ํธ์ ์ฐ๊ฒฐํด์ผ ํฉ๋๋ค. jackson-databind ์ ์๋ฅผ ๋ค์ด ํ์ํ ํ์ผ์ ๊ฒ์ํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
-
Maven ๋ฆฌํฌ์งํ ๋ฆฌ ์น ์ฌ์ดํธ ๋ก ์ด๋ํฉ๋๋ค .
-
๊ฒ์ ์์์ " jackson-databind "๋ฅผ ์ ๋ ฅํฉ๋๋ค. ๋ค์์ ์ป์ ์ ์์ต๋๋ค.
-
์ฒซ ๋ฒ์งธ ๊ฒ์ ๊ฒฐ๊ณผ๋ ์ฐ๋ฆฌ๊ฐ ๊ด์ฌ ์๋ ๊ฒ์ ๋๋ค. ๋งํฌ๋ฅผ ๋ฐ๋ผ๊ฐ์ญ์์ค.
-
๊ฒฝ์ฐ์ ๋ฐ๋ผ ํ๋ก์ ํธ์ ๋ค๋ฅธ ๊ตฌ์ฑ ์์์์ ํธํ์ฑ์ ๋ณด์ฅํ๊ธฐ ์ํด ํน์ ๋ฒ์ ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํ ์ ์์ต๋๋ค. ์ต์ ๋ฒ์ ์ด ์๋ํด์ผ ํฉ๋๋ค(์ด ๊ฐ์๋ฅผ ์์ฑํ ๋น์์๋ 2.13.2.2์). ๋งํฌ๋ฅผ ๋ฐ๋ฅด์ญ์์ค.
-
์ด๋ฆฌ๋ ํ์ด์ง์์ "๋ฒ๋ค" ๋งํฌ๋ฅผ ์ํฉ๋๋ค.
-
์ด ๋งํฌ๋ฅผ ์ฌ์ฉํ์ฌ jar ํ์ผ์ ๋ค์ด๋ก๋ํ์ญ์์ค .
์ ์ฌํ ์ ์ฐจ์ ๋ฐ๋ผ ๋๋จธ์ง ํ์ํ jar ํ์ผ์ ์ฐพ์ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค.
ํ์ํ ๋ชจ๋ ํ์ผ์ ๋ค์ด๋ก๋ํ ํ IntelliJ IDEA์ ํ๋ก์ ํธ์ ์ฐ๊ฒฐํฉ๋๋ค.
-
ํ๋ก์ ํธ ์ค์ ์ ์ฝ๋๋ค( Ctrl+Alt+Shift+S ํค ์กฐํฉ์ผ๋ก ์ํํ ์ ์์).
-
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก ์ด๋ํฉ๋๋ค .
-
+๋ฅผ ๋๋ฅธ ๋ค์ "Java"๋ฅผ ๋๋ฆ ๋๋ค. ๋ค์ด๋ก๋ํ ํ์ผ์ ๋ชจ๋ ์ ํํฉ๋๋ค. ์ฐ๋ฆฌ๊ฐ ๋๋ด์ผ ํ ๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
-
์ด๊ฒ์ผ๋ก ์ค๋น ์์ ์ ๋ง์นฉ๋๋ค. ์ด์ ObjectMapper๋ฅผ ์ค์ ๋ก ์ฌ์ฉํด ๋ณผ ์ ์์ต๋๋ค .
JSON์ผ๋ก ์ง๋ ฌํ
๋จผ์ ์ผ๋ถ ๊ฐ์ฒด๋ฅผ JSON์ผ๋ก ์ง๋ ฌํํฉ๋๋ค.
import com.fasterxml.jackson.databind.ObjectMapper;
class Book {
public String title;
public String author;
public int pages;
}
public class Solution {
public static void main(String[] args) throws Exception {
Book book = new Book();
book.title = "Good Omens";
book.author = "Pratchett T., Gaiman N.";
book.pages = 383;
ObjectMapper mapper = new ObjectMapper();
String jsonBook = mapper.writeValueAsString(book);
System.out.println(jsonBook);
}
}
main์ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋ํ๋ฉ๋๋ค.
ObjectMapper ์๋ ๋ง์ ๊ณ ๊ธ ์ค์ ์ด ์์ต๋๋ค. ๊ทธ ์ค ํ๋๋ฅผ ์ฌ์ฉํ์ฌ JSON ๋ฌธ์์ด์ ๋ ์ฝ๊ธฐ ์ฝ๊ฒ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค. ์์ฑ ํ๊ฐ์ฒด ๋งคํผ๋ค์ ๋ช ๋ น๋ฌธ์ ์คํํฉ๋๋ค.
์ถ๋ ฅ์ ์ ๋ณด๋ ๋์ผํ๊ฒ ์ ์ง๋์ง๋ง ์ด์ ๋ค์ฌ์ฐ๊ธฐ์ ์ค ๋ฐ๊ฟ์ด ์์ต๋๋ค.
"์ ๋ชฉ": "์ข์ ์ง์กฐ",
"์ ์": "Pratchett T., Gaiman N.",
"ํ์ด์ง": 383
}
JSON์์ ์ญ์ง๋ ฌํ
์ด์ ๋ฐ๋ ์์ ์ ์ํํด ๋ณด๊ฒ ์ต๋๋ค. ๋ฌธ์์ด์ ๊ฐ์ฒด๋ก deserializeํฉ๋๋ค. ํ๋ก๊ทธ๋จ์ด ์ํํ๋ ์์ ์ ๋ณผ ์ ์๋๋ก Book ํด๋์ค ์์ toString ๋ฉ์๋๋ฅผ ์ฌ์ ์ํด ๋ณด๊ฒ ์ต๋๋ค .
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", pages=" + pages +
'}';
}
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ main ๋ฉ์๋ ์์ ๋ค์์ ์ํํ ๊ฒ์ ๋๋ค .
public static void main(String[] args) throws Exception {
String jsonString = "{\"title\":\"Good Omens\",\"author\":\"Pratchett T., Gaiman N.\",\"pages\":383}";
Book book = new ObjectMapper().readValue(jsonString, Book.class);
System.out.println(book);
}
์ฐ์ถ:
readValue ๋ฉ์๋๋ ์ค๋ฒ๋ก๋๋์ด ํ์ผ, ๋งํฌ, ๋ค์ํ ์ ๋ ฅ ์คํธ๋ฆผ ๋ฑ์ ์ทจํ๋ ๋ง์ ๋ณํ์ด ์์ต๋๋ค. ๋จ์์ฑ์ ์ํด ์์ ์์๋ JSON ๋ฌธ์์ด์ ํ์ฉํ๋ ๋ณํ์ ์ฌ์ฉํฉ๋๋ค .
์์์ ์ธ๊ธํ๋ฏ์ด ObjectMapper ์๋ ๋ง์ ์ค์ ์ด ์์ต๋๋ค. ๊ทธ ์ค ๋ช ๊ฐ์ง๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ ์ ์๋ ์์ฑ ๋ฌด์
JSON ๋ฌธ์์ด์ Book ํด๋์ค ์ ์๋ ์์ฑ์ด ์๋ ์ํฉ์ ๊ณ ๋ คํ์ญ์์ค .
public static void main(String[] args) throws Exception {
String jsonString = """
{
"title" : "Good Omens",
"author" : "Pratchett T., Gaiman N.",
"pages" : 383,
"unknown property" : 42
}""";
ObjectMapper mapper = new ObjectMapper();
Book book = mapper.readValue(jsonString, Book.class);
System.out.println(book);
}
์ด ์ฝ๋๋ฅผ ์คํํ๋ฉด UnrecognizedPropertyException ์ด ๋ฐ์ํฉ๋๋ค . ์ด๊ฒ์ ๊ธฐ๋ณธ ๋์์ด์ง๋ง ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
ObjectMapper mapper =
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
๋ง๋ค ๋๊ฐ์ฒด ๋งคํผ๊ฐ์ฒด์ ๋ํด์๋ configure ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์ค์ ์ false ๋ก ์ค์ ํฉ๋๋ค . configure ๋ฉ์๋๋ ํธ์ถ๋ ๊ฐ์ฒด๋ฅผ ์์ ํ ๋ค์ ๋์ผํ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฏ๋ก ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ์ด ํธ์ถ์ ์ํํ ์ ์์ต๋๋ค .
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
๊ธฐ๋ฅ๋ฉด์์ ์ด ํ๊ธฐ๋ฒ์ ์ด์ ํ๊ธฐ๋ฒ๊ณผ ๊ฐ์ต๋๋ค.
์ด์ ๊ธฐ๋ณธ ๋ฉ์๋๋ฅผ ์คํํ๋ฉด deserialization์ด ์ฑ๊ณตํ๊ณ unknown ์์ฑ์ด ๋ฌด์๋ฉ๋๋ค.
ํธ๋ฆฌํ ์ฃผ์
Jackson์ ๋ชจ๋ ์ข ๋ฅ์ ๋ฐฉ์์ผ๋ก ์ง๋ ฌํ ํ๋ก์ธ์ค๋ฅผ ์ฌ์ฉ์ ์ ์ํ ์ ์๋ ๋ช ๊ฐ์ง ์ฃผ์์ ์ ๊ณตํฉ๋๋ค. ๊ฐ์ฅ ์ ์ฉํ ๋ช ๊ฐ์ง๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
@JsonIgnore โ ์ด ์ฃผ์์ ์ง๋ ฌํ/์ญ์ง๋ ฌํ ์ค์ ๋ฌด์ํด์ผ ํ๋ ์์ ์์ ๋ฐฐ์น๋ฉ๋๋ค.
class Book {
public String title;
@JsonIgnore
public String author;
public int pages;
}
์ฌ๊ธฐ์์๊ฐํ๋๋ ์ง๋ ฌํ ์ค์ ๊ฒฐ๊ณผ JSON์ ํฌํจ๋์ง ์์ต๋๋ค. ์ญ์ง๋ ฌํ ์,์๊ฐํ๋๋ JSON์ ๋ค๋ฅธ ๊ฐ์ด ์๋๋ผ๋ ๊ธฐ๋ณธ๊ฐ(null)์ ๊ฐ์ ธ์ต๋๋ค.
@JsonFormat โ ์ด ์ฃผ์์ ์ฌ์ฉํ๋ฉด ์ง๋ ฌํ๋ ๋ฐ์ดํฐ์ ํ์์ ์ค์ ํ ์ ์์ต๋๋ค. Book ํด๋์ค ์ Date ํ๋๋ฅผ ํ๋ ๋ ์ถ๊ฐํด ๋ณด๊ฒ ์ต๋๋ค .
class Book {
public String title;
public String author;
public int pages;
public Date createdDate = new Date();
}
์ง๋ ฌํ ํ ๋ค์ JSON์ ์ป์ต๋๋ค.
"title": "Good Omens",
"author": "Pratchett T., Gaiman N.",
"pages": 383,
"createdDate": 1649330880788
}
๋ณด์๋ค์ํผ ๋ ์ง๊ฐ ์ซ์๋ก ์ฐ์ฌ๋์์ต๋๋ค. ์ฃผ์์ ์ถ๊ฐํ๊ณ ํ์์ ์ค์ ํฉ๋๋ค.
class Book {
public String title;
public String author;
public int pages;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
public Date createdDate = new Date();
}
์ด์ ์ง๋ ฌํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
"title": "Good Omens",
"author": "Pratchett T., Gaiman N.",
"pages": 383,
"createdDate": "2022-04-07"
}
@JsonProperty โ ์ด ์ฃผ์์ ์ฌ์ฉํ๋ฉด ์ง๋ ฌํ๋ ํ๋๋ฅผ ๋ํ๋ด๋ ์์ฑ์ ์ด๋ฆ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ์ด ์ฃผ์์ผ๋ก ๋ฉ์๋๋ฅผ ํ์ํ ์๋ ์์ต๋๋ค. ๊ทธ๋ ๊ฒ ํ๋ฉด ์ง๋ ฌํ ์ค์ ๋ฐํ ๊ฐ์ด JSON ์์ฑ์ผ๋ก ๋ณํ๋ฉ๋๋ค.
class Book {
@JsonProperty("name")
public String title;
public String author;
public int pages;
@JsonProperty("quotedTitle")
public String getQuotedTitle() {
return "\"" + title + "\"";
}
}
์ง๋ ฌํ ๊ฒฐ๊ณผ:
"์ ์": "Pratchett T., Gaiman N.",
"ํ์ด์ง": 383,
"์ด๋ฆ": "์ข์ ์ง์กฐ",
"quotedTitle": "\"์ข์ ์ง์กฐ\""
}
@JsonInclude โ ์ด ์ฃผ์์ ์ฌ์ฉํ์ฌ ์ง๋ ฌํํ ํ๋์ ๋ํด ์ถฉ์กฑํด์ผ ํ๋ ์กฐ๊ฑด์ ์ง์ ํ ์ ์์ต๋๋ค. ๊ฐ๋ณ ํ๋ ๋๋ ์ ์ฒด ํด๋์ค์ ์ ์ฉํ ์ ์์ต๋๋ค. ๋จผ์ ์ด๊ธฐํ๋์ง ์์ ํ๋๊ฐ ์๋ ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํํด ๋ณด๊ฒ ์ต๋๋ค.
public class Solution {
public static void main(String[] args) throws Exception {
Book book = new Book();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String jsonBook = mapper.writeValueAsString(book);
System.out.println(jsonBook);
}
}
์ง๋ ฌํ ๊ฒฐ๊ณผ:
"์ ๋ชฉ": null,
"์ ์": null,
"ํ์ด์ง": 0
}
์ฃผ์์ ์ถ๊ฐํ๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
@JsonInclude(JsonInclude.Include.NON_NULL)
class Book {
public String title;
public String author;
public int pages;
}
๊ทธ๋ฐ ๋ค์ ๋ค์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ต๋๋ค.
"ํ์ด์ง" : 0
}
์ด์ null ์ธ ํ๋๋ ์ง๋ ฌํ๋์ง ์์ต๋๋ค.
@JsonPropertyOrder โ ์ด ์ฃผ์์ ์ฌ์ฉํ๋ฉด ํ๋๊ฐ ์ง๋ ฌํ๋๋ ์์๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
@JsonPropertyOrder({"author", "title", "pages"})
class Book {
public String title;
public String author;
public int pages;
}
์ง๋ ฌํ ๊ฒฐ๊ณผ:
"์ ์": "Pratchett T., Gaiman N.",
"์ ๋ชฉ": "Good Omens",
"ํ์ด์ง": 383
}
์ง๊ธ์ ์ฃผ์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ง ๊ธฐ์ตํ์ธ์. ์ด ๋ชจ๋์ ๋ง์น๋ฉด ์ด๋ค์ ๋ํด ๋ ์ ์๊ฒ ๋๊ณ ๊ณ ์ ํ ์ฃผ์์ ๋ง๋ค ์๋ ์์ต๋๋ค.
XML์ ์ง๋ ฌํ ๋ฐ ์ญ์ง๋ ฌํ
XML๋ก ์ง๋ ฌํํด์ผ ํ๋ ๊ฒฝ์ฐ ๋์ผํ ์ค์ ๊ณผ ์ฃผ์์ ๋ชจ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ ์ผํ ์ฐจ์ด์ ์๋งคํผ ๊ฐ์ฒด:
public static void main(String[] args) throws Exception {
Book book = new Book();
book.title = "Good Omens";
book.author = "Pratchett T., Gaiman N.";
book.pages = 383;
ObjectMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String xmlBook = mapper.writeValueAsString(book);
System.out.println(xmlBook);
}
์ฐ์ถ:
<title>Good Omens</title>
<author>Pratchett T., Gaiman N.</author>
<pages>383</pages>
</Book>
XML ์ญ์ง๋ ฌํ:
public static void main(String[] args) throws Exception {
String xmlString = """
<Book>
<title>Good Omens</title>
<author>Pratchett T., Gaiman N.</author>
<pages>383</pages>
</Book>""";
ObjectMapper mapper = new XmlMapper();
Book book = mapper.readValue(xmlString, Book.class);
System.out.println(book);
}
YAML์ ์ง๋ ฌํ ๋ฐ ์ญ์ง๋ ฌํ
XML์ ์ฒ๋ฆฌํ๋ ๊ฒ๊ณผ ๋์ผํ ๋ฐฉ์์ผ๋ก YAML์ ์ฒ๋ฆฌํฉ๋๋ค.
public static void main(String[] args) throws Exception {
Book book = new Book();
book.title = "Good Omens";
book.author = "Pratchett T., Gaiman N.";
book.pages = 383;
ObjectMapper mapper = new YAMLMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String yamlBook = mapper.writeValueAsString(book);
System.out.println(yamlBook);
}
์ฐ์ถ:
์ ๋ชฉ: "Good Omens"
์ ์: "Pratchett T., Gaiman N."
ํ์ด์ง: 383
YAML ์ญ์ง๋ ฌํ:
public static void main(String[] args) throws Exception {
String yamlString = """
---
title: "Good Omens"
author: "Pratchett T., Gaiman N."
pages: 383""";
ObjectMapper mapper = new YAMLMapper();
Book book = mapper.readValue(yamlString, Book.class);
System.out.println(book);
}