From d79881420b4564a4dfb40f13d3b0623b4fbc46ad Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 16 Jan 2020 17:21:46 +0100 Subject: Support for custom Decimal type in the common target --- .../kotlin/pl/treksoft/kvision/types/Decimal.kt | 24 ++++++++ .../kotlin/pl/treksoft/kvision/types/Decimal.kt | 24 ++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 71 ++++++++++++++++------ .../kotlin/pl/treksoft/kvision/types/Decimal.kt | 47 ++++++++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 19 +++--- .../kotlin/pl/treksoft/kvision/types/Decimal.kt | 47 ++++++++++++++ .../pl/treksoft/kvision/remote/KVServiceManager.kt | 5 +- .../kotlin/pl/treksoft/kvision/types/Decimal.kt | 47 ++++++++++++++ 8 files changed, 257 insertions(+), 27 deletions(-) create mode 100644 kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt create mode 100644 kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt create mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt create mode 100644 kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt create mode 100644 kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt (limited to 'kvision-modules') diff --git a/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt new file mode 100644 index 00000000..26d64654 --- /dev/null +++ b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.types + +expect class Decimal diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt new file mode 100644 index 00000000..d1c0366e --- /dev/null +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.types + +actual typealias Decimal = Double diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index ac799769..6cc2b73d 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -40,12 +40,13 @@ import org.jooby.Response import org.slf4j.Logger import org.slf4j.LoggerFactory import pl.treksoft.kvision.types.* -import kotlin.reflect.KClass -import java.time.LocalDateTime +import java.math.BigDecimal import java.time.LocalDate +import java.time.LocalDateTime import java.time.LocalTime import java.time.OffsetDateTime import java.time.OffsetTime +import kotlin.reflect.KClass /** @@ -67,11 +68,13 @@ actual open class KVServiceManager actual constructor(val serviceClass: module.addSerializer(LocalTime::class.java, LocalTimeSerializer()) module.addSerializer(OffsetDateTime::class.java, OffsetDateTimeSerializer()) module.addSerializer(OffsetTime::class.java, OffsetTimeSerializer()) + module.addSerializer(BigDecimal::class.java, BigDecimalSerializer()) module.addDeserializer(LocalDateTime::class.java, LocalDateTimeDeserializer()) module.addDeserializer(LocalDate::class.java, LocalDateDeserializer()) module.addDeserializer(LocalTime::class.java, LocalTimeDeserializer()) module.addDeserializer(OffsetDateTime::class.java, OffsetDateTimeDeserializer()) module.addDeserializer(OffsetTime::class.java, OffsetTimeDeserializer()) + module.addDeserializer(BigDecimal::class.java, BigDecimalDeserializer()) this.registerModule(module) } var counter: Int = 0 @@ -108,8 +111,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } }.invoke(this) @@ -148,8 +155,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } } else { @@ -192,8 +203,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } } else { @@ -238,8 +253,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } } else { @@ -285,8 +304,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } } else { @@ -334,8 +357,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } } else { @@ -448,8 +475,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } } else { @@ -493,8 +524,12 @@ actual open class KVServiceManager actual constructor(val serviceClass: ) } catch (e: Exception) { if (!(e is ServiceException)) LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName)) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, error = e.message ?: "Error", + exceptionType = e.javaClass.canonicalName + ) + ) } } } else { diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt new file mode 100644 index 00000000..1d37ae51 --- /dev/null +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.types + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider +import java.io.IOException +import java.math.BigDecimal + +actual typealias Decimal = BigDecimal + +class BigDecimalSerializer : JsonSerializer() { + @Throws(IOException::class) + override fun serialize(value: BigDecimal, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeNumber(value.toDouble()) + } +} + +class BigDecimalDeserializer : JsonDeserializer() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): BigDecimal? { + return p.doubleValue.toBigDecimal() + } +} diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index ec487e08..19bb8db4 100644 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -52,6 +52,7 @@ import kotlinx.coroutines.launch import org.slf4j.Logger import org.slf4j.LoggerFactory import pl.treksoft.kvision.types.* +import java.math.BigDecimal import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime @@ -88,11 +89,13 @@ actual open class KVServiceManager actual constructor(val serviceClass: module.addSerializer(LocalTime::class.java, LocalTimeSerializer()) module.addSerializer(OffsetDateTime::class.java, OffsetDateTimeSerializer()) module.addSerializer(OffsetTime::class.java, OffsetTimeSerializer()) + module.addSerializer(BigDecimal::class.java, BigDecimalSerializer()) module.addDeserializer(LocalDateTime::class.java, LocalDateTimeDeserializer()) module.addDeserializer(LocalDate::class.java, LocalDateDeserializer()) module.addDeserializer(LocalTime::class.java, LocalTimeDeserializer()) module.addDeserializer(OffsetDateTime::class.java, OffsetDateTimeDeserializer()) module.addDeserializer(OffsetTime::class.java, OffsetTimeDeserializer()) + module.addDeserializer(BigDecimal::class.java, BigDecimalDeserializer()) this.registerModule(module) } var counter: Int = 0 @@ -130,7 +133,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } @@ -170,7 +173,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } @@ -219,7 +222,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } @@ -270,7 +273,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } @@ -322,7 +325,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } @@ -376,7 +379,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } @@ -476,7 +479,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } @@ -526,7 +529,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) } diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt new file mode 100644 index 00000000..1d37ae51 --- /dev/null +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.types + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider +import java.io.IOException +import java.math.BigDecimal + +actual typealias Decimal = BigDecimal + +class BigDecimalSerializer : JsonSerializer() { + @Throws(IOException::class) + override fun serialize(value: BigDecimal, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeNumber(value.toDouble()) + } +} + +class BigDecimalDeserializer : JsonDeserializer() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): BigDecimal? { + return p.doubleValue.toBigDecimal() + } +} diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index fcd4b411..291a9481 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -43,6 +43,7 @@ import org.springframework.web.reactive.function.server.bodyValueAndAwait import org.springframework.web.reactive.function.server.json import org.springframework.web.reactive.socket.WebSocketSession import pl.treksoft.kvision.types.* +import java.math.BigDecimal import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime @@ -83,11 +84,13 @@ actual open class KVServiceManager actual constructor(val serviceClass: module.addSerializer(LocalTime::class.java, LocalTimeSerializer()) module.addSerializer(OffsetDateTime::class.java, OffsetDateTimeSerializer()) module.addSerializer(OffsetTime::class.java, OffsetTimeSerializer()) + module.addSerializer(BigDecimal::class.java, BigDecimalSerializer()) module.addDeserializer(LocalDateTime::class.java, LocalDateTimeDeserializer()) module.addDeserializer(LocalDate::class.java, LocalDateDeserializer()) module.addDeserializer(LocalTime::class.java, LocalTimeDeserializer()) module.addDeserializer(OffsetDateTime::class.java, OffsetDateTimeDeserializer()) module.addDeserializer(OffsetTime::class.java, OffsetTimeDeserializer()) + module.addDeserializer(BigDecimal::class.java, BigDecimalDeserializer()) this.registerModule(module) } var counter: Int = 0 @@ -152,7 +155,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: JsonRpcResponse( id = jsonRpcRequest.id, error = e.message ?: "Error", - exceptionType = e.javaClass.canonicalName + exceptionType = e.javaClass.canonicalName ) ) ) diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt new file mode 100644 index 00000000..1d37ae51 --- /dev/null +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/types/Decimal.kt @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.types + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider +import java.io.IOException +import java.math.BigDecimal + +actual typealias Decimal = BigDecimal + +class BigDecimalSerializer : JsonSerializer() { + @Throws(IOException::class) + override fun serialize(value: BigDecimal, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeNumber(value.toDouble()) + } +} + +class BigDecimalDeserializer : JsonDeserializer() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): BigDecimal? { + return p.doubleValue.toBigDecimal() + } +} -- cgit