-
Notifications
You must be signed in to change notification settings - Fork 109
Implement java.io.Serializable for some of the classes #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ec1061
Implement java.io.Serializable for some of the classes
dkhalanskyjb 3b94059
Fix compilation after a merge
dkhalanskyjb a29c32a
val serialVersionUID -> const val
dkhalanskyjb 17bac4b
Use externalizable replacement for serializable entities
ilya-g ca7d2d1
Rename serialization surrogate and annotate with PublishedAPI
ilya-g a67b418
Remove some leftovers
dkhalanskyjb 8c767ea
Do not implement Instant: java.io.Serializable
dkhalanskyjb 1776d8c
Add the API dump
dkhalanskyjb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
@file:Suppress("PackageDirectoryMismatch") | ||
package kotlinx.datetime | ||
|
||
import java.io.* | ||
|
||
@PublishedApi // changing the class name would result in serialization incompatibility | ||
internal class Ser(private var typeTag: Int, private var value: Any?) : Externalizable { | ||
constructor() : this(0, null) | ||
|
||
override fun writeExternal(out: ObjectOutput) { | ||
out.writeByte(typeTag) | ||
val value = this.value | ||
when (typeTag) { | ||
DATE_TAG -> { | ||
value as LocalDate | ||
out.writeLong(value.value.toEpochDay()) | ||
} | ||
TIME_TAG -> { | ||
value as LocalTime | ||
out.writeLong(value.toNanosecondOfDay()) | ||
} | ||
DATE_TIME_TAG -> { | ||
value as LocalDateTime | ||
out.writeLong(value.date.value.toEpochDay()) | ||
out.writeLong(value.time.toNanosecondOfDay()) | ||
} | ||
UTC_OFFSET_TAG -> { | ||
value as UtcOffset | ||
out.writeInt(value.totalSeconds) | ||
} | ||
else -> throw IllegalStateException("Unknown type tag: $typeTag for value: $value") | ||
} | ||
} | ||
|
||
override fun readExternal(`in`: ObjectInput) { | ||
typeTag = `in`.readByte().toInt() | ||
value = when (typeTag) { | ||
DATE_TAG -> | ||
LocalDate(java.time.LocalDate.ofEpochDay(`in`.readLong())) | ||
TIME_TAG -> | ||
LocalTime.fromNanosecondOfDay(`in`.readLong()) | ||
DATE_TIME_TAG -> | ||
LocalDateTime( | ||
LocalDate(java.time.LocalDate.ofEpochDay(`in`.readLong())), | ||
LocalTime.fromNanosecondOfDay(`in`.readLong()) | ||
) | ||
UTC_OFFSET_TAG -> | ||
UtcOffset(seconds = `in`.readInt()) | ||
else -> throw IOException("Unknown type tag: $typeTag") | ||
} | ||
} | ||
|
||
private fun readResolve(): Any = value!! | ||
|
||
companion object { | ||
private const val serialVersionUID: Long = 0L | ||
const val DATE_TAG = 2 | ||
const val TIME_TAG = 3 | ||
const val DATE_TIME_TAG = 4 | ||
const val UTC_OFFSET_TAG = 10 | ||
} | ||
} |
This file contains hidden or 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,87 @@ | ||
/* | ||
* 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 serializeLocalTime() { | ||
roundTripSerialization(LocalTime(12, 34, 56, 789)) | ||
roundTripSerialization(LocalTime.MIN) | ||
roundTripSerialization(LocalTime.MAX) | ||
expectedDeserialization(LocalTime(23, 59, 15, 995_003_220), "090300004e8a52680954") | ||
} | ||
|
||
@Test | ||
fun serializeLocalDate() { | ||
roundTripSerialization(LocalDate(2022, 1, 23)) | ||
roundTripSerialization(LocalDate.MIN) | ||
roundTripSerialization(LocalDate.MAX) | ||
expectedDeserialization(LocalDate(2024, 8, 12), "09020000000000004deb") | ||
} | ||
|
||
@Test | ||
fun serializeLocalDateTime() { | ||
roundTripSerialization(LocalDateTime(2022, 1, 23, 21, 35, 53, 125_123_612)) | ||
roundTripSerialization(LocalDateTime.MIN) | ||
roundTripSerialization(LocalDateTime.MAX) | ||
expectedDeserialization(LocalDateTime(2024, 8, 12, 10, 15, 0, 997_665_331), "11040000000000004deb0000218faedb9233") | ||
} | ||
|
||
@Test | ||
fun serializeUtcOffset() { | ||
roundTripSerialization(UtcOffset(hours = 3, minutes = 30, seconds = 15)) | ||
roundTripSerialization(UtcOffset(java.time.ZoneOffset.MIN)) | ||
roundTripSerialization(UtcOffset(java.time.ZoneOffset.MAX)) | ||
expectedDeserialization(UtcOffset.parse("-04:15:30"), "050affffc41e") | ||
} | ||
|
||
@Test | ||
fun serializeTimeZone() { | ||
assertFailsWith<NotSerializableException> { | ||
roundTripSerialization(TimeZone.of("Europe/Moscow")) | ||
} | ||
} | ||
|
||
private fun serialize(value: Any?): ByteArray { | ||
val bos = ByteArrayOutputStream() | ||
val oos = ObjectOutputStream(bos) | ||
oos.writeObject(value) | ||
return bos.toByteArray() | ||
} | ||
|
||
private fun deserialize(serialized: ByteArray): Any? { | ||
val bis = ByteArrayInputStream(serialized) | ||
ObjectInputStream(bis).use { ois -> | ||
return ois.readObject() | ||
} | ||
} | ||
|
||
private fun <T> roundTripSerialization(value: T) { | ||
val serialized = serialize(value) | ||
val deserialized = deserialize(serialized) | ||
assertEquals(value, deserialized) | ||
} | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
private fun expectedDeserialization(expected: Any, blockData: String) { | ||
val serialized = "aced0005737200146b6f746c696e782e6461746574696d652e53657200000000000000000c0000787077${blockData}78" | ||
val hexFormat = HexFormat { bytes.byteSeparator = "" } | ||
|
||
try { | ||
val deserialized = deserialize(serialized.hexToByteArray(hexFormat)) | ||
if (expected != deserialized) { | ||
assertEquals(expected, deserialized, "Golden serial form: $serialized\nActual serial form: ${serialize(expected).toHexString(hexFormat)}") | ||
} | ||
} catch (e: Throwable) { | ||
fail("Failed to deserialize $serialized\nActual serial form: ${serialize(expected).toHexString(hexFormat)}", e) | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
java.time
classes also have areadObject
method that always throws an exception with a comment that this is about defending against malicious streams. I'm not very familiar with Java's serialization, so I don't know if this is needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this post about serialization via
writeReplace
/readResolve
. This section is about the always throwingreadObject
method. Without it, a hand crafted byte stream could cause deserialization of e.g.LocalDate
without going throughSer.readResolve
. This could violate invariants of the classes. I think it could for example sneak innull
to the non-nullable propertyLocalDate.value
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Yes,
deserialize("aced00057372001a6b6f746c696e782e6461746574696d652e4c6f63616c44617465618443f17dae33e70200014c00097472756556616c75657400154c6a6176612f74696d652f4c6f63616c446174653b787070" .hexToByteArray(HexFormat { bytes.byteSeparator = "" }))
does indeed create aLocalDate
whosevalue
isnull
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lukellmann, would you like to make a PR that fixes (and tests!) this problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, but it will probably take a few days
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on it but it will take a bit (I'm going through the spec).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @lukellmann! Do you have updates regarding this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've started implementing it, still refining the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#522
Sorry that it took so long, I was initially trying to do more than what's in that PR (testing that classes stick to a certain pattern when implementing
java.io.Serializable
, see the other branches in my fork). But I've now shrunk it down to the most important fixes and might open additional PRs later.