Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.annotation;
  
  import java.util.Locale;
  import java.util.TimeZone;

General-purpose annotation used for configuring details of how values of properties are to be serialized. Unlike most other Jackson annotations, annotation does not have specific universal interpretation: instead, effect depends on datatype of property being annotated (or more specifically, deserializer and serializer being used).

Common uses include choosing between alternate representations -- for example, whether java.util.Date is to be serialized as number (Java timestamp) or String (such as ISO-8601 compatible time value) -- as well as configuring exact details with pattern property.

As of Jackson 2.1, known special handling include:

  • java.util.Date: Shape can be Shape.STRING or Shape.NUMBER; pattern may contain java.text.SimpleDateFormat-compatible pattern definition.
Jackson 2.1 added following new features:
  • Can now be used on Classes (types) as well, for modified default behavior, possibly overridden by per-property annotation
  • java.lang.Enums: Shapes Shape.STRING and Shape.NUMBER can be used to change between numeric (index) and textual (name or toString()); but it is also possible to use Shape.OBJECT to serialize (but not deserialize) java.lang.Enums as JSON Objects (as if they were POJOs). NOTE: serialization as JSON Object only works with class annotation; will not work as per-property annotation.
  • java.util.Collections can be serialized as (and deserialized from) JSON Objects, if Shape.OBJECT is used. NOTE: can ONLY be used as class annotation; will not work as per-property annotation.

Since:
2.0
 
     .})
 public @interface JsonFormat
 {
    
Value that indicates that default java.util.Locale (from deserialization or serialization context) should be used: annotation does not define value to use.
 
     public final static String DEFAULT_LOCALE = "##default";

    
Value that indicates that default java.util.TimeZone (from deserialization or serialization context) should be used: annotation does not define value to use.
 
     public final static String DEFAULT_TIMEZONE = "##default";
    
    
Datatype-specific additional piece of configuration that may be used to further refine formatting aspects. This may, for example, determine low-level format String used for java.util.Date serialization; however, exact use is determined by specific JsonSerializer
 
     public String pattern() default "";

    
Structure to use for serialization: definition of mapping depends on datatype, but usually has straight-forward counterpart in data format (JSON). Note that commonly only a subset of shapes is available; and if 'invalid' value is chosen, defaults are usually used.
 
     public Shape shape() default .;

    
java.util.Locale to use for serialization (if needed). Special value of DEFAULT_LOCALE can be used to mean "just use the default", where default is specified by the serialization context, which in turn defaults to system defaults (java.util.Locale.getDefault()) unless explicitly set to another locale.
 
     public String locale() default ;
    
    
java.util.TimeZone to use for serialization (if needed). Special value of DEFAULT_TIMEZONE can be used to mean "just use the default", where default is specified by the serialization context, which in turn defaults to system defaults (java.util.TimeZone.getDefault()) unless explicitly set to another locale.
    public String timezone() default ;
    
    /*
    /**********************************************************
    /* Value enumeration(s), value class(es)
    /**********************************************************
     */

    
Value enumeration used for indicating preferred Shape; translates loosely to JSON types, with some extra values to indicate less precise choices (i.e. allowing one of multiple actual shapes)
    public enum Shape
    {
        
Marker enum value that indicates "default" (or "whatever") choice; needed since Annotations can not have null values for enums.
        ANY,

        
Value that indicates shape should not be structural (that is, not ARRAY or OBJECT, but can be any other shape.
        SCALAR,

        
Value that indicates that (JSON) Array type should be used.
        ARRAY,
        
        
Value that indicates that (JSON) Object type should be used.
        OBJECT,

        
Value that indicates that a numeric (JSON) type should be used (but does not specify whether integer or floating-point representation should be used)
        NUMBER,

        
Value that indicates that floating-point numeric type should be used
        NUMBER_FLOAT,

        
Value that indicates that integer number type should be used (and not NUMBER_FLOAT).
        NUMBER_INT,

        
Value that indicates that (JSON) String type should be used.
        STRING,
        
        
Value that indicates that (JSON) boolean type (true, false) should be used.
        BOOLEAN
        ;
        public boolean isNumeric() {
            return (this == ) || (this == ) || (this == );
        }
        public boolean isStructured() {
            return (this == ) || (this == );
        }
    }

    
Helper class used to contain information from a single JsonFormat annotation.
    public static class Value
    {
        private final String pattern;
        private final Shape shape;
        private final Locale locale;
        private final TimeZone timezone;
        public Value() {
            this(""."""");
        }
        
        public Value(JsonFormat ann) {
            this(ann.pattern(), ann.shape(), ann.locale(), ann.timezone());
        }
        public Value(String pShape shString localeStrString tzStr)
        {
            this(psh
                    ,(localeStr == null || localeStr.length() == 0 || .equals(localeStr)) ?
                            null : new Locale(localeStr)
                    ,(tzStr == null || tzStr.length() == 0 || .equals(tzStr)) ?
                            null : TimeZone.getTimeZone(tzStr)
            );
        }

        

Since:
2.1
        public Value(String pShape shLocale lTimeZone tz)
        {
             = p;
             = sh;
             = l;
             = tz;
        }

        

Since:
2.1
        public Value withPattern(String p) {
            return new Value(p);
        }

        

Since:
2.1
        public Value withShape(Shape s) {
            return new Value(s);
        }

        

Since:
2.1
        public Value withLocale(Locale l) {
            return new Value(l);
        }

        

Since:
2.1
        public Value withTimeZone(TimeZone tz) {
            return new Value(tz);
        }
        
        public String getPattern() { return ; }
        public Shape getShape() { return ; }
        public Locale getLocale() { return ; }
        public TimeZone getTimeZone() { return ; }
    }
New to GrepCode? Check out our FAQ X