Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
 
Serializer implementation where given Java type is first converted to an intermediate "delegate type" (using a configured com.fasterxml.jackson.databind.util.Converter, and then this delegate value is serialized by Jackson.

Note that although types may be related, they must not be same; trying to do this will result in an exception.

Since:
2.1
 
 public class StdDelegatingSerializer
     extends StdSerializer<Object>
     implements ContextualSerializer,
 {
     protected final Converter<Object,?> _converter;
    
    
Fully resolved delegate type, with generic information if any available.
 
     protected final JavaType _delegateType;
    
    
Underlying serializer for type T<.code>.
 
     protected final JsonSerializer<Object_delegateSerializer;
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
 
     @SuppressWarnings("unchecked")
     public StdDelegatingSerializer(Converter<?,?> converter)
     {
         super(Object.class);
          = (Converter<Object,?>)converter;
          = null;
          = null;
     }
 
     @SuppressWarnings("unchecked")
     public <T> StdDelegatingSerializer(Class<T> clsConverter<T,?> converter)
     {
         super(clsfalse);
          = (Converter<Object,?>)converter;
          = null;
          = null;
     }
     
     @SuppressWarnings("unchecked")
     protected StdDelegatingSerializer(Converter<Object,?> converter,
             JavaType delegateTypeJsonSerializer<?> delegateSerializer)
     {
         super(delegateType);
          = converter;
          = delegateType;
          = (JsonSerializer<Object>) delegateSerializer;
     }

    
Method used for creating resolved contextual instances. Must be overridden when sub-classing.
 
     protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
             JavaType delegateTypeJsonSerializer<?> delegateSerializer)
     {
         if (getClass() != StdDelegatingSerializer.class) {
             throw new IllegalStateException("Sub-class "+getClass().getName()+" must override 'withDelegate'");
         }
         return new StdDelegatingSerializer(converterdelegateTypedelegateSerializer);
     }
     
     /*
     /**********************************************************
     /* Contextualization
     /**********************************************************
     */
    
    // @Override
    public JsonSerializer<?> createContextual(SerializerProvider providerBeanProperty property)
        throws JsonMappingException
    {
        // First: figure out what is the fully generic delegate type:
        TypeFactory tf = provider.getTypeFactory();
        JavaType implType = tf.constructType(.getClass());
        JavaType[] params = tf.findTypeParameters(implTypeConverter.class);
        if (params == null || params.length != 2) {
            throw new JsonMappingException("Could not determine Converter parameterization for "
                    +implType);
        }
        // and then we can find serializer to delegate to, construct a new instance:
        JavaType delegateType = params[1];
        return withDelegate(delegateType,
                provider.findValueSerializer(delegateTypeproperty));
    }
    /*
    /**********************************************************
    /* Accessors
    /**********************************************************
     */
    protected Converter<Object, ?> getConverter() {
        return ;
    }
    @Override
    public JsonSerializer<?> getDelegatee() {
        return ;
    }
    
    /*
    /**********************************************************
    /* Serialization
    /**********************************************************
     */
    @Override
    public void serialize(Object valueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonProcessingException
    {
        Object delegateValue = convertValue(value);
        // should we accept nulls?
        if (delegateValue == null) {
            provider.defaultSerializeNull(jgen);
            return;
        }
        .serialize(delegateValuejgenprovider);
    }
    @Override
    public void serializeWithType(Object valueJsonGenerator jgenSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonProcessingException
    {
        /* 03-Oct-2012, tatu: This is actually unlikely to work ok... but for now,
         *    let's give it a chance?
         */
        Object delegateValue = convertValue(value);
        .serializeWithType(delegateValuejgenprovidertypeSer);
    }
    @Override
    public boolean isEmpty(Object value)
    {
        Object delegateValue = convertValue(value);
        return .isEmpty(delegateValue);
    }
    
    /*
    /**********************************************************
    /* Schema functionality
    /**********************************************************
     */
    @Override
    public JsonNode getSchema(SerializerProvider providerType typeHint)
        throws JsonMappingException
    {
        if ( instanceof SchemaAware) {
            return ((SchemaAware).getSchema(providertypeHint);
        }
        return super.getSchema(providertypeHint);
    }
    @Override
    public JsonNode getSchema(SerializerProvider providerType typeHint,
        boolean isOptionalthrows JsonMappingException
    {
        if ( instanceof SchemaAware) {
            return ((SchemaAware).getSchema(providertypeHintisOptional);
        }
        return super.getSchema(providertypeHint);
    }
    @Override
    public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        throws JsonMappingException
    {
        /* 03-Sep-2012, tatu: Not sure if this can be made to really work
         *    properly... but for now, try this:
         */
        .acceptJsonFormatVisitor(visitortypeHint);
    }
    /*
    /**********************************************************
    /* Overridable methods
    /**********************************************************
     */

    
Method called to convert from source Java value into delegate value (which will be serialized using standard Jackson serializer for delegate type)

The default implementation uses configured com.fasterxml.jackson.databind.util.Converter to do conversion.

Parameters:
delegateValue
Returns:
    protected Object convertValue(Object value) {
        return .convert(value);
    }
New to GrepCode? Check out our FAQ X