When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?

前端 未结 6 1113
挽巷
挽巷 2020-11-30 02:51

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

6条回答
  •  庸人自扰
    2020-11-30 02:58

    I wanted to improve Lyla's answer a little bit. First, I would like to get rid of public methods public HashMap getDateLastChanged() public HashMap getDateCreated(). 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;
        }
    
    }
    

提交回复
热议问题