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

Encode by this lib,can't be decode by java lib

Open lasting0001 opened this issue 9 years ago • 5 comments

Encode by this lib,can't be decode by java lib,version 0.6.12

lasting0001 avatar Feb 21 '17 13:02 lasting0001

You need to show your input data, or encoded data, if you want any comments.

kawanet avatar Feb 21 '17 13:02 kawanet

The result is Uint8Array after encode by js-lib,But decoding in java need a byte,How js-lib encode it to Int8Array?

lasting0001 avatar Feb 21 '17 13:02 lasting0001

Uint8Array is just an array of bytes. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array

kawanet avatar Feb 21 '17 13:02 kawanet

For js:

var source = {userId: 18662, grade: 66, nickName: 'qwe'}; var uint8Arr = msgpack.encode(source); console.log(uint8Arr instanceof Uint8Array);// print: true console.log(uint8Arr);// print:[131,166,117,115,101,114,73,100,205,72,230,165,103,114,97,100,101,66,168,110,105,99,107,78,97,109,101,163,113,119,101]

For Java: SimpleObj obj = new SimpleObj(); obj.setUserId(18662); obj.setGrade(66); obj.setNickName("qwe"); MessagePack pack = new MessagePack(); byte[] bs = pack.write(obj); System.out.println(Arrays.toString(bs));//print: [-109, -51, 72, -26, 66, -93, 113, 119, 101]

How can I transfer the data and decode it between them?

lasting0001 avatar Feb 22 '17 03:02 lasting0001

Try http://kawanet.github.io/msgpack-lite/ and ask it to Java lib maintainer.

[-109, -51, 72, -26, 66, -93, 113, 119, 101] represents an array:

[
  18662,
  66,
  "qwe"
]

[131,166,117,115,101,114,73,100,205,72,230,165,103,114,97,100,101,66,168,110,105,99,107,78,97,109,101,163,113,119,101] represents a plain object:

{
  "userId": 18662,
  "grade": 66,
  "nickName": "qwe"
}

kawanet avatar Feb 22 '17 04:02 kawanet