diff --git a/common/build.gradle b/common/build.gradle index d4d1c98d..55ca6d41 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -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" } } diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt index 99400a31..51b31c6a 100644 --- a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt @@ -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.sum(): BigDecimal { } +@JsonDeserialize(using = BigDecimalDeserializer::class) actual class BigDecimal actual constructor(decimal: String) : java.math.BigDecimal(decimal) { actual companion object { diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt index a6b0d4a6..4105a958 100644 --- a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt @@ -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()) diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/File.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/File.kt index dcf1191e..fe90d4bf 100644 --- a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/File.kt +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/File.kt @@ -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) diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/BigDecimalDeserializer.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/BigDecimalDeserializer.kt new file mode 100644 index 00000000..167b0b4b --- /dev/null +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/BigDecimalDeserializer.kt @@ -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::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) + } + +} \ No newline at end of file diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/DateDeserializer.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/DateDeserializer.kt new file mode 100644 index 00000000..03a62a6a --- /dev/null +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/DateDeserializer.kt @@ -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::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) + } + +} \ No newline at end of file diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/FileDeserializer.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/FileDeserializer.kt new file mode 100644 index 00000000..31548681 --- /dev/null +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/serialization/FileDeserializer.kt @@ -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::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) + } + +} \ No newline at end of file