Implemented Jackson deserializers for BigDecimal, Date and File (otherwise Jackson does not know how to deserialize these classes)
This commit is contained in:
parent
360c49a08b
commit
f36237ebcc
|
@ -53,6 +53,8 @@ kotlin {
|
|||
api kotlin("stdlib-jdk7")
|
||||
|
||||
compileOnly "org.slf4j:slf4j-api:$slf4jVersion"
|
||||
|
||||
compileOnly "com.fasterxml.jackson.core:jackson-databind:2.9.9"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
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 {
|
||||
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 companion object {
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package net.dankito.utils.multiplatform
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
|
||||
import net.dankito.utils.multiplatform.serialization.DateDeserializer
|
||||
import java.text.DateFormat
|
||||
|
||||
|
||||
@JsonDeserialize(using = DateDeserializer::class)
|
||||
actual class Date actual constructor(millisSinceEpoch: Long) : java.util.Date(millisSinceEpoch) {
|
||||
|
||||
actual constructor() : this(System.currentTimeMillis())
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
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 {
|
||||
return File(this.absolutePath)
|
||||
}
|
||||
|
||||
|
||||
@JsonDeserialize(using = FileDeserializer::class)
|
||||
actual class File actual constructor(path: String) : java.io.File(path) {
|
||||
|
||||
actual constructor(folder: File, filename: String)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue