-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from efenderbosch/inline-unsigned-numbers
added (de)serializers for Kotlin unsigned number types
- Loading branch information
Showing
10 changed files
with
303 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,12 @@ | |
<parent> | ||
<groupId>com.fasterxml.jackson</groupId> | ||
<artifactId>jackson-base</artifactId> | ||
<version>2.12.2-SNAPSHOT</version> | ||
<version>2.13.0-SNAPSHOT</version> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dinomite
Author
Member
|
||
</parent> | ||
<groupId>com.fasterxml.jackson.module</groupId> | ||
<artifactId>jackson-module-kotlin</artifactId> | ||
<name>jackson-module-kotlin</name> | ||
<version>2.12.2-SNAPSHOT</version> | ||
<version>2.13.0-SNAPSHOT</version> | ||
<packaging>bundle</packaging> | ||
<description>Add-on module for Jackson (https://github.com/FasterXML/jackson/) to support | ||
Kotlin language, specifically introspection of method/constructor parameter names, | ||
|
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) | ||
} | ||
} | ||
} |
Whops. Assuming accidental, will change back to 2.12.2-SNAPSHOT