Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  
Base class used by all standard serializers, and can also be used for custom serializers (in fact, this is the recommended base class to use). Provides convenience methods for implementing com.fasterxml.jackson.databind.jsonschema.SchemaAware
 
 public abstract class StdSerializer<T>
     extends JsonSerializer<T>
     implements JsonFormatVisitableSchemaAware
 {
    
Nominal type supported, usually declared type of property for which serializer is used.
 
     protected final Class<T> _handledType;
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     protected StdSerializer(Class<T> t) {
          = t;
     }
 
     @SuppressWarnings("unchecked")
     protected StdSerializer(JavaType type) {
          = (Class<T>) type.getRawClass();
     }
    
    
Alternate constructor that is (alas!) needed to work around kinks of generic type handling
 
     @SuppressWarnings("unchecked")
     protected StdSerializer(Class<?> tboolean dummy) {
          = (Class<T>) t;
     }
     /*
     /**********************************************************
     /* Accessors
     /**********************************************************
      */
     
     @Override
     public Class<T> handledType() { return ; }
 
     /*
     /**********************************************************
     /* Serialization
     /**********************************************************
      */
     
     @Override
     public abstract void serialize(T valueJsonGenerator jgenSerializerProvider provider)
         throws IOExceptionJsonGenerationException;
 
     /*
     /**********************************************************
     /* Helper methods for JSON Schema generation
     /**********************************************************
      */
    
    
Default implementation simply claims type is "string"; usually overriden by custom serializers.
 
 //  @Override
     public JsonNode getSchema(SerializerProvider providerType typeHint)
         throws JsonMappingException
     {
         return createSchemaNode("string");
     }
    
    
Default implementation simply claims type is "string"; usually overriden by custom serializers.
 
 //    @Override
     public JsonNode getSchema(SerializerProvider providerType typeHintboolean isOptional)
         throws JsonMappingException
     {
     	ObjectNode schema = (ObjectNodegetSchema(providertypeHint);
    	if (!isOptional) {
    		schema.put("required", !isOptional);
    	}
        return schema;
    }
    
    protected ObjectNode createObjectNode() {
        return ..objectNode();
    }
    
    protected ObjectNode createSchemaNode(String type)
    {
        ObjectNode schema = createObjectNode();
        schema.put("type"type);
        return schema;
    }
    
    protected ObjectNode createSchemaNode(String typeboolean isOptional)
    {
        ObjectNode schema = createSchemaNode(type);
        // as per [JACKSON-563]. Note that 'required' defaults to false
        if (!isOptional) {
            schema.put("required", !isOptional);
        }
        return schema;
    }
    
    
Default implementation specifies no format. This behavior is usually overriden by custom serializers.
    @Override
    public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        throws JsonMappingException
    {
    	visitor.expectAnyFormat(typeHint);
    }
            
    /*
    /**********************************************************
    /* Helper methods for exception handling
    /**********************************************************
     */
    
    
Method that will modify caught exception (passed in as argument) as necessary to include reference information, and to ensure it is a subtype of java.io.IOException, or an unchecked exception.

Rules for wrapping and unwrapping are bit complicated; essentially:

    public void wrapAndThrow(SerializerProvider provider,
            Throwable tObject beanString fieldName)
        throws IOException
    {
        /* 05-Mar-2009, tatu: But one nasty edge is when we get
         *   StackOverflow: usually due to infinite loop. But that
         *   usually gets hidden within an InvocationTargetException...
         */
        while (t instanceof InvocationTargetException && t.getCause() != null) {
            t = t.getCause();
        }
        // Errors and "plain" IOExceptions to be passed as is
        if (t instanceof Error) {
            throw (Errort;
        }
        // Ditto for IOExceptions... except for mapping exceptions!
        boolean wrap = (provider == null) || provider.isEnabled(.);
        if (t instanceof IOException) {
            if (!wrap || !(t instanceof JsonMappingException)) {
                throw (IOExceptiont;
            }
        } else if (!wrap) { // [JACKSON-407] -- allow disabling wrapping for unchecked exceptions
            if (t instanceof RuntimeException) {
                throw (RuntimeExceptiont;
            }
        }
        // [JACKSON-55] Need to add reference information
        throw JsonMappingException.wrapWithPath(tbeanfieldName);
    }
    public void wrapAndThrow(SerializerProvider provider,
            Throwable tObject beanint index)
        throws IOException
    {
        while (t instanceof InvocationTargetException && t.getCause() != null) {
            t = t.getCause();
        }
        // Errors are to be passed as is
        if (t instanceof Error) {
            throw (Errort;
        }
        // Ditto for IOExceptions... except for mapping exceptions!
        boolean wrap = (provider == null) || provider.isEnabled(.);
        if (t instanceof IOException) {
            if (!wrap || !(t instanceof JsonMappingException)) {
                throw (IOExceptiont;
            }
        } else if (!wrap) { // [JACKSON-407] -- allow disabling wrapping for unchecked exceptions
            if (t instanceof RuntimeException) {
                throw (RuntimeExceptiont;
            }
        }
        // [JACKSON-55] Need to add reference information
        throw JsonMappingException.wrapWithPath(tbeanindex);
    }
    /*
    /**********************************************************
    /* Helper methods, other
    /**********************************************************
     */
    
    
Method that can be called to determine if given serializer is the default serializer Jackson uses; as opposed to a custom serializer installed by a module or calling application. Determination is done using com.fasterxml.jackson.databind.annotation.JacksonStdImpl annotation on serializer class.
    protected boolean isDefaultSerializer(JsonSerializer<?> serializer) {
        return (serializer != null && serializer.getClass().getAnnotation(JacksonStdImpl.class) != null);
    }
New to GrepCode? Check out our FAQ X