Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  import java.util.HashMap;
  
  
Dummy container class to group standard homogenous array serializer implementations (primitive arrays and String array).
 
 public class StdArraySerializers
 {
     protected final static HashMap<StringJsonSerializer<?>> _arraySerializers =
         new HashMap<StringJsonSerializer<?>>();
     static {
         // Arrays of various types (including common object types)
         .put(boolean[].class.getName(), new StdArraySerializers.BooleanArraySerializer());
         .put(byte[].class.getName(), new StdArraySerializers.ByteArraySerializer());
         .put(char[].class.getName(), new StdArraySerializers.CharArraySerializer());
         .put(short[].class.getName(), new StdArraySerializers.ShortArraySerializer());
         .put(int[].class.getName(), new StdArraySerializers.IntArraySerializer());
         .put(long[].class.getName(), new StdArraySerializers.LongArraySerializer());
         .put(float[].class.getName(), new StdArraySerializers.FloatArraySerializer());
         .put(double[].class.getName(), new StdArraySerializers.DoubleArraySerializer());
     }
 
     protected StdArraySerializers() { }

    
Accessor for checking to see if there is a standard serializer for given primitive value type.
 
     public static JsonSerializer<?> findStandardImpl(Class<?> cls)
     {
         return .get(cls.getName());
     }
     
     /*
      ****************************************************************
     /* Intermediate base classes
      ****************************************************************
      */

    
Intermediate base class used for cases where we may add type information (excludes boolean/int/double arrays).
 
     protected abstract static class TypedPrimitiveArraySerializer<T>
         extends ArraySerializerBase<T>
     {
        
Type serializer to use for values, if any.
 
         protected final TypeSerializer _valueTypeSerializer;
         
         protected TypedPrimitiveArraySerializer(Class<T> cls) {
             super(cls);
              = null;
         }
 
                 BeanProperty propTypeSerializer vts) {
             super(srcprop);
              = vts;
         }
     }
     
     /*
     /****************************************************************
     /* Concrete serializers, arrays
     /****************************************************************
      */
 
     @JacksonStdImpl
     public final static class BooleanArraySerializer
         extends ArraySerializerBase<boolean[]>
     {
         // as above, assuming no one re-defines primitive/wrapper types
         private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Boolean.class);
 
         public BooleanArraySerializer() { super(boolean[].classnull); }

        
Booleans never add type info; hence, even if type serializer is suggested, we'll ignore it...
 
         @Override
            return this;
        }
        @Override
        public JavaType getContentType() {
            return ;
        }
        @Override
        public JsonSerializer<?> getContentSerializer() {
            // 14-Jan-2012, tatu: We could refer to an actual serializer if absolutely necessary
            return null;
        }
        
        @Override
        public boolean isEmpty(boolean[] value) {
            return (value == null) || (value.length == 0);
        }
        @Override
        public boolean hasSingleElement(boolean[] value) {
            return (value.length == 1);
        }
        
        @Override
        public void serializeContents(boolean[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            for (int i = 0, len = value.lengthi < len; ++i) {
                jgen.writeBoolean(value[i]);
            }
        }
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            ObjectNode o = createSchemaNode("array"true);
            o.put("items"createSchemaNode("boolean"));
            return o;
        }
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }

    
Unlike other integral number array serializers, we do not just print out byte values as numbers. Instead, we assume that it would make more sense to output content as base64 encoded bytes (using default base64 encoding).

