在 AutoValue POJO 上节省 firebase ServerValue.TIMESTAMP
Saving firebase ServerValue.TIMESTAMP on AutoValue POJOs
Android chat crashes on DataSnapshot.getValue() for timestamp
我正在尝试向我的 POJO 添加时间戳 属性。上面的解决方案告诉杰克逊忽略应用程序使用的真实数据成员。我正在使用 AutoValue 并且无法弄清楚如何注释我的 class 以使其工作。
@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);
}
}
我尝试使用自定义序列化程序:
public class TimeStampSerializer extends JsonSerializer<Long> {
@Override public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(ServerValue.TIMESTAMP.toString());
}
}
但是将字符串 date: "{.sv=timestamp}"
写入 firebase 而不是生成时间戳
发现我的错误:
@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);
}
}
Android chat crashes on DataSnapshot.getValue() for timestamp
我正在尝试向我的 POJO 添加时间戳 属性。上面的解决方案告诉杰克逊忽略应用程序使用的真实数据成员。我正在使用 AutoValue 并且无法弄清楚如何注释我的 class 以使其工作。
@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);
}
}
我尝试使用自定义序列化程序:
public class TimeStampSerializer extends JsonSerializer<Long> {
@Override public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(ServerValue.TIMESTAMP.toString());
}
}
但是将字符串 date: "{.sv=timestamp}"
写入 firebase 而不是生成时间戳
发现我的错误:
@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);
}
}