proposal: encoding/json/v2: MarshalFunc with reflect.Type #73457
Labels
LibraryProposal
Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool
Proposal
Milestone
Proposal Details
As recommended by Joe Tsai (go-json-experiment/json#168), I am opening this proposal.
The current v2 JSON API allows for customized generic marshalers and unmarshalers, such as:
I attempted to adapt my enum package package to the new v2 API in order to support interface-based marshalling and unmarshalling using various styles (e.g., internally or externally tagged). However, I encountered a limitation: the current API only supports generic type parameters for defining custom marshalers and unmarshalers. Since my implementation relies on dynamic type discovery via reflect.Type, I cannot instantiate the required generic types.
To address this, I propose extending the API with additional functions that accept reflect.Type, for example:
This approach would be consistent with how the reflect package provides both TypeOf(any) and TypeFor[T], enabling both static and dynamic use cases.
Alternatively, or in addition, I would also welcome native support in the v2 package for interface-based marshalling and unmarshalling, which would significantly improve flexibility for libraries relying on dynamic types.
The text was updated successfully, but these errors were encountered: