-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Construct JsonPrimitives from unsigned types #2130
Comments
As a workaround, it would be possible to use |
I've taken a look into this, but I've come across a weird issue, and I'm not sure how to proceed. I created functions that can accept unsigned numbers, and will use public fun JsonPrimitive(value: UByte?): JsonPrimitive =
if (value == null) JsonNull else JsonUnquotedLiteral(value.toString())
public fun JsonPrimitive(value: UShort?): JsonPrimitive =
if (value == null) JsonNull else JsonUnquotedLiteral(value.toString())
public fun JsonPrimitive(value: UInt?): JsonPrimitive =
if (value == null) JsonNull else JsonUnquotedLiteral(value.toString())
public fun JsonPrimitive(value: ULong?): JsonPrimitive =
if (value == null) JsonNull else JsonUnquotedLiteral(value.toString()) This works correctly when the type is explicitly specified val maxL: ULong = 18446744073709551615u
assertEquals("18446744073709551615", JsonPrimitive(maxL).toString()) It also works when Kotlin can figure out the exact type, for example if the number is so large it must be a ULong assertEquals("18446744073709551615", JsonPrimitive(18446744073709551615u).toString()) However, Kotlin can't figure out what to do when it's ambiguous which unsigned number to use: JsonPrimitive(0u)
OptionsHere's a few options I can think of
Update: I've decided to go with option 1. It's the least intrusive, and it's possible to expand the behaviour later to be more convenient. |
hi @rhdunn & @sandwwraith, this issue can be closed as done now https://github.com/Kotlin/kotlinx.serialization/releases/tag/v1.5.1 |
What is your use-case and why do you need this feature?
Various JSON protocols (e.g. Language Server Protocol) make use of unsigned types.
Currently, code like
JsonPrimitive(7u)
does not work asUInt
does not implement theNumber
interface -- you need to do e.g.JsonPrimitive(value.toLong())
instead.Note: This means that for
ULong
types, the max value will be capped atInt.MAX_INT
due to the conversion to a signed type.Describe the solution you'd like
Implement overloads for
JsonPrimitive
that take the unsigned integer types:UByte
,UShort
,UInt
, andULong
.The text was updated successfully, but these errors were encountered: