Created
September 7, 2016 21:21
-
-
Save claytantor/7e57e7624509280b1f81271f3254d569 to your computer and use it in GitHub Desktop.
simple factory pattern for Gson gives complete control over serialization
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
package com.dronze.json; | |
import com.dronze.json.deserialize.*; | |
import com.dronze.json.serialize.*; | |
import com.dronze.model.*; | |
import com.google.common.collect.HashBasedTable; | |
import com.google.common.collect.TreeBasedTable; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import java.util.Date; | |
/** | |
* Created by claytongraham on 12/7/15. | |
*/ | |
public class GsonFactory { | |
public enum Type{ | |
DEFAULT, | |
MODEL, | |
USER, | |
WIKI, | |
} | |
protected static Gson makeSerializerGson(Type type, Boolean pretty){ | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
if(pretty) | |
gsonBuilder.setPrettyPrinting(); | |
switch (type){ | |
case MODEL: | |
gsonBuilder.setDateFormat("yyyy-MM-dd"); | |
gsonBuilder.registerTypeAdapter(User.class, new UserModelSerializer()); | |
gsonBuilder.registerTypeAdapter(Ticker.class, new TickerModelSerializer()); | |
gsonBuilder.registerTypeAdapter(Account.class, new AccountModelSerializer()); | |
break; | |
case DEFAULT: | |
default: | |
gsonBuilder.registerTypeAdapter(Date.class, new DronzeDateSerializer()); | |
gsonBuilder.registerTypeAdapter(HashBasedTable.class, new GuavaTableSerializer()); | |
gsonBuilder.registerTypeAdapter(TreeBasedTable.class, new GuavaTableSerializer()); | |
gsonBuilder.registerTypeAdapter(Screen.class, new ScreenSerializer()); | |
break; | |
} | |
return gsonBuilder.create(); | |
} | |
protected static Gson makeDeserializerGson(Type type){ | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
switch (type){ | |
case MODEL: | |
gsonBuilder.registerTypeAdapter(User.class, new UserDeserializer()); | |
gsonBuilder.registerTypeAdapter(Ticker.class, new ModelTickerDeserializer()); | |
gsonBuilder.registerTypeAdapter(Simulation.class, new SimulationDeserializer()); | |
gsonBuilder.registerTypeAdapter(Dronze.class, new DronzeDeserializer()); | |
gsonBuilder.registerTypeAdapter(Strategy.class, new StrategyDeserializer()); | |
gsonBuilder.registerTypeAdapter(AccountDeserializer.class, new AccountDeserializer()); | |
break; | |
case WIKI: | |
gsonBuilder.registerTypeAdapter(Ticker.class, new WikiTickerDeserializer()); | |
break; | |
case USER: | |
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); | |
break; | |
// case TRANSACTION_FORM: | |
case DEFAULT: | |
gsonBuilder.setDateFormat("yyyy-MM-dd"); | |
break; | |
default: | |
break; | |
} | |
return gsonBuilder.create(); | |
} | |
public static String toJson(Object entity, Type type){ | |
return makeSerializerGson(type, false).toJson(entity); | |
} | |
public static JsonElement toJsonElement(Object entity, Type type){ | |
return makeSerializerGson(type, false).toJsonTree(entity); | |
} | |
public static String toJson(Object entity, Type type, Boolean pretty){ | |
return makeSerializerGson(type, pretty).toJson(entity); | |
} | |
public static <T> T fromJson(String json, Class<T> classOfT, Type type){ | |
return makeDeserializerGson(type).fromJson(json, classOfT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment