chill icon indicating copy to clipboard operation
chill copied to clipboard

Serializing ZonedDateTime with ZoneOffset.UTC results in invalid ZoneOffset

Open YuvalItzchakov opened this issue 8 years ago • 6 comments

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

YuvalItzchakov avatar Mar 17 '16 14:03 YuvalItzchakov

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?)

johnynek avatar Mar 23 '16 17:03 johnynek

Yes, that's what I ended up doing. I can post it here if it helps anyone.

YuvalItzchakov avatar Mar 23 '16 17:03 YuvalItzchakov

yeah a PR to chill-java would be great (should be in java though. :/)

johnynek avatar Mar 23 '16 18:03 johnynek

@YuvalItzchakov Could you post it/made pull request into chill with that one serializer? Thanks

modelga avatar Dec 14 '16 11:12 modelga

Btw, kryo 4 already provides serializers for java.time classes.

magro avatar Dec 17 '16 00:12 magro

@YuvalItzchakov any chance you can post the code?

johnynek avatar Jan 03 '17 23:01 johnynek