Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.jsontype.impl;
  
  
  
Type wrapper that tries to use an extra JSON Object, with a single entry that has type name as key, to serialize type information. If this is not possible (value is serialize as array or primitive), will use com.fasterxml.jackson.annotation.JsonTypeInfo.As.WRAPPER_ARRAY mechanism as fallback: that is, just use a wrapping array with type information as the first element and value as second.

Author(s):
tatu
 
 public class AsWrapperTypeSerializer
     extends TypeSerializerBase
 {
     public AsWrapperTypeSerializer(TypeIdResolver idResBeanProperty property)
     {
         super(idResproperty);
     }
 
     @Override
         if ( == propreturn this;
         return new AsWrapperTypeSerializer(this.prop);
     }
     
     @Override
     public As getTypeInclusion() { return .; }
     
     @Override
     public void writeTypePrefixForObject(Object valueJsonGenerator jgen)
         throws IOExceptionJsonProcessingException
     {
         // wrapper
         jgen.writeStartObject();
         // and then JSON Object start caller wants
         jgen.writeObjectFieldStart(idFromValue(value));
     }
 
     @Override
     public void writeTypePrefixForObject(Object valueJsonGenerator jgen,
             Class<?> type)
         throws IOExceptionJsonProcessingException
     {
         // wrapper
         jgen.writeStartObject();
         // and then JSON Object start caller wants
         jgen.writeObjectFieldStart(idFromValueAndType(valuetype));
     }
     
     @Override
     public void writeTypePrefixForArray(Object valueJsonGenerator jgen)
         throws IOExceptionJsonProcessingException
     {
         // can still wrap ok
         jgen.writeStartObject();
         // and then JSON Array start caller wants
         jgen.writeArrayFieldStart(idFromValue(value));
     }
 
     @Override
     public void writeTypePrefixForArray(Object valueJsonGenerator jgen,
             Class<?> type)
         throws IOExceptionJsonProcessingException
     {
         // can still wrap ok
         jgen.writeStartObject();
         // and then JSON Array start caller wants
         jgen.writeArrayFieldStart(idFromValueAndType(valuetype));
     }
 
     @Override
     public void writeTypePrefixForScalar(Object valueJsonGenerator jgen)
         throws IOExceptionJsonProcessingException
     {
         // can still wrap ok
         jgen.writeStartObject();
         jgen.writeFieldName(idFromValue(value));
     }
 
     @Override
     public void writeTypePrefixForScalar(Object valueJsonGenerator jgen,
             Class<?> type)
         throws IOExceptionJsonProcessingException
     {
         // can still wrap ok
         jgen.writeStartObject();
         jgen.writeFieldName(idFromValueAndType(valuetype));
     }
     
     @Override
    public void writeTypeSuffixForObject(Object valueJsonGenerator jgen)
        throws IOExceptionJsonProcessingException
    {
        // first close JSON Object caller used
        jgen.writeEndObject();
        // and then wrapper
        jgen.writeEndObject();
    }
    @Override
    public void writeTypeSuffixForArray(Object valueJsonGenerator jgen)
        throws IOExceptionJsonProcessingException
    {
        // first close array caller needed
        jgen.writeEndArray();
        // then wrapper object
        jgen.writeEndObject();
    }
    
    @Override
    public void writeTypeSuffixForScalar(Object valueJsonGenerator jgen)
        throws IOExceptionJsonProcessingException
    {
        // just need to close the wrapper object
        jgen.writeEndObject();
    }
    /*
    /**********************************************************
    /* Writing with custom type id
    /**********************************************************
     */
    
    @Override
    public void writeCustomTypePrefixForObject(Object valueJsonGenerator jgenString typeId)
        throws IOExceptionJsonProcessingException
    {
        jgen.writeStartObject();
        jgen.writeObjectFieldStart(typeId);
    }
    
    @Override
    public void writeCustomTypePrefixForArray(Object valueJsonGenerator jgenString typeId)
        throws IOExceptionJsonProcessingException
    {
        jgen.writeStartObject();
        jgen.writeArrayFieldStart(typeId);
    }
    @Override
    public void writeCustomTypePrefixForScalar(Object valueJsonGenerator jgenString typeId)
        throws IOExceptionJsonProcessingException
    {
        jgen.writeStartObject();
        jgen.writeFieldName(typeId);
    }
    @Override
    public void writeCustomTypeSuffixForObject(Object valueJsonGenerator jgenString typeId)
            throws IOExceptionJsonProcessingException {
        writeTypeSuffixForObject(valuejgen); // standard impl works fine
    }
    @Override
    public void writeCustomTypeSuffixForArray(Object valueJsonGenerator jgenString typeId)
            throws IOExceptionJsonProcessingException {
        writeTypeSuffixForArray(valuejgen); // standard impl works fine
    }
    @Override
    public void writeCustomTypeSuffixForScalar(Object valueJsonGenerator jgenString typeId)
            throws IOExceptionJsonProcessingException {
        writeTypeSuffixForScalar(valuejgen); // standard impl works fine
    }
New to GrepCode? Check out our FAQ X