Search before asking
Describe the bug
TokenBuffer serialization does not consider the case when buffer contains integer value encoded as String. After it is determined that the value is not Integer, BigInteger, Long or Short, serialization logic tries to cast it to Number and fails with ClassCastException.
I first encountered this issue when trying to serialize a TokenBuffer that was previously copied from JsonParser which pointed at a big integer value. Copying was done using copyCurrentStructure() which called _copyBufferValue() which in turn wrote a big integer using a "deferred" value which at that point turned out to be a String because it hasn't been decoded yet.
After examining TokenBuffer serialization logic I realized that this behaviour was not specific to big integers and it would lead to the same serialization error for any integer encoded as String.
Version Information
First encountered in 2.19.4 but it still exists in 3.x
Reproduction
Reproducing test case:
|
@Test |
|
void testNumberIntAsStringSerialization() throws IOException { |
|
try (TokenBuffer buf = new TokenBuffer(null, false)) { |
|
buf.writeNumber("42", true); |
|
assertEquals("42", MAPPER.writeValueAsString(buf)); |
|
} |
|
} |
Expected behavior
No response
Additional context
Suggested fix: dd1c910
|
} else if (n instanceof String s) { |
|
gen.writeNumber(s); |
|
} else { |
|
gen.writeNumber(((Number) n).intValue()); |
|
} |
Search before asking
Describe the bug
TokenBufferserialization does not consider the case when buffer contains integer value encoded asString. After it is determined that the value is notInteger,BigInteger,LongorShort, serialization logic tries to cast it toNumberand fails withClassCastException.I first encountered this issue when trying to serialize a
TokenBufferthat was previously copied fromJsonParserwhich pointed at a big integer value. Copying was done usingcopyCurrentStructure()which called_copyBufferValue()which in turn wrote a big integer using a "deferred" value which at that point turned out to be aStringbecause it hasn't been decoded yet.After examining
TokenBufferserialization logic I realized that this behaviour was not specific to big integers and it would lead to the same serialization error for any integer encoded asString.Version Information
First encountered in 2.19.4 but it still exists in 3.x
Reproduction
Reproducing test case:
jackson-databind/src/test/java/tools/jackson/databind/util/TokenBufferTest.java
Lines 313 to 319 in 53baad9
Expected behavior
No response
Additional context
Suggested fix: dd1c910
jackson-databind/src/main/java/tools/jackson/databind/util/TokenBuffer.java
Lines 501 to 505 in dd1c910