Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.datatype.guava.deser;
  
  import java.util.List;
  
  
 
 
 public class MultimapDeserializer extends JsonDeserializer<Multimap<?, ?>>
     implements ContextualDeserializer
 {
     private static final List<StringMETHOD_NAMES = ImmutableList.of("copyOf""create");
 
     private final MapLikeType type;
     private final KeyDeserializer keyDeserializer;
     private final TypeDeserializer elementTypeDeserializer;
     private final JsonDeserializer<?> elementDeserializer;

    
Since we have to use a method to transform from a known multi-map type into actual one, we'll resolve method just once, use it. Note that if this is set to null, we can just construct a com.google.common.collect.LinkedListMultimap instance and be done with it.
 
     private final Method creatorMethod;
     
     public MultimapDeserializer(MapLikeType type,
             KeyDeserializer keyDeserializer,
             TypeDeserializer elementTypeDeserializerJsonDeserializer<?> elementDeserializer)
     {
         this(typekeyDeserializerelementTypeDeserializerelementDeserializer,
                 findTransformer(type.getRawClass()));
     }
     
     public MultimapDeserializer(MapLikeType type,
             KeyDeserializer keyDeserializer,
             TypeDeserializer elementTypeDeserializerJsonDeserializer<?> elementDeserializer,
             Method creatorMethod)
     {
         this. = type;
         this. = keyDeserializer;
         this. = elementTypeDeserializer;
         this. = elementDeserializer;
         this. = creatorMethod;
     }

    
We need to use this method to properly handle possible contextual variants of key and value deserializers, as well as type deserializers.
 
             BeanProperty propertythrows JsonMappingException
     {
         KeyDeserializer kd = ;
         if (kd == null) {
             kd = ctxt.findKeyDeserializer(.getKeyType(), property);
         }
         JsonDeserializer<?> ed = ;
         if (ed == null) {
             ed = ctxt.findContextualValueDeserializer(.getContentType(), property);
         }
         // Type deserializer is slightly different; must be passed, but needs to become contextual:
         TypeDeserializer etd = ;
         if (etd != null && property != null) {
             etd = etd.forProperty(property);
         }
         return new MultimapDeserializer(kdetdedthis.);
     }
     
     @Override
     public Multimap<?, ?> deserialize(JsonParser jpDeserializationContext ctxt)
         throws IOExceptionJsonProcessingException
     {
         // Picked LLM since it is preserves both K, V ordering and supports nulls.
         LinkedListMultimap<ObjectObjectbuilder = LinkedListMultimap.create();
 
         //     private static final List<String> METHOD_NAMES = ImmutableList.of("create", "copyOf");
 
         
         while (jp.nextToken() != .)
         {
             final Object key;
             if ( != null)
             {
                 key = .deserializeKey(jp.getCurrentName(), ctxt);
             }
             else
             {
                key = jp.getCurrentName();
            }
            jp.nextToken();
            expect(jp.);
            while (jp.nextToken() != .)
            {
                if ( != null)
                {
                    builder.put(key.deserializeWithType(jpctxt));
                }
                else
                {
                    builder.put(key.deserialize(jpctxt));
                }
            }
        }
        if ( == null) {
            return builder;
        }
        try {
            return (Multimap<?, ?>) .invoke(nullbuilder);
        } catch (InvocationTargetException e) {
            throw new JsonMappingException("Could not map to " + _peel(e));
        } catch (IllegalArgumentException e) {
            throw new JsonMappingException("Could not map to " + _peel(e));
        } catch (IllegalAccessException e) {
            throw new JsonMappingException("Could not map to " + _peel(e));
        }
    }
    private static Method findTransformer(Class<?> rawType)
    {
        // Very first thing: if it's a "standard multi-map type", can avoid copying
        if (rawType == LinkedListMultimap.class
                || rawType == ListMultimap.class
                || rawType == Multimap.class) {
            return null;
        }
        
        // First, check type itself for matching methods
        for (String methodName : ) {
            try {
                Method m = rawType.getMethod(methodNameMultimap.class);
                if (m != null) {
                    return m;
                }
            } catch (NoSuchMethodException e) { }
            // pass SecurityExceptions as-is:
            // } catch (SecurityException e) { }
        }
        // If not working, possibly super types too (should we?)
        for (String methodName : ) {
            try {
                Method m = rawType.getMethod(methodNameMultimap.class);
                if (m != null) {
                    return m;
                }
            } catch (NoSuchMethodException e) { }
            // pass SecurityExceptions as-is:
            // } catch (SecurityException e) { }
        }
        return null;
    }
    
    private void expect(JsonParser jpJsonToken tokenthrows IOException
    {
        if (jp.getCurrentToken() != token) {
            throw new JsonMappingException("Expecting " + token + ", found " + jp.getCurrentToken(), jp.getCurrentLocation());
        }
    }
    private Throwable _peel(Throwable t)
    {
        while (t.getCause() != null) {
            t = t.getCause();
        }
        return t;
    }
New to GrepCode? Check out our FAQ X