Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.datatype.hibernate4;
  
  
  
  
Wrapper serializer used to handle aspects of lazy loading that can be used for Hibernate collection datatypes; which includes both Collection and Map types (unlike in JDK).
 
     extends JsonSerializer<Object>
     implements ContextualSerializer
 {
    
Whether loading of values is forced for lazy references.
 
     protected final boolean _forceLazyLoading;

    
Serializer that does actual value serialization when value is available (either already or with forced access).
 
     protected final JsonSerializer<Object_serializer;
 
     /*
     /**********************************************************************
     /* Life cycle
     /**********************************************************************
      */
 
     @SuppressWarnings("unchecked")
     public PersistentCollectionSerializer(boolean forceLazyLoading,
             JsonSerializer<?> serializer)
     {
          = forceLazyLoading;
          = (JsonSerializer<Object>) serializer;
     }

    
We need to resolve actual serializer once we know the context; specifically must know type of property being serialized. If not known
 
     @Override
     public JsonSerializer<?> createContextual(SerializerProvider provider,
             BeanProperty property)
         throws JsonMappingException
     {
         /* 18-Oct-2013, tatu: Whether this is for the primary property or secondary is
          *   not quite certain; presume primary one for now.
          */
         JsonSerializer<?> ser = provider.handlePrimaryContextualization(property);
 
         // If we use eager loading, or force it, can just return underlying serializer as is
         if ( || !usesLazyLoading(property)) {
             return ser;
         }
         if (ser != ) {
             return new PersistentCollectionSerializer(ser);
         }
         return this;
     }
     
     /*
     /**********************************************************************
     /* JsonSerializer impl
     /**********************************************************************
      */
 
     // since 2.3
     @Override
     public boolean isEmpty(Object value)
     {
         if (value == null) { // is null ever passed?
             return true;
         }
         if (value instanceof PersistentCollection) {
             return findLazyValue((PersistentCollectionvalue) == null;
         }
         return false;
     }
     
     @Override
     public void serialize(Object valueJsonGenerator jgenSerializerProvider provider)
         throws IOExceptionJsonProcessingException
     {
         if (value instanceof PersistentCollection) {
             value = findLazyValue((PersistentCollectionvalue);
             if (value == null) {
                 provider.defaultSerializeNull(jgen);
                return;
            }
        }
        if ( == null) { // sanity check...
            throw new JsonMappingException("PersistentCollection does not have serializer set");
        }
        .serialize(valuejgenprovider);
    }
    
    @Override
    public void serializeWithType(Object valueJsonGenerator jgenSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonProcessingException
    {
        if (value instanceof PersistentCollection) {
            value = findLazyValue((PersistentCollectionvalue);
            if (value == null) {
                provider.defaultSerializeNull(jgen);
                return;
            }
        }
        if ( == null) { // sanity check...
            throw new JsonMappingException("PersistentCollection does not have serializer set");
        }
        .serializeWithType(valuejgenprovidertypeSer);
    }
    /*
    /**********************************************************************
    /* Helper methods
    /**********************************************************************
     */
    protected Object findLazyValue(PersistentCollection coll)
    {
        // If lazy-loaded, not yet loaded, may serialize as null?
        if (! && !coll.wasInitialized()) {
            return null;
        }
        return coll.getValue();
    }
    
    
Method called to see whether given property indicates it uses lazy resolution of reference contained.
    protected boolean usesLazyLoading(BeanProperty property)
    {
        if (property != null) {
            // As per [Issue#36]
            ElementCollection ec = property.getAnnotation(ElementCollection.class);
            if (ec != null) {
                return (ec.fetch() == .);
            }
            OneToMany ann1 = property.getAnnotation(OneToMany.class);
            if (ann1 != null) {
                return (ann1.fetch() == .);
            }
            OneToOne ann2 = property.getAnnotation(OneToOne.class);
            if (ann2 != null) {
                return (ann2.fetch() == .);
            }
            ManyToOne ann3 = property.getAnnotation(ManyToOne.class);
            if (ann3 != null) {
                return (ann3.fetch() == .);
            }
            ManyToMany ann4 = property.getAnnotation(ManyToMany.class);
            if (ann4 != null) {
                return (ann4.fetch() == .);
            }
        }
        return false;
    }
New to GrepCode? Check out our FAQ X