Skip to content

Commit 0e06d15

Browse files
committed
Fix #1991
1 parent dd57a2d commit 0e06d15

File tree

6 files changed

+67
-12
lines changed

6 files changed

+67
-12
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project: jackson-databind
1616
(reported by ptirador@github)
1717
#1990: MixIn `@JsonProperty` for `Object.hashCode()` is ignored
1818
(reported by Freddy B)
19+
#1991: Context attributes are not passed/available to custom serializer if object is in POJO
20+
(reported by dletin@github)
1921
#1998: Removing "type" attribute with Mixin not taken in account if
2022
using ObjectMapper.copy()
2123
(reported by SBKila@github)

src/main/java/com/fasterxml/jackson/databind/node/POJONode.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,16 @@ public double asDouble(double defaultValue)
102102
*/
103103

104104
@Override
105-
public final void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException
105+
public final void serialize(JsonGenerator gen, SerializerProvider ctxt) throws IOException
106106
{
107107
if (_value == null) {
108-
serializers.defaultSerializeNull(gen);
108+
ctxt.defaultSerializeNull(gen);
109109
} else if (_value instanceof JsonSerializable) {
110-
((JsonSerializable) _value).serialize(gen, serializers);
110+
((JsonSerializable) _value).serialize(gen, ctxt);
111111
} else {
112-
gen.writeObject(_value);
112+
// 25-May-2018, tatu: [databind#1991] do not call via generator but through context;
113+
// this to preserve contextual information
114+
ctxt.defaultSerializeValue(_value, gen);
113115
}
114116
}
115117

src/test/java/com/fasterxml/jackson/databind/node/ArrayNodeTest.java

-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ public void testDirectCreation() throws IOException
8888
n.addPOJO("foo");
8989
assertEquals(6, n.size());
9090

91-
// Try serializing it for fun, too...
92-
JsonGenerator g = objectMapper().getFactory().createGenerator(new StringWriter());
93-
n.serialize(g, null);
94-
g.close();
95-
9691
n.removeAll();
9792
assertEquals(0, n.size());
9893
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fasterxml.jackson.databind.node;
2+
3+
import java.io.IOException;
4+
import java.util.*;
5+
6+
import com.fasterxml.jackson.core.JsonGenerator;
7+
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9+
import com.fasterxml.jackson.databind.node.ObjectNode;
10+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
11+
12+
public class POJONodeTest extends NodeTestBase
13+
{
14+
@JsonSerialize(using = CustomSer.class)
15+
public static class Data {
16+
public String aStr;
17+
}
18+
19+
@SuppressWarnings("serial")
20+
public static class CustomSer extends StdSerializer<Data> {
21+
public CustomSer() {
22+
super(Data.class);
23+
}
24+
25+
@Override
26+
public void serialize(Data value, JsonGenerator gen, SerializerProvider provider) throws IOException {
27+
String attrStr = (String) provider.getAttribute("myAttr");
28+
gen.writeStartObject();
29+
gen.writeStringField("aStr", "The value is: " + (attrStr == null ? "NULL" : attrStr));
30+
gen.writeEndObject();
31+
}
32+
}
33+
34+
final ObjectMapper MAPPER = newObjectMapper();
35+
36+
public void testPOJONodeCustomSer() throws Exception
37+
{
38+
Data data = new Data();
39+
data.aStr = "Hello";
40+
41+
Map<String, Object> mapTest = new HashMap<>();
42+
mapTest.put("data", data);
43+
44+
ObjectNode treeTest = MAPPER.createObjectNode();
45+
treeTest.putPOJO("data", data);
46+
47+
final String EXP = "{\"data\":{\"aStr\":\"The value is: Hello!\"}}";
48+
49+
String mapOut = MAPPER.writer().withAttribute("myAttr", "Hello!").writeValueAsString(mapTest);
50+
assertEquals(EXP, mapOut);
51+
52+
String treeOut = MAPPER.writer().withAttribute("myAttr", "Hello!").writeValueAsString(treeTest);
53+
assertEquals(EXP, treeOut);
54+
}
55+
}

src/test/java/com/fasterxml/jackson/failing/CreatorProperties1401Test.java renamed to src/test/java/com/fasterxml/jackson/failing/CreatorAnySetter1401Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// for [databind#1401]: should allow "Any Setter" to back up otherwise
88
// problematic Creator properties?
9-
public class CreatorProperties1401Test extends BaseMapTest
9+
public class CreatorAnySetter1401Test extends BaseMapTest
1010
{
1111
// for [databind#1401]
1212
static class NoSetter1401 {

src/test/java/com/fasterxml/jackson/failing/ImplicitParamsForCreator806Test.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static class XY {
2424
protected int x, y;
2525

2626
// annotation should NOT be needed with 2.6 any more (except for single-arg case)
27-
//@com.fasterxml.jackson.annotation.JsonCreator
27+
// @com.fasterxml.jackson.annotation.JsonCreator
2828
public XY(int x, int y) {
2929
this.x = x;
3030
this.y = y;
@@ -42,7 +42,8 @@ public XY(int x, int y) {
4242
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
4343
;
4444

45-
// for [databind#806]
45+
// for [databind#806]: problem is that renaming occurs too late for implicitly detected
46+
// Creators
4647
public void testImplicitNameWithNamingStrategy() throws Exception
4748
{
4849
XY value = MAPPER.readValue(aposToQuotes("{'param_name0':1,'param_name1':2}"), XY.class);

0 commit comments

Comments
 (0)