Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.module;
  
  import java.util.*;
  
  
Simple implementation com.fasterxml.jackson.databind.ser.Serializers which allows registration of serializers based on raw (type erased class). It can work well for basic bean and scalar type serializers, but is not a good fit for handling generic types (like java.util.Maps and java.util.Collections).

Type registrations are assumed to be general; meaning that registration of serializer for a super type will also be used for handling subtypes, unless an exact match is found first. As an example, handler for java.lang.CharSequence would also be used serializing java.lang.StringBuilder instances, unless a direct mapping was found.

 
 public class SimpleSerializers
     extends Serializers.Base
     implements java.io.Serializable
 {
     private static final long serialVersionUID = 8531646511998456779L;

    
Class-based mappings that are used both for exact and sub-class matches.
 
     protected HashMap<ClassKey,JsonSerializer<?>> _classMappings = null;

    
Interface-based matches.
 
     protected HashMap<ClassKey,JsonSerializer<?>> _interfaceMappings = null;
 
     /*
     /**********************************************************
     /* Life-cycle, construction and configuring
     /**********************************************************
      */
     
     public SimpleSerializers() { }

    

Since:
2.1
 
     public SimpleSerializers(List<JsonSerializer<?>> sers) {
         addSerializers(sers);
     }
    
    
Method for adding given serializer for type that com.fasterxml.jackson.databind.JsonSerializer.handledType() specifies (which MUST return a non-null class; and can NOT be java.lang.Object, as a sanity check). For serializers that do not declare handled type, use the variant that takes two arguments.

Parameters:
ser
 
     public void addSerializer(JsonSerializer<?> ser)
     {
         // Interface to match?
         Class<?> cls = ser.handledType();
         if (cls == null || cls == Object.class) {
             throw new IllegalArgumentException("JsonSerializer of type "+ser.getClass().getName()
                     +" does not define valid handledType() -- must either register with method that takes type argument "
                     +" or make serializer extend 'com.fasterxml.jackson.databind.ser.std.StdSerializer'"); 
         }
         _addSerializer(clsser);
     }
 
     public <T> void addSerializer(Class<? extends T> typeJsonSerializer<T> ser)
     {
         _addSerializer(typeser);
     }

    

Since:
2.1
 
     public void addSerializers(List<JsonSerializer<?>> sers) {
         for (JsonSerializer<?> ser : sers) {
             addSerializer(ser);
         }
     }
 
     private void _addSerializer(Class<?> clsJsonSerializer<?> ser)
     {
         ClassKey key = new ClassKey(cls);
         // Interface or class type?
         if (cls.isInterface()) {
             if ( == null) {
                 = new HashMap<ClassKey,JsonSerializer<?>>();
            }
            .put(keyser);
        } else { // nope, class:
            if ( == null) {
                 = new HashMap<ClassKey,JsonSerializer<?>>();
            }
            .put(keyser);
        }
    }
    
    /*
    /**********************************************************
    /* Serializers implementation
    /**********************************************************
     */
    
    @Override
            JavaType typeBeanDescription beanDesc)
    {
        Class<?> cls = type.getRawClass();
        ClassKey key = new ClassKey(cls);
        JsonSerializer<?> ser = null;
        // First: direct match?
        if (cls.isInterface()) {
            if ( != null) {
                ser = .get(key);
                if (ser != null) {
                    return ser;
                }
            }
        } else {
            if ( != null) {
                ser = .get(key);
                if (ser != null) {
                    return ser;
                }
                // If not direct match, maybe super-class match?
                for (Class<?> curr = cls; (curr != null); curr = curr.getSuperclass()) {
                    key.reset(curr);
                    ser = .get(key);
                    if (ser != null) {
                        return ser;
                    }
                }
            }
        }
        // No direct match? How about super-interfaces?
        if ( != null) {
            ser = _findInterfaceMapping(clskey);
            if (ser != null) {
                return ser;
            }
            // still no matches? Maybe interfaces of super classes
            if (!cls.isInterface()) {
                while ((cls = cls.getSuperclass()) != null) {
                    ser = _findInterfaceMapping(clskey);
                    if (ser != null) {
                        return ser;
                    }
                }
            }
        }
        return null;
    }
    @Override
            ArrayType typeBeanDescription beanDesc,
            TypeSerializer elementTypeSerializerJsonSerializer<ObjectelementValueSerializer) {
        return findSerializer(configtypebeanDesc);
    }
    @Override
            CollectionType typeBeanDescription beanDesc,
            TypeSerializer elementTypeSerializerJsonSerializer<ObjectelementValueSerializer) {
        return findSerializer(configtypebeanDesc);
    }
    @Override
            CollectionLikeType typeBeanDescription beanDesc,
            TypeSerializer elementTypeSerializerJsonSerializer<ObjectelementValueSerializer) {
        return findSerializer(configtypebeanDesc);
    }
        
    @Override
            MapType typeBeanDescription beanDesc,
            JsonSerializer<ObjectkeySerializer,
            TypeSerializer elementTypeSerializerJsonSerializer<ObjectelementValueSerializer) {
        return findSerializer(configtypebeanDesc);
    }
    @Override
            MapLikeType typeBeanDescription beanDesc,
            JsonSerializer<ObjectkeySerializer,
            TypeSerializer elementTypeSerializerJsonSerializer<ObjectelementValueSerializer) {
        return findSerializer(configtypebeanDesc);
    }
    
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    
    protected JsonSerializer<?> _findInterfaceMapping(Class<?> clsClassKey key)
    {
        for (Class<?> iface : cls.getInterfaces()) {
            key.reset(iface);
            JsonSerializer<?> ser = .get(key);
            if (ser != null) {
                return ser;
            }
            ser = _findInterfaceMapping(ifacekey);
            if (ser != null) {
                return ser;
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X