Skip to content

Commit aba736d

Browse files
committed
working on #433
1 parent e400d16 commit aba736d

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

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

+4
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

+36-4
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)