-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added (de)serializers for Kotlin unsigned number types
- Loading branch information
Eric Fenderbosch
committed
Feb 26, 2021
1 parent
c2be1f4
commit 787c887
Showing
4 changed files
with
222 additions
and
17 deletions.
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
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 | ||
} |
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) } | ||
} | ||
} |