fix: Preserve integral Number type in toJsonTree for byte/short/int#2998
fix: Preserve integral Number type in toJsonTree for byte/short/int#2998daguimu wants to merge 1 commit intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
The BYTE, SHORT, and INTEGER type adapters' write methods called value(byteValue()), value(shortValue()), and value(intValue()) respectively. Since JsonWriter only has value(long), Java auto-widened the primitive to long, causing JsonTreeWriter.value(long) to store a Long in the JsonPrimitive instead of the original Byte/Short/Integer. Fix by casting the narrowed primitive to Number, which triggers autoboxing to the correct type and resolves to value(Number) instead of value(long). Fixes google#2680
eef4366 to
34378c3
Compare
|
Thanks for the PR, for what it's worth, there is already an existing (though by now slightly outdated) PR: #2814 I guess the maintainers will decide which one they prefer. So feel free to keep your PR here open. |
Sorry, I'm not sure why we didn't get around to merging #2814. If that one and this are functionally identical (are they?) then we might go with this one just because #2814 would have to be brought up to date. I'm testing it against all of Google's internal tests, which will take a little while. |
|
#2814 also tried to keep reusing the existing |
Problem
When using
toJsonTreewithbyte,short, orinttypes, the resultingJsonPrimitivestores aLonginstead of the expectedByte,Short, orInteger. This is a regression introduced by commit 3e3266c.Root Cause
The
BYTE,SHORT, andINTEGERtype adapters callout.value(value.byteValue()),out.value(value.shortValue()), andout.value(value.intValue())respectively. SinceJsonWriteronly hasvalue(long)(novalue(int)), Java auto-widens the primitive tolong. InJsonTreeWriter,value(long)createsnew JsonPrimitive(long), storing aLongregardless of the original type.Fix
Cast the narrowed primitive to
Numberbefore passing tovalue(). This triggers autoboxing toByte/Short/Integerand causes Java to resolvevalue(Number)instead ofvalue(long).JsonTreeWriter.value(Number)preserves the actual Number type in theJsonPrimitive. For regularJsonWriter,value(Number)produces identical text output.Tests Added
testToJsonTreePreservesIntegralType— verifies thattoJsonTreepreserves:Bytetype forbyte/Byte.classShorttype forshort/Short.classIntegertype forint/Integer.classLongtype forlong/Long.class(unchanged, regression guard)Double→Integer,Long→Byte)Impact
Only affects code paths using
JsonTreeWriter(i.e.,toJsonTree). Regular JSON string serialization viatoJsonis unaffected sincevalue(Number)andvalue(long)produce the same text output. All 4587 existing tests pass.Fixes #2680