Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.module;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  
Simple com.fasterxml.jackson.databind.Module implementation that allows registration of serializers and deserializers, and bean serializer and deserializer modifiers.
 
 public class SimpleModule
     extends Module
     implements java.io.Serializable
 {
     private static final long serialVersionUID = 3132264350026957446L;
 
     protected final String _name;
     protected final Version _version;
     
     protected SimpleSerializers _serializers = null;
     protected SimpleDeserializers _deserializers = null;
 
     protected SimpleSerializers _keySerializers = null;
     protected SimpleKeyDeserializers _keyDeserializers = null;

    
Lazily-constructed resolver used for storing mappings from abstract classes to more specific implementing classes (which may be abstract or concrete)
 
     protected SimpleAbstractTypeResolver _abstractTypes = null;

    
Lazily-constructed resolver used for storing mappings from abstract classes to more specific implementing classes (which may be abstract or concrete)
 
     protected SimpleValueInstantiators _valueInstantiators = null;

    

Since:
2.2
 
     protected BeanDeserializerModifier _deserializerModifier = null;

    

Since:
2.2
 
     protected BeanSerializerModifier _serializerModifier = null;

    
Lazily-constructed map that contains mix-in definitions, indexed by target class, value being mix-in to apply.
 
     protected HashMap<Class<?>, Class<?>> _mixins = null;
    
    
Set of subtypes to register, if any.
 
     protected LinkedHashSet<NamedType_subtypes = null;
     
     /*
     /**********************************************************
     /* Life-cycle: creation
     /**********************************************************
      */

    
Constructors that should only be used for non-reusable convenience modules used by app code: "real" modules should use actual name and version number information.
 
     public SimpleModule() {
         // when passing 'this', can not chain constructors...
          = "SimpleModule-"+System.identityHashCode(this);
          = Version.unknownVersion();
     }
    
    
Convenience constructor that will default version to com.fasterxml.jackson.core.Version.unknownVersion().
 
     public SimpleModule(String name) {
         this(name, Version.unknownVersion());
     }

    
Convenience constructor that will use specified Version, including name from com.fasterxml.jackson.core.Version.getArtifactId()
 
    public SimpleModule(Version version) {
         = version.getArtifactId();
         = version;
    }
    
    
Constructor to use for actual reusable modules. ObjectMapper may use name as identifier to notice attempts for multiple registrations of the same module (although it does not have to).

Parameters:
name Unique name of the module
version Version of the module
    public SimpleModule(String nameVersion version) {
         = name;
         = version;
    }

    

Since:
2.1
    public SimpleModule(String nameVersion version,
            Map<Class<?>,JsonDeserializer<?>> deserializers) {
        this(nameversiondeserializersnull);
    }

    

Since:
2.1
    public SimpleModule(String nameVersion version,
            List<JsonSerializer<?>> serializers) {
        this(nameversionnullserializers);
    }
    
    

Since:
2.1
    public SimpleModule(String nameVersion version,
            Map<Class<?>,JsonDeserializer<?>> deserializers,
            List<JsonSerializer<?>> serializers)
    {
         = name;
         = version;
        if (deserializers != null) {
             = new SimpleDeserializers(deserializers);
        }
        if (serializers != null) {
             = new SimpleSerializers(serializers);
        }
    }
    
    /*
    /**********************************************************
    /* Simple setters to allow overriding
    /**********************************************************
     */

    
Resets all currently configured serializers.
    public void setSerializers(SimpleSerializers s) {
         = s;
    }

    
Resets all currently configured deserializers.
    public void setDeserializers(SimpleDeserializers d) {
         = d;
    }

    
Resets all currently configured key serializers.
    public void setKeySerializers(SimpleSerializers ks) {
         = ks;
    }

    
Resets all currently configured key deserializers.
    public void setKeyDeserializers(SimpleKeyDeserializers kd) {
         = kd;
    }

    
Resets currently configured abstract type mappings
    public void setAbstractTypes(SimpleAbstractTypeResolver atr) {
         = atr;        
    }

    
Resets all currently configured value instantiators
    public void setValueInstantiators(SimpleValueInstantiators svi) {
         = svi;
    }

    

Since:
2.2
         = mod;
        return this;
    }

    

