question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`ObjectMapper.valueToTree()` Fails When `DeserializationFeature.FAIL_ON_TRAILING_TOKENS` Is Enabled

See original GitHub issue

Describe 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 per DeserializationFeature.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.

https://github.com/FasterXML/jackson-databind/blob/70c5dfbd52410d99d36181072711125ac5240a15/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java#L3388-L3393

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Nov 9, 2021

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.

1reaction
raphaelNguyencommented, Oct 29, 2021

@cowtowncoder Glad to be of help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ObjectMapper doesn't fail on trailing characters - Stack Overflow
ObjectMapper mapper = JsonMapper.builder() .enable(DeserializationFeature. ... If feature is enabled, one more call to JsonParser.
Read more >
ObjectMapper (jackson-databind 2.8.0 API) - FasterXML
Method for checking whether given serialization-specific feature is enabled. int, mixInCount(). ObjectReader · reader(). Factory method for constructing ...
Read more >
com.fasterxml.jackson.databind.ObjectMapper.readValue java ...
public void testJackson() throws IOException { ObjectMapper mapper = new ObjectMapper(); File from = new File("albumnList.txt"); ...
Read more >
Jackson ObjectMapper - Jenkov.com
The Jackson ObjectMapper can read JSON into Java objects and ... a Java object to a JsonNode using the ObjectMapper valueToTree() method:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found