Pedro Gabriel
Pedro Gabriel

Reputation: 505

How to decode arrayBuffer from object

I have tried many things to decode an arrayBuffer made of an object like {"foo":"bar"} This is the arrayBuffer:

ArrayBuffer {
  [Uint8Contents]: <5b 6f 62 6a 65 63 74 20 4f 62 6a 65 63 74 5d>,
  byteLength: 15
}

Using the simplest approach as TextDecoder it just return [object Object] so maybe the problem is the encoder? I'm out of ideias... I'm using the send method from this client here to send the data: https://www.npmjs.com/package/websocket

So, how to decode the arrayBuffer? Thanks

Upvotes: 1

Views: 2045

Answers (1)

user120242
user120242

Reputation: 15268

JSON.stringify before you send it to the client. Your decoding is fine. It wasn't serialized properly on the server side. Object.toString() is giving you that string.

If you want to send the object in binary form, you need to find a binary serializer if that's what you're looking for. However, unless you are dealing with really serious loads or specialized needs where optimization is necessary, I'd avoid the pain of doing that. The data has to be serialized at some point and then deserialized on the other end. For your average API, it's highly doubtful you need binary serialization

There are some well known options for binary serialization like FlatBuffers, BSON, Thrift, protobuf, Avro, MsgPack, etc. I'd search for binary serialization on Google to find the latest options. And you'll want to add to the search keywords and check that they have bindings for the languages you are interested in.

Upvotes: 1

Related Questions