msgpack / msgpack-node

MessagePack implementation for Node.js
Other
311 stars 71 forks source link

Feature request: manually control type mapping #52

Open jacobq opened 4 years ago

jacobq commented 4 years ago

I would like to use this library in a node.js application that communicates with a msgpack API. Unfortunately, I need to be able to send messages that use specific data types for specific fields (e.g. Array<fixint>, bin8, ...) rather than have the library automatically choose/guess as described in https://github.com/msgpack/msgpack-node#type-mapping.

How hard would it be to incorporate this? For example, I'd like to be able to do this:

// Binary data
const buff = getBufferFromSomewhere();
msgpack.pack(buff, { family: 'bin' }); // ==> chooses `bin8`, `bin16` or `bin32` depending on buffer size

// Force particular numeric type
msgpack.pack(123, { type: 'fixint' });

// Specify custom type mapping when packing arrays
const a = [{ data: Math.PI, type: 'float64' }, { data: 3.14, type: 'float32' }];
msgpack.pack(a, {
  interpret(item) {
    return {
      data: item.data,
      type: item.type,
      // alternatively:
      family: item.family,
    };
  }
});