Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  import java.util.Map;
  
Container class for serializers used for handling standard JDK-provided types.
 
 public class NumberSerializers
 {
     protected NumberSerializers() { }
     
     public static void addAll(Map<StringJsonSerializer<?>> allDeserializers)
     {
         final JsonSerializer<?> intS = new IntegerSerializer();
         allDeserializers.put(Integer.class.getName(), intS);
         allDeserializers.put(..getName(), intS);
         allDeserializers.put(Long.class.getName(), .);
         allDeserializers.put(..getName(), .);
         allDeserializers.put(Byte.class.getName(), .);
         allDeserializers.put(..getName(), .);
         allDeserializers.put(Short.class.getName(), .);
         allDeserializers.put(..getName(), .);
 
         // Numbers, limited length floating point
         allDeserializers.put(Float.class.getName(), .);
         allDeserializers.put(..getName(), .);
         allDeserializers.put(Double.class.getName(), .);
         allDeserializers.put(..getName(), .);
     }
     
     /*
     /**********************************************************
     /* Concrete serializers
     /**********************************************************
      */
     
 
 
     /*
     /**********************************************************
     /* Concrete serializers, numerics
     /**********************************************************
      */

    
This is the special serializer for regular java.lang.Integers (and primitive ints)

Since this is one of "native" types, no type information is ever included on serialization (unlike for most scalar types as of 1.5)

 
     @JacksonStdImpl
     public final static class IntegerSerializer
         extends NonTypedScalarSerializerBase<Integer>
     {
         public IntegerSerializer() { super(Integer.class); }
     
         @Override
         public void serialize(Integer valueJsonGenerator jgenSerializerProvider provider)
             throws IOExceptionJsonGenerationException
         {
             jgen.writeNumber(value.intValue());
         }
     
         @Override
         public JsonNode getSchema(SerializerProvider providerType typeHint)
         {
             return createSchemaNode("integer"true);
         }
         
         @Override
         public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
         {
         	visitor.expectIntegerFormat(typeHint);
         }
     }

    
Similar to NumberSerializers.IntegerSerializer, but will not cast to Integer: instead, cast is to java.lang.Number, and conversion is by calling java.lang.Number.intValue().
 
     @JacksonStdImpl
     public final static class IntLikeSerializer
         extends StdScalarSerializer<Number>
     {
         final static IntLikeSerializer instance = new IntLikeSerializer();
     
         public IntLikeSerializer() { super(Number.class); }
         
        @Override
        public void serialize(Number valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            jgen.writeNumber(value.intValue());
        }
    
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            return createSchemaNode("integer"true);
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        {
        	visitor.expectIntegerFormat(typeHint);
        }
    }
    public final static class LongSerializer
        extends StdScalarSerializer<Long>
    {
        final static LongSerializer instance = new LongSerializer();
    
        public LongSerializer() { super(Long.class); }
        
        @Override
        public void serialize(Long valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            jgen.writeNumber(value.longValue());
        }
    
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            return createSchemaNode("number"true);
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        {
        	visitor.expectNumberFormat(typeHint);
        }
    }
    
    public final static class FloatSerializer
        extends StdScalarSerializer<Float>
    {
        final static FloatSerializer instance = new FloatSerializer();
    
        public FloatSerializer() { super(Float.class); }
        
        @Override
        public void serialize(Float valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            jgen.writeNumber(value.floatValue());
        }
    
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            return createSchemaNode("number"true);
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        {
        	visitor.expectNumberFormat(typeHint);
        }
    }

    
This is the special serializer for regular java.lang.Doubles (and primitive doubles)

Since this is one of "native" types, no type information is ever included on serialization (unlike for most scalar types as of 1.5)

    public final static class DoubleSerializer
        extends NonTypedScalarSerializerBase<Double>
    {
        final static DoubleSerializer instance = new DoubleSerializer();
    
        public DoubleSerializer() { super(Double.class); }
    
        @Override
        public void serialize(Double valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            jgen.writeNumber(value.doubleValue());
        }
    
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            return createSchemaNode("number"true);
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        {
        	visitor.expectNumberFormat(typeHint);
        }
    }
    
    
As a fallback, we may need to use this serializer for other types of java.lang.Numbers (custom types).
    public final static class NumberSerializer
        extends StdScalarSerializer<Number>
    {
        public final static NumberSerializer instance = new NumberSerializer();
    
        public NumberSerializer() { super(Number.class); }
    
        @Override
        public void serialize(Number valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            // As per [JACKSON-423], handling for BigInteger and BigDecimal was missing!
            if (value instanceof BigDecimal) {
                jgen.writeNumber((BigDecimalvalue);
            } else if (value instanceof BigInteger) {
                jgen.writeNumber((BigIntegervalue);
                
            /* These shouldn't match (as there are more specific ones),
             * but just to be sure:
             */
            } else if (value instanceof Integer) {
                jgen.writeNumber(value.intValue());
            } else if (value instanceof Long) {
                jgen.writeNumber(value.longValue());
            } else if (value instanceof Double) {
                jgen.writeNumber(value.doubleValue());
            } else if (value instanceof Float) {
                jgen.writeNumber(value.floatValue());
            } else if ((value instanceof Byte) || (value instanceof Short)) {
                jgen.writeNumber(value.intValue()); // doesn't need to be cast to smaller numbers
            } else {
                // We'll have to use fallback "untyped" number write method
                jgen.writeNumber(value.toString());
            }
        }
    
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            return createSchemaNode("number"true);
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        {
        	visitor.expectNumberFormat(typeHint);
        }
    }
New to GrepCode? Check out our FAQ X