Closed
Description
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
If you configure these:
.configure(SerializationFeature.WRAP_ROOT_VALUE, true)
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Then mapper.valueToTree()
fails with an exception that looks like this ('Thing' is the type being written to a tree):
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name ('Thing') does not match expected ('JsonNode') for type `com.fasterxml.jackson.databind.JsonNode`
at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: com.fasterxml.jackson.databind.JsonNode["Thing"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportPropertyInputMismatch(DeserializationContext.java:1802)
at com.fasterxml.jackson.databind.DeserializationContext.reportPropertyInputMismatch(DeserializationContext.java:1818)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext._unwrapAndDeserialize(DefaultDeserializationContext.java:367)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:339)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4881)
at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:3145)
at com.fasterxml.jackson.databind.ObjectMapper.valueToTree(ObjectMapper.java:3610)
Version Information
2.17.2
Reproduction
class WrappingObjectMapperTest {
private record Thing(String foo) {}
@Test
void canConvertValuesToTrees() {
final Thing thing = new Thing("bar");
final ObjectMapper mapper = new ObjectMapper()
.configure(SerializationFeature.WRAP_ROOT_VALUE, true)
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
final JsonNode tree = mapper.valueToTree(thing);
assertThat(tree.toString()).isEqualTo("{\"Thing\":{\"foo\":\"bar\"}}");
}
}
Expected behavior
No response
Additional context
No response