Skip to content

Commit

Permalink
Make Transaction serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 26, 2023
1 parent d3d709a commit edd5484
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.madhead.tyzenhaus.entity.serializers

import java.time.Instant
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* [KSerializer] for [Instant]s.
*/
class InstantSerializer : KSerializer<Instant> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Instant", PrimitiveKind.INT)
override fun serialize(encoder: Encoder, value: Instant) = encoder.encodeLong(value.toEpochMilli())
override fun deserialize(decoder: Decoder): Instant = Instant.ofEpochMilli(decoder.decodeLong())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package me.madhead.tyzenhaus.entity.transaction

import java.math.BigDecimal
import java.time.Instant
import kotlinx.serialization.Serializable
import me.madhead.tyzenhaus.entity.serializers.BigDecimalSerializer
import me.madhead.tyzenhaus.entity.serializers.InstantSerializer

/**
* Shared expense.
*/
@Serializable
data class Transaction(
val id: Long?,
val groupId: Long,
val payer: Long,
val recipients: Set<Long>,
@Serializable(BigDecimalSerializer::class)
val amount: BigDecimal,
val currency: String,
val title: String? = null,
@Serializable(InstantSerializer::class)
val timestamp: Instant = Instant.now(),
)

0 comments on commit edd5484

Please sign in to comment.