`ObjectMapper.valueToTree()` Fails When `DeserializationFeature.FAIL_ON_TRAILING_TOKENS` Is Enabled
See original GitHub issueDescribe the bug
ObjectMapper.valueToTree()
throws the following error when being called on an ObjectMapper
instance made with DeserializationFeature.FAIL_ON_TRAILING_TOKENS
feature enabled.
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Trailing token (of type START_OBJECT) found after value (bound as
com.fasterxml.jackson.databind.JsonNode
): not allowed as perDeserializationFeature.FAIL_ON_TRAILING_TOKENS
at [Source: UNKNOWN; byte offset: #UNKNOWN] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) at com.fasterxml.jackson.databind.DeserializationContext.reportTrailingTokens(DeserializationContext.java:1814) at com.fasterxml.jackson.databind.ObjectMapper._verifyNoTrailingTokens(ObjectMapper.java:4783) at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4656) at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2941) at com.fasterxml.jackson.databind.ObjectMapper.valueToTree(ObjectMapper.java:3392)
Version information 2.13.0
To Reproduce
Create an ObjectMapper with the fail on trailing tokens option enabled and call valueToTree()
using that mapper.
ObjectMapper mapper = JsonMapper.builder().enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS).build;
Expected behavior ObjectMapper should return a JsonNode representation of the object without any error.
Additional context
From what I can see debugging the method valueToTree()
. It seems that the method works by serializing the java Object
into a TokenBuffer
before deserializing that buffer back into JsonNode
.
However, as can be seen in the snippet referenced below, a recent change (https://github.com/FasterXML/jackson-databind/commit/2e986dfe5937b28ba39b4d28e0f993802c7c9f68) has added a line to serialize the java Object
into the buffer (context.serializeValue(buf, fromValue)
) without deleting the old code that does the same thing (writeValue(buf, fromValue)
). This cause the buffer to contain 2 copies of the deserialized object instead of one.
The subsequent readTree()
will serialize the 1st copy of the object out and judge the remaining 2nd copy of the serialized object to be trailing garbage token and failed according to the DeserializationFeature.FAIL_ON_TRAILING_TOKENS
feature.
It seems to me that the fix is to simply remove the redundant call to writeValue
in this method. Please advise if this is indeed a problem or I’ve made a mistake somewhere.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
At some point, yes, it’s always a balance between my spending 2-3 hours cutting the release – no, it’s not a trivial thing to do unfortunately, as every repo must be released for full patch set – and timely delivery of fixes. At this point the job that pays for bills (no real revenue from my OSS work) is busy enough that I don’t have much extra time so it’ll have to be one of weekends.
I probably won’t have time to do 2.13.1 release this week or next, but will try to get it out by November.
@cowtowncoder Glad to be of help.