Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addExtension like msgpackr #85

Open
Alistairot opened this issue Sep 2, 2023 · 1 comment
Open

addExtension like msgpackr #85

Alistairot opened this issue Sep 2, 2023 · 1 comment

Comments

@Alistairot
Copy link

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?

@Alistairot
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant