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.PROPERTY inclusion mechanism. Uses regular form (additional key/value entry before actual data) when typed object is expressed as JSON Object; otherwise behaves similar to how com.fasterxml.jackson.annotation.JsonTypeInfo.As.WRAPPER_ARRAY works. Latter is used if JSON representation is polymorphic

Author(s):
tatu
 
 {
     private static final long serialVersionUID = 1L;
 
     public AsPropertyTypeDeserializer(JavaType btTypeIdResolver idRes,
             String typePropertyNameboolean typeIdVisibleClass<?> defaultImpl)
     {
         super(btidRestypePropertyNametypeIdVisibledefaultImpl);
     }
 
         super(srcproperty);
     }
     
     @Override
     public TypeDeserializer forProperty(BeanProperty prop) {
         if (prop == ) { // usually if it's null
             return this;
         }
         return new AsPropertyTypeDeserializer(thisprop);
     }
     
     @Override
     public As getTypeInclusion() {
         return .;
     }

    
This is the trickiest thing to handle, since property we are looking for may be anywhere...
 
     @Override
         throws IOExceptionJsonProcessingException
     {
         // but first, sanity check to ensure we have START_OBJECT or FIELD_NAME
         JsonToken t = jp.getCurrentToken();
         if (t == .) {
             t = jp.nextToken();
         } else if (t == .) {
             /* This is most likely due to the fact that not all Java types are
              * serialized as JSON Objects; so if "as-property" inclusion is requested,
              * serialization of things like Lists must be instead handled as if
              * "as-wrapper-array" was requested.
              * But this can also be due to some custom handling: so, if "defaultImpl"
              * is defined, it will be asked to handle this case.
              */
             return _deserializeTypedUsingDefaultImpl(jpctxtnull);
         } else if (t != .) {
             return _deserializeTypedUsingDefaultImpl(jpctxtnull);
         }
         // Ok, let's try to find the property. But first, need token buffer...
         TokenBuffer tb = null;
 
         for (; t == .t = jp.nextToken()) {
             String name = jp.getCurrentName();
             jp.nextToken(); // to point to the value
             if (.equals(name)) { // gotcha!
                 return _deserializeTypedForId(jpctxttb);
             }
             if (tb == null) {
                 tb = new TokenBuffer(null);
             }
             tb.writeFieldName(name);
             tb.copyCurrentStructure(jp);
         }
         return _deserializeTypedUsingDefaultImpl(jpctxttb);
     }
 
     protected final Object _deserializeTypedForId(JsonParser jpDeserializationContext ctxt,
             TokenBuffer tb)
         throws IOExceptionJsonProcessingException
     {
         String typeId = jp.getText();
         JsonDeserializer<Objectdeser = _findDeserializer(ctxttypeId);
         if () { // need to merge id back in JSON input?
            if (tb == null) {
                tb = new TokenBuffer(null);
            }
            tb.writeFieldName(jp.getCurrentName());
            tb.writeString(typeId);
        }
        if (tb != null) { // need to put back skipped properties?
            jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
        }
        // Must point to the next value; tb had no current, jp pointed to VALUE_STRING:
        jp.nextToken(); // to skip past String value
        // deserializer should take care of closing END_OBJECT as well
        return deser.deserialize(jpctxt);
    }
    
    // off-lined to keep main method lean and mean...
            DeserializationContext ctxtTokenBuffer tb)
        throws IOExceptionJsonProcessingException
    {
        // As per [JACKSON-614], may have default implementation to use
        if ( != null) { 
            JsonDeserializer<Objectdeser = _findDefaultImplDeserializer(ctxt);
            if (tb != null) {
                tb.writeEndObject();
                jp = tb.asParser(jp);
                // must move to point to the first token:
                jp.nextToken();
            }
            return deser.deserialize(jpctxt);
        }
        // or, perhaps we just bumped into a "natural" value (boolean/int/double/String)?
        Object result = _deserializeIfNatural(jpctxt);
        if (result != null) {
            return result;
        }
        // or, something for which "as-property" won't work, changed into "wrapper-array" type:
        if (jp.getCurrentToken() == .) {
            return super.deserializeTypedFromAny(jpctxt);
        }
        throw ctxt.wrongTokenException(jp.,
                "missing property '"++"' that is to contain type id  (for class "+baseTypeName()+")");
    }
    /* As per [JACKSON-352], also need to re-route "unknown" version. Need to think
     * this through bit more in future, but for now this does address issue and has
     * no negative side effects (at least within existing unit test suite).
     */
    @Override
        throws IOExceptionJsonProcessingException
    {
        /* [JACKSON-387]: Sometimes, however, we get an array wrapper; specifically
         *   when an array or list has been serialized with type information.
         */
        if (jp.getCurrentToken() == .) {
            return super.deserializeTypedFromArray(jpctxt);
        }
        return deserializeTypedFromObject(jpctxt);
    }    
    
    // These are fine from base class:
    //public Object deserializeTypedFromArray(JsonParser jp, DeserializationContext ctxt)
    //public Object deserializeTypedFromScalar(JsonParser jp, DeserializationContext ctxt)    

    
Helper method used to check if given parser might be pointing to a "natural" value, and one that would be acceptable as the result value (compatible with declared base type)
        throws IOExceptionJsonProcessingException
    {
        switch (jp.getCurrentToken()) {
        case :
            if (.getRawClass().isAssignableFrom(String.class)) {
                return jp.getText();
            }
            break;
        case :
            if (.getRawClass().isAssignableFrom(Integer.class)) {
                return jp.getIntValue();
            }
            break;
        case :
            if (.getRawClass().isAssignableFrom(Double.class)) {
                return Double.valueOf(jp.getDoubleValue());
            }
            break;
        case :
            if (.getRawClass().isAssignableFrom(Boolean.class)) {
                return .;
            }
            break;
        case :
            if (.getRawClass().isAssignableFrom(Boolean.class)) {
                return .;
            }
            break;
        }
        return null;
    }
New to GrepCode? Check out our FAQ X