model.CurrencyBSONCodec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baku Show documentation
Show all versions of baku Show documentation
helps you focus your REST API back-end on the business logic
package com.github.fluidsonic.baku
import org.bson.BsonReader
import org.bson.BsonWriter
import java.util.Currency
internal object CurrencyBSONCodec : AbstractBSONCodec() {
override fun BsonReader.decode(context: BSONCodingContext) =
readString().let { code ->
runCatching { Currency.getInstance(code) }.getOrNull() ?: throw BSONException("Invalid currency code '$code'")
}
override fun BsonWriter.encode(value: Currency, context: BSONCodingContext) {
writeString(value.currencyCode)
}
}