JSON marshall not working correctly when invoked via function parameter as interface
urbim opened this issue · 0 comments
urbim commented
The following program sample.go
triggers an unexpected result
package main
import (
"encoding/json"
"fmt"
)
type Foo interface {
}
type Foo1 struct{
Bar1 string
}
func (f Foo1) MarshalJSON() ([]byte, error) {
return []byte(`{"test": "foo"}`), nil
}
func main() {
f1 := Foo1{
Bar1: "bar1",
}
printJson(f1)
}
func printJson(f Foo) {
marshal, err := json.Marshal(f)
if err != nil {
panic(err)
}
fmt.Println(string(marshal))
}
Expected result
{"test":"foo"}
Got
null
Yaegi Version
v0.15.1
Additional Notes
Another sample with different result:
package main
import (
"encoding/json"
"fmt"
)
type Foo interface {
}
type Foo1 struct{
Bar1 string
}
func (f Foo1) MarshalJSON() ([]byte, error) {
return []byte(`{"test": "foo"}`), nil
}
type Foo2 struct{
Bar2 string
Foo1
}
func main() {
f1 := Foo1{
Bar1: "bar1",
}
f2 := Foo2{
Bar2: "bar2",
Foo1: f1,
}
printJson(f2)
}
func printJson(f Foo) {
marshal, err := json.Marshal(f)
if err != nil {
panic(err)
}
fmt.Println(string(marshal))
}
Expected result
{"test":"foo"}
Got
{"Bar2":"bar2","Bar1":"bar1"}