Skip to content

Commit

Permalink
Fix #1895
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 17, 2018
1 parent 4e71662 commit 4c448ee
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project: jackson-databind
(requested by Ville K)
#1878: `@JsonBackReference` property is always ignored when deserializing since 2.9.0
(reported by reda-alaoui@github)
#1895: Per-type config override "JsonFormat.Shape.OBJECT" for Map.Entry not working
(reported by mcortella@github)

2.9.3 (09-Dec-2017)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,10 @@ protected JsonSerializer<?> buildMapEntrySerializer(SerializerProvider prov,
{
// [databind#865]: Allow serialization "as POJO" -- note: to undo, declare
// serialization as `Shape.NATURAL` instead; that's JSON Object too.
JsonFormat.Value format = beanDesc.findExpectedFormat(null);
if (format != null && format.getShape() == JsonFormat.Shape.OBJECT) {
JsonFormat.Value formatOverride = prov.getDefaultPropertyFormat(Map.Entry.class);
JsonFormat.Value formatFromAnnotation = beanDesc.findExpectedFormat(null);
JsonFormat.Value format = JsonFormat.Value.merge(formatFromAnnotation, formatOverride);
if (format.getShape() == JsonFormat.Shape.OBJECT) {
return null;
}
MapEntrySerializer ser = new MapEntrySerializer(valueType, keyType, valueType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,5 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
}
return this;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,12 @@ public EmptyEntryWrapper(String key, String value) {

/*
/**********************************************************
/* Test methods
/* Test methods, basic
/**********************************************************
*/

private final ObjectMapper MAPPER = newObjectMapper();

public void testAsNaturalRoundtrip() throws Exception
{
BeanWithMapEntry input = new BeanWithMapEntry("foo" ,"bar");
String json = MAPPER.writeValueAsString(input);
assertEquals(aposToQuotes("{'entry':{'foo':'bar'}}"), json);
BeanWithMapEntry result = MAPPER.readValue(json, BeanWithMapEntry.class);
assertEquals("foo", result.entry.getKey());
assertEquals("bar", result.entry.getValue());
}
// should work via class annotation
public void testAsObjectRoundtrip() throws Exception
{
MapEntryAsObject input = new MapEntryAsObject("foo" ,"bar");
String json = MAPPER.writeValueAsString(input);
assertEquals(aposToQuotes("{'key':'foo','value':'bar'}"), json);

// 16-Oct-2016, tatu: Happens to work by default because it's NOT basic
// `Map.Entry` but subtype.

MapEntryAsObject result = MAPPER.readValue(json, MapEntryAsObject.class);
assertEquals("foo", result.getKey());
assertEquals("bar", result.getValue());
}

public void testInclusion() throws Exception
{
assertEquals(aposToQuotes("{'entry':{'a':'b'}}"),
Expand Down Expand Up @@ -159,4 +135,45 @@ public void testInclusionWithReference() throws Exception
assertEquals(aposToQuotes("{}"),
MAPPER.writeValueAsString(new EntryWithNonAbsentWrapper("a", null)));
}

/*
/**********************************************************
/* Test methods, as-Object (Shape)
/**********************************************************
*/

public void testAsNaturalRoundtrip() throws Exception
{
BeanWithMapEntry input = new BeanWithMapEntry("foo" ,"bar");
String json = MAPPER.writeValueAsString(input);
assertEquals(aposToQuotes("{'entry':{'foo':'bar'}}"), json);
BeanWithMapEntry result = MAPPER.readValue(json, BeanWithMapEntry.class);
assertEquals("foo", result.entry.getKey());
assertEquals("bar", result.entry.getValue());
}
// should work via class annotation
public void testAsObjectRoundtrip() throws Exception
{
MapEntryAsObject input = new MapEntryAsObject("foo" ,"bar");
String json = MAPPER.writeValueAsString(input);
assertEquals(aposToQuotes("{'key':'foo','value':'bar'}"), json);

// 16-Oct-2016, tatu: Happens to work by default because it's NOT basic
// `Map.Entry` but subtype.

MapEntryAsObject result = MAPPER.readValue(json, MapEntryAsObject.class);
assertEquals("foo", result.getKey());
assertEquals("bar", result.getValue());
}

// [databind#1895]
public void testDefaultShapeOverride() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.configOverride(Map.Entry.class)
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.OBJECT));
Map.Entry<String,String> input = new BeanWithMapEntry("foo", "bar").entry;
assertEquals(aposToQuotes("{'key':'foo','value':'bar'}"),
mapper.writeValueAsString(input));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
* Unit tests for verifying serialization of {@link java.util.concurrent.AtomicReference}
* Unit tests for verifying serialization of {@link java.util.concurrent.atomic.AtomicReference}
* and other atomic types, via various settings.
*/
public class AtomicTypeSerializationTest
Expand Down

0 comments on commit 4c448ee

Please sign in to comment.