For numeric tokens, the blocking SmileParser.getText(Writer) writes the contents of _textBuffer. But Smile never puts number values into _textBuffer (they are decoded into the number fields), so whatever text was last decoded gets written: typically the previous String value. The return value is that text's length.
getText() handles numbers correctly (getNumberValue().toString()), and so does the non-blocking NonBlockingByteArrayParser.
Reproduction
SmileFactory f = new SmileFactory();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
try (JsonGenerator g = f.createGenerator(bo)) {
g.writeStartArray();
g.writeString("abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789");
g.writeNumber(42);
g.writeNumber(1.25);
g.writeNumber(new BigInteger("123456789012345678901234567890"));
g.writeEndArray();
}
try (JsonParser p = f.createParser(bo.toByteArray())) {
JsonToken t;
while ((t = p.nextToken()) != null) {
if (t.isScalarValue()) {
StringWriter w = new StringWriter();
int n = p.getText(w);
System.out.println(t + ": getText()=" + p.getText() + ", getText(Writer)=" + w + " (" + n + ")");
}
}
}
Observed (2.18 branch):
| Token |
getText() |
Blocking getText(Writer) |
Non-blocking getText(Writer) |
VALUE_STRING |
"abcdefghij..." |
"abcdefghij..." (72) |
"abcdefghij..." (72) |
VALUE_NUMBER_INT 42 |
"42" |
"abcdefghij..." (72) |
"42" (2) |
VALUE_NUMBER_FLOAT 1.25 |
"1.25" |
"abcdefghij..." (72) |
"1.25" (4) |
VALUE_NUMBER_INT (BigInteger) |
"1234567890..." |
"abcdefghij..." (72) |
"1234567890..." (30) |
Cause
In SmileParser.getText(Writer):
if (t != null) {
if (t.isNumeric()) {
return _textBuffer.contentsToWriter(writer); // stale content
}
...
Suggested fix
Write getNumberValue().toString() for numeric tokens, as getText() does. Note: the token may still be incomplete (lazily decoded), and getNumberValue() takes care of finishing it.
Found while reviewing #826.
For numeric tokens, the blocking
SmileParser.getText(Writer)writes the contents of_textBuffer. But Smile never puts number values into_textBuffer(they are decoded into the number fields), so whatever text was last decoded gets written: typically the previous String value. The return value is that text's length.getText()handles numbers correctly (getNumberValue().toString()), and so does the non-blockingNonBlockingByteArrayParser.Reproduction
Observed (2.18 branch):
getText()getText(Writer)getText(Writer)VALUE_STRING"abcdefghij...""abcdefghij..."(72)"abcdefghij..."(72)VALUE_NUMBER_INT42"42""abcdefghij..."(72)"42"(2)VALUE_NUMBER_FLOAT1.25"1.25""abcdefghij..."(72)"1.25"(4)VALUE_NUMBER_INT(BigInteger)"1234567890...""abcdefghij..."(72)"1234567890..."(30)Cause
In
SmileParser.getText(Writer):Suggested fix
Write
getNumberValue().toString()for numeric tokens, asgetText()does. Note: the token may still be incomplete (lazily decoded), andgetNumberValue()takes care of finishing it.Found while reviewing #826.