addExtension like msgpackr
Alistairot opened this issue · comments
Hi, I have been using msgpackr in the past,
Now I want to switch to cbor-x.
in msgpackr, i add extension like this
addExtension({
Class: MyCustomClass,
type: 12,
read: function(data) {
Object.setPrototypeOf(data, MyCustomClass.prototype)
return data
},
write: function(data) {
return data
}
})
// it is work good
but in cbor-x
addExtension({
Class: MyCustomClass,
tag: 9999,
encode(data, encodeFn: (item: MyCustomClass) => Uint8Array) {
return encodeFn(data); // infinite recursion
},
decode(data) {
Object.setPrototypeOf(data, MyCustomClass.prototype);
return data;
}
})
// not work
How can I make it work like msgpackr?
addExtension({
Class: MyCustomClass,
tag: 9999,
encode(data, encodeFn: (item: MyCustomClass) => Uint8Array) {
return encodeFn(Object.assign({}, data)); // create new obj avoid infinite recursion
},
decode(data) {
Object.setPrototypeOf(data, MyCustomClass.prototype);
return data;
}
})
That's OK.