Skip to content

Commit fadb5f0

Browse files
committed
Fix #1554
1 parent 02279b5 commit fadb5f0

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Project: jackson-databind
6262
(reported by Lyor G)
6363
#1550: Unexpected behavior with `@JsonInclude(JsonInclude.Include.NON_EMPTY)` and
6464
`java.util.Date` serialization
65+
#1554: Support deserialization of `Shape.OBJECT` ("as POJO") for `Map`s (and map-like types)
6566

6667
2.8.8 (not yet released)
6768

src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,19 @@ protected JsonDeserializer<?> _createDeserializer2(DeserializationContext ctxt,
372372
return factory.createArrayDeserializer(ctxt, (ArrayType) type, beanDesc);
373373
}
374374
if (type.isMapLikeType()) {
375-
MapLikeType mlt = (MapLikeType) type;
376-
if (mlt.isTrueMapType()) {
377-
return factory.createMapDeserializer(ctxt,(MapType) mlt, beanDesc);
375+
// 11-Mar-2017, tatu: As per [databind#1554], also need to block
376+
// handling as Map if overriden with "as POJO" option.
377+
// Ideally we'd determine it bit later on (to allow custom handler checks)
378+
// but that won't work for other reasons. So do it here.
379+
// (read: rewrite for 3.0)
380+
JsonFormat.Value format = beanDesc.findExpectedFormat(null);
381+
if ((format == null) || format.getShape() != JsonFormat.Shape.OBJECT) {
382+
MapLikeType mlt = (MapLikeType) type;
383+
if (mlt.isTrueMapType()) {
384+
return factory.createMapDeserializer(ctxt,(MapType) mlt, beanDesc);
385+
}
386+
return factory.createMapLikeDeserializer(ctxt, mlt, beanDesc);
378387
}
379-
return factory.createMapLikeDeserializer(ctxt, mlt, beanDesc);
380388
}
381389
if (type.isCollectionLikeType()) {
382390
/* 03-Aug-2012, tatu: As per [databind#40], one exception is if shape

src/test/java/com/fasterxml/jackson/databind/format/MapFormat476Test.java renamed to src/test/java/com/fasterxml/jackson/databind/format/MapFormatShapeTest.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.fasterxml.jackson.databind.*;
99

1010
@SuppressWarnings("serial")
11-
public class MapFormat476Test extends BaseMapTest
11+
public class MapFormatShapeTest extends BaseMapTest
1212
{
1313
@JsonPropertyOrder({ "extra" })
1414
static class Map476Base extends LinkedHashMap<String,Integer> {
@@ -56,14 +56,14 @@ public Bean476Override(int value) {
5656

5757
/*
5858
/**********************************************************
59-
/* Test methods
59+
/* Test methods, serialization
6060
/**********************************************************
6161
*/
6262

6363
final private ObjectMapper MAPPER = objectMapper();
6464
// for [databind#476]: Maps as POJOs
6565

66-
public void testAsPOJOViaClass() throws Exception
66+
public void testSerializeAsPOJOViaClass() throws Exception
6767
{
6868
String result = MAPPER.writeValueAsString(new Bean476Container(1,2,0));
6969
assertEquals(aposToQuotes("{'a':{'extra':13,'empty':false},'b':{'value':2}}"),
@@ -73,19 +73,33 @@ public void testAsPOJOViaClass() throws Exception
7373
// Can't yet use per-property overrides at all, see [databind#1419]
7474

7575
/*
76-
public void testAsPOJOViaProperty() throws Exception
76+
public void testSerializeAsPOJOViaProperty() throws Exception
7777
{
7878
String result = MAPPER.writeValueAsString(new Bean476Container(1,0,3));
7979
assertEquals(aposToQuotes("{'a':{'extra':13,'empty':false},'c':{'empty':false,'value':3}}"),
8080
result);
8181
}
8282
83-
public void testNaturalViaOverride() throws Exception
83+
public void testSerializeNaturalViaOverride() throws Exception
8484
{
8585
String result = MAPPER.writeValueAsString(new Bean476Override(123));
8686
assertEquals(aposToQuotes("{'stuff':{'value':123}}"),
8787
result);
8888
}
8989
*/
9090

91+
/*
92+
/**********************************************************
93+
/* Test methods, deserialization/roundtrip
94+
/**********************************************************
95+
*/
96+
97+
// [databind#1554]
98+
public void testDeserializeAsPOJOViaClass() throws Exception
99+
{
100+
Map476AsPOJO result = MAPPER.readValue(aposToQuotes("{'extra':42}"),
101+
Map476AsPOJO.class);
102+
assertEquals(0, result.size());
103+
assertEquals(42, result.extra);
104+
}
91105
}

0 commit comments

Comments
 (0)