Skip to content

Commit 2270e10

Browse files
committed
Fix #2657
1 parent 8af03c1 commit 2270e10

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Project: jackson-databind
4747
#2643: Change default textual serialization of `java.util.Date`/`Calendar`
4848
to include colon in timezone offset
4949
#2647: Add `ObjectMapper.createParser()` and `createGenerator()` methods
50+
#2657: Allow serialization of `Properties` with non-String values
5051
#2663: Add new factory method for creating custom `EnumValues` to pass to `EnumDeserializer
5152
(requested by Rafal K)
5253
- Add `SerializerProvider.findContentValueSerializer()` methods

src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,18 @@ public static MapSerializer construct(Set<String> ignoredEntries, JavaType mapTy
304304
Object filterId)
305305
{
306306
JavaType keyType, valueType;
307-
307+
308308
if (mapType == null) {
309309
keyType = valueType = UNSPECIFIED_TYPE;
310310
} else {
311311
keyType = mapType.getKeyType();
312-
valueType = mapType.getContentType();
312+
if (mapType.hasRawClass(java.util.Properties.class)) {
313+
// 25-Mar-2020, tatu: [databind#2657] Since non-standard Properties may actually
314+
// contain non-Strings, demote value type to raw `Object`
315+
valueType = TypeFactory.unknownType();
316+
} else {
317+
valueType = mapType.getContentType();
318+
}
313319
}
314320
// If value type is final, it's same as forcing static value typing:
315321
if (!staticValueType) {

src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,15 @@ public void testVoidSerialization() throws Exception
222222
assertEquals(aposToQuotes("{'value':null}"),
223223
MAPPER.writeValueAsString(new VoidBean()));
224224
}
225+
226+
// [databind#2657]
227+
public void testNonStandardProperties() throws Exception
228+
{
229+
Properties properties = new Properties();
230+
// Bad usage: Properties should NOT contain non-Strings. But
231+
// some do that regardless and compiler won't stop it so.
232+
properties.put("key", 1);
233+
String json = MAPPER.writeValueAsString(properties);
234+
assertEquals("{\"key\":1}", json);
235+
}
225236
}

0 commit comments

Comments
 (0)