packBytes
Wyzix33 opened this issue · comments
Hi,
thanks for this library, just started using it and works great, but i was wandering if we can improve the encoding by using something like this: packBytes
Thanks
You could certainly use packBytes with msgpackr. You could set up packBytes to convert a set of small numbers to a single aggregated bitfield number (32-bit or whatever), and then use the aggregated number in the data you encode with msgpackr, and use packBytes to unpack the bitfield after decoding.
I am not sure if you are suggesting actually using packBytes internally in msgpackr. MessagePack is intended perform fully encapsulated data encoding, whereas packBytes is using extra data (bitfield names and lengths) to encode/decode that needs to be known by both encoder and decoder. Adding that data into the encoding itself would make it bigger and slower than standard MessagePack encodings.
I understand so i can just make a extension and use it to encode/decode any object value (another object), will something like this work
addExtension({
Class: packBytes,
type: 33, // register your own extension code (a type code from 1-100)
write(instance) {
// define how your custom class should be encoded
return ???; // return some data to be encoded
}
read(data) {
// define how your custom class should be decoded,
// data will already be unpacked/decoded
return ???; // return decoded value
}
});
I will have to send the bits to the client, but how will i know what bits to use on decode because i will have multiple objects with different bits, can i have access to the property name of the object inside the read function so i can send something like this
{
firstType: packBytes(1, data.field1, 1, data.field2),
secondType: packBytes(2, data.field3, 5, data.field4)
}
and decode it to
{
firstType: { data: { field1Value, field2Value}},
secondType: {data: {field3Value, field4Value}}
?
Thanks
I think you would do something like this:
addExtension({
Class: FirstType,
type: 33,
write(instance) {
return packBytes(1, instance.field1, instance.field2);
}
read(data) {
let [field1, field2 ] = unpackBytes(data, 1, 1);
return { field1, field2 };
}
});
let first = new FirstType()
first.field1 = 1;
first.field2 = 0;
pack({name: 'has a bitfield in first property', first});
Thanks