io.gsonfire.GsonFireBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gson-fire Show documentation
Show all versions of gson-fire Show documentation
A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getter) serialization, pre and post processors and many more. Check out the documentation to learn how to use it!
package io.gsonfire;
import io.gsonfire.gson.FireTypeAdapterFactory;
import io.gsonfire.postprocessors.MergeMapPostProcessor;
import io.gsonfire.postprocessors.MethodInvokerPostProcessor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.*;
/**
* @autor: julio
*/
public class GsonFireBuilder {
private final Map classConfigMap = new HashMap();
private final List orderedClasses = new ArrayList();
private DateSerializationPolicy dateSerializationPolicy;
private TimeZone serializeTimeZone = TimeZone.getDefault();
private ClassConfig getClassConfig(Class clazz){
ClassConfig result = classConfigMap.get(clazz);
if(result == null){
result = new ClassConfig(clazz);
classConfigMap.put(clazz, result);
insertOrdered(orderedClasses, clazz);
}
return result;
}
private static void insertOrdered(List classes, Class clazz) {
for(int i = classes.size() - 1; i >= 0; i--) {
Class current = classes.get(i);
if(current.isAssignableFrom(clazz)) {
classes.add(i + 1, clazz);
return;
}
}
classes.add(0, clazz);
}
/**
* Registers a Type selector for the Class specified.
* A type selector is in charge of deciding which sub class to use when converting a json
* into an object.
* See docs and example
* @param clazz
* @param factory
* @param
* @return
*/
public GsonFireBuilder registerTypeSelector(Class clazz, TypeSelector factory){
ClassConfig config = getClassConfig(clazz);
config.setTypeSelector(factory);
return this;
}
/**
* Registers a Post processor for the Class specified.
* A post processor is a class that will add new fields to a generated json just after generation, or that
* will prepare a class just created from a json.
* See docs and example
*
* @param clazz
* @param postProcessor
* @param
* @return
*/
public GsonFireBuilder registerPostProcessor(Class clazz, PostProcessor super T> postProcessor){
ClassConfig config = getClassConfig(clazz);
config.getPostProcessors().add(postProcessor);
return this;
}
/**
* Registers a pre processor for the Class specified.
* A pre processor is a class that will be given the gson to be deserialized in case it wants to change it before
* it actually gets deserialized into a class
* See docs and example
*
* @param clazz
* @param preProcessor
* @param
* @return
*/
public GsonFireBuilder registerPreProcessor(Class clazz, PreProcessor super T> preProcessor){
ClassConfig config = getClassConfig(clazz);
config.getPreProcessors().add(preProcessor);
return this;
}
/**
* Configures the resulting Gson to serialize/unserialize Date instances with a policy
* @param policy
* @return
*/
public GsonFireBuilder dateSerializationPolicy(DateSerializationPolicy policy){
dateSerializationPolicy = policy;
return this;
}
/**
* By enabling this, all methods with the annotation {@link io.gsonfire.annotations.ExposeMethodResult} will
* be evaluated and it result will be added to the resulting json
* @return
*/
public GsonFireBuilder enableExposeMethodResult(){
registerPostProcessor(Object.class, new MethodInvokerPostProcessor