-
Notifications
You must be signed in to change notification settings - Fork 175
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
added (de)serializers for Kotlin unsigned number types #419
Merged
dinomite
merged 8 commits into
FasterXML:2.12
from
efenderbosch:inline-unsigned-numbers
Feb 28, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
332ec82
Start 2.13 branch
cowtowncoder bcb48c2
Merge branch '2.12' into 2.13
cowtowncoder acf8651
Merge branch '2.12' into 2.13
cowtowncoder e0c5ec2
Added Test and Documentation for the sealed-classes-without-@JsonSubT…
happyherp 7110447
Use Kotlin style in readme
dinomite c2be1f4
Add failing test for GitHub #196
dinomite 787c887
added (de)serializers for Kotlin unsigned number types
02698b8
Credit efenderbosch for #182 fix
dinomite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/fasterxml/jackson/module/kotlin/UnsignedNumbers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@file:Suppress("EXPERIMENTAL_API_USAGE") | ||
|
||
package com.fasterxml.jackson.module.kotlin | ||
|
||
import java.math.BigInteger | ||
|
||
fun Short.asUByte() = when { | ||
this >= 0 && this <= UByte.MAX_VALUE.toShort() -> this.toUByte() | ||
else -> null | ||
} | ||
|
||
fun Int.asUShort() = when { | ||
this >= 0 && this <= UShort.MAX_VALUE.toInt() -> this.toUShort() | ||
else -> null | ||
} | ||
|
||
fun Long.asUInt() = when { | ||
this >= 0 && this <= UInt.MAX_VALUE.toLong() -> this.toUInt() | ||
else -> null | ||
} | ||
|
||
private val uLongMaxValue = BigInteger(ULong.MAX_VALUE.toString()) | ||
fun BigInteger.asULong() = when { | ||
this >= BigInteger.ZERO && this <= uLongMaxValue -> this.toLong().toULong() | ||
else -> null | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/SealedClassTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.fasterxml.jackson.module.kotlin.test | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeInfo | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.test.SealedClassTest.SuperClass.B | ||
import org.junit.Test | ||
import kotlin.test.assertTrue | ||
|
||
class SealedClassTest { | ||
private val mapper = jacksonObjectMapper() | ||
|
||
/** | ||
* Json of a Serialized B-Object. | ||
*/ | ||
private val jsonB = """{"@type":"SealedClassTest${"$"}SuperClass${"$"}B"}""" | ||
|
||
/** | ||
* Tests that the @JsonSubTypes-Annotation is not necessary when working with Sealed-Classes. | ||
*/ | ||
@Test | ||
fun sealedClassWithoutSubTypes() { | ||
val result = mapper.readValue(jsonB, SuperClass::class.java) | ||
assertTrue { result is B } | ||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME) | ||
sealed class SuperClass { | ||
class A : SuperClass() | ||
class B : SuperClass() | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/UnsignedNumbersTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
@file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS") | ||
|
||
package com.fasterxml.jackson.module.kotlin.test | ||
|
||
import com.fasterxml.jackson.core.exc.InputCoercionException | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.junit.Test | ||
import java.math.BigInteger | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Assert.assertThrows | ||
|
||
internal class UnsignedNumbersTests { | ||
|
||
val mapper: ObjectMapper = jacksonObjectMapper() | ||
|
||
@Test | ||
fun `test UByte`() { | ||
val json = mapper.writeValueAsString(UByte.MAX_VALUE) | ||
val deserialized = mapper.readValue<UByte>(json) | ||
assertThat(deserialized, equalTo(UByte.MAX_VALUE)) | ||
} | ||
|
||
@Test | ||
fun `test UByte overflow`() { | ||
val json = mapper.writeValueAsString(UByte.MAX_VALUE + 1u) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<UByte>(json) } | ||
} | ||
|
||
@Test | ||
fun `test UByte underflow`() { | ||
val json = mapper.writeValueAsString(-1) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<UByte>(json) } | ||
} | ||
|
||
@Test | ||
fun `test UShort`() { | ||
val json = mapper.writeValueAsString(UShort.MAX_VALUE) | ||
val deserialized = mapper.readValue<UShort>(json) | ||
assertThat(deserialized, equalTo(UShort.MAX_VALUE)) | ||
} | ||
|
||
@Test | ||
fun `test UShort overflow`() { | ||
val json = mapper.writeValueAsString(UShort.MAX_VALUE + 1u) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<UShort>(json) } | ||
} | ||
|
||
@Test | ||
fun `test UShort underflow`() { | ||
val json = mapper.writeValueAsString(-1) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<UShort>(json) } | ||
} | ||
|
||
@Test | ||
fun `test UInt`() { | ||
val json = mapper.writeValueAsString(UInt.MAX_VALUE) | ||
val deserialized = mapper.readValue<UInt>(json) | ||
assertThat(deserialized, equalTo(UInt.MAX_VALUE)) | ||
} | ||
|
||
@Test | ||
fun `test UInt overflow`() { | ||
val json = mapper.writeValueAsString(UInt.MAX_VALUE.toULong() + 1u) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<UInt>(json) } | ||
} | ||
|
||
@Test | ||
fun `test UInt underflow`() { | ||
val json = mapper.writeValueAsString(-1) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<UInt>(json) } | ||
} | ||
|
||
@Test | ||
fun `test ULong`() { | ||
val json = mapper.writeValueAsString(ULong.MAX_VALUE) | ||
val deserialized = mapper.readValue<ULong>(json) | ||
assertThat(deserialized, equalTo(ULong.MAX_VALUE)) | ||
} | ||
|
||
@Test | ||
fun `test ULong overflow`() { | ||
val value = BigInteger(ULong.MAX_VALUE.toString()) + BigInteger.ONE | ||
val json = mapper.writeValueAsString(value) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<ULong>(json) } | ||
} | ||
|
||
@Test | ||
fun `test ULong underflow`() { | ||
val json = mapper.writeValueAsString(-1) | ||
assertThrows(InputCoercionException::class.java) { mapper.readValue<ULong>(json) } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/failing/Github196.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.fasterxml.jackson.module.kotlin.test.github.failing | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.fasterxml.jackson.module.kotlin.test.expectFailure | ||
import org.junit.Test | ||
import kotlin.test.assertSame | ||
|
||
class TestGithub196 { | ||
@Test | ||
fun testUnitSingletonDeserialization() { | ||
// An empty object should be deserialized as *the* Unit instance, but is not | ||
expectFailure<AssertionError>("GitHub #196 has been fixed!") { | ||
assertSame(jacksonObjectMapper().readValue("{}"), Unit) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
when
is one of my favorite Kotlin features, I'm happy every time I see it.