Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  import java.util.Locale;
  import java.util.TimeZone;
  
 
 public abstract class DateTimeSerializerBase<T>
     extends StdScalarSerializer<T>
     implements ContextualSerializer
 {
    
Flag that indicates that serialization must be done as the Java timetamp, regardless of other settings.
 
     protected final boolean _useTimestamp;
    
    
Specific format to use, if not default format: non null value also indicates that serialization is to be done as JSON String, not numeric timestamp, unless _useTimestamp is true.
 
     protected final DateFormat _customFormat;
 
     protected DateTimeSerializerBase(Class<T> type,
             boolean useTimestampDateFormat customFormat)
     {
         super(type);
          = useTimestamp;
          = customFormat;
     }
 
     public abstract DateTimeSerializerBase<T> withFormat(boolean timestampDateFormat customFormat);
 
     @Override
             BeanProperty propertythrows JsonMappingException
     {
         if (property != null) {
             JsonFormat.Value format = prov.getAnnotationIntrospector().findFormat((Annotated)property.getMember());
             if (format != null) {
                 // Simple case first: serialize as numeric timestamp?
                 if (format.getShape().isNumeric()) {
                     return withFormat(truenull);
                 }
                 // If not, do we have a pattern?
                 TimeZone tz = format.getTimeZone();
                 String pattern = format.getPattern();
                 if (pattern.length() > 0){
                     Locale loc = format.getLocale();
                     if (loc == null) {
                         loc = prov.getLocale();
                     }
                     SimpleDateFormat df = new SimpleDateFormat(patternloc);
                     if (tz == null) {
                         tz = prov.getTimeZone();
                     }
                     df.setTimeZone(tz);
                     return withFormat(falsedf);
                 }
                 // If not, do we at least have a custom timezone?
                 if (tz != null) {
                     DateFormat df = prov.getConfig().getDateFormat();
                     // one shortcut: with our custom format, can simplify handling a bit
                     if (df.getClass() == StdDateFormat.class) {
                         df = StdDateFormat.getISO8601Format(tz);
                     } else {
                         // otherwise need to clone, re-set timezone:
                         df = (DateFormatdf.clone();
                         df.setTimeZone(tz);
                     }
                     return withFormat(falsedf);
                 }
             }
         }
         return this;
     }
 
     /*
     /**********************************************************
     /* Accessors
     /**********************************************************
      */
 
     @Override
     public boolean isEmpty(T value) {
         // let's assume "null date" (timestamp 0) qualifies for empty
        return (value == null) || (_timestamp(value) == 0L);
    }
    protected abstract long _timestamp(T value);
    
    @Override
    public JsonNode getSchema(SerializerProvider providerType typeHint)
    {
        //todo: (ryan) add a format for the date in the schema?
        boolean asNumber = ;
        if (!asNumber) {
            if ( == null) {
                asNumber = provider.isEnabled(.);
            }
        }
        return createSchemaNode(asNumber ? "number" : "string"true);
    }
    
    @Override
    public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        throws JsonMappingException
    {
        boolean asNumber = ;
        if (!asNumber) {
            if ( == null) {
                asNumber = visitor.getProvider().isEnabled(.);
            }
        }
        if (asNumber) {
            JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
            if (v2 != null) {
                v2.numberType(..);
                v2.format(.);
            }
        } else {
            JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
            if (v2 != null) {
                v2.format(.);
            }
        }
    }
    /*
    /**********************************************************
    /* Actual serialization
    /**********************************************************
     */
    @Override
    public abstract void serialize(T valueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException;
New to GrepCode? Check out our FAQ X