From 2270e1008a7288a7f8995b0441733f8305dfe8ae Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 25 Mar 2020 18:43:18 -0700 Subject: [PATCH] Fix #2657 --- release-notes/VERSION-2.x | 1 + .../jackson/databind/ser/std/MapSerializer.java | 10 ++++++++-- .../databind/ser/jdk/JDKTypeSerializationTest.java | 11 +++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index cd2ca98df4..c32596c3d6 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -47,6 +47,7 @@ Project: jackson-databind #2643: Change default textual serialization of `java.util.Date`/`Calendar` to include colon in timezone offset #2647: Add `ObjectMapper.createParser()` and `createGenerator()` methods +#2657: Allow serialization of `Properties` with non-String values #2663: Add new factory method for creating custom `EnumValues` to pass to `EnumDeserializer (requested by Rafal K) - Add `SerializerProvider.findContentValueSerializer()` methods diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java index 9a5574c45c..66056ff7ac 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java @@ -304,12 +304,18 @@ public static MapSerializer construct(Set ignoredEntries, JavaType mapTy Object filterId) { JavaType keyType, valueType; - + if (mapType == null) { keyType = valueType = UNSPECIFIED_TYPE; } else { keyType = mapType.getKeyType(); - valueType = mapType.getContentType(); + if (mapType.hasRawClass(java.util.Properties.class)) { + // 25-Mar-2020, tatu: [databind#2657] Since non-standard Properties may actually + // contain non-Strings, demote value type to raw `Object` + valueType = TypeFactory.unknownType(); + } else { + valueType = mapType.getContentType(); + } } // If value type is final, it's same as forcing static value typing: if (!staticValueType) { diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java index 5c4da8791c..289e2696ed 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java @@ -222,4 +222,15 @@ public void testVoidSerialization() throws Exception assertEquals(aposToQuotes("{'value':null}"), MAPPER.writeValueAsString(new VoidBean())); } + + // [databind#2657] + public void testNonStandardProperties() throws Exception + { + Properties properties = new Properties(); + // Bad usage: Properties should NOT contain non-Strings. But + // some do that regardless and compiler won't stop it so. + properties.put("key", 1); + String json = MAPPER.writeValueAsString(properties); + assertEquals("{\"key\":1}", json); + } }