Implemented Jackson deserializers for BigDecimal, Date and File (otherwise Jackson does not know how to deserialize these classes)

This commit is contained in:
dankito 2020-07-20 13:53:31 +02:00
parent 360c49a08b
commit f36237ebcc
7 changed files with 76 additions and 0 deletions

View File

@ -53,6 +53,8 @@ kotlin {
api kotlin("stdlib-jdk7") api kotlin("stdlib-jdk7")
compileOnly "org.slf4j:slf4j-api:$slf4jVersion" compileOnly "org.slf4j:slf4j-api:$slf4jVersion"
compileOnly "com.fasterxml.jackson.core:jackson-databind:2.9.9"
} }
} }

View File

@ -1,5 +1,8 @@
package net.dankito.utils.multiplatform package net.dankito.utils.multiplatform
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import net.dankito.utils.multiplatform.serialization.BigDecimalDeserializer
fun java.math.BigDecimal.toBigDecimal(): BigDecimal { fun java.math.BigDecimal.toBigDecimal(): BigDecimal {
return BigDecimal(this.toPlainString()) // TODO: find a better way than double string conversion return BigDecimal(this.toPlainString()) // TODO: find a better way than double string conversion
@ -10,6 +13,7 @@ actual fun Collection<BigDecimal>.sum(): BigDecimal {
} }
@JsonDeserialize(using = BigDecimalDeserializer::class)
actual class BigDecimal actual constructor(decimal: String) : java.math.BigDecimal(decimal) { actual class BigDecimal actual constructor(decimal: String) : java.math.BigDecimal(decimal) {
actual companion object { actual companion object {

View File

@ -1,8 +1,11 @@
package net.dankito.utils.multiplatform package net.dankito.utils.multiplatform
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import net.dankito.utils.multiplatform.serialization.DateDeserializer
import java.text.DateFormat import java.text.DateFormat
@JsonDeserialize(using = DateDeserializer::class)
actual class Date actual constructor(millisSinceEpoch: Long) : java.util.Date(millisSinceEpoch) { actual class Date actual constructor(millisSinceEpoch: Long) : java.util.Date(millisSinceEpoch) {
actual constructor() : this(System.currentTimeMillis()) actual constructor() : this(System.currentTimeMillis())

View File

@ -1,11 +1,15 @@
package net.dankito.utils.multiplatform package net.dankito.utils.multiplatform
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import net.dankito.utils.multiplatform.serialization.FileDeserializer
fun java.io.File.toFile(): File { fun java.io.File.toFile(): File {
return File(this.absolutePath) return File(this.absolutePath)
} }
@JsonDeserialize(using = FileDeserializer::class)
actual class File actual constructor(path: String) : java.io.File(path) { actual class File actual constructor(path: String) : java.io.File(path) {
actual constructor(folder: File, filename: String) actual constructor(folder: File, filename: String)

View File

@ -0,0 +1,21 @@
package net.dankito.utils.multiplatform.serialization
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import net.dankito.utils.multiplatform.BigDecimal
open class BigDecimalDeserializer : StdDeserializer<BigDecimal>(BigDecimal::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): BigDecimal? {
val doubleString = parser.readValueAs(String::class.java)
if (doubleString.isNullOrBlank() || doubleString == "null") {
return null
}
return BigDecimal(doubleString)
}
}

View File

@ -0,0 +1,21 @@
package net.dankito.utils.multiplatform.serialization
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import net.dankito.utils.multiplatform.Date
open class DateDeserializer : StdDeserializer<Date>(Date::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): Date? {
val millisSinceEpoch = parser.readValueAs(Long::class.java)
if (millisSinceEpoch == null) {
return null
}
return Date(millisSinceEpoch)
}
}

View File

@ -0,0 +1,21 @@
package net.dankito.utils.multiplatform.serialization
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import net.dankito.utils.multiplatform.File
open class FileDeserializer : StdDeserializer<File>(File::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): File? {
val path = parser.readValueAs(String::class.java)
if (path == null || path == "null") {
return null
}
return File(path)
}
}