Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  import java.util.*;
  
  
Deserializer for EnumMap values.

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

Author(s):
tsaloranta
 
 @SuppressWarnings({ "unchecked""rawtypes" }) 
 public class EnumMapDeserializer
     extends StdDeserializer<EnumMap<?,?>>
     implements ContextualDeserializer
 {
     protected final JavaType _mapType;
     
     protected final Class<?> _enumClass;
 
     protected JsonDeserializer<Enum<?>> _keyDeserializer;
 
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     public EnumMapDeserializer(JavaType mapType,
             JsonDeserializer<?> keyDeserializerJsonDeserializer<?> valueDeser)
     {
         super(EnumMap.class);
          = mapType;
          = mapType.getKeyType().getRawClass();
          = (JsonDeserializer<Enum<?>>) keyDeserializer;
          = (JsonDeserializer<Object>) valueDeser;
     }
 
     public EnumMapDeserializer withResolved(JsonDeserializer<?> keyDeserializer,
             JsonDeserializer<?> valueDeserializer)
     {
         if ((keyDeserializer == ) && valueDeserializer == ) {
             return this;
         }
         return new EnumMapDeserializer(,
                 keyDeserializervalueDeserializer);
     }
    
    
Method called to finalize setup of this deserializer, when it is known for which property deserializer is needed for.
 
 //  @Override
             BeanProperty propertythrows JsonMappingException
     {
         // note: instead of finding key deserializer, with enums we actually
         // work with regular deserializers (less code duplication; but not
         // quite as clean as it ought to be)
         JsonDeserializer<?> kd = ;
         if (kd == null) {
             kd = ctxt.findContextualValueDeserializer(.getKeyType(), property);
         }
         JsonDeserializer<?> vd = ;
         if (vd == null) {
             vd = ctxt.findContextualValueDeserializer(.getContentType(), property);
         } else { // if directly assigned, probably not yet contextual, so:
             if (vd instanceof ContextualDeserializer) {
                 vd = ((ContextualDeserializervd).createContextual(ctxtproperty);
             }
         }
 
         return withResolved(kdvd);
     }
    
    
Because of costs associated with constructing Enum resolvers, let's cache instances by default.
 
     @Override
     public boolean isCachable() { return true; }
     
     /*
     /**********************************************************
     /* Actual deserialization
     /**********************************************************
      */
 
    @Override
    public EnumMap<?,?> deserialize(JsonParser jpDeserializationContext ctxt)
        throws IOExceptionJsonProcessingException
    {
        // Ok: must point to START_OBJECT
        if (jp.getCurrentToken() != .) {
            throw ctxt.mappingException(EnumMap.class);
        }
        EnumMap result = constructMap();
        while ((jp.nextToken()) != .) {
            Enum<?> key = .deserialize(jpctxt);
            if (key == null) {
                if (!ctxt.isEnabled(.)) {
                    String value = null;
                    try { // bit ugly, but will have to do; works with usual scalars
                        if (jp.hasCurrentToken()) {
                            value = jp.getText();
                        }
                    } catch (Exception e) { }
                    throw ctxt.weirdStringException(value"value not one of declared Enum instance names");
                }
                /* 24-Mar-2012, tatu: Null won't work as a key anyway, so let's
                 *  just skip the entry then. But we must skip the value then.
                 */
                jp.nextToken();
                jp.skipChildren();
                continue;
            }
            // And then the value...
            JsonToken t = jp.nextToken();
            /* note: MUST check for nulls separately: deserializers will
             * not handle them (and maybe fail or return bogus data)
             */
            Object value = (t == .) ?
                null :  .deserialize(jpctxt);
            result.put(keyvalue);
        }
        return result;
    }
    @Override
            TypeDeserializer typeDeserializer)
        throws IOExceptionJsonProcessingException
    {
        // In future could check current token... for now this should be enough:
        return typeDeserializer.deserializeTypedFromObject(jpctxt);
    }
    
    private EnumMap<?,?> constructMap() {
        return new EnumMap();
    }
New to GrepCode? Check out our FAQ X