2011-12-07 3 views
1

나는 같은 JSON 문자열을 역 직렬화하려고와 계층 구조로(잭슨 1.9.2) 작업을 @JsonIgnore 얻기 : 직렬화를 믹스 인 주석과 다형성 유형

{ 
    "$type": "com.example.StringProperty", 
    "value": "hello world", 
    "name": "text", 
    "readOnly": false 
} 

을 (문제를 보여주기 위해이 비트를 추출) 다음 믹스 인 주석

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="$type") 
abstract public class PropertyMixin 
{ 
    @JsonProperty 
    public String name; 
    //@JsonIgnore 
    //public boolean readOnly; 
    @JsonProperty 
    public int type; 

    PropertyMixin(@JsonProperty("name") String pName, @JsonProperty("type") int pType) 
    { 
    } 
} 

abstract public class StringPropertyMixin 
{ 
    @JsonProperty 
    public String value; 
    //@JsonIgnore 
    //public boolean readOnly; 

    @JsonCreator 
    public StringPropertyMixin(@JsonProperty("name") String pName, @JsonProperty("value") String value) 
    { 
    } 
} 

를 사용

public class Property 
{ 
    public String name; 
    public int type; 

    Property(String pName, int pType) { name = pName; type = pType; } 
} 

public class StringProperty extends Property 
{ 
    public String value;   
    StringProperty(String pName, String pValue) { 
     super(pName, String); 
     value = pValue; 
    }  
} 

과 같이 클래스의 잭슨 1.9.2로, 나는 점점 오전 오류가 나는 @JsonIgnore를 사용하여 시도

Unrecognized field "readOnly" (Class com.example.StringProperty), not marked as ignorable

입니다,하지만 (나는 그것을 시도하는 방법을 볼 수 주석 코드의 위치를 ​​확인) 도움이되지 않았다. 나는 아마 뭔가를 놓치고 있지만, 나는 눈의 또 다른 세트가 그것을보고 도움이 될 필요가 있다고 생각한다.

이 역 직렬화 환경과 같은 모습입니다 :

 objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() 
       .withFieldVisibility(JsonAutoDetect.Visibility.NONE) 
       .withGetterVisibility(JsonAutoDetect.Visibility.NONE) 
       .withSetterVisibility(JsonAutoDetect.Visibility.NONE) 
       .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); 

     MixinRegistrations module = new MixinRegistrations(); 
     objectMapper.registerModule(module); 

과 믹스 인 모듈이 나는 모든 도움을 주셔서 감사합니다

@Override 
public void setupModule(SetupContext context) 
{ 
    context.setMixInAnnotations(Property.class, PropertyMixin.class); 
    context.setMixInAnnotations(StringProperty.class, StringPropertyMixin.class); 
} 

것 같습니다. 미리 감사드립니다.

추신 :이 차이가 있는지는 모르지만 JSON은 .Net Framework 버전 4로 작성된 라이브러리에서 생성됩니다.

답변

3

작성자로 위임 한 메소드에 readOnly 필드의 매개 변수가 없기 때문에 Jackson이 문제가 있다고 생각합니다. 따라서이 누락 된 매개 변수를 자동으로 무시하는 대신 Jackson은 오류 (양호한 동작)를 발생시킵니다. JsonIgnoreProperties 주석을 사용해보십시오. 이전에는 사용하지 않아도되었지만 비 직렬화의 경우 작성자가 필요하지 않은 필드를 명시 적으로 나타낼 수 있습니다.

오, this도 관련이 있습니다.

편집 : Another question that the asker found that was useful.

+0

네가 원하는 동작이면 @JsonIgnoreProperties (ignoreUnknown = true)를 추가 할 수있다. –

+0

@Dunes :이 상황에서'@JsonIgnore'가 작동하지 않으면 어떻게'@JsonIgnoreProperties'가 도움이 될까요? '@ JsonIgnoreProperties'는'@JsonIgnore'를 여러 값으로 확장 한 것으로 보입니다. – alokoko

+0

왜'@ JsonIgnore'가 제 경우에는 작동하지 않았지만 [@ JsonIgnoreProperties'는 [이 게시물] (http : // stackoverflow.co.kr/questions/7438474/what-is-wrong-with-my-jsoncreator-and-mixin-annotation)을 참조하십시오. @ JsonIgnoreProperties 주석을 지적 해 주셔서 감사합니다. – alokoko

3

주 문제는 그 클래스가 아닙니다 잭슨 인식 할 수없는 속성을 가지고 있지만, JSON은 속성이 아닌 것을. 이와 같이 this wiki page을 읽고 싶을 수 있습니다.

언급 한 바와 같이 @JsonIgnoreProperties은 클래스에 주석을 추가 할 때 필드 또는 설정자가 필요하지 않으므로 @JsonIgnore과 달리 작동합니다. 또한 모든 알 수없는 속성을 무시하는 데 사용할 수 있습니다.

+0

링크를 제공해 주셔서 감사합니다. 30 초 전에 답을 얻었는데 어떻게 일이 어떻게 돌아 갔는지 이해하는 방법에 대한 의견을 게시했습니다. :-) – alokoko

+0

좋아요! 다행이 지금 일하고 있습니다. – StaxMan

0
@JsonIgnoreProperties({"propX", "propY"}) 

클래스 수준에서 정상적으로 작동합니까?