Skip to content

Commit 4c448ee

Browse files
committed
Fix #1895
1 parent 4e71662 commit 4c448ee

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Project: jackson-databind
2222
(requested by Ville K)
2323
#1878: `@JsonBackReference` property is always ignored when deserializing since 2.9.0
2424
(reported by reda-alaoui@github)
25+
#1895: Per-type config override "JsonFormat.Shape.OBJECT" for Map.Entry not working
26+
(reported by mcortella@github)
2527

2628
2.9.3 (09-Dec-2017)
2729

src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,10 @@ protected JsonSerializer<?> buildMapEntrySerializer(SerializerProvider prov,
863863
{
864864
// [databind#865]: Allow serialization "as POJO" -- note: to undo, declare
865865
// serialization as `Shape.NATURAL` instead; that's JSON Object too.
866-
JsonFormat.Value format = beanDesc.findExpectedFormat(null);
867-
if (format != null && format.getShape() == JsonFormat.Shape.OBJECT) {
866+
JsonFormat.Value formatOverride = prov.getDefaultPropertyFormat(Map.Entry.class);
867+
JsonFormat.Value formatFromAnnotation = beanDesc.findExpectedFormat(null);
868+
JsonFormat.Value format = JsonFormat.Value.merge(formatFromAnnotation, formatOverride);
869+
if (format.getShape() == JsonFormat.Shape.OBJECT) {
868870
return null;
869871
}
870872
MapEntrySerializer ser = new MapEntrySerializer(valueType, keyType, valueType,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
140140
}
141141
return this;
142142
}
143-
}
143+
}
144144
}

src/test/java/com/fasterxml/jackson/databind/format/MapEntryFormatTest.java

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,36 +99,12 @@ public EmptyEntryWrapper(String key, String value) {
9999

100100
/*
101101
/**********************************************************
102-
/* Test methods
102+
/* Test methods, basic
103103
/**********************************************************
104104
*/
105105

106106
private final ObjectMapper MAPPER = newObjectMapper();
107107

108-
public void testAsNaturalRoundtrip() throws Exception
109-
{
110-
BeanWithMapEntry input = new BeanWithMapEntry("foo" ,"bar");
111-
String json = MAPPER.writeValueAsString(input);
112-
assertEquals(aposToQuotes("{'entry':{'foo':'bar'}}"), json);
113-
BeanWithMapEntry result = MAPPER.readValue(json, BeanWithMapEntry.class);
114-
assertEquals("foo", result.entry.getKey());
115-
assertEquals("bar", result.entry.getValue());
116-
}
117-
// should work via class annotation
118-
public void testAsObjectRoundtrip() throws Exception
119-
{
120-
MapEntryAsObject input = new MapEntryAsObject("foo" ,"bar");
121-
String json = MAPPER.writeValueAsString(input);
122-
assertEquals(aposToQuotes("{'key':'foo','value':'bar'}"), json);
123-
124-
// 16-Oct-2016, tatu: Happens to work by default because it's NOT basic
125-
// `Map.Entry` but subtype.
126-
127-
MapEntryAsObject result = MAPPER.readValue(json, MapEntryAsObject.class);
128-
assertEquals("foo", result.getKey());
129-
assertEquals("bar", result.getValue());
130-
}
131-
132108
public void testInclusion() throws Exception
133109
{
134110
assertEquals(aposToQuotes("{'entry':{'a':'b'}}"),
@@ -159,4 +135,45 @@ public void testInclusionWithReference() throws Exception
159135
assertEquals(aposToQuotes("{}"),
160136
MAPPER.writeValueAsString(new EntryWithNonAbsentWrapper("a", null)));
161137
}
138+
139+
/*
140+
/**********************************************************
141+
/* Test methods, as-Object (Shape)
142+
/**********************************************************
143+
*/
144+
145+
public void testAsNaturalRoundtrip() throws Exception
146+
{
147+
BeanWithMapEntry input = new BeanWithMapEntry("foo" ,"bar");
148+
String json = MAPPER.writeValueAsString(input);
149+
assertEquals(aposToQuotes("{'entry':{'foo':'bar'}}"), json);
150+
BeanWithMapEntry result = MAPPER.readValue(json, BeanWithMapEntry.class);
151+
assertEquals("foo", result.entry.getKey());
152+
assertEquals("bar", result.entry.getValue());
153+
}
154+
// should work via class annotation
155+
public void testAsObjectRoundtrip() throws Exception
156+
{
157+
MapEntryAsObject input = new MapEntryAsObject("foo" ,"bar");
158+
String json = MAPPER.writeValueAsString(input);
159+
assertEquals(aposToQuotes("{'key':'foo','value':'bar'}"), json);
160+
161+
// 16-Oct-2016, tatu: Happens to work by default because it's NOT basic
162+
// `Map.Entry` but subtype.
163+
164+
MapEntryAsObject result = MAPPER.readValue(json, MapEntryAsObject.class);
165+
assertEquals("foo", result.getKey());
166+
assertEquals("bar", result.getValue());
167+
}
168+
169+
// [databind#1895]
170+
public void testDefaultShapeOverride() throws Exception
171+
{
172+
ObjectMapper mapper = new ObjectMapper();
173+
mapper.configOverride(Map.Entry.class)
174+
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.OBJECT));
175+
Map.Entry<String,String> input = new BeanWithMapEntry("foo", "bar").entry;
176+
assertEquals(aposToQuotes("{'key':'foo','value':'bar'}"),
177+
mapper.writeValueAsString(input));
178+
}
162179
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1111

1212
/**
13-
* Unit tests for verifying serialization of {@link java.util.concurrent.AtomicReference}
13+
* Unit tests for verifying serialization of {@link java.util.concurrent.atomic.AtomicReference}
1414
* and other atomic types, via various settings.
1515
*/
1616
public class AtomicTypeSerializationTest

0 commit comments

Comments
 (0)