chill
chill copied to clipboard
Serializing ZonedDateTime with ZoneOffset.UTC results in invalid ZoneOffset
I'm using Kryo 2.21 on Java8 with Scala 2.10.6. Kryo comes packaged along with Apache Spark.
As part of an object I'm trying to serialize, I have a ZonedDateTime which I'm trying to serialize/deserialize. When I do so, the Z
part of the offset is replaced with null
.
Reproduce:
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.time.{ZoneOffset, ZonedDateTime}
import com.esotericsoftware.kryo.io.{Input, Output}
import com.twitter.chill.ScalaKryoInstantiator
object X {
val dt = ZonedDateTime.now(ZoneOffset.UTC)
val kryo = new ScalaKryoInstantiator().setRegistrationRequired(false).newKryo()
val stream = new ByteArrayOutputStream()
val output = new Output(stream)
kryo.writeObject(output, dt)
output.close()
val inputStream = new ByteArrayInputStream(stream.toByteArray)
val input = new Input(inputStream)
kryo.readObject(input, classOf[ZonedDateTime])
}
Yields:
res2: java.time.ZonedDateTime = 2016-03-17T14:39:48.117null
I assume that the problem here is that in that class the ZoneOffset is transient.
The only solution is to implement a serializer for this class or to change to a different class that represents the data in a way that works with kryo (a case class of primitives?)
Yes, that's what I ended up doing. I can post it here if it helps anyone.
yeah a PR to chill-java would be great (should be in java though. :/)
@YuvalItzchakov Could you post it/made pull request into chill with that one serializer? Thanks
Btw, kryo 4 already provides serializers for java.time classes.
@YuvalItzchakov any chance you can post the code?