When you\'re making a Plain Old Java Object that\'s meant to be serialized from and deserialized to Firebase, is there a way to use the ServerValue.TIMESTAMP va
I wanted to improve Lyla's answer a little bit. First, I would like to get rid of public methods public HashMap. In order to do that you can mark dateCreated property with @JsonProperty annotation. Another possible way to do so is to change property detection like that: @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
Second, I don't understand why we need to put ServerValue.TIMESTAMP into HashMap while we can just store them as property. So my final code is:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.firebase.client.ServerValue;
public class ShoppingList {
private String listName;
private String owner;
@JsonProperty
private Object created;
public ShoppingList() {
}
public ShoppingList(String listName, String owner) {
this.listName = listName;
this.owner = owner;
this.created = ServerValue.TIMESTAMP;
}
public String getListName() {
return listName;
}
public String getOwner() {
return owner;
}
@JsonIgnore
public Long getCreatedTimestamp() {
if (created instanceof Long) {
return (Long) created;
}
else {
return null;
}
}
@Override
public String toString() {
return listName + " by " + owner;
}
}