All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fasterxml.jackson.module.hibernate.HibernateSerializers Maven / Gradle / Ivy

Go to download

Add-on module for Jackson (http://jackson.codehaus.org) to support Hibernate (http://hibernate.org) data types.

There is a newer version: 1.9.1
Show newest version
package com.fasterxml.jackson.module.hibernate;

import org.hibernate.collection.PersistentCollection;
import org.hibernate.collection.PersistentMap;
import org.hibernate.proxy.HibernateProxy;

import org.codehaus.jackson.map.*;
import org.codehaus.jackson.map.type.CollectionType;
import org.codehaus.jackson.map.type.MapType;
import org.codehaus.jackson.type.JavaType;

import com.fasterxml.jackson.module.hibernate.HibernateModule.Feature;

public class HibernateSerializers extends Serializers.None
{
    protected final int _moduleFeatures;
    
    public HibernateSerializers(int features)
    {
        _moduleFeatures = features;
    }

    @Override
    public JsonSerializer findSerializer(
            SerializationConfig config, JavaType type,
            BeanDescription beanDesc, BeanProperty beanProperty )
    {
        Class raw = type.getRawClass();

        /* Note: PersistentCollection does not implement Collection, so we
         * may get some types here...
         */
        if (PersistentCollection.class.isAssignableFrom(raw)) {
            // TODO: handle iterator types?
        }
        
        if (HibernateProxy.class.isAssignableFrom(raw)) {
            return new HibernateProxySerializer(beanProperty, isEnabled(Feature.FORCE_LAZY_LOADING));
        }
        return null;
    }

    @Override
    public JsonSerializer findCollectionSerializer(SerializationConfig config,
            CollectionType type, BeanDescription beanDesc, BeanProperty property,
            TypeSerializer elementTypeSerializer, JsonSerializer elementValueSerializer)
    {
        Class raw = type.getRawClass();
        // only handle PersistentCollection style collections...
        if (PersistentCollection.class.isAssignableFrom(raw)) {
            return new PersistentCollectionSerializer(property, type, isEnabled(Feature.FORCE_LAZY_LOADING));
        }
        return null;
    }

    @Override
    public JsonSerializer findMapSerializer(SerializationConfig config,
            MapType type, BeanDescription beanDesc, BeanProperty property,
            JsonSerializer keySerializer,
            TypeSerializer elementTypeSerializer, JsonSerializer elementValueSerializer)
    {
        Class raw = type.getRawClass();
        if (PersistentMap.class.isAssignableFrom(raw)) {
            return new PersistentCollectionSerializer(property, type, isEnabled(Feature.FORCE_LAZY_LOADING));
        }
        return null;
    }
    
    public final boolean isEnabled(HibernateModule.Feature f) {
        return (_moduleFeatures & f.getMask()) != 0;
    }
}