From 1643c1ef821ae916bc668b12379135e0fce530ff Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 12 Oct 2024 17:28:36 -0700 Subject: [PATCH] More work towards #4659 --- .../ser/impl/StringArraySerializer.java | 9 ++--- .../databind/ser/std/StdArraySerializers.java | 40 +++++++++---------- .../jackson/databind/type/TypeFactory.java | 2 + 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java index f555307791..821439b0c0 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java @@ -38,7 +38,7 @@ public class StringArraySerializer /** * Value serializer to use, if it's not the standard one - * (if it is we can optimize serialization a lot) + * (if it is we can optimize serialization significantly) */ protected final JsonSerializer _elementSerializer; @@ -85,10 +85,9 @@ public JsonSerializer createContextual(SerializerProvider provider, BeanProperty property) throws JsonMappingException { - /* 29-Sep-2012, tatu: Actually, we need to do much more contextual - * checking here since we finally know for sure the property, - * and it may have overrides - */ + // 29-Sep-2012, tatu: Actually, we need to do much more contextual + // checking here since we finally know for sure the property, + // and it may have overrides JsonSerializer ser = null; // First: if we have a property, may have property-annotation overrides diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java index d5ea4cfd03..2a04bbb973 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java @@ -23,17 +23,17 @@ public class StdArraySerializers { protected final static HashMap> _arraySerializers = - new HashMap>(); + new HashMap<>(); static { // Arrays of various types (including common object types) - _arraySerializers.put(boolean[].class.getName(), new StdArraySerializers.BooleanArraySerializer()); + _arraySerializers.put(boolean[].class.getName(), new BooleanArraySerializer()); _arraySerializers.put(byte[].class.getName(), new ByteArraySerializer()); - _arraySerializers.put(char[].class.getName(), new StdArraySerializers.CharArraySerializer()); - _arraySerializers.put(short[].class.getName(), new StdArraySerializers.ShortArraySerializer()); - _arraySerializers.put(int[].class.getName(), new StdArraySerializers.IntArraySerializer()); - _arraySerializers.put(long[].class.getName(), new StdArraySerializers.LongArraySerializer()); - _arraySerializers.put(float[].class.getName(), new StdArraySerializers.FloatArraySerializer()); - _arraySerializers.put(double[].class.getName(), new StdArraySerializers.DoubleArraySerializer()); + _arraySerializers.put(char[].class.getName(), new CharArraySerializer()); + _arraySerializers.put(short[].class.getName(), new ShortArraySerializer()); + _arraySerializers.put(int[].class.getName(), new IntArraySerializer()); + _arraySerializers.put(long[].class.getName(), new LongArraySerializer()); + _arraySerializers.put(float[].class.getName(), new FloatArraySerializer()); + _arraySerializers.put(double[].class.getName(), new DoubleArraySerializer()); } protected StdArraySerializers() { } @@ -46,6 +46,12 @@ public static JsonSerializer findStandardImpl(Class cls) { return _arraySerializers.get(cls.getName()); } + // @since 2.19 + @SuppressWarnings("deprecation") + static JavaType simpleElementType(Class elemClass) { + return TypeFactory.defaultInstance().uncheckedSimpleType(elemClass); + } + /* **************************************************************** /* Intermediate base classes @@ -88,8 +94,7 @@ public static class BooleanArraySerializer extends ArraySerializerBase { // as above, assuming no one re-defines primitive/wrapper types - @SuppressWarnings("deprecation") - private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Boolean.class); + private final static JavaType VALUE_TYPE = simpleElementType(Boolean.TYPE); public BooleanArraySerializer() { super(boolean[].class); } @@ -179,8 +184,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t public static class ShortArraySerializer extends TypedPrimitiveArraySerializer { // as above, assuming no one re-defines primitive/wrapper types - @SuppressWarnings("deprecation") - private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Short.TYPE); + private final static JavaType VALUE_TYPE = simpleElementType(Short.TYPE); public ShortArraySerializer() { super(short[].class); } public ShortArraySerializer(ShortArraySerializer src, BeanProperty prop, @@ -340,8 +344,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t public static class IntArraySerializer extends ArraySerializerBase { // as above, assuming no one re-defines primitive/wrapper types - @SuppressWarnings("deprecation") - private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Integer.TYPE); + private final static JavaType VALUE_TYPE = simpleElementType(Integer.TYPE); public IntArraySerializer() { super(int[].class); } @@ -429,8 +432,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t public static class LongArraySerializer extends TypedPrimitiveArraySerializer { // as above, assuming no one re-defines primitive/wrapper types - @SuppressWarnings("deprecation") - private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Long.TYPE); + private final static JavaType VALUE_TYPE = simpleElementType(Long.TYPE); public LongArraySerializer() { super(long[].class); } public LongArraySerializer(LongArraySerializer src, BeanProperty prop, @@ -508,8 +510,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t public static class FloatArraySerializer extends TypedPrimitiveArraySerializer { // as above, assuming no one re-defines primitive/wrapper types - @SuppressWarnings("deprecation") - private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Float.TYPE); + private final static JavaType VALUE_TYPE = simpleElementType(Float.TYPE); public FloatArraySerializer() { super(float[].class); @@ -587,8 +588,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t public static class DoubleArraySerializer extends ArraySerializerBase { // as above, assuming no one re-defines primitive/wrapper types - @SuppressWarnings("deprecation") - private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Double.TYPE); + private final static JavaType VALUE_TYPE = simpleElementType(Double.TYPE); public DoubleArraySerializer() { super(double[].class); } diff --git a/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java b/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java index f141cce1d5..6df8f9f254 100644 --- a/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java +++ b/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java @@ -342,6 +342,8 @@ public static Class rawClass(Type t) { return rawClass(((WildcardType) t).getUpperBounds()[0]); } // fallback + // 12-Oct-2024, tatu: Seems unnecessary (replacing with throw does not + // fail any tests), but left for 2.x, changed in 3.0 return defaultInstance().constructType(t).getRawClass(); }