Possible issue with java.util.Date

809 views
Skip to first unread message

Schotrain

unread,
Feb 19, 2010, 9:27:38 AM2/19/10
to kryo-users
Hello,

I'm evaulating Kryo and it seems to be working great with one
exception. For some reason, whenever I serialize/deserialize a
java.util.Date, it always deserializes with the date set to now. See
the following sample code

Kryo k = new Kryo();
k.register(Date.class);

ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);

k.writeClassAndObject(buf, new Date(0));

buf.flip();

System.out.println(k.readClassAndObject(buf));


This should print out
Wed Dec 31 19:00:00 EST 1969

But instead it prints out
Fri Feb 19 09:27:26 EST 2010

Nate

unread,
Feb 19, 2010, 10:01:58 AM2/19/10
to kryo-users
Hi Schotrain,

I can see how what is happening is not readily apparent. You are
registering java.util.Date.class with a FieldSerializer.
FieldSerializer looks at all non-static, non-transient, public and
private instance fields and serializes those. All of Date's instance
fields happen to be transient! So FieldSerializer has nothing to store
for the date. When deserialization happens, a new Date is created.
Since there is no further data, nothing else is done and you always
get "now".

This is a bit of a gotcha. Serializing a class without fields is
reasonably common (eg, sending some messages in a networked app), so
it doesn't make sense to throw up a big warning. The only way you'd
know it was happening is to turn logging to TRACE using
"Log.set(Log.LEVEL_TRACE);", then you'd see:

00:03 TRACE: [kryo] Wrote class 18: java.util.Date
00:03 TRACE: [kryo] Wrote object: Wed Dec 31 16:00:00 PST 1969

Normally, with FieldSerializer, you would see entries like this:

00:03 TRACE: [kryo] Wrote class 18:
com.esotericsoftware.kryo.serialize.SerializerTest$TestClass
00:03 TRACE: [kryo] Writing field: abc
(com.esotericsoftware.kryo.serialize.SerializerTest$TestClass)
00:03 TRACE: [kryo] Wrote float: 1.2
// More fields written.
00:03 TRACE: [kryo] Wrote object:
com.esotericsoftware.kryo.serialize.SerializerTest$TestClass@54c4ad

At any rate, to fix your problem you can use the DateSerializer class
that comes with Kryo:

kryo.register(Date.class, new DateSerializer());

It might be worth it to take a look and see how it works:
http://code.google.com/p/kryo/source/browse/trunk/src/com/esotericsoftware/kryo/serialize/DateSerializer.java
When writing your own serializers, it can bit tidier to use the
SimpleSerializer class.

Finally, you may wonder why DateSerializer is included but not
registered by default. Each time you send an object, Kryo has to write
the length, the type of object, then the data for the object. When a
type is registered it is given an int ID. In the log message "Wrote
class X: Y", X is the type ID. The type ID is serialized with
IntSerializer with optimizePositive=true:
http://kryo.googlecode.com/svn/api/com/esotericsoftware/kryo/serialize/IntSerializer.html#IntSerializer(boolean)
As you can see from the javadocs, if the type ID is <= 127 then it
only requires one byte to send. Because of this Kryo registers only
the minimal number of types by default (17 total: 8 primitive types, 8
primitive type wrappers, and String).

-Nate

Schotrain

unread,
Feb 19, 2010, 1:30:42 PM2/19/10
to kryo-users
Hi Nate,

Thanks very much for the reply. I completely missed the DateSerializer
class when I looked through the docs.

> It might be worth it to take a look and see how it works:http://code.google.com/p/kryo/source/browse/trunk/src/com/esotericsof...


> When writing your own serializers, it can bit tidier to use the
> SimpleSerializer class.
>
> Finally, you may wonder why DateSerializer is included but not
> registered by default. Each time you send an object, Kryo has to write
> the length, the type of object, then the data for the object. When a
> type is registered it is given an int ID. In the log message "Wrote
> class X: Y", X is the type ID. The type ID is serialized with

> IntSerializer with optimizePositive=true:http://kryo.googlecode.com/svn/api/com/esotericsoftware/kryo/serializ...)

Reply all
Reply to author
Forward
0 new messages