Android chat crashes on DataSnapshot.getValue() for timestamp
I'm trying to add a timestamp property to my POJO. The solution above tells jackson to ignore the real data member which is used by the application. I'm using AutoValue and can't figure out how I could annotate my class to get it to work.
@AutoValue
public abstract class Pojo {
@JsonProperty("id") public abstract String id();
@JsonProperty("name") public abstract String name();
@JsonProperty("date") public abstract long date();
@JsonCreator public static Pojo create(String id, String name, long date) {
return new AutoValue_Pojo(id, name, date);
}
}
I tried using a custom serializer:
public class TimeStampSerializer extends JsonSerializer<Long> {
@Override public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(ServerValue.TIMESTAMP.toString());
}
}
but that wrote the string date: "{.sv=timestamp}"
to firebase instead of generating the timestamp
Caught my mistake:
@AutoValue
public abstract class Pojo {
@JsonProperty("id") public abstract String id();
@JsonProperty("name") public abstract String name();
//Custom serializer
@JsonSerialize(using = TimestampSerializer.class) @JsonProperty("date") public abstract long date();
@JsonCreator public static Pojo create(@JsonProperty("id") String id, @JsonProperty("name") String name, @JsonProperty("date") long date) {
return new AutoValue_Pojo(id, name, date);
}
}
public class TimestampSerializer extends JsonSerializer<Long> {
@Override public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
//Use writeObject() instead of writeString()
jgen.writeObject(ServerValue.TIMESTAMP);
}
}