Since:
2.2
         = mod;
        return this;
    }
    
    /*
    /**********************************************************
    /* Configuration methods
    /**********************************************************
     */
    
    public SimpleModule addSerializer(JsonSerializer<?> ser)
    {
        if ( == null) {
             = new SimpleSerializers();
        }
        .addSerializer(ser);
        return this;
    }
    
    public <T> SimpleModule addSerializer(Class<? extends T> typeJsonSerializer<T> ser)
    {
        if ( == null) {
             = new SimpleSerializers();
        }
        .addSerializer(typeser);
        return this;
    }
    public <T> SimpleModule addKeySerializer(Class<? extends T> typeJsonSerializer<T> ser)
    {
        if ( == null) {
             = new SimpleSerializers();
        }
        .addSerializer(typeser);
        return this;
    }
    
    public <T> SimpleModule addDeserializer(Class<T> typeJsonDeserializer<? extends T> deser)
    {
        if ( == null) {
             = new SimpleDeserializers();
        }
        .addDeserializer(typedeser);
        return this;
    }
    public SimpleModule addKeyDeserializer(Class<?> typeKeyDeserializer deser)
    {
        if ( == null) {
             = new SimpleKeyDeserializers();
        }
        .addDeserializer(typedeser);
        return this;
    }

    
Lazily-constructed resolver used for storing mappings from abstract classes to more specific implementing classes (which may be abstract or concrete)
    public <T> SimpleModule addAbstractTypeMapping(Class<T> superType,
            Class<? extends T> subType)
    {
        if ( == null) {
             = new SimpleAbstractTypeResolver();
        }
        // note: addMapping() will verify arguments
         = .addMapping(superTypesubType);
        return this;
    }

    
Method for registering com.fasterxml.jackson.databind.deser.ValueInstantiator to use when deserializing instances of type beanType.

Instantiator is registered when module is registered for ObjectMapper.

    public SimpleModule addValueInstantiator(Class<?> beanTypeValueInstantiator inst)
    {
        if ( == null) {
             = new SimpleValueInstantiators();
        }
        return this;
    }

    
Method for adding set of subtypes to be registered with com.fasterxml.jackson.databind.ObjectMapper this is an alternative to using annotations in super type to indicate subtypes.
    public SimpleModule registerSubtypes(Class<?> ... subtypes)
    {
        if ( == null) {
             = new LinkedHashSet<NamedType>(Math.max(16, subtypes.length));
        }
        for (Class<?> subtype : subtypes) {
            .add(new NamedType(subtype));
        }
        return this;
    }

    
Method for adding set of subtypes (along with type name to use) to be registered with com.fasterxml.jackson.databind.ObjectMapper this is an alternative to using annotations in super type to indicate subtypes.
    public SimpleModule registerSubtypes(NamedType ... subtypes)
    {
        if ( == null) {
             = new LinkedHashSet<NamedType>(Math.max(16, subtypes.length));
        }
        for (NamedType subtype : subtypes) {
            .add(subtype);
        }
        return this;
    }
    
    
Method for specifying that annotations define by mixinClass should be "mixed in" with annotations that targetType has (as if they were directly included on it!).

Mix-in annotations are registered when module is registered for ObjectMapper.

    public SimpleModule setMixInAnnotation(Class<?> targetTypeClass<?> mixinClass)
    {
        if ( == null) {
             = new HashMap<Class<?>, Class<?>>();
        }
        .put(targetTypemixinClass);
        return this;
    }
    
    /*
    /**********************************************************
    /* Module impl
    /**********************************************************
     */
    
    @Override
    public String getModuleName() {
        return ;
    }
    @Override
    public void setupModule(SetupContext context)
    {
        if ( != null) {
            context.addSerializers();
        }
        if ( != null) {
            context.addDeserializers();
        }
        if ( != null) {
            context.addKeySerializers();
        }
        if ( != null) {
            context.addKeyDeserializers();
        }
        if ( != null) {
            context.addAbstractTypeResolver();
        }
        if ( != null) {
            context.addValueInstantiators();
        }
        if ( != null) {
            context.addBeanDeserializerModifier();
        }
        if ( != null) {
            context.addBeanSerializerModifier();
        }
        if ( != null && .size() > 0) {
            context.registerSubtypes(.toArray(new NamedType[.size()]));
        }
        if ( != null) {
            for (Map.Entry<Class<?>,Class<?>> entry : .entrySet()) {
                context.setMixInAnnotations(entry.getKey(), entry.getValue());
            }
        }
    }
    @Override
    public Version version() {
        return ;
    }
New to GrepCode? Check out our FAQ X