String serialisation producing wrong output

72 views
Skip to first unread message

Oliver Reid

unread,
May 19, 2022, 11:20:56 PM5/19/22
to kryo-users
Hi all,

I am attempting to use Kryo to serialise and deserialise objects. This is working for complex types (like `Optional` and `Option`) but not simpler types like `String`. The issue looks to be on the serialisation side (deserialisation looks to be working fine). The serialisation code looks like:

private String serialize(T input) {
  Output output = new Output(2, -1);
  kryo.writeObjectOrNull(output, input, type);
  output.close();
  final var bytes = output.toBytes();
  return new String(bytes);
}


where `type` is `Class<T>` (e.g. if `T` is `String` then `type` would be `String.class`). If I compare `input.getBytes()` to `bytes` I can see that all of the bytes in the `byte[]` are correct other than the last byte. This would result in an input of something like `test144` and a serialised output of `test14�`.

I stepped through the code using an example of type `String` and can see that the output value is actually correct until this line is executed (looks to be doing some sort of Bitwise OR for whatever reason).

Unsure if this is something I am doing wrong or a bug in the library.

Thanks for any help!

Thomas Heigl

unread,
May 20, 2022, 6:50:32 AM5/20/22
to kryo-...@googlegroups.com
Hi Oliver,

I'm not sure what you are trying to achieve. Kryo doesn't guarantee that a String is serialized exactly as-is. It only guarantees that it can serialize and then deserialize the String.

If you want to read the string back, you have to use readObjectOrNull.

Best,

Thomas

--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kryo-users/70f1d524-b346-4f65-9b3e-f1474c1dffefn%40googlegroups.com.

Oliver Reid

unread,
May 23, 2022, 12:26:05 AM5/23/22
to kryo-users
Hey Thomas,

Sorry if my example was confusing or not clear! Using `readObjectOrNull` results in the `test14�` String. Specifically, the deserialisation looks like:

private T deserialize(Response response) {
    if(response == null) {
        return null;
    }
    final var input = new Input(response.toBytes());
    input.close();
    return kryo.readObjectOrNull(input, type);
}


`Response` is the type being returned from the location I am storing the byte array. I suspect this coupled with `response.toBytes()` is somehow messing up Kryo?

Thanks,
Oliver

Thomas Heigl

unread,
May 25, 2022, 9:50:15 AM5/25/22
to kryo-...@googlegroups.com
Hi Oliver,

This is really hard to diagnose without knowing the exact implementation of Response.toBytes() and your complete roundtrip code.

I would suggest that you construct a simpler test-case that uses only Kryo and byte arrays without any external dependencies. If you can provide such a test-case, please create a ticket on Github, so I can take a look.

Best,

Thomas

Reply all
Reply to author
Forward
0 new messages