Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  
  
Deserializer class that can deserialize instances of specified Enum class from Strings and Integers.
 
 public class EnumDeserializer
     extends StdScalarDeserializer<Enum<?>>
 {
     protected final EnumResolver<?> _resolver;
     
     public EnumDeserializer(EnumResolver<?> res)
     {
         super(Enum.class);
          = res;
     }

    
Factory method used when Enum instances are to be deserialized using a creator (static factory method)

Returns:
Deserializer based on given factory method, if type was suitable; null if type can not be used
 
     public static JsonDeserializer<?> deserializerForCreator(DeserializationConfig config,
             Class<?> enumClassAnnotatedMethod factory)
     {
         
         // note: caller has verified there's just one arg; but we must verify its type
         Class<?> paramClass = factory.getRawParameterType(0);
         if (paramClass == String.class) {
             paramClass = null;
         } else  if (paramClass == . || paramClass == Integer.class) {
             paramClass = Integer.class;
         } else  if (paramClass == . || paramClass == Long.class) {
             paramClass = Long.class;
         } else {
             throw new IllegalArgumentException("Parameter #0 type for factory method ("+factory
                     +") not suitable, must be java.lang.String or int/Integer/long/Long");
         }
         if (config.canOverrideAccessModifiers()) {
             ClassUtil.checkAndFixAccess(factory.getMember());
         }
         return new FactoryBasedDeserializer(enumClassfactoryparamClass);
     }
     
     /*
     /**********************************************************
     /* Default JsonDeserializer implementation
     /**********************************************************
      */

    
Because of costs associated with constructing Enum resolvers, let's cache instances by default.
 
     @Override
     public boolean isCachable() { return true; }
     
     @Override
     public Enum<?> deserialize(JsonParser jpDeserializationContext ctxt)
         throws IOExceptionJsonProcessingException
     {
         JsonToken curr = jp.getCurrentToken();
         
         // Usually should just get string value:
         if (curr == . || curr == .) {
             String name = jp.getText();
             Enum<?> result = .findEnum(name);
             if (result == null  && !ctxt.isEnabled(.)) {
                 throw ctxt.weirdStringException(name.getEnumClass(),
                         "value not one of declared Enum instance names");
             }
             return result;
         }
         // But let's consider int acceptable as well (if within ordinal range)
         if (curr == .) {
             /* ... unless told not to do that. :-)
              * (as per [JACKSON-412]
              */
             if (ctxt.isEnabled(.)) {
                 throw ctxt.mappingException("Not allowed to deserialize Enum value out of JSON number (disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow)");
             }
             
             int index = jp.getIntValue();
             Enum<?> result = .getEnum(index);
            if (result == null && !ctxt.isEnabled(.)) {
                throw ctxt.weirdNumberException(Integer.valueOf(index), .getEnumClass(),
                        "index value outside legal index range [0.."+.lastValidIndex()+"]");
            }
            return result;
        }
        throw ctxt.mappingException(.getEnumClass());
    }
    /*
    /**********************************************************
    /* Default JsonDeserializer implementation
    /**********************************************************
     */

    
Deserializer that uses a single-String static factory method for locating Enum values by String id.
    protected static class FactoryBasedDeserializer
        extends StdScalarDeserializer<Object>
    {
        protected final Class<?> _enumClass;
        // Marker type; null if String expected; otherwise numeric wrapper
        protected final Class<?> _inputType;
        protected final Method _factory;
        
        public FactoryBasedDeserializer(Class<?> clsAnnotatedMethod f,
                Class<?> inputType)
        {
            super(Enum.class);
             = cls;
             = f.getAnnotated();
             = inputType;
        }
        @Override
        public Object deserialize(JsonParser jpDeserializationContext ctxt)
            throws IOExceptionJsonProcessingException
        {
            // couple of accepted types...
            Object value;
            if ( == null) {
                value = jp.getText();
            } else  if ( == Integer.class) {
                value = Integer.valueOf(jp.getValueAsInt());
            } else  if ( == Long.class) {
                value = Long.valueOf(jp.getValueAsLong());
            } else {
                throw ctxt.mappingException();
            }
            try {
                return .invoke(value);
            } catch (Exception e) {
                ClassUtil.unwrapAndThrowAsIAE(e);
            }
            return null;
        }
    }
New to GrepCode? Check out our FAQ X