-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCSP-35884: custom serializer example
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 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
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
7 changes: 7 additions & 0 deletions
7
source/examples/generated/KotlinXSerializationTest.snippet.kserializer-dataclass.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,7 @@ | ||
@Serializable | ||
data class PaintOrder( | ||
val color: String, | ||
val qty: Int, | ||
@Serializable(with = InstantAsBsonDateTime::class) | ||
val orderDate: Instant, | ||
) |
17 changes: 17 additions & 0 deletions
17
source/examples/generated/KotlinXSerializationTest.snippet.kserializer.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,17 @@ | ||
object InstantAsBsonDateTime : KSerializer<Instant> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("InstantAsBsonDateTime", PrimitiveKind.LONG) | ||
|
||
override fun serialize(encoder: Encoder, value: Instant) { | ||
when (encoder) { | ||
is BsonEncoder -> encoder.encodeBsonValue(BsonDateTime(value.toEpochMilliseconds())) | ||
else -> throw SerializationException("Instant is not supported by ${encoder::class}") | ||
} | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Instant { | ||
return when (decoder) { | ||
is BsonDecoder -> Instant.fromEpochMilliseconds(decoder.decodeBsonValue().asDateTime().value) | ||
else -> throw SerializationException("Instant is not supported by ${decoder::class}") | ||
} | ||
} | ||
} |
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