Question

Edit: Using Kryo 1.04

I'm right now serializing a User class that contains a java.sql.Timestamp field in Scala. For some reason, Kryo can't find a zero-arg constructor and throws an error:

Caused by: com.esotericsoftware.kryo.SerializationException: Class cannot be created (missing no-arg constructor): java.sql.Timestamp
Serialization trace:
created (com.threetierlogic.AccountService.models.User)
        at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:688)
        at com.esotericsoftware.kryo.Serializer.newInstance(Serializer.java:75)
        at com.esotericsoftware.kryo.serialize.FieldSerializer.readObjectData(FieldSerializer.java:200)
        at com.esotericsoftware.kryo.serialize.FieldSerializer.readObjectData(FieldSerializer.java:220)
        at com.esotericsoftware.kryo.serialize.FieldSerializer.readObjectData(FieldSerializer.java:200)
        at com.esotericsoftware.kryo.Serializer.readObject(Serializer.java:61)
        at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:589)
        ... 84 more
Caused by: java.lang.InstantiationException: java.sql.Timestamp
        at java.lang.Class.newInstance0(Class.java:340)
        at java.lang.Class.newInstance(Class.java:308)
        at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:676)
        ... 90 more

This is part of a converter class to convert domain objects for Riak. Here's my converter class:

/**
 * Kryo converter for passing domain objects into Riak
 */
class UserConverter(val bucket: String) extends Converter[User] {

  def fromDomain(domainObject: User, vclock: VClock): IRiakObject = {
    val key = domainObject.guid

    if(key == null) throw new NoKeySpecifedException(domainObject)

    val kryo = new Kryo()
    kryo.register(classOf[User])
    kryo.register(classOf[Timestamp])

    val ob = new ObjectBuffer(kryo)
    val value = ob.writeObject(domainObject)

    RiakObjectBuilder.newBuilder(bucket, key)
        .withValue(value)
        .withVClock(vclock)
        .withContentType(Constants.CTYPE_OCTET_STREAM)
        .build()
  }

  def toDomain(riakObject: IRiakObject): User = {
    if(riakObject == null) null

    val kryo = new Kryo()
    kryo.register(classOf[User])
    kryo.register(classOf[Timestamp])
    val ob = new ObjectBuffer(kryo)

    ob.readObject(riakObject.getValue(), classOf[User])
  }
}

Do I need to extend Timestamp and create a zero argument constructor? Or is there a better workaround?

If I need to upgrade to 2.20, what's the replacement for ObjectBuffer without writing to a file?

Was it helpful?

Solution 2

You can do something like this :

  class KryoSO {
    import com.esotericsoftware.kryo.KryoSerializable
    import de.javakaffee.kryoserializers.KryoReflectionFactorySupport
    import com.esotericsoftware.kryo.Kryo
    import com.esotericsoftware.kryo.Serializer
    import java.io.{ InputStream, OutputStream }
    import com.esotericsoftware.kryo.io.{ Output, Input }
    import java.sql.Timestamp

    object TimestampSerializer extends Serializer[Timestamp] {
      override def write(kryo: Kryo, output: Output, t: Timestamp): Unit = {
        output.writeLong(t.getTime(), true);
      }

      override def read(kryo: Kryo, input: Input, t: Class[Timestamp]): Timestamp = {
        new Timestamp(input.readLong(true));
      }

      override def copy(kryo: Kryo, original: Timestamp): Timestamp = {
        new Timestamp(original.getTime());
      }
    }
    val kryo: Kryo = new KryoReflectionFactorySupport
    kryo.addDefaultSerializer(classOf[Timestamp], TimestampSerializer)

    def serialize(o: Any, os: OutputStream) = {
      val output = new Output(os);
      this.kryo.writeClassAndObject(output, o);
      output.flush();
    }

    def deserialize(is: InputStream): Any = {
      kryo.readClassAndObject(new Input(is));
    }

  }
  val k = new KryoSO
  val b = new java.io.ByteArrayOutputStream
  val timestamp = new java.sql.Timestamp(System.currentTimeMillis())

  k.serialize(timestamp, b)

  val result = k.deserialize(new java.io.ByteArrayInputStream(b.toByteArray()))

  println(timestamp)
  println(result.getClass)
  println(result.isInstanceOf[java.sql.Timestamp])
  println(timestamp == result)

Result :

2013-02-07 10:59:19.482
class java.sql.Timestamp
true
true

OTHER TIPS

A quick look at the Kryo home page suggests that in the absence of a zero-arg constructor, you can create what Kryo calls an "Instantion Strategy" to handle that class. Look in the "Object Creation" section.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top