You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
type A struct {
X int
Y string
Z float64
}
type C struct {
X2 int
A
}
func (a *A) MarshalJSON() ([]byte, error) {
type aliasA A
a2:=aliasA(*a)
return json.Marshal(a2) // Essentially a no-op custom marshaler to illustrate the problem
}
func main() {
a := A{X:10, Y:"faster", Z:2.43}
c := C{X2:101, A:a}
jc, _ := json.Marshal(&c)
fmt.Println(string(jc))
}
The UnmarshalJSON MarshalJSON method is promoted from A to C because of embedding
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
I believe this is not a general discusison or a doubt I am trying to ask. I can take it to other forums but will be great if you can check the issue description again.
I am not talking about UnmarshalJSON but MarshalJSON.
The problem is not it is getting promoted from A to C, but it is NOT getting promoted as expected.
It gets promoted as expected when a dummy custom marshaller is added to C which is expected. But it does not get promoted if there is not custom marshaler.
Uh oh!
There was an error while loading. Please reload this page.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes. Checked using go1.16.5 on play.golang.org.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
C
which embeds another structA
and adds additional fieldsA
has a custom marshaler with pointer receiver. (func (a *A) MarshalJSON() ([]byte, error)
)C
, returns marshaled JSON with only A.Example: https://play.golang.org/p/euugvbFXI76
Additional notes
C
(Like this: https://play.golang.org/p/dmst19c-N9G)What did you expect to see?
{"X2":101,"X":10,"Y":"faster","Z":2.43}
What did you see instead?
{"X":10,"Y":"faster","Z":2.43}
The text was updated successfully, but these errors were encountered: