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 marshall not working correctly when invoked via function parameter as interface #1605

Open
urbim opened this issue Jan 9, 2024 · 0 comments

Comments

@urbim
Copy link

urbim commented Jan 9, 2024

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"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant