kknd22
4/30/2014 - 7:10 PM

register for hibernate proxy gson factory adapter

register for hibernate proxy gson factory adapter

/**
 * in grails-app/resource/spring/resource.groovy
 * beans = {
 *	hibernateProxyAdapterFactory(hbadapter.HibernateProxyAdapterFactory)
 * }
 * 
 */
package hbadapter;

import org.hibernate.proxy.HibernateProxy;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

public class HibernateProxyAdapterFactory implements TypeAdapterFactory {
    public TypeAdapter create(Gson gson, TypeToken type) {
        return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ?  new HibernateProxyTypeAdapter(gson) : null);
    }
}