CORE
HOME > JAVA > J2SE > CORE
2016.11.03 / 10:10

Java Json library jackson »ç¿ë¹ý

±â¸®¾Æºü
Ãßõ ¼ö 465

 

°³¿ä

 ¿©±â¸¦ Ŭ¸¯ÇÏ¿© ÆîÄ¡±â...

 

Jackson Àº ÀÚ¹Ù¿ë json ¶óÀ̺귯¸®·Î Àß ¾Ë·ÁÁ® ÀÖÁö¸¸ Json »Ó¸¸ ¾Æ´Ï¶ó XML/YAML/CSV µî ´Ù¾çÇÑ Çü½ÄÀÇ µ¥ÀÌŸ¸¦ Áö¿øÇÏ´Â data-processing ÅøÀÌ´Ù.

½ºÆ®¸² ¹æ½ÄÀ̹ǷΠ¼Óµµ°¡ ºü¸£¸ç À¯¿¬ÇÏ¸ç ´Ù¾çÇÑ third party µ¥ÀÌŸ ŸÀÔÀ» Áö¿øÇϸç annotation ¹æ½ÄÀ¸·Î ¸ÞŸ µ¥ÀÌŸ¸¦ ±â¼úÇÒ ¼ö ÀÖÀ¸¹Ç·Î JSON ÀÇ ¾àÁ¡Áß ÇϳªÀÎ ¹®¼­È­¿Í µ¥ÀÌŸ validation ¹®Á¦¸¦ ÇØ°áÇÒ ¼ö ÀÖ´Ù.

±¸¼º

core module

»ç¿ë

maven ¼³Á¤

databind ´Â jackson-core, jackson-annotation ¿¡ ÀÇÁ¸¼ºÀÌ ÀÖÀ¸¹Ç·Î pom ÆÄÀÏ¿¡´Â databind ¸ðµâ¸¸ ±â¼úÇØÁÖ¸é µÈ´Ù.

<properties>
    <jackson.version>2.4.4</jackson.version>
</properties>
  
<dependencies>       
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
</dependencies>

 

POJO ¿Í »ç¿ë

https://github.com/FasterXML/jackson-databind/

// getter/setter ¸¦ ¸¸µéÁö ¾Ê±â À§ÇØ public À¸·Î ¼±¾ð
// lombok À» »ç¿ëÇÏ¸é °£Æí
public class MyValue {
  public String name;
  public int age; 
}

 

com.fasterxml.jackson.databind.ObjectMapper ÀνºÅϽº »ý¼º

ObjectMapper mapper = new ObjectMapper(); // create once, reuse

 

json µ¥ÀÌŸ¸¦ java object ·Î º¯È¯

data.json
{
    "name""Bob",
    "age"13
}

 

File, URL, String ¹æ½ÄÀ¸·Î µ¥ÀÌŸ¸¦ Àоî¿Ã ¼ö ÀÖÀ½.

MyValue value = mapper.readValue(new File("data.json"), MyValue.class);
//  URL ¿¡¼­ Àбâ
value = mapper.readValue(new URL("http://some.com/api/entry.json"), MyValue.class);
// String À¸·Î Àбâ
value = mapper.readValue("{\"name\":\"Bob\", \"age\":13}", MyValue.class);

java object ¸¦ json À¸·Î º¯È¯

MyValue  myResultObject = new MyValue();
myResultObject.name = "MyName";
myResultObject.age= 11;
  
// result.json ÆÄÀÏ·Î ÀúÀå
mapper.writeValue(new File("result.json"), myResultObject);
// byte[] ·Î ÀúÀå
byte[] jsonBytes = mapper.writeValueAsBytes(myResultObject);
// string À¸·Î ÀúÀå
String jsonString = mapper.writeValueAsString(myResultObject);

 

json À¸·Î º¯È¯½Ã °³ÇàÇÏ¿© Æ÷¸ËÆÃ

{"string":"foo","number":5,"array":[1,2,3],"object":{"property":"value","subobj":{"arr":["foo","ha"],"numero":1}}}

À§¿Í °°Àº json µ¥ÀÌŸ¸¦ ´ÙÀ½Ã³·³ º¸±â ÁÁ°Ô Ãâ·ÂÇÏ·Á¸é ObjectMapper.writerWithDefaultPrettyPrinter¸¦ »ç¿ëÇÏ¸é µÈ´Ù.

 ¿©±â¸¦ Ŭ¸¯ÇÏ¿© ÆîÄ¡±â...
