3

use messagepack on android, the can serialize/deserialize a class, but not absolutely right .

the simple test class:

    @Message
public class Account {

    public String Code;
    public int Sequence;
    public float Lot;

    public String toString(){
        return "Seq:"+Sequence;
    }
}

The test code :

MessagePack msgpack = new MessagePack();
    msgpack.register(Account.class);
    try {
    Account a = new Account();
    a.Code ="name";
    a.Sequence = 105;
    a.Lot = (float)1.05;
    byte[] b = msgpack.write(a);
    //byte[] c = MessagePack.pack(a);
    Account aa = msgpack.read(b, Account.class );
    System.out.println(new String(b));
    System.out.println("test00: aa.Lot "+aa.Lot);
    }catch(IOException e){
        e.printStackTrace();
    }

after run "byte[] b = msgpack.write(a);" on android the output byte array b[] is incorrect (compare to java)

[-109, -92, 110, 97, 109, 101, 105, -54, 63, -122, 102, 102]   on java

[-109, -92, 110, 97, 109, 101, -54, 63, -122, 102, 102, 105]   on android

I have review Using MessagePack with Android

Community
  • 1
  • 1

2 Answers2

1

MessagePack is not, intrinsically, an object serializer. It is meant to serialize well-defined data structures. In Java there is no notion of order between the fields of an object. These MessagePack implementations probably introduce one so that they can serialize and deserialize objects but it is not guaranteed that it will be the same on different machines.

If you unpack your byte arrays, here is what they actually contain:

["name", 105, 1.05] on Java
["name", 1.05, 105] on Android

If you want to serialize an object in a portable way, write a specific serialization method with a well-defined order for members, or use a map type, which corresponds more closely to an object.

Note that this is a reported bug here: http://jira.msgpack.org/browse/MSGPACK-51

catwell
  • 6,770
  • 1
  • 23
  • 21
  • Link is dead. Is this still the case with object order? – T.Coutlakis Jul 05 '17 at 09:43
  • Yes, they have closed their old bugtracker... I think the issue still exists. The suggested way to fix it is to use the `@index` annotation. See https://github.com/msgpack/msgpack-java/issues/56 and https://github.com/msgpack/msgpack-java/issues/98 – catwell Jul 10 '17 at 20:28
0

From MessagePack-Java v0.6.7, it officially supports Android.

It's released on Dec 09, 2012. Please upgrade your msgpack library.

Kazuki Ohta
  • 1,441
  • 17
  • 17
  • I am using MessagePack-Java v0.6.11 and I get different result on android and my mac osx computer. Any clue about this? – cpz Nov 27 '14 at 03:41