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

JSON custom encoderFunc for certain types #48627

Closed
PumpkinSeed opened this issue Sep 25, 2021 · 2 comments
Closed

JSON custom encoderFunc for certain types #48627

PumpkinSeed opened this issue Sep 25, 2021 · 2 comments

Comments

@PumpkinSeed
Copy link

Feature request

The JSON marshalling of compact types like structs can be unique in some cases, which means we need to have unique encoder implementations. It would be nice to make the encoding/json/encode.go's newTypeEncoder function customisable. I mean this function accepts type information and based on that it will return an encoderFunc. Somehow if we are able to add custom implementations for different kinds that would make the json encoder more flexible.

High-level example of my thoughts:

// ------------ json package

const (
  StructKind = "struct"
)

type customEncoder interface {
  encode(e *encodeState, v reflect.Value, opts encOpts)
}

func SetEncoder(kind string, impl customEncoder) {
  // TODO
}

// ------------ client impl

json.SetEncoder(json.StructKind, CustomStruct{})

type CustomStruct struct{}

func (CustomStruct) encode(e *encodeState, v reflect.Value, opts encOpts) {
  // TODO
}

@dsnet
Copy link
Member

dsnet commented Sep 25, 2021

This sounds like a duplicate of #5901?

@seankhliao
Copy link
Member

Duplicate of #5901

@seankhliao seankhliao marked this as a duplicate of #5901 Sep 25, 2021
@golang golang locked and limited conversation to collaborators Sep 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants