Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.datatype.guava.ser;
  
  
  
 
 
 public class MultimapSerializer
     extends JsonSerializer<Multimap<?, ?>>
     implements ContextualSerializer
 {
     private final MapLikeType type;
     private final BeanProperty property;
     private final JsonSerializer<ObjectkeySerializer;
     private final TypeSerializer valueTypeSerializer;
     private final JsonSerializer<ObjectvalueSerializer;
 
     public MultimapSerializer(SerializationConfig config,
             MapLikeType type,
             BeanDescription beanDesc,
             JsonSerializer<ObjectkeySerializer,
             TypeSerializer valueTypeSerializer,
             JsonSerializer<ObjectvalueSerializer)
     {
         this. = type;
         this. = null;
         this. = keySerializer;
         this. = valueTypeSerializer;
         this. = valueSerializer;
     }
 
     @SuppressWarnings("unchecked")
     protected MultimapSerializer(MultimapSerializer srcBeanProperty property,
                 JsonSerializer<?> keySerializer,
                 TypeSerializer valueTypeSerializerJsonSerializer<?> valueSerializer)
     {
         this. = src.type;
         this. = property;
         this. = (JsonSerializer<Object>) keySerializer;
         this. = valueTypeSerializer;
         this. = (JsonSerializer<Object>) valueSerializer;
     }
             
     
     protected MultimapSerializer withResolved(BeanProperty property,
             JsonSerializer<?> keySerializer,
             TypeSerializer valueTypeSerializerJsonSerializer<?> valueSerializer)
     {
         return new MultimapSerializer(thispropertykeySerializer,
                 valueTypeSerializervalueSerializer);
     }
     
     /*
     /**********************************************************
     /* Post-processing (contextualization)
     /**********************************************************
      */
 
     public JsonSerializer<?> createContextual(SerializerProvider provider,
             BeanProperty propertythrows JsonMappingException
     {
         JsonSerializer<?> valueSer = ;
         if (valueSer == null) { // if type is final, can actually resolve:
             JavaType valueType = .getContentType();
             if (valueType.isFinal()) {
                 valueSer = provider.findValueSerializer(valueTypeproperty);
             }
         } else if (valueSer instanceof ContextualSerializer) {
             valueSer = ((ContextualSerializervalueSer).createContextual(providerproperty);
         }
         JsonSerializer<?> keySer = ;
         if (keySer == null) {
             keySer = provider.findKeySerializer(.getKeyType(), property);
         } else if (keySer instanceof ContextualSerializer) {
             keySer = ((ContextualSerializerkeySer).createContextual(providerproperty);
         }
         // finally, TypeSerializers may need contextualization as well
         TypeSerializer typeSer = ;
         if (typeSer != null) {
             typeSer = typeSer.forProperty(property);
         }
         return withResolved(propertykeySertypeServalueSer);
     }
 
     /*
     /**********************************************************
     /* JsonSerializer implementation
     /**********************************************************
      */
     
    @Override
    public void serialize(Multimap<?, ?> valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonProcessingException
    {
        jgen.writeStartObject();
        if (!value.isEmpty()) {
            serializeFields(valuejgenprovider);
        }        
        jgen.writeEndObject();
    }
    @Override
    public void serializeWithType(Multimap<?,?> valueJsonGenerator jgenSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonGenerationException
    {
        typeSer.writeTypePrefixForObject(valuejgen);
        serializeFields(valuejgenprovider);
        typeSer.writeTypeSuffixForObject(valuejgen);
    }
    private final void serializeFields(Multimap<?, ?> valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonProcessingException
    {
        for (Entry<?, ? extends Collection<?>> e : value.asMap().entrySet()) {
            if ( != null) {
                .serialize(e.getKey(), jgenprovider);
            } else {
                provider.findKeySerializer(provider.constructType(String.class), )
                    .serialize(e.getKey(), jgenprovider);
            }
            if ( != null) {
                // note: value is a List, but generic type is for contents... so:
                jgen.writeStartArray();
                for (Object vv : e.getValue()) {
                    .serialize(vvjgenprovider);
                }
                jgen.writeEndArray();
            } else {
                provider.defaultSerializeValue(Lists.newArrayList(e.getValue()), jgen);
            }
        }
    }
New to GrepCode? Check out our FAQ X