0

I'm trying to serialize a quickfix.Message instance using kryo serialization.

I'm using kryo version 3.0.3.

The code snippet is as follows:

    Message fixMessage = getFixMessage();

    Kryo kryo = new Kryo();
    kryo.register(Message.class);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Output output = new Output(outputStream);
    kryo.writeObject(output, fixMessage);
    output.flush();
    byte[] result = outputStream.toByteArray();
    output.close();

    ByteArrayInputStream inputStream = new ByteArrayInputStream(result);
    Input input = new Input(inputStream);

    Message fixMessage2 = kryo.readObject(input, Message.class);

and I keep getting this error:

com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): quickfix.StringField Serialization trace: fields (quickfix.Message) at com.esotericsoftware.kryo.Kryo$DefaultInstantiatorStrategy.newInstantiatorOf(Kryo.java:1272) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1078) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1087) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:570) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:546) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:790) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:161) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:39) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:708) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:125) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:551) ~[kryo-3.0.3.jar:?] at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:686) ~[kryo-3.0.3.jar:?]

if I register a kryo's JavaSerializer like this:

kryo.register(Message.class, new JavaSerializer());

I get this one:

com.esotericsoftware.kryo.KryoException: Error during Java deserialization. at com.esotericsoftware.kryo.serializers.JavaSerializer.read(JavaSerializer.java:65) ~[kryo-3.0.3.jar:?]

I start to think that this object simply can't be serialized using kryo...

Any thoughts on the issue will be much appreciated.

theDima
  • 746
  • 4
  • 12

2 Answers2

0

Your first error says "Class cannot be created (missing no-arg constructor)" but I think you got around that and it's the 2nd error that's the issue. Doesn't the 2nd error come from the line:

Message fixMessage2 = kryo.readObject(input, Message.class);

so it did serialise and get across the wire. Can you show more of the deserialization error stack?

rupweb
  • 3,052
  • 1
  • 30
  • 57
  • The next line in the error stack is: Message fixMessage2 = kryo.readObject(input, Message.class); – theDima May 08 '16 at 08:08
  • 1
    It's looking for a non args constructor on StringField, which is a class in use of Message. Both are classes of quickfix package, which I get from an external jar (not my code). I'm not supposed to (and can't) change it. – theDima May 08 '16 at 08:10
  • yes, but you fixed the non args constructor issue using that `JavaSerializer` and now the issue is doing the same on the deserialization step. Is there a way to use a `JavaDeserializer` on the `readObject` ? – rupweb May 09 '16 at 09:06
  • Another thing is to question why your stakeholders won't allow you to add a non args constructor to StringField, that does nothing, and then recompile QuickFix? Nice learning curve in that. – rupweb May 09 '16 at 09:08
  • Using JavaSerializer didn't help. I mentioned it as one thing I've tried and didn't work. I'm trying to find a more convenient way instead of extracting, fixing and recompiling third party common code. Also no-one guarantees that once this StringFiled issue will be solved their won't be any other issues with serializing this quickfix package. – theDima May 09 '16 at 09:32
  • Hang on, I thought the non args constructor error was in line 3 of your code above, but the javaserializer error was in line 12 ? – rupweb May 11 '16 at 16:03
0
Class cannot be created (missing no-arg constructor): quickfix.StringField 

You should have a no-arg constructor for all classes that are serialized. If is not your class and you cannot change it, then you should create your own serializer like in this link

iviorel
  • 312
  • 3
  • 10