Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  import java.util.*;
  
  
Standard deserializer for java.util.EnumSets.

Note: casting within this class is all messed up -- just could not figure out a way to properly deal with recursive definition of "EnumSet<K extends Enum<K>, V>

 
 @SuppressWarnings("rawtypes")
 public class EnumSetDeserializer
     extends StdDeserializer<EnumSet<?>>
     implements ContextualDeserializer
 {
     protected final JavaType _enumType;
 
     protected final Class<Enum_enumClass;
 
     protected JsonDeserializer<Enum<?>> _enumDeserializer;
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     @SuppressWarnings("unchecked" )
     public EnumSetDeserializer(JavaType enumTypeJsonDeserializer<?> deser)
     {
         super(EnumSet.class);
          = enumType;
          = (Class<Enum>) enumType.getRawClass();
          = (JsonDeserializer<Enum<?>>) deser;
     }
 
         if ( == deser) {
             return this;
         }
         return new EnumSetDeserializer(deser);
     }
    
    
Because of costs associated with constructing Enum resolvers, let's cache instances by default.
 
     @Override
     public boolean isCachable() { return true; }
     
 //    @Override
             BeanProperty propertythrows JsonMappingException
     {
         JsonDeserializer<?> deser = ;
         if (deser == null) {
             deser = ctxt.findContextualValueDeserializer(property);
         } else { // if directly assigned, probably not yet contextual, so:
             if (deser instanceof ContextualDeserializer) {
                 deser = ((ContextualDeserializerdeser).createContextual(ctxtproperty);
             }
         }
         return withDeserializer(deser);
     }
 
     /*
     /**********************************************************
     /* JsonDeserializer API
     /**********************************************************
      */
     
     @SuppressWarnings("unchecked"
     @Override
     public EnumSet<?> deserialize(JsonParser jpDeserializationContext ctxt)
         throws IOExceptionJsonProcessingException
     {
         // Ok: must point to START_ARRAY (or equivalent)
         if (!jp.isExpectedStartArrayToken()) {
             throw ctxt.mappingException(EnumSet.class);
         }
         EnumSet result = constructSet();
         JsonToken t;
 
         while ((t = jp.nextToken()) != .) {
             /* What to do with nulls? Fail or ignore? Fail, for now
              * (note: would fail if we passed it to EnumDeserializer, too,
              * but in general nulls should never be passed to non-container
              * deserializers)
              */
             if (t == .) {
                 throw ctxt.mappingException();
             }
            Enum<?> value = .deserialize(jpctxt);
            /* 24-Mar-2012, tatu: As per [JACKSON-810], may actually get nulls;
             *    but EnumSets don't allow nulls so need to skip.
             */
            if (value != null) { 
                result.add(value);
            }
        }
        return result;
    }
    @Override
            TypeDeserializer typeDeserializer)
        throws IOExceptionJsonProcessingException
    {
        return typeDeserializer.deserializeTypedFromArray(jpctxt);
    }
    
    @SuppressWarnings("unchecked"
    private EnumSet constructSet()
    {
    	// superbly ugly... but apparently necessary
    	return EnumSet.noneOf();
    }
New to GrepCode? Check out our FAQ X