Skip to content

TokenBuffer serialization fails when buffer contains integer encoded as String #5706

Description

@kirmerzlikin

Search before asking

  • I searched in the issues and found nothing similar.

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());
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions