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, numerics
     /**********************************************************
      */
 
     @JacksonStdImpl
     public final static class ShortSerializer
         extends StdScalarSerializer<Short>
     {
         final static ShortSerializer instance = new ShortSerializer();
     
         public ShortSerializer() { super(Short.class); }
         
         @Override
         public void serialize(Short valueJsonGenerator jgenSerializerProvider provider)
             throws IOExceptionJsonGenerationException
         {
             jgen.writeNumber(value.shortValue());
         }
     
         @Override
         public JsonNode getSchema(SerializerProvider providerType typeHint)
         {
             return createSchemaNode("number"true);		// msteiger: maybe "integer" or "short" ?
         }
         
         @Override
         public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                 throws JsonMappingException
         {
             JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
             if (v2 != null) {
                 v2.numberType(..);			// should be SHORT
             }
         }
     }
    
    
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)
                throws JsonMappingException
        {
            JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
            if (v2 != null) {
                v2.numberType(..);
            }
        }
    }

    
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().
    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)
                throws JsonMappingException
        {
            JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
            if (v2 != null) {
                v2.numberType(..);
            }
        }
    }
    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)
                throws JsonMappingException
        {
            JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
            if (v2 != null) {
                v2.numberType(..);
            }
        }
    }
    
    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)
                throws JsonMappingException
        {
            JsonNumberFormatVisitor v2 = visitor.expectNumberFormat(typeHint);
            if (v2 != null) {
                v2.numberType(..);
            }
        }
    }

    
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)
            throws JsonMappingException
        {
            JsonNumberFormatVisitor v2 = visitor.expectNumberFormat(typeHint);
            if (v2 != null) {
                v2.numberType(..);
            }
        }
    }
    
    
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) {
                // 07-Jul-2013, tatu: Should be handled by propagating setting to JsonGenerator
                //    so this should not be needed:
                /*
                if (provider.isEnabled(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN)) {
                    // [Issue#232]: Ok, rather clumsy, but let's try to work around the problem with:
                    if (!(jgen instanceof TokenBuffer)) {
                        jgen.writeNumber(((BigDecimal) value).toPlainString());
                        return;
                    }
                }
                */
                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)
            throws JsonMappingException
        {
            // Hmmh. What should it be? Ideally should probably indicate BIG_DECIMAL
            // to ensure no information is lost? But probably won't work that well...
            JsonNumberFormatVisitor v2 = visitor.expectNumberFormat(typeHint);
            if (v2 != null) {
                v2.numberType(..);
            }
        }
    }
New to GrepCode? Check out our FAQ X