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

Configured to encode as "usemap: true" but still receives data as ArrayBuffer

Open crazyyi opened this issue 8 years ago • 6 comments

I have configured msgpack server side to "usemap:true", then I check the data received on client side it is still recognized as ArrayBuffer. Is this the default behavior? How can I send a map and receive it directly?

crazyyi avatar Jan 16 '17 09:01 crazyyi

usemap is an option for decoder.

Set option usemap: true on decoder-side but not on encoder-side.

      var options = {codec: msgpack.createCodec({usemap: true})};
      var decoded = msgpack.decode(data, options);
      assert.equal(true, decoded instanceof Map);

UPDATE: Above is not correct, sorry.

kawanet avatar Jan 16 '17 10:01 kawanet

Thanks for quick response. How about on encode-side? I have tried putting your code on decode-side which is my client side, but here is what I get: Uncaught Error: Invalid type: undefined. If I wrap data with Uint8Array(data) I get a map with 0 length. On the encode-side I am sending out a Map using this:

const options = {codec: msgpack.createCodec({binarraybuffer: true, preset: true})};
encode(object, opt=options) {
		return msgpack.encode(object, opt);
	}

I guess I need to change binarraybuffer to usemap:true on encode-side too?

If I only use usemap:true on encode-side, not on decode-side, I can get a object with the correct map information after I wrap data with Uint8Array(data). But this is the wrong type.

crazyyi avatar Jan 16 '17 14:01 crazyyi

I was wrong. Setting usemap: true is needed for both sides of encoder and decoder.

kawanet avatar Jan 16 '17 14:01 kawanet

Thanks using usemap: true turns everything into a map, even the object in the value field is turned into a map. This is not what I want. I think I will just use the default encode and decode method.

crazyyi avatar Jan 17 '17 05:01 crazyyi

I see. You are using a mixture of Object and Map. I'm rarely using Map in fact. Could you let me know a bit about your use case in brief? Mapping Map object to an ext type of msgpack might be a better way to transfer it. https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map

kawanet avatar Jan 18 '17 01:01 kawanet

Thanks using usemap: true turns everything into a map, even the object in the value field is turned into a map. This is not what I want.

I have the same issue as I am using objects for some things and Maps for others. I'm trying to use the library to save my redux state.

Here's a basic mre:


const options = {codec: msgpack.createCodec({binarraybuffer: true, preset: true})};

result = msgpack.decode(msgpack.encode({objKey: new Map([["a", "hi"], ["b", "loooo"]])}, options),options)

result comes back as:

Map { 'objKey' => Map { 'a' => 'hi', 'b' => 'loooo' } }

so the outer object has been converted to a Map

mungojam avatar Nov 02 '18 09:11 mungojam