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_OBJECT inclusion mechanism. Simple since JSON structure used is always the same, regardless of structure used for actual value: wrapping is done using a single-element JSON Object where type id is the key, and actual object data as the value.

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

    
Deserializing type id enclosed using WRAPPER_OBJECT style is straightforward
 
     @Override
         throws IOExceptionJsonProcessingException
     {
         return _deserialize(jpctxt);
     }    
 
     @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
     {
         // first, sanity checks
         if (jp.getCurrentToken() != .) {
            throw ctxt.wrongTokenException(jp.,
                    "need JSON Object to contain As.WRAPPER_OBJECT type information for class "+baseTypeName());
        }
        // should always get field name, but just in case...
        if (jp.nextToken() != .) {
            throw ctxt.wrongTokenException(jp.,
                    "need JSON String that contains type id (for subtype of "+baseTypeName()+")");
        }
        final String typeId = jp.getText();
        JsonDeserializer<Objectdeser = _findDeserializer(ctxttypeId);
        jp.nextToken();
        // 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_OBJECT
        if (jp.nextToken() != .) {
            throw ctxt.wrongTokenException(jp.,
                    "expected closing END_OBJECT after type information and deserialized value");
        }
        return value;
    }
New to GrepCode? Check out our FAQ X