2012-06-22 2 views
0

개체와 비슷한 JSON을 사용하고 있으며 이진 값도 있습니다. 바이너리 (byte[]) 데이터를 직렬화하고 싶지 않습니다.바이트 배열을 직렬화하지 않도록 jackson을 구성하는 방법은 무엇입니까?

byte[]에 대한 사용자 지정 serializer를 추가하려고했습니다. 그러나 그것은 효과가 없었습니다. 모두 기본 시리얼을 무시 실패,

public class ByteArraySerialiser extends SerializerBase<Byte> { 

    protected ByteArraySerialiser() { 
     super(Byte.class, false); 
    } 

    @Override 
    public void serialize(Byte arg0, JsonGenerator arg1, 
      SerializerProvider arg2) throws IOException, 
      JsonGenerationException { 
     arg1.writeString(""); 
    } 

} 

그러나 :

public class ByteArraySerialiser extends SerializerBase<Byte[]> { 

    protected ByteArraySerialiser() { 
     super(Byte[].class, false); 
    } 

    @Override 
    public void serialize(Byte[] arg0, JsonGenerator arg1, 
      SerializerProvider arg2) throws IOException, 
      JsonGenerationException { 
     arg1.writeString(""); 
    } 

} 

2를보십시오 :

1 시도해보십시오.

어노테이션을 사용할 수 없으므로 해당 Map<Object, Object>입니다.

감사합니다.

답변

1

getter 또는 필드 자체에서 JsonIgnore 주석을 사용해 보았습니까? 그렇게하기 위해 MixIn을 사용할 수도 있습니다. 예는 (oVirt 오픈 소스에서 가져온) :

public abstract class JsonNGuidMixIn extends NGuid { 

    /** 
    * Tells Jackson that the constructor with the {@link String} argument is to be used to deserialize the entity, 
    * using the "uuid" property as the argument. 
    * 
    * @param candidate 
    */ 
    @JsonCreator 
    public JsonNGuidMixIn(@JsonProperty("uuid") String candidate) { 
     super(candidate); 
    } 

    /** 
    * Ignore this method since Jackson will try to recursively dereference it and fail to serialize. 
    */ 
    @JsonIgnore 
    @Override 
    public abstract Guid getValue(); 
} 

그리고 사용이 JSonObjectSerializer에있다

@Override 
    public String serialize(Serializable payload) throws SerializationExeption { 
     ObjectMapper mapper = new ObjectMapper(); 
     mapper.getSerializationConfig().addMixInAnnotations(NGuid.class, JsonNGuidMixIn.class); 
     mapper.getSerializationConfig().addMixInAnnotations(Guid.class, JsonNGuidMixIn.class); 
     mapper.configure(Feature.INDENT_OUTPUT, true); 
     mapper.enableDefaultTyping(); 
     return writeJsonAsString(payload, mapper); 
    } 
(여기의 일부를 붙여 복사)
관련 문제