-
Notifications
You must be signed in to change notification settings - Fork 667
add functions for creating JsonPrimitives from unsigned numbers #2160
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,33 +49,57 @@ public sealed class JsonPrimitive : JsonElement() { | |
| public override fun toString(): String = content | ||
| } | ||
|
|
||
| /** | ||
| * Creates [JsonPrimitive] from the given boolean. | ||
| */ | ||
| /** Creates a [JsonPrimitive] from the given boolean. */ | ||
| public fun JsonPrimitive(value: Boolean?): JsonPrimitive { | ||
| if (value == null) return JsonNull | ||
| return JsonLiteral(value, isString = false) | ||
| } | ||
|
|
||
| /** | ||
| * Creates [JsonPrimitive] from the given number. | ||
| */ | ||
| /** Creates a [JsonPrimitive] from the given number. */ | ||
| public fun JsonPrimitive(value: Number?): JsonPrimitive { | ||
| if (value == null) return JsonNull | ||
| return JsonLiteral(value, isString = false) | ||
| } | ||
|
|
||
| /** | ||
| * Creates [JsonPrimitive] from the given string. | ||
| * Creates a numeric [JsonPrimitive] from the given [UByte]. | ||
| * | ||
| * The value will be encoded as a JSON number. | ||
| */ | ||
| @ExperimentalSerializationApi | ||
| public fun JsonPrimitive(value: UByte): JsonPrimitive = JsonPrimitive(value.toULong()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other functions use nullable parameters. I think for unsigned types we should do the same.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we shouldn't, and nullability for other functions is rather a mistake.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried adding nullability, but I ran into issues: #2130 (comment)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @qwwdfsad, shouldn't we leave an explanatory comment so that in the future there will be no questions why a different approach is used?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to have |
||
|
|
||
| /** | ||
| * Creates a numeric [JsonPrimitive] from the given [UShort]. | ||
| * | ||
| * The value will be encoded as a JSON number. | ||
| */ | ||
| @ExperimentalSerializationApi | ||
| public fun JsonPrimitive(value: UShort): JsonPrimitive = JsonPrimitive(value.toULong()) | ||
|
|
||
| /** | ||
| * Creates a numeric [JsonPrimitive] from the given [UInt]. | ||
| * | ||
| * The value will be encoded as a JSON number. | ||
| */ | ||
| @ExperimentalSerializationApi | ||
| public fun JsonPrimitive(value: UInt): JsonPrimitive = JsonPrimitive(value.toULong()) | ||
|
|
||
| /** | ||
| * Creates a numeric [JsonPrimitive] from the given [ULong]. | ||
| * | ||
| * The value will be encoded as a JSON number. | ||
| */ | ||
| @ExperimentalSerializationApi | ||
| public fun JsonPrimitive(value: ULong): JsonPrimitive = JsonUnquotedLiteral(value.toString()) | ||
|
|
||
| /** Creates a [JsonPrimitive] from the given string. */ | ||
| public fun JsonPrimitive(value: String?): JsonPrimitive { | ||
| if (value == null) return JsonNull | ||
| return JsonLiteral(value, isString = true) | ||
| } | ||
|
|
||
| /** | ||
| * Creates [JsonNull]. | ||
| */ | ||
| /** Creates [JsonNull]. */ | ||
| @ExperimentalSerializationApi | ||
| @Suppress("FunctionName", "UNUSED_PARAMETER") // allows to call `JsonPrimitive(null)` | ||
| public fun JsonPrimitive(value: Nothing?): JsonNull = JsonNull | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add serialization tests, like
JsonUnquotedLiteralTestThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added
parametrizedTest {}, like inJsonUnquotedLiteralTest