Add optional support for json.Marshaler and json.Unmarshaler via transcoding#673
Merged
fxamacker merged 2 commits intofxamacker:masterfrom Jun 18, 2025
Merged
Conversation
5f9eb34 to
6f23503
Compare
If the user provides a JSON-to-CBOR transcode function, a value whose type implements json.Marshaler and not cbor.Marshaler will be encoded by first calling its MarshalJSON method, then transcoding the result to CBOR. Signed-off-by: Ben Luddy <bluddy@redhat.com>
Users can provide a function to transcode an encoded CBOR data item to JSON. If provided, then unmarshaling into a value whose type implements json.Unmarshaler, but not cbor.Unmarshaler, will first transcode the input bytes to JSON and then invoke UnmarshalJSON on the destination value. Signed-off-by: Ben Luddy <bluddy@redhat.com>
6f23503 to
ba129eb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Recognition of types implementing these interfaces is important for users requiring drop-in compatibility with
encoding/json. Without it, if a type implements the JSON interfaces without also implementing the corresponding CBOR interfaces, its JSON and CBOR representations can have substantial structural differences.Consider what can happen with a type like https://pkg.go.dev/k8s.io/apimachinery/pkg/util/intstr#IntOrString if it were to only implement
json.Marshalerandjson.Unmarshaler(and notcbor.Marshalerorcbor.Unmarshaler):ToJSON(IntOrString{Type: String, StrVal: "a"}) => "a"FromJSON[any]("a") => string("a")ToCBOR(string("a")) => 0x6161FromCBOR[IntOrString](0x6161) => **error: can't unmarshal CBOR text string number to struct**With automatic transcoding, we can preserve the faithful roundtrip:
FromCBOR[IntOrString](0x6161) => FromJSON[IntOrString](CBORToJSON(0x6161)) => FromJSON[IntOrString]("a") => IntOrString{Type: String, StrVal: "a"}PR Was Proposed and Welcomed in Currently Open Issue
Checklist (for code PR only, ignore for docs PR)
Last line of each commit message should be in this format:
Signed-off-by: Firstname Lastname firstname.lastname@example.com
(see next section).
Certify the Developer's Certificate of Origin 1.1
the Developer Certificate of Origin 1.1.