Skip to content

Commit d222b0b

Browse files
committed
Fix #433
1 parent f939634 commit d222b0b

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

release-notes/VERSION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Version: 2.3.3 (xx-xxx-2014)
66
#422: Allow use of "True" and "False" as aliases for booleans when coercing from
77
JSON String
88
#423: Fix `CalendarSerializer` to work with custom format
9-
(repored by sergeymetallic@github)
9+
(reported by sergeymetallic@github)
10+
#433: `ObjectMapper`'s `.valueToTree()` wraps `JsonSerializable` objects into a POJONode
11+
(reported by Francis G)
1012
- Fix null-handling for `CollectionSerializer`
1113

1214
------------------------------------------------------------------------

src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ protected final JsonNode deserializeAny(JsonParser jp, DeserializationContext ct
294294
if (type == byte[].class) { // most common special case
295295
return nodeFactory.binaryNode((byte[]) ob);
296296
}
297+
if (JsonNode.class.isAssignableFrom(type)) {
298+
// [Issue#433]: but could also be a JsonNode hiding in there!
299+
return (JsonNode) ob;
300+
}
297301
// any other special handling needed?
298302
return nodeFactory.pojoNode(ob);
299303
}

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import org.junit.Assert;
1010

1111
import com.fasterxml.jackson.core.*;
12-
1312
import com.fasterxml.jackson.databind.*;
1413
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
14+
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
1515
import com.fasterxml.jackson.databind.util.TokenBuffer;
1616

1717
/**
@@ -43,7 +43,7 @@ public static class LeafMixIn
4343
/**********************************************************
4444
*/
4545

46-
private final static ObjectMapper MAPPER = new ObjectMapper();
46+
private final ObjectMapper MAPPER = objectMapper();
4747

4848
public void testAsInt() throws Exception
4949
{
@@ -200,8 +200,7 @@ public void testEmbeddedObjectInObject() throws Exception
200200
}
201201

202202
// [Issue#232]
203-
public void testBigDecimalAsPlainStringTreeConversion()
204-
throws Exception
203+
public void testBigDecimalAsPlainStringTreeConversion() throws Exception
205204
{
206205
ObjectMapper mapper = new ObjectMapper();
207206
mapper.enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN);
@@ -213,5 +212,38 @@ public void testBigDecimalAsPlainStringTreeConversion()
213212
assertEquals(1, tree.size());
214213
assertTrue(tree.has("pi"));
215214
}
215+
216+
static class CustomSerializedPojo implements JsonSerializable
217+
{
218+
private final ObjectNode node = JsonNodeFactory.instance.objectNode();
219+
220+
public void setFoo(final String foo) {
221+
node.put("foo", foo);
222+
}
223+
224+
@Override
225+
public void serialize(final JsonGenerator jgen, final SerializerProvider provider)
226+
throws IOException
227+
{
228+
jgen.writeTree(node);
229+
}
230+
231+
@Override
232+
public void serializeWithType(JsonGenerator jgen,
233+
SerializerProvider provider, TypeSerializer typeSer) throws IOException {
234+
typeSer.writeTypePrefixForObject(this, jgen);
235+
serialize(jgen, provider);
236+
typeSer.writeTypeSuffixForObject(this, jgen);
237+
}
238+
}
239+
240+
// [Issue#433]
241+
public void testBeanToTree() throws Exception
242+
{
243+
final CustomSerializedPojo pojo = new CustomSerializedPojo();
244+
pojo.setFoo("bar");
245+
final JsonNode node = MAPPER.valueToTree(pojo);
246+
assertEquals(JsonNodeType.OBJECT, node.getNodeType());
247+
}
216248
}
217249

0 commit comments

Comments
 (0)