ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.83k stars 294 forks source link

Expose Handle.Name() in some form through Encoder and Decoder for use by codec.Selfer #396

Closed extemporalgenome closed 8 months ago

extemporalgenome commented 1 year ago

Relating to #395, when working with data in a way equivalent to codec.Raw or json.RawMessage, it's useful to know what encoding is being encoded to or decoded from. codec.Selfer methods don't have this ability, since neither codec.Encoder nor codec.Decoder expose either the codec.Handle or its Name method.

For example, if I have a type which works similarly to codec.Raw, which may at any time contain either JSON or CBOR, I want to be able to optimistically represent the contained data in the same format, for example:

If encoding to JSON, and the contained data is already encoded in JSON, behave the same as json.RawMessage. Otherwise, re-encode as JSON.

The equivalent behavior would apply when encoding to other formats (pass-through or re-encode then pass-through).

Giving Encoder and Decoder a method which returns the Handle.Name() value would provide sufficient information to satisfy this need.

ugorji commented 8 months ago

Thought through this. We can add this without any adverse effects.