Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.jsontype.impl;
  
  
  
Type deserializer used with com.fasterxml.jackson.annotation.JsonTypeInfo.As.WRAPPER_ARRAY inclusion mechanism. Simple since JSON structure used is always the same, regardless of structure used for actual value: wrapping is done using a 2-element JSON Array where type id is the first element, and actual object data as second element.

Author(s):
tatu
 
 public class AsArrayTypeDeserializer
     extends TypeDeserializerBase
     implements java.io.Serializable
 {
     private static final long serialVersionUID = 5345570420394408290L;
 
     public AsArrayTypeDeserializer(JavaType btTypeIdResolver idRes,
             String typePropertyNameboolean typeIdVisibleClass<?> defaultImpl)
     {
         super(btidRestypePropertyNametypeIdVisibledefaultImpl);
     }
 
     public AsArrayTypeDeserializer(AsArrayTypeDeserializer srcBeanProperty property) {
         super(srcproperty);
     }
     
     @Override
     public TypeDeserializer forProperty(BeanProperty prop) {
         if (prop == ) { // usually if it's null
             return this;
         }
         return new AsArrayTypeDeserializer(thisprop);
     }
     
     @Override
     public As getTypeInclusion() {
         return .;
     }

    
Method called when actual object is serialized as JSON Array.
 
     @Override
         throws IOExceptionJsonProcessingException
     {
         return _deserialize(jpctxt);
     }

    
Method called when actual object is serialized as JSON Object
 
     @Override
         throws IOExceptionJsonProcessingException
     {
         return _deserialize(jpctxt);
     }
     
     @Override
         throws IOExceptionJsonProcessingException
     {
         return _deserialize(jpctxt);
     }    
 
     @Override
         throws IOExceptionJsonProcessingException
     {
         return _deserialize(jpctxt);
     }    
     
     /*
     /***************************************************************
     /* Internal methods
     /***************************************************************
      */

    
Method that handles type information wrapper, locates actual subtype deserializer to use, and calls it to do actual deserialization.
 
     private final Object _deserialize(JsonParser jpDeserializationContext ctxt)
         throws IOExceptionJsonProcessingException
     {
        boolean hadStartArray = jp.isExpectedStartArrayToken();
        String typeId = _locateTypeId(jpctxt);
        JsonDeserializer<Objectdeser = _findDeserializer(ctxttypeId);
        // Minor complication: we may need to merge type id in?
        if ( && jp.getCurrentToken() == .) {
            // but what if there's nowhere to add it in? Error? Or skip? For now, skip.
            TokenBuffer tb = new TokenBuffer(null);
            tb.writeStartObject(); // recreate START_OBJECT
            tb.writeFieldName();
            tb.writeString(typeId);
            jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
            jp.nextToken();
        }
        Object value = deser.deserialize(jpctxt);
        // And then need the closing END_ARRAY
        if (hadStartArray && jp.nextToken() != .) {
            throw ctxt.wrongTokenException(jp.,
                    "expected closing END_ARRAY after type information and deserialized value");
        }
        return value;
    }    
    
    protected final String _locateTypeId(JsonParser jpDeserializationContext ctxt)
        throws IOExceptionJsonProcessingException
    {
        if (!jp.isExpectedStartArrayToken()) {
            // [JACKSON-712] Need to allow even more customized handling, if something unexpected seen...
            // but should there be a way to limit this to likely success cases?
            if ( != null) {
                return .idFromBaseType();
            }
            throw ctxt.wrongTokenException(jp."need JSON Array to contain As.WRAPPER_ARRAY type information for class "+baseTypeName());
        }
        // And then type id as a String
        if (jp.nextToken() != .) {
            if ( != null) {
                return .idFromBaseType();
            }
            throw ctxt.wrongTokenException(jp."need JSON String that contains type id (for subtype of "+baseTypeName()+")");
        }
        String result = jp.getText();
        jp.nextToken();
        return result;
    }
New to GrepCode? Check out our FAQ X