我和杰克逊有以下代码:
public class Header implements Serializable {
@JsonProperty("numeroUnico")
private Integer numeroCliente;
@JsonProperty("oficina")
private Integer oficina;
@JsonProperty("fecha")
@JsonSerialize(using = CustomDateSerializer.class)
private Date fechaInscripcion;
}
这是我的班级“CustomDateSerializer.class”
public class CustomDateSerializer extends StdSerializer<Date> {
private SimpleDateFormat formatter
= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public CustomDateSerializer() {
this(null);
}
public CustomDateSerializer(Class t) {
super(t);
}
@Override
public void serialize (Date value, JsonGenerator gen, SerializerProvider arg2)
throws IOException, JsonProcessingException {
gen.writeString(formatter.format(value));
}
}
他们让我将Jackson的所有实现迁移到Gson。考虑到Jackson @JsonProperty中的符号在Gson中具有@SerializedName的等价性。但对杰克逊的符号:
@JsonSerialize (using = CustomDateSerializer.class)
什么是Gson的等价物?如果没有,因为它应该是我的DTO中Date类型的属性的实现。
我认为最接近也许唯一的比赛是@TypeAdapter
。但是,您需要编码JsonSerializer<T>
或TypeAdapter<T>
以与该注释一起使用。
例如,如何使像CustomDateSerializer
这样的东西看到this question的接受答案。
或者也许你可以用Gson CustomDateSerializer
包装现有的TypeAdapter<Date>
并在注释中使用它。