Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
fix: use TimestampSerializer for timestamp data serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
AH-dark committed Dec 9, 2023
1 parent 2489d2c commit 616f030
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ahdark.code.entities.github.webhook

import com.ahdark.code.serializer.TimestampSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -12,11 +13,14 @@ data class Commit(

val distinct: Boolean,
val message: String,
val timestamp: String,

@Serializable(with = TimestampSerializer::class)
val timestamp: Long,

val url: String,
val author: Author,
val committer: Author,
val added: List<String>,
val removed: List<String>,
val modified: List<String>
val modified: List<String>,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ahdark.code.entities.github.webhook

import com.ahdark.code.serializer.TimestampSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down Expand Up @@ -134,12 +135,15 @@ data class Repository(
@SerialName("deployments_url")
val deploymentsURL: String,

@Serializable(with = TimestampSerializer::class)
@SerialName("created_at")
val createdAt: Long,

@Serializable(with = TimestampSerializer::class)
@SerialName("updated_at")
val updatedAt: String,
val updatedAt: Long,

@Serializable(with = TimestampSerializer::class)
@SerialName("pushed_at")
val pushedAt: Long,

Expand Down
52 changes: 52 additions & 0 deletions src/main/kotlin/com/ahdark/code/serializer/TimestampSerializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ahdark.code.serializer

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.Serializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.long
import kotlinx.serialization.json.longOrNull
import java.text.SimpleDateFormat
import java.util.*

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = Long::class)
object TimestampSerializer : KSerializer<Long> {
private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US).apply {
timeZone = TimeZone.getTimeZone("UTC")
}

override fun serialize(encoder: Encoder, value: Long) {
encoder.encodeLong(value)
}

override fun deserialize(decoder: Decoder): Long {
if (decoder !is JsonDecoder) {
throw SerializationException("This serializer can only be used with JSON")
}

val element = decoder.decodeJsonElement()
return when {
element is JsonPrimitive && element.longOrNull != null -> element.long
element is JsonPrimitive -> parseDate(element.content)
else -> throw SerializationException("Invalid JSON element found for TimestampSerializer")
}
}

/**
* Parses a date string in the format "yyyy-MM-dd'T'HH:mm:ss'Z'".
*
* @throws SerializationException if the date string is not in the correct format.
*/
private fun parseDate(dateString: String): Long {
return try {
dateFormat.parse(dateString)?.time ?: throw SerializationException("Invalid date format")
} catch (e: Exception) {
throw SerializationException("Error parsing date string: $dateString", e)
}
}
}

0 comments on commit 616f030

Please sign in to comment.