NOTE: since it is NOT serialized as an array, can not use AsArraySerializer as base

    public final static class ByteArraySerializer
        extends StdSerializer<byte[]>
    {
        public ByteArraySerializer() {
            super(byte[].class);
        }
        
        @Override
        public boolean isEmpty(byte[] value) {
            return (value == null) || (value.length == 0);
        }
        
        @Override
        public void serialize(byte[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            jgen.writeBinary(provider.getConfig().getBase64Variant(),
                    value, 0, value.length);
        }
        @Override
        public void serializeWithType(byte[] valueJsonGenerator jgenSerializerProvider provider,
                TypeSerializer typeSer)
            throws IOExceptionJsonGenerationException
        {
            typeSer.writeTypePrefixForScalar(valuejgen);
            jgen.writeBinary(provider.getConfig().getBase64Variant(),
                    value, 0, value.length);
            typeSer.writeTypeSuffixForScalar(valuejgen);
        }
        
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            ObjectNode o = createSchemaNode("array"true);
            ObjectNode itemSchema = createSchemaNode("string"); //binary values written as strings?
            o.put("items"itemSchema);
            return o;
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }
    public final static class ShortArraySerializer
        extends TypedPrimitiveArraySerializer<short[]>
    {
        // as above, assuming no one re-defines primitive/wrapper types
        private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(.);
        public ShortArraySerializer() { super(short[].class); }
        public ShortArraySerializer(ShortArraySerializer srcBeanProperty propTypeSerializer vts) {
            super(srcpropvts);
        }
        @Override
            return new ShortArraySerializer(thisvts);
        }
        @Override
        public JavaType getContentType() {
            return ;
        }
        @Override
        public JsonSerializer<?> getContentSerializer() {
            // 14-Jan-2012, tatu: We could refer to an actual serializer if absolutely necessary
            return null;
        }
        
        @Override
        public boolean isEmpty(short[] value) {
            return (value == null) || (value.length == 0);
        }
        @Override
        public boolean hasSingleElement(short[] value) {
            return (value.length == 1);
        }
        
        @SuppressWarnings("cast")
        @Override
        public void serializeContents(short[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            if ( != null) {
                for (int i = 0, len = value.lengthi < len; ++i) {
                    .writeTypePrefixForScalar(nulljgen.);
                    jgen.writeNumber(value[i]);
                    .writeTypeSuffixForScalar(nulljgen);
                }
                return;
            }
            for (int i = 0, len = value.lengthi < len; ++i) {
                jgen.writeNumber((int)value[i]);
            }
        }
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            //no "short" type defined by json
            ObjectNode o = createSchemaNode("array"true);
            o.put("items"createSchemaNode("integer"));
            return o;
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }

    
Character arrays are different from other integral number arrays in that they are most likely to be textual data, and should be written as Strings, not arrays of entries.

NOTE: since it is NOT serialized as an array, can not use AsArraySerializer as base

    public final static class CharArraySerializer
        extends StdSerializer<char[]>
    {
        public CharArraySerializer() { super(char[].class); }
        
        @Override
        public boolean isEmpty(char[] value) {
            return (value == null) || (value.length == 0);
        }
        
        @Override
        public void serialize(char[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            // [JACKSON-289] allows serializing as 'sparse' char array too:
            if (provider.isEnabled(.)) {
                jgen.writeStartArray();
                _writeArrayContents(jgenvalue);
                jgen.writeEndArray();
            } else {
                jgen.writeString(value, 0, value.length);
            }
        }
        @Override
        public void serializeWithType(char[] valueJsonGenerator jgenSerializerProvider provider,
                TypeSerializer typeSer)
            throws IOExceptionJsonGenerationException
        {
            // [JACKSON-289] allows serializing as 'sparse' char array too:
            if (provider.isEnabled(.)) {
                typeSer.writeTypePrefixForArray(valuejgen);
                _writeArrayContents(jgenvalue);
                typeSer.writeTypeSuffixForArray(valuejgen);
            } else { // default is to write as simple String
                typeSer.writeTypePrefixForScalar(valuejgen);
                jgen.writeString(value, 0, value.length);
                typeSer.writeTypeSuffixForScalar(valuejgen);
            }
        }
        private final void _writeArrayContents(JsonGenerator jgenchar[] value)
            throws IOExceptionJsonGenerationException
        {
            for (int i = 0, len = value.lengthi < len; ++i) {
                jgen.writeString(valuei, 1);
            }
        }
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            ObjectNode o = createSchemaNode("array"true);
            ObjectNode itemSchema = createSchemaNode("string");
            itemSchema.put("type""string");
            o.put("items"itemSchema);
            return o;
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }
    public final static class IntArraySerializer
        extends ArraySerializerBase<int[]>
    {
        // as above, assuming no one re-defines primitive/wrapper types
        private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(.);
        public IntArraySerializer() { super(int[].classnull); }

        
Ints never add type info; hence, even if type serializer is suggested, we'll ignore it...
        @Override
            return this;
        }        
        @Override
        public JavaType getContentType() {
            return ;
        }
        @Override
        public JsonSerializer<?> getContentSerializer() {
            // 14-Jan-2012, tatu: We could refer to an actual serializer if absolutely necessary
            return null;
        }
        
        @Override
        public boolean isEmpty(int[] value) {
            return (value == null) || (value.length == 0);
        }
        @Override
        public boolean hasSingleElement(int[] value) {
            return (value.length == 1);
        }
        @Override
        public void serializeContents(int[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            for (int i = 0, len = value.lengthi < len; ++i) {
                jgen.writeNumber(value[i]);
            }
        }
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            ObjectNode o = createSchemaNode("array"true);
            o.put("items"createSchemaNode("integer"));
            return o;
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }
    public final static class LongArraySerializer
        extends TypedPrimitiveArraySerializer<long[]>
    {
        // as above, assuming no one re-defines primitive/wrapper types
        private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(.);
        public LongArraySerializer() { super(long[].class); }
        public LongArraySerializer(LongArraySerializer srcBeanProperty prop,
                TypeSerializer vts) {
            super(srcpropvts);
        }
        @Override
            return new LongArraySerializer(thisvts);
        }
        @Override
        public JavaType getContentType() {
            return ;
        }
        @Override
        public JsonSerializer<?> getContentSerializer() {
            // 14-Jan-2012, tatu: We could refer to an actual serializer if absolutely necessary
            return null;
        }
        
        @Override
        public boolean isEmpty(long[] value) {
            return (value == null) || (value.length == 0);
        }
        @Override
        public boolean hasSingleElement(long[] value) {
            return (value.length == 1);
        }
        
        @Override
        public void serializeContents(long[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            if ( != null) {
                for (int i = 0, len = value.lengthi < len; ++i) {
                    .writeTypePrefixForScalar(nulljgen.);
                    jgen.writeNumber(value[i]);
                    .writeTypeSuffixForScalar(nulljgen);
                }
                return;
            }
            
            for (int i = 0, len = value.lengthi < len; ++i) {
                jgen.writeNumber(value[i]);
            }
        }
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            ObjectNode o = createSchemaNode("array"true);
            o.put("items"createSchemaNode("number"true));
            return o;
        }
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }
    public final static class FloatArraySerializer
        extends TypedPrimitiveArraySerializer<float[]>
    {
        // as above, assuming no one re-defines primitive/wrapper types
        private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(.);
        
        public FloatArraySerializer() {
            super(float[].class);
        }
        public FloatArraySerializer(FloatArraySerializer srcBeanProperty prop,
                TypeSerializer vts) {
            super(srcpropvts);
        }
        @Override
            return new FloatArraySerializer(thisvts);
        }
        @Override
        public JavaType getContentType() {
            return ;
        }
        @Override
        public JsonSerializer<?> getContentSerializer() {
            // 14-Jan-2012, tatu: We could refer to an actual serializer if absolutely necessary
            return null;
        }
        
        @Override
        public boolean isEmpty(float[] value) {
            return (value == null) || (value.length == 0);
        }
        @Override
        public boolean hasSingleElement(float[] value) {
            return (value.length == 1);
        }
        
        @Override
        public void serializeContents(float[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            if ( != null) {
                for (int i = 0, len = value.lengthi < len; ++i) {
                    .writeTypePrefixForScalar(nulljgen.);
                    jgen.writeNumber(value[i]);
                    .writeTypeSuffixForScalar(nulljgen);
                }
                return;
            }
            for (int i = 0, len = value.lengthi < len; ++i) {
                jgen.writeNumber(value[i]);
            }
        }
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            ObjectNode o = createSchemaNode("array"true);
            o.put("items"createSchemaNode("number"));
            return o;
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }
    public final static class DoubleArraySerializer
        extends ArraySerializerBase<double[]>
    {
        // as above, assuming no one re-defines primitive/wrapper types
        private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(.);
        public DoubleArraySerializer() { super(double[].classnull); }

        
Doubles never add type info; hence, even if type serializer is suggested, we'll ignore it...
        @Override
            return this;
        }
        @Override
        public JavaType getContentType() {
            return ;
        }
        @Override
        public JsonSerializer<?> getContentSerializer() {
            // 14-Jan-2012, tatu: We could refer to an actual serializer if absolutely necessary
            return null;
        }
        
        @Override
        public boolean isEmpty(double[] value) {
            return (value == null) || (value.length == 0);
        }
        @Override
        public boolean hasSingleElement(double[] value) {
            return (value.length == 1);
        }
        
        @Override
        public void serializeContents(double[] valueJsonGenerator jgenSerializerProvider provider)
            throws IOExceptionJsonGenerationException
        {
            for (int i = 0, len = value.lengthi < len; ++i) {
                jgen.writeNumber(value[i]);
            }
        }
        @Override
        public JsonNode getSchema(SerializerProvider providerType typeHint)
        {
            ObjectNode o = createSchemaNode("array"true);
            o.put("items"createSchemaNode("number"));
            return o;
        }
        
        @Override
        public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
                throws JsonMappingException
        {
            if (visitor != null) {
                JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
                if (v2 != null) {
                    v2.itemsFormat(.);
                }
            }
        }
    }