How to specify interface type for serialization?

1,158 views
Skip to first unread message

Deep

unread,
Jun 14, 2011, 7:08:43 PM6/14/11
to kryo-users
I am new to Kryo.
I am getting error (can't serialize due to missing default
constructor) when try to serialize an object of class A using the
interface type B (A implements B abd both A and B are registered in
Kryo).
How to get this working?
Thnaks in advance for any help.

Nate

unread,
Jun 14, 2011, 7:15:07 PM6/14/11
to kryo-...@googlegroups.com
Deserialization needs to know the concrete class, so it knows what type to actually instantiate. I assume you're doing something like...

interface B {}
class A implements B {}
B b =new A();
kryo.writeObjectData(buffer, b);
...
B b2 = kryo.readObjectData(buffer, B.class);

This doesn't work because readObjectData cannot instantiate the type B, which is an interface. If you want to read back an object hidden behind an interface where you don't know the concrete type, you need to have the concrete type serialized:

kryo.writeClassAndObject(buffer, b);
...
B b2 = (B)kryo.readClassAndObject(buffer);

Here writeClassAndObject writes the concrete type for the object referenced by the "b" variable. readClassAndObject reads the concrete type and uses it to create an object.

-Nate



--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users

Deep

unread,
Jun 14, 2011, 7:44:03 PM6/14/11
to kryo-users
Thank you for your reply. Your assumption is right, I will try that.

I have few more questions (below) -

1) I was also trying to use Kryo to serialize and de-serialize some
JAXB generated types with no joy.
First of all I had to register "javax.xml.bind.JAXBElement" and even
"java.lang.Class.class" to serialize the custom generated types.
But it failed in the server during de-serialize with no default
constructor for javax.xml.bind.JAXBElement error.
I assume using writeClassAndObject/readObjectData would resolve this
for me ?
Is there any performance overhead in using these methods over the
readObject/writeObjectData?

2) I am doing my test using HTTP protocol to serialize the objects and
send to server (using Kryo api).
Is there any advantage in using Kryonet instead?
If I run a Kryonet server instead, will it scale with increased number
of concurrent connections?

Thank you

Nate

unread,
Jun 14, 2011, 8:06:16 PM6/14/11
to kryo-...@googlegroups.com
Replies inline:

On Tue, Jun 14, 2011 at 4:44 PM, Deep <she...@gmail.com> wrote:
Thank you for your reply. Your assumption is right, I will try that.

I have few more questions (below) -

1) I was also trying to use Kryo to serialize and de-serialize some
JAXB generated types with no joy.
First of all I had to register "javax.xml.bind.JAXBElement" and even
"java.lang.Class.class" to serialize the custom generated types.
But it failed in the server during de-serialize with no default
constructor for javax.xml.bind.JAXBElement error.
I assume using writeClassAndObject/readObjectData would resolve this
for me ?

No, if JAXBElement is a class (not an interface) without a default constructor, you will need to provide a serializer when you register the JAXBElement class that knows how to create an instance of this class.
 
Is there any performance overhead in using these methods over the
readObject/writeObjectData?

write/readObject writes 1 byte to denote null. write/readObjectData does not. write/readClassAndObject writes the int class ID (assuming the types are registered), which is usually a single byte. So, not much difference between the methods.
 

2) I am doing my test using HTTP protocol to serialize the objects and
send to server (using Kryo api).
Is there any advantage in using Kryonet instead?

KryoNet may be lower latency. It provides a persistent TCP connection and optionally a UDP connection.
 
If I run a Kryonet server instead, will it scale with increased number
of concurrent connections?

It should scale to hundreds and probably 1000+ connections.

-Nate


Martin Grotzke

unread,
Jun 16, 2011, 3:33:40 PM6/16/11
to kryo-...@googlegroups.com
Hi,

I just want to mention kryo-serializers here
(https://github.com/magro/kryo-serializers) which provides several
custom serializers, so it would be a good place for a
JAXBElementSerializer. It does also provide the
KryoReflectionFactorySupport, which is a Kryo specialization that uses
Sun's ReflectionFactory to create new instances for classes without a
default constructor (it's tied to a sun/oracle/openjdk jvm). Perhaps
this is also useful for you.

Cheers,
Martin

--
Martin Grotzke
http://twitter.com/martin_grotzke

signature.asc

Nate

unread,
Jun 16, 2011, 3:52:17 PM6/16/11
to kryo-...@googlegroups.com
Hi Martin,

Good point. I have added a link to your project from the Kryo project, and also added a paragraph to the documentation under Registration.

-Nate

Martin Grotzke

unread,
Jun 16, 2011, 7:19:34 PM6/16/11
to kryo-...@googlegroups.com
Cool!

Cheers,
Martin


On 06/16/2011 09:52 PM, Nate wrote:
> Hi Martin,
>
> Good point. I have added a link to your project from the Kryo project,
> and also added a paragraph to the documentation under Registration.
>
> -Nate
>
>
> On Thu, Jun 16, 2011 at 12:33 PM, Martin Grotzke

> <martin....@googlemail.com <mailto:martin....@googlemail.com>>

signature.asc
Reply all
Reply to author
Forward
0 new messages