Skip to content

Commit

Permalink
Fix #1991
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 25, 2018
1 parent dd57a2d commit 0e06d15
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Expand Up @@ -16,6 +16,8 @@ Project: jackson-databind
(reported by ptirador@github) (reported by ptirador@github)
#1990: MixIn `@JsonProperty` for `Object.hashCode()` is ignored #1990: MixIn `@JsonProperty` for `Object.hashCode()` is ignored
(reported by Freddy B) (reported by Freddy B)
#1991: Context attributes are not passed/available to custom serializer if object is in POJO
(reported by dletin@github)
#1998: Removing "type" attribute with Mixin not taken in account if #1998: Removing "type" attribute with Mixin not taken in account if
using ObjectMapper.copy() using ObjectMapper.copy()
(reported by SBKila@github) (reported by SBKila@github)
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/fasterxml/jackson/databind/node/POJONode.java
Expand Up @@ -102,14 +102,16 @@ public double asDouble(double defaultValue)
*/ */


@Override @Override
public final void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException public final void serialize(JsonGenerator gen, SerializerProvider ctxt) throws IOException
{ {
if (_value == null) { if (_value == null) {
serializers.defaultSerializeNull(gen); ctxt.defaultSerializeNull(gen);
} else if (_value instanceof JsonSerializable) { } else if (_value instanceof JsonSerializable) {
((JsonSerializable) _value).serialize(gen, serializers); ((JsonSerializable) _value).serialize(gen, ctxt);
} else { } else {
gen.writeObject(_value); // 25-May-2018, tatu: [databind#1991] do not call via generator but through context;
// this to preserve contextual information
ctxt.defaultSerializeValue(_value, gen);
} }
} }


Expand Down
Expand Up @@ -88,11 +88,6 @@ public void testDirectCreation() throws IOException
n.addPOJO("foo"); n.addPOJO("foo");
assertEquals(6, n.size()); assertEquals(6, n.size());


// Try serializing it for fun, too...
JsonGenerator g = objectMapper().getFactory().createGenerator(new StringWriter());
n.serialize(g, null);
g.close();

n.removeAll(); n.removeAll();
assertEquals(0, n.size()); assertEquals(0, n.size());
} }
Expand Down
@@ -0,0 +1,55 @@
package com.fasterxml.jackson.databind.node;

import java.io.IOException;
import java.util.*;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class POJONodeTest extends NodeTestBase
{
@JsonSerialize(using = CustomSer.class)
public static class Data {
public String aStr;
}

@SuppressWarnings("serial")
public static class CustomSer extends StdSerializer<Data> {
public CustomSer() {
super(Data.class);
}

@Override
public void serialize(Data value, JsonGenerator gen, SerializerProvider provider) throws IOException {
String attrStr = (String) provider.getAttribute("myAttr");
gen.writeStartObject();
gen.writeStringField("aStr", "The value is: " + (attrStr == null ? "NULL" : attrStr));
gen.writeEndObject();
}
}

final ObjectMapper MAPPER = newObjectMapper();

public void testPOJONodeCustomSer() throws Exception
{
Data data = new Data();
data.aStr = "Hello";

Map<String, Object> mapTest = new HashMap<>();
mapTest.put("data", data);

ObjectNode treeTest = MAPPER.createObjectNode();
treeTest.putPOJO("data", data);

final String EXP = "{\"data\":{\"aStr\":\"The value is: Hello!\"}}";

String mapOut = MAPPER.writer().withAttribute("myAttr", "Hello!").writeValueAsString(mapTest);
assertEquals(EXP, mapOut);

String treeOut = MAPPER.writer().withAttribute("myAttr", "Hello!").writeValueAsString(treeTest);
assertEquals(EXP, treeOut);
}
}
Expand Up @@ -6,7 +6,7 @@


// for [databind#1401]: should allow "Any Setter" to back up otherwise // for [databind#1401]: should allow "Any Setter" to back up otherwise
// problematic Creator properties? // problematic Creator properties?
public class CreatorProperties1401Test extends BaseMapTest public class CreatorAnySetter1401Test extends BaseMapTest
{ {
// for [databind#1401] // for [databind#1401]
static class NoSetter1401 { static class NoSetter1401 {
Expand Down
Expand Up @@ -24,7 +24,7 @@ static class XY {
protected int x, y; protected int x, y;


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


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

0 comments on commit 0e06d15

Please sign in to comment.