Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.impl;
  
Helper container used for resolving serializers for dynamic (possibly but not necessarily polymorphic) properties: properties whose type is not forced to use dynamic (declared) type and that are not final. If so, serializer to use can only be established once actual value type is known. Since this happens a lot unless static typing is forced (or types are final) this implementation is optimized for efficiency. Instances are immutable; new instances are created with factory methods: this is important to ensure correct multi-threaded access.
 
 public abstract class PropertySerializerMap
 {
    
Main lookup method. Takes a "raw" type since usage is always from place where parameterization is fixed such that there can not be type-parametric variations.
 
     public abstract JsonSerializer<ObjectserializerFor(Class<?> type);

    

Deprecated:
Since 2.3; use either findAndAddPrimarySerializer or findAndAddSecondarySerializer. To be removed from 2.4
 
     @Deprecated
     public final SerializerAndMapResult findAndAddSerializer(Class<?> type,
             SerializerProvider providerBeanProperty property)
         throws JsonMappingException
     {
         return findAndAddSecondarySerializer(typeproviderproperty);
     }

    

Deprecated:
Since 2.3; use either findAndAddPrimarySerializer or findAndAddSecondarySerializer. To be removed from 2.4
 
     @Deprecated
     public final SerializerAndMapResult findAndAddSerializer(JavaType type,
             SerializerProvider providerBeanProperty property)
         throws JsonMappingException
     {
         return findAndAddSecondarySerializer(typeproviderproperty);
     }

    
Method called if initial lookup fails, when looking for a primary serializer (one that is directly attached to a property). Will both find serializer and construct new map instance if warranted, and return both.

 
     public final SerializerAndMapResult findAndAddPrimarySerializer(Class<?> type,
             SerializerProvider providerBeanProperty property)
         throws JsonMappingException
     {
         JsonSerializer<Objectserializer = provider.findPrimaryPropertySerializer(typeproperty);
         return new SerializerAndMapResult(serializernewWith(typeserializer));
     }
 
             SerializerProvider providerBeanProperty property)
         throws JsonMappingException
     {
         JsonSerializer<Objectserializer = provider.findPrimaryPropertySerializer(typeproperty);
         return new SerializerAndMapResult(serializernewWith(type.getRawClass(), serializer));
     }
    
    
Method called if initial lookup fails, when looking for a non-primary serializer (one that is not directly attached to a property). Will both find serializer and construct new map instance if warranted, and return both.

 
             SerializerProvider providerBeanProperty property)
         throws JsonMappingException
     {
         JsonSerializer<Objectserializer = provider.findValueSerializer(typeproperty);
         return new SerializerAndMapResult(serializernewWith(typeserializer));
     }
 
            SerializerProvider providerBeanProperty property)
        throws JsonMappingException
    {
        JsonSerializer<Objectserializer = provider.findValueSerializer(typeproperty);
        return new SerializerAndMapResult(serializernewWith(type.getRawClass(), serializer));
    }
    
    public abstract PropertySerializerMap newWith(Class<?> typeJsonSerializer<Objectserializer);
    
    public static PropertySerializerMap emptyMap() {
        return .;
    }
    
    /*
    /**********************************************************
    /* Helper classes
    /**********************************************************
     */

    
Value class used for returning tuple that has both serializer that was retrieved and new map instance
    public final static class SerializerAndMapResult
    {
        public final JsonSerializer<Objectserializer;
        public final PropertySerializerMap map;
        
        public SerializerAndMapResult(JsonSerializer<Objectserializer,
                PropertySerializerMap map)
        {
            this. = serializer;
            this. = map;
        }
    }

    
Trivial container for bundling type + serializer entries.
    private final static class TypeAndSerializer
    {
        public final Class<?> type;
        public final JsonSerializer<Objectserializer;
        public TypeAndSerializer(Class<?> typeJsonSerializer<Objectserializer) {
            this. = type;
            this. = serializer;
        }
    }
    /*
    /**********************************************************
    /* Implementations
    /**********************************************************
     */

    
Bogus instance that contains no serializers; used as the default map with new serializers.
    private final static class Empty extends PropertySerializerMap
    {
        protected final static Empty instance = new Empty();
        @Override
        public JsonSerializer<ObjectserializerFor(Class<?> type) {
            return null// empty, nothing to find
        }        
        @Override
        public PropertySerializerMap newWith(Class<?> typeJsonSerializer<Objectserializer) {
            return new Single(typeserializer);
        }
    }

    
Map that contains a single serializer; although seemingly silly this is probably the most commonly used variant because many theoretically dynamic or polymorphic types just have single actual type.
    private final static class Single extends PropertySerializerMap
    {
        private final Class<?> _type;
        private final JsonSerializer<Object_serializer;
        public Single(Class<?> typeJsonSerializer<Objectserializer) {
             = type;
             = serializer;
        }
        @Override
        public JsonSerializer<ObjectserializerFor(Class<?> type)
        {
            if (type == ) {
                return ;
            }
            return null;
        }
        @Override
        public PropertySerializerMap newWith(Class<?> typeJsonSerializer<Objectserializer) {
            return new Double(typeserializer);
        }
    }
    private final static class Double extends PropertySerializerMap
    {
        private final Class<?> _type1_type2;
        private final JsonSerializer<Object_serializer1_serializer2;
        public Double(Class<?> type1JsonSerializer<Objectserializer1,
                Class<?> type2JsonSerializer<Objectserializer2)
        {
             = type1;
             = serializer1;
             = type2;
             = serializer2;
        }
        @Override
        public JsonSerializer<ObjectserializerFor(Class<?> type)
        {
            if (type == ) {
                return ;
            }
            if (type == ) {
                return ;
            }
            return null;
        }        
        @Override
        public PropertySerializerMap newWith(Class<?> typeJsonSerializer<Objectserializer) {
            // Ok: let's just create generic one
            TypeAndSerializer[] ts = new TypeAndSerializer[2];
            ts[0] = new TypeAndSerializer();
            ts[1] = new TypeAndSerializer();
            return new Multi(ts);
        }
    }
    
    private final static class Multi extends PropertySerializerMap
    {
        
Let's limit number of serializers we actually cache; linear lookup won't scale too well beyond smallish number, and if we really want to support larger collections should use a hash map. But it seems unlikely this is a common use case so for now let's just stop building after hard-coded limit. 8 sounds like a reasonable stab for now.
        private final static int MAX_ENTRIES = 8;
        
        private final TypeAndSerializer[] _entries;
        public Multi(TypeAndSerializer[] entries) {
             = entries;
        }
        @Override
        public JsonSerializer<ObjectserializerFor(Class<?> type)
        {
            for (int i = 0, len = .i < len; ++i) {
                TypeAndSerializer entry = [i];
                if (entry.type == type) {
                    return entry.serializer;
                }
            }
            return null;
        }
        @Override
        public PropertySerializerMap newWith(Class<?> typeJsonSerializer<Objectserializer)
        {
            int len = .;
            // Will only grow up to N entries
            if (len == ) {
                return this;
            }
            // 1.6 has nice resize methods but we are still 1.5
            TypeAndSerializer[] entries = new TypeAndSerializer[len+1];
            System.arraycopy(, 0, entries, 0, len);
            entries[len] = new TypeAndSerializer(typeserializer);
            return new Multi(entries);
        }
    }
New to GrepCode? Check out our FAQ X