// Æ÷¸ËÆÃÇÏ¿© ½ºÆ®¸µÀ¸·Î º¯È¯
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(myResultObject);
  
// Æ÷¸ËÆÃÇÏ¿© ÆÄÀÏ·Î ÀúÀå
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("output.json"), myResultObject);

 

Generic Collation/Tree Model ·Î »ç¿ë

  

 

Configurator

 

 

Annotation

@JsonIgnoreProperties

Å©°Ô 2°¡Áö ¿ëµµ°¡ ÀÖÀ¸¸ç ù ¹ø°´Â Serializer/Deserialize ½Ã Á¦¿Ü½Ãų ÇÁ·ÎÆÛƼ¸¦ ÁöÁ¤ÇÑ´Ù. ´ÙÀ½°ú °°ÀÌ ÁöÁ¤Çϸé foo, bar´Â Á¦¿ÜµÈ´Ù. °³º° ÇÁ·ÎÆÛƼ¸¦ Á¦¿ÜÇÏ·Á¸é 

@JsonIgnore annotationÀ» ÇÁ·ÎÆÛƼ¿¡ Àû¿ëÇÏ¸é µÈ´Ù.

@JsonIgnoreProperties({ "foo""bar" })
public class MyBean
{
   //¾Æ·¡ µÎ °³´Â Á¦¿ÜµÊ.
   public String foo;
   public String bar;
 
   // will not be written as JSON; nor assigned from JSON:
   @JsonIgnore
   public String internal;
 
   // no annotation, public field is read/written normally
   public String external;
    
   @JsonIgnore
   public void setCode(int c) { _code = c; }
 
   // note: will also be ignored because setter has annotation!
   public int getCode() { return _code; }
}

 

@JsonProperty

getter/setter ÀÇ À̸§À» property ¿Í ´Ù¸¥ À̸§À» »ç¿ëÇÒ ¼ö ÀÖµµ·Ï ¼³Á¤ÇÑ´Ù. Database ¸¦ Àڹ٠Ŭ·¡½º·Î ¸ÅÇÎÇϴµ¥ DBÀÇ Ä÷³¸íÀÌ ¾Ë±â ¾î·Á¿ï °æ¿ìµî¿¡ À¯¿ëÇÏ°Ô »ç¿ëÇÒ ¼ö ÀÖ´Ù.

´ÙÀ½°ú °°Àº Å×À̺íÀÌ ÀÖÀ» °æ¿ì

CREATE TABLE Users (
  INT NOT NULL,
  INT NOT NULL,
  VARCHAR(80) NOT NULL
);

´ÙÀ½°ú °°ÀÌ JsonProperty ¸¦ »ç¿ëÇϸé DB ÀÇ Ä÷³¸íÀ» º¯°æÇÏÁö ¾Ê¾Æµµ °¡µ¶¼ºÀ» ³ôÀÏ ¼ö ÀÖ´Ù.

public class User
{
    @JsonProperty("userId");
    public Integer u;
  
    @JsonProperty("age");
    public Integer a;
  
    @JsonProperty("email");
    public String e;
}

json À¸·Î º¯È¯µÈ °á°ú

{
    "userId"1,
    "age"13,
    "email""user@host.com"
}

 

@JsonInclude

Serialize ½Ã µ¿ÀÛÀ» ÁöÁ¤ÇÑ´Ù. ±âº»ÀûÀ¸·Î Àè½¼Àº °ªÀÇ À¯¹«¿Í »ó°ü¾øÀÌ ¹«Á¶°Ç serialize ÇÏ°Ô µÇÁö¸¸ ´ÙÀ½°ú °°ÀÌ ¼³Á¤ÇÒ °æ¿ì not null À̰ųª none empty ÀÏ °æ¿ì¿¡¸¸ serialize µÈ´Ù.

public class User
{
    public Integer u;
     
    @JsonInclude(Include.NON_NULL)
    public Integer age;
 
 
    @JsonInclude(Include.NON_EMPTY)
    public String email;
}

 

 

¿ÜºÎ datatype Áö¿ø

JodaTime

JodaTime °ú jackson json library ¿¬µ¿ Âü°í