Money
Modern JVM money handling uses javax.money
backed by Moneta.
val amount = Money.of("123.45", "USD")Installation
Section titled “Installation”Install kairo-money.
You don’t need to install javax.money or Moneta separately —
they’re included by default.
dependencies { implementation("software.airborne.kairo:kairo-money")}Refer to the JavaMoney specification.
Serialization
Section titled “Serialization”For serialization to work, add MoneyModule() to your Json instance
and mark Money instances as @Contextual.
val json: KairoJson = KairoJson { addModule(MoneyModule()) }json.serialize(Money.of("123.45", "USD"))// => {"amount":123.45,"currency":"USD"}
json.deserialize<Money>("""{"amount":123.45,"currency":"USD"}""")// => Money.of("123.45", "USD")Custom serializer
Section titled “Custom serializer”If you want to customize Money serialization,
set moneyFormat when you instantiate MoneyModule.
object CustomMoneyFormat : MoneyFormat() { override val serializer: JsonSerializer<Money> = object : StdSerializer<Money>(Money::class.java) { override fun serialize( value: Money, gen: JsonGenerator, provider: SerializerProvider, ) { // Your implementation. } }
override val deserializer: JsonDeserializer<Money> = object : StdDeserializer<Money>(Money::class.java) { override fun deserialize( p: JsonParser, ctxt: DeserializationContext, ): Money { // Your implementation. } }}
val json: KairoJson = KairoJson { addModule( MoneyModule { moneyFormat = CustomMoneyFormat } ) }