msgpack-java icon indicating copy to clipboard operation
msgpack-java copied to clipboard

Got "java.lang.String cannot be cast to [B" when using messagePack.read();

Open wennicky opened this issue 7 years ago • 1 comments

Hi,

I use messagePack.write(myCalss) and stored it to Redis. Then I opened a terminal and by using Redis command "hget" I got a what I just stored in Redis. This is what I've got: "\xac\xed\x00\x05ur\x00\x02[B\xac\xf3\x17\xf8\x06\bT\xe0\x02\x00\x00xp\x00\x00\x00A\x98\xaa8621161813\x00\xda\x00,mKHuEReeu7RYSxcdWFRMd+IE7FaskrViwxXdMdCUEKQ=\xc0\xc0\xc0\x00\x00" Now I want to deserialization this by messagePack.read() to convert this to MyClass. This is my codes:

Object o = "\xac\xed\x00\x05ur\x00\x02[B\xac\xf3\x17\xf8\x06\bT\xe0\x02\x00\x00xp\x00\x00\x00A\x98\xaa8621161813\x00\xda\x00,mKHuEReeu7RYSxcdWFRMd+IE7FaskrViwxXdMdCUEKQ=\xc0\xc0\xc0\x00\x00"; MessagePack messagePack = new MessagePack(); MyClass myClass= messagePack.read((byte[]) o, MyClass.class); System.out.println(myClass.toString());

Then I got an exception on this line " MyClass myClass= messagePack.read((byte[]) o, MyClass.class);". It said "java.lang.ClassCastException: java.lang.String cannot be cast to [B"

I copied the string "\xac...\x00" from the terminal because I have to write the query in Redis and convert this string to MyClass in codes.

Can anyone help me? Thank you so much!

wennicky avatar Apr 23 '18 06:04 wennicky

@wennicky Are you using msgpack-java 0.6 or earlier? If so, they're already deprecated and we strongly recommend latest 0.8 although it has incompatible APIs with 0.6.

komamitsu avatar May 17 '18 06:05 komamitsu