나는 같은 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로 작성된 라이브러리에서 생성됩니다.
네가 원하는 동작이면 @JsonIgnoreProperties (ignoreUnknown = true)를 추가 할 수있다. –
@Dunes :이 상황에서'@JsonIgnore'가 작동하지 않으면 어떻게'@JsonIgnoreProperties'가 도움이 될까요? '@ JsonIgnoreProperties'는'@JsonIgnore'를 여러 값으로 확장 한 것으로 보입니다. – alokoko
왜'@ JsonIgnore'가 제 경우에는 작동하지 않았지만 [@ JsonIgnoreProperties'는 [이 게시물] (http : // stackoverflow.co.kr/questions/7438474/what-is-wrong-with-my-jsoncreator-and-mixin-annotation)을 참조하십시오. @ JsonIgnoreProperties 주석을 지적 해 주셔서 감사합니다. – alokoko