1

I have the following code with Jackson:

public class Header implements Serializable {

    @JsonProperty("numeroUnico")
    private Integer numeroCliente;

    @JsonProperty("oficina")
    private Integer oficina;

    @JsonProperty("fecha")
    @JsonSerialize(using = CustomDateSerializer.class)
    private Date fechaInscripcion;

}

this is my class "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));
    }
}

They asked me to migrate all the implementations of Jackson to Gson. Taking into account that the notation in Jackson @JsonProperty has an equivalence in Gson that is @SerializedName. But for the notation in Jackson of:

    

@JsonSerialize (using = CustomDateSerializer.class)

What is its equivalent for Gson? if not, as it should be the implementation for attributes of type Date in my DTO.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

I think the closest and probably the only match is @TypeAdapter. However you need to code either JsonSerializer<T> or TypeAdapter<T> to be used with that annotation.

For example how to make something like your CustomDateSerializer see accepted answer for this question.

Or maybe you can wrap your existing CustomDateSerializer with Gson TypeAdapter<Date> and use that in the annotation.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • Did you find my answer useful? If you did consider accepting it. If you did not not please comment my answer. If you found better solution create a new answer based on it. – pirho Nov 22 '18 at 09:47