-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement java.io.Serializable for some of the classes
Implement java.io.Serializable for * Instant * LocalDate * LocalTime * LocalDateTime * UtcOffset TimeZone is not `Serializable` because its behavior is system-dependent. We can make it `java.io.Serializable` later if there is demand. We are using string representations instead of relying on Java's entities being `java.io.Serializable` so that we have more freedom to change our implementation later. Fixes #143
- Loading branch information
1 parent
bc8adee
commit 94c5957
Showing
6 changed files
with
158 additions
and
6 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
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
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 @@ | ||
/* | ||
* Copyright 2019-2024 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime | ||
|
||
import java.io.* | ||
import kotlin.test.* | ||
|
||
class JvmSerializationTest { | ||
|
||
@Test | ||
fun serializeInstant() { | ||
roundTripSerialization(Instant.fromEpochSeconds(1234567890, 123456789)) | ||
} | ||
|
||
@Test | ||
fun serializeLocalTime() { | ||
roundTripSerialization(LocalTime(12, 34, 56, 789)) | ||
} | ||
|
||
@Test | ||
fun serializeLocalDateTime() { | ||
roundTripSerialization(LocalDateTime(2022, 1, 23, 21, 35, 53, 125_123_612)) | ||
} | ||
|
||
@Test | ||
fun serializeUtcOffset() { | ||
roundTripSerialization(UtcOffset(hours = 3, minutes = 30, seconds = 15)) | ||
} | ||
|
||
@Test | ||
fun serializeTimeZone() { | ||
assertFailsWith<NotSerializableException> { | ||
roundTripSerialization(TimeZone.of("Europe/Moscow")) | ||
} | ||
} | ||
|
||
private fun <T> roundTripSerialization(value: T) { | ||
val bos = ByteArrayOutputStream() | ||
val oos = ObjectOutputStream(bos) | ||
oos.writeObject(value) | ||
val serialized = bos.toByteArray() | ||
val bis = ByteArrayInputStream(serialized) | ||
ObjectInputStream(bis).use { ois -> | ||
assertEquals(value, ois.readObject()) | ||
} | ||
} | ||
} |