javajsonmessagepack

MessagePack Java serializing as array


Is it accepted behaviour that MessagePack official implementation in Java serializes public fields as Arrays?

In what universe is this "like JSON"?

My case: I have a simple class like so:

@Message
public class MySuperClass(){
    public int mySuperID; // let's say 4
    public byte[] mySuperData; // let's say nothing
    public String mySuperType; // let's say null
    public String mySuperExtra; // let's say HI!

    public MySuperClass(){}

    // other constructors
}

And i'm simply serializing with

MessagePack msgpack = new MessagePack();
msgpack.write(mySuperInstance);

And sending that to a distant server written in NodeJS.

Node JS can easily unpack it, to

['HI!', �, 4, null]

Which means that MessagePack is nothing like JSON, because it parses Java objects as arrays and then only repopulates them alphabetically!

Does anyone have a solution that does not include mapping every object I have? (Also HashMap is not unpackable by Node, which means that messagePack is not cross-platform ,thus, repeating, noting like JSON)


Solution

  • Judging from my MessagePack experience, it usually treats objects as MessagePack maps.

    It doesn't use any language specific constructs so it is cross - platform.

    I think you may be having issues because of the library you are using. You should consider the official one. You can read more about it here. It is basically an extension for Jackson that let's serialise and deserialise Java objects directly to / from MessagePack and was working (at least when I had to use it).