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>
 {
     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")
     public 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 void resolve(SerializerProvider providerthrows JsonMappingException
     {
        if (( != null)
                && ( instanceof ResolvableSerializer)) {
            ((ResolvableSerializer).resolve(provider);
        }
    }
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider providerBeanProperty property)
        throws JsonMappingException
    {
        // First: if already got serializer to delegate to, contextualize it:
        if ( != null) {
            if ( instanceof ContextualSerializer) {
                JsonSerializer<?> ser = provider.handleSecondaryContextualization(property);
                if (ser == ) {
                    return this;
                }
                return withDelegate(ser);
            }
            return this;
        }
        // Otherwise, need to locate serializer to delegate to. For that we need type information...
        JavaType delegateType = ;
        if (delegateType == null) {
            delegateType = .getOutputType(provider.getTypeFactory());
        }
        // and then find the thing...
        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:
value Value to convert
Returns:
Result of conversion
    protected Object convertValue(Object value) {
        return .convert(value);
    }
New to GrepCode? Check out our FAQ X