I'm testing out go json marshaling with embedded structs. However, I see that when I embed time.Time, why does it always override the other embedded structs even though they also provide their own custom marshaling? The code below always print out "0001-01-01T00:00:00Z"
package main
import (
"encoding/json"
"fmt"
"time"
)
type A struct {
}
func (a A) Print() {
fmt.Println("A")
}
type B struct {
B int
}
func (a A) MarshalJSON() ([]byte, error) {
return []byte(`"a"`), nil
}
func (b B) MarshalJSON() ([]byte, error) {
return []byte(`"b"`), nil
}
func (a B) Print() {
fmt.Println("A")
}
type C struct {
A
B
time.Time
C int `json:"C"`
}
func main() {
fmt.Println("Hello, 世界")
c := C{}
decode, err := json.Marshal(c)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(decode))
}
CodePudding user response:
If you embed multiple types that have identically named fields or methods, then those fields or methods will not be accessible directly anymore.
x.f
- For a value
xof typeTor*TwhereTis not a pointer or interface type,x.fdenotes the field or method at the shallowest depth inTwhere there is such anf. If there is not exactly onefwith shallowest depth, the selector expression is illegal.
That means that, given the following set of types:
type S1 struct { F string }
type S2 struct { F string }
type S3 struct {
S1
S2
}
the expression s3.F is illegal:
var s3 S3
_ = s3.F // ambiguous selector s3.F
this is because there are more than one F at the shallowest depth. You can try it on playground.
The same rules apply to methods. From this it follows that your type C does NOT satisfy the json.Marshaler interface because it embeds, at the same depth, more that one type that implements the MarshalJSON() method. You can see that for yourself on playground.
Then, however, the question still remains as to why the embedded time.Time's custom marshaling is used regardless. This is because time.Time implements not only the json.Marshaler interface but also the encoding.TextMarshaler interface (docs & playground). And the json.Marshal's documentation says the following:
Marshal traverses the value v recursively. If an encountered value implements the
Marshalerinterface and is not a nil pointer, Marshal calls itsMarshalJSONmethod to produce JSON. If noMarshalJSONmethod is present but the value implementsencoding.TextMarshalerinstead, Marshal calls itsMarshalTextmethod and encodes the result as a JSON string.
You can see here that the behaviour described above holds once you also have A or B implement the encoding.TextMarshaler interface, then the time.Time's MarshalText method will not be used anymore.
