Last active
January 29, 2018 16:40
-
-
Save epool/131531644952f47072c2f6d8792ddcaa to your computer and use it in GitHub Desktop.
Firebase util class to serialize and deserialize Object for Firebase 9.2.0 using Auto-Value and Jackson.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.PropertyAccessor; | |
import com.fasterxml.jackson.databind.JavaType; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.GenericTypeIndicator; | |
import java.util.Map; | |
public enum FirebaseUtil { | |
; | |
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | |
private static final JavaType JAVA_TYPE = OBJECT_MAPPER.getTypeFactory().constructMapType(Map.class, String.class, Object.class); | |
static { | |
OBJECT_MAPPER.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | |
} | |
public static Map<String, Object> serialize(Object o) { | |
// Convert to a Map<String,Object> using Jackson and then pass that to Firebase | |
return OBJECT_MAPPER.convertValue(o, JAVA_TYPE); | |
} | |
public static <T> T deserialize(DataSnapshot dataSnapshot, Class<T> tClass) { | |
// Use Firebase to convert to a Map<String,Object> | |
GenericTypeIndicator<Map<String, Object>> genericTypeIndicator = new GenericTypeIndicator<Map<String, Object>>() { | |
}; | |
Map<String, Object> map = dataSnapshot.getValue(genericTypeIndicator); | |
// Use Jackson to convert from a Map to an Office object | |
return OBJECT_MAPPER.convertValue(map, tClass); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.google.auto.value.AutoValue; | |
@AutoValue | |
@JsonDeserialize(builder = AutoValue_Phone.Builder.class) | |
public abstract class Phone { | |
@JsonProperty("type") | |
public abstract int type(); | |
@JsonProperty("number") | |
public abstract String number(); | |
@AutoValue.Builder | |
public static abstract class Builder { | |
@JsonProperty("type") | |
public abstract Builder type(int type); | |
@JsonProperty("number") | |
public abstract Builder number(String number); | |
public abstract Phone build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.google.auto.value.AutoValue; | |
import java.util.List; | |
@AutoValue | |
@JsonDeserialize(builder = AutoValue_User.Builder.class) | |
public abstract class User { | |
@JsonProperty("first_name") | |
public abstract String firstName(); | |
@JsonProperty("last_name") | |
public abstract String lastName(); | |
@JsonProperty("phones") | |
public abstract List<Phone> phones(); | |
@AutoValue.Builder | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public static abstract class Builder { | |
@JsonProperty("first_name") | |
public abstract Builder firstName(String firstName); | |
@JsonProperty("last_name") | |
public abstract Builder lastName(String lastName); | |
@JsonProperty("phones") | |
public abstract Builder phones(List<Phone> phones); | |
public abstract User build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment