-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ca50f1
commit cdf4948
Showing
8 changed files
with
151 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...nce/src/main/kotlin/net/adoptium/api/v3/dataSources/persitence/mongo/codecs/DateCodecs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package net.adoptium.api.v3.dataSources.persitence.mongo.codecs | ||
|
||
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.JsonNode | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import com.fasterxml.jackson.databind.node.LongNode | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import net.adoptium.api.v3.TimeSource | ||
import java.time.Instant | ||
import java.time.format.DateTimeFormatter | ||
import java.util.* | ||
|
||
object DateCodecs { | ||
class DateSerializer : JsonSerializer<Date>() { | ||
override fun serialize(p0: Date?, p1: JsonGenerator?, p2: SerializerProvider?) { | ||
p1?.writeStartObject(); | ||
p1?.writeStringField( | ||
"\$date", | ||
p0?.toInstant()?.atZone(TimeSource.ZONE)?.format(DateTimeFormatter.ISO_INSTANT) | ||
); | ||
p1?.writeEndObject(); | ||
} | ||
} | ||
|
||
class DateDeserializer : JsonDeserializer<Date>() { | ||
override fun deserialize(jsonParser: JsonParser?, context: DeserializationContext?): Date { | ||
when (val value = jsonParser?.readValueAsTree<JsonNode>()) { | ||
is ObjectNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.get("\$date").asText()) | ||
return Date(Instant.from(datetime).toEpochMilli()) | ||
} | ||
|
||
is TextNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.asText()) | ||
return Date(Instant.from(datetime).toEpochMilli()) | ||
} | ||
|
||
is LongNode -> { | ||
return Date(value.asLong()) | ||
} | ||
} | ||
|
||
throw IllegalArgumentException("Could not parse ZonedDateTime") | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ain/kotlin/net/adoptium/api/v3/dataSources/persitence/mongo/codecs/ZonedDateTimeCodecs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package net.adoptium.api.v3.dataSources.persitence.mongo.codecs | ||
|
||
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.JsonNode | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import com.fasterxml.jackson.databind.node.LongNode | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import net.adoptium.api.v3.TimeSource | ||
import java.time.Instant | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
object ZonedDateTimeCodecs { | ||
class ZonedDateTimeDeserializer : JsonDeserializer<ZonedDateTime>() { | ||
override fun deserialize(jsonParser: JsonParser?, context: DeserializationContext?): ZonedDateTime { | ||
when (val value = jsonParser?.readValueAsTree<JsonNode>()) { | ||
is ObjectNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.get("\$date").asText()) | ||
return Instant.from(datetime).atZone(TimeSource.ZONE) | ||
} | ||
|
||
is TextNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.asText()) | ||
return Instant.from(datetime).atZone(TimeSource.ZONE) | ||
} | ||
|
||
is LongNode -> { | ||
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(value.asLong()), TimeSource.ZONE) | ||
} | ||
} | ||
|
||
throw IllegalArgumentException("Could not parse ZonedDateTime") | ||
} | ||
|
||
} | ||
|
||
class ZonedDateTimeSerializer : JsonSerializer<ZonedDateTime>() { | ||
override fun serialize(p0: ZonedDateTime?, p1: JsonGenerator?, p2: SerializerProvider?) { | ||
p1?.writeStartObject(); | ||
p1?.writeStringField("\$date", p0?.format(DateTimeFormatter.ISO_INSTANT)); | ||
p1?.writeEndObject(); | ||
} | ||
|
||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
...tlin/net/adoptium/api/v3/dataSources/persitence/mongo/codecs/ZonedDateTimeDeserializer.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters