سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top