javascriptarraysjsonbsonuint8array

Sending Uint8Array (BSON) in a JSON object


I'm using the 'bson' npm package in the browser for converting / serializing a JSON object to BSON. The documentation says that it returns a Node.js buffer. The documentation of Node.js says that a buffer is of type 'Uint8Array'. Now I want to send this Uint8Array in another JSON object (infoJSON) but JSON does not support Uint8Array. I tried to convert the buffer (Uint8Array) to a simple array, inserting it in the JSON object (infoJSON) and then I convert it back to Uint8Array directly out of the JSON object. But this new Uint8Array can not be deserialized back to the original person object (see the result). It is broken.

Why I'm using BSON? I want to split the original object into multiple chunks to send it through a WebRTC data channel which has a limitation of data size. I need to be able to identify each chunk (type). It is the reason why I am using nested objects.

  var personJSON = { 'name': 'sarah' } // JSON
  var personBuffer = Bson.serialize(personJSON) // Uint8Array
  var personArray = Array.from(personBuffer) // Simple array
  var infoJSON = { 'count': 1, 'person': personArray } // Inserting array into JSON
  var personUint8Array = Uint8Array.from(infoJSON.person) // Converting array back to Uint8Array
  console.log('deserializedObj:')
  console.log(Bson.deserialize(personUint8Array))

Result:

result


Solution

  • This is because Bson.serialize and Bson.deserialize use a Node.js like Buffer object.

    You can correct this by using the underlying ArrayBuffer to create a Uint8Array (test online) and use then use Buffer.from :

    var personJSON = { 'name': 'sarah' } // JSON
    var personBuffer = new Uint8Array(Bson.serialize(personJSON).buffer) // Uint8Array
    var personArray = Array.from(personBuffer) // Simple array
    var infoJSON = { 'count': 1, 'person': personArray } // Inserting array into JSON
    var personUint8Array = Uint8Array.from(infoJSON.person) // Converting array back to Uint8Array
    console.log('deserializedObj:')
    console.log(Bson.deserialize(Buffer.from(personBuffer)))