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

feature: Add new interface as alternative to existing Marshaler interface #507

Open
fxamacker opened this issue Mar 17, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@fxamacker
Copy link
Owner

Currently, cbor.Marshaler interface matches json.Marshaler to make it easier for users familiar with encoding/json package.

type Marshaler interface {
	MarshalCBOR() ([]byte, error)
}

However, this also inherits the limitations from matching those interfaces, as already noted in comments and discussions at:

For example, the proposed new API for encoding/json/v2 includes MarshalerV2 or MarshalerTo, etc.

Similarly for this library, a new alternative to Marshaler interface can address known limitations of the old API.

@fxamacker fxamacker added the enhancement New feature or request label Mar 17, 2024
@fxamacker fxamacker changed the title Add new interface as alternative to existing Marshaler interface feature: Add new interface as alternative to existing Marshaler interface Mar 18, 2024
@benluddy
Copy link
Contributor

benluddy commented Apr 24, 2024

Another wish-list item for this: customized marshal behavior for types implementing a particular interface. That probably implies that you could register multiple custom marshal behaviors matching the same concrete type.

My motivating use case is to interoperate seamlessly with an ecosystem already tied to encoding/json by recognizing types that implement MarshalJSON (but not MarshalCBOR). They'd be marshaled by calling MarshalJSON then transcoding to CBOR, or possibly putting the JSON bytes into a string enclosed by some "encoded JSON" tag.

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

No branches or pull requests

2 participants