1

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

5
  • 1
    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. Commented Apr 20, 2020 at 4:47
  • Im doing JSON.stringify for now while I don't have any idea on how to deal with object, but you are saying there's nothing more "native" other than doing it as string? I was expecting too much from binary/buffers? :/ Commented Apr 20, 2020 at 5:01
  • 1
    Oh. 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 Commented Apr 20, 2020 at 5:09
  • Well I can't accept your answer because it is a comment... Can you answer it then? It makes sense and it helped me by giving directions. Thanks! Commented Apr 20, 2020 at 5:34
  • Alright, posting as an answer :) Commented Apr 20, 2020 at 5:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Probably gonna change all json to protobuf as im looking for many websocket calls. Thanks for your directions, really helped me out.
Good luck! I added a list of some binary serialization options I can think of off the top of my head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.