본문 바로가기
  • AI (Artificial Intelligence)
Programming/Java

Jackson How-To: Ignoring Unknown Properties

by 로샤스 2014. 9. 11.
  1. Jackson How-To: Ignoring Unknown Properties
    • http://wiki.fasterxml.com/JacksonHowToIgnoreUnknown
    • JSON 데이터를 자바 객체로 바인딩 , JSON 모든 프로퍼티가 자바 객체에 일치할 필요가 있다.
    • 자바 객체의 프로퍼티와 일치하지 않는 프로퍼티를 만난다면 Exception 발생하게 된다.
    • Sometimes, Unknown Properties 대해 예외를 발생시키지 않고 Ignore 해야 필요가 있다.

     

  2. Add "Any setter" to handle any unknown properties
    • @JsonAnySetter 어노테이션으로 특정 메소드를 명시해 주는 방법.(자동으로 되지 않는다.)

      // note: name does not matter; never auto-detected, need to annotate

      // (also note that formal argument type #1 must be "String"; second one is usually

       /  "Object", but can be something else -- as long as JSON can be bound to that type)

      @JsonAnySetter

      public void handleUnknown(String key, Object value) {

        // do something: put to a Map; log a warning, whatever

      }

    • Unknown Properties 만날때마다 해당 메소드가 호출된다.
    • 이는 다양한 이름의 프로퍼티가 들어올 있거나, 각각의 분리된 Setter methods 만들고 싶지 않을 유용하다.
    • 단점으로는 Object.class 같이 Common value type 사용해야 한다.
    • 하나의 클래스에 하나의 핸들러 메소드만 등록해 사용할 있다. 복수개일 경우 Deserializer할때 Exception 발생한다.

     

  3. Register Problem Handler
    • DeserializationProblemHandler(ObjectMapper.getDeserializationConfig().addHandler())를 등록하는 것도 가능하다.
    • Unknown Property 만날때마다 핸들러의 handleUnknownProperty 메소드가 호출된다.
    • 이는 Unknown Property 만났을 단순히 로그를 찍기위해 사용할 용이하다.
      • , 문제도 없고 데이터 바인딩에 심각한 문제를 일으키진 않지만, 정확한 데이터가 들어오는 것을 확인하기 위해 사용한다.

     

  4. Globally disable checking
    • 모든 unknown Properties 대한 Exception 없애기 위해 사용한다.
    • 이를 사용하기 위해서는 SerializationConfig.Featue.FAIL_ON_UNKNOWN_PROPERTIES(Deserialization Features) Disabled해주어야 한다.
    • Unknown properties 대해 무시하는 방법으로 가장 좋은 Approach이다!!
    • This feature is available with version 1.2 and later.

     

  5. Deserialization Features
    • Version 1.4 adds new annotation
    •  @JsonIgnoreProperties 어노테이션을 사용해서 무시해버린다.
      • Annotate 클래스의 특정 프로퍼티 목록들을 무시한다.
      • Annotate 클래스의 모든 Unknown Properties 대해 Ignore 함을 정의할 있다.( ignoreUnknown Properties true 설정)


















출처 : http://youreme.blog.me/110082668135




















'Programming > Java' 카테고리의 다른 글

Spring에서 Json으로의 입출력 정리  (0) 2014.09.11
Jackson Tutorial - 2  (0) 2014.09.11
Jackson Tutorial - 1  (0) 2014.09.11
파싱(Parsing)강좌  (0) 2014.05.23
JNI사용하기 (여기서는 java + c, 사용툴 : Visual studio)  (0) 2014.02.26

댓글