Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.impl;
  
  import java.util.Iterator;
  
Variant of com.fasterxml.jackson.databind.ser.BeanPropertyWriter which will handle unwrapping of JSON Object (including of properties of Object within surrounding JSON object, and not as sub-object).
 
     extends BeanPropertyWriter
 {
    
Transformer used to add prefix and/or suffix for properties of unwrapped POJO.
 
     protected final NameTransformer _nameTransformer;
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     public UnwrappingBeanPropertyWriter(BeanPropertyWriter baseNameTransformer unwrapper) {
         super(base);
          = unwrapper;
     }
 
             SerializedString name) {
         super(basename);
          = transformer;
     }
 
     @Override
     public UnwrappingBeanPropertyWriter rename(NameTransformer transformer)
     {
         String oldName = .getValue();
         String newName = transformer.transform(oldName);
 
         // important: combine transformers:
         transformer = NameTransformer.chainedTransformer(transformer);
     
         return new UnwrappingBeanPropertyWriter(thistransformernew SerializedString(newName));
     }
 
     /*
     /**********************************************************
     /* Overrides, public methods
     /**********************************************************
      */
 
     @Override
     public boolean isUnwrapping() {
         return true;
     }
     
     @Override
     public void serializeAsField(Object beanJsonGenerator jgenSerializerProvider prov)
         throws Exception
     {
         Object value = get(bean);
         if (value == null) {
             // Hmmh. I assume we MUST pretty much suppress nulls, since we
             // can't really unwrap them...
             return;
         }
         JsonSerializer<Objectser = ;
         if (ser == null) {
             Class<?> cls = value.getClass();
             PropertySerializerMap map = ;
             ser = map.serializerFor(cls);
             if (ser == null) {
                 ser = _findAndAddDynamic(mapclsprov);
             }
         }
         if ( != null) {
             if ( == ) {
                 if (ser.isEmpty(value)) {
                     return;
                 }
             } else if (.equals(value)) {
                 return;
             }
         }
         // For non-nulls, first: simple check for direct cycles
        if (value == bean) {
            _handleSelfReference(beanser);
        }
        // note: must verify we are using unwrapping serializer; if not, will write field name
        if (!ser.isUnwrappingSerializer()) {
            jgen.writeFieldName();
        }
        if ( == null) {
            ser.serialize(valuejgenprov);
        } else {
            ser.serializeWithType(valuejgenprov);
        }
    }
    // need to override as we must get unwrapping instance...
    @Override
    public void assignSerializer(JsonSerializer<Objectser)
    {
        super.assignSerializer(ser);
        if ( != null) {
            NameTransformer t = ;
            if (.isUnwrappingSerializer()) {
                t = NameTransformer.chainedTransformer(t, ((UnwrappingBeanSerializer).);
            }
             = .unwrappingSerializer(t);
        }
    }
    /*
    /**********************************************************
    /* Overrides: schema generation
    /**********************************************************
     */
    // TODO: if/how to change this? Base class has this
/*    
    @Override
    public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor)
        throws JsonMappingException
    {
        if (objectVisitor != null) {
            if (isRequired()) {
                objectVisitor.property(this); 
            } else {
                objectVisitor.optionalProperty(this);
            }
        }
    }
    */
    
    // Override needed to support legacy JSON Schema generator
    @Override
    protected void _depositSchemaProperty(ObjectNode propertiesNodeJsonNode schemaNode)
    {
        JsonNode props = schemaNode.get("properties");
        if (props != null) {
            Iterator<Entry<StringJsonNode>> it = props.fields();
            while (it.hasNext()) {
                Entry<String,JsonNodeentry = it.next();
                String name = entry.getKey();
                if ( != null) {
                    name = .transform(name);
                }
                propertiesNode.put(nameentry.getValue());
            }
        }
    }
    /*
    /**********************************************************
    /* Overrides: internal, other
    /**********************************************************
     */
    
    // need to override as we must get unwrapping instance...
    @Override
            Class<?> typeSerializerProvider providerthrows JsonMappingException
    {
        JsonSerializer<Objectserializer;
        if ( != null) {
            JavaType subtype = provider.constructSpecializedType(type);
            serializer = provider.findValueSerializer(subtypethis);
        } else {
            serializer = provider.findValueSerializer(typethis);
        }
        NameTransformer t = ;
        if (serializer.isUnwrappingSerializer()) {
            t = NameTransformer.chainedTransformer(t, ((UnwrappingBeanSerializerserializer).);
        }
        serializer = serializer.unwrappingSerializer(t);
        
         = .newWith(typeserializer);
        return serializer;
    }
New to GrepCode? Check out our FAQ X