{"action":"created","issue":{"url":"https://api.github.com/repos/go-yaml/yaml/issues/107","labels_url":"https://api.github.com/repos/go-yaml/yaml/issues/107/labels{/name}","comments_url":"https://api.github.com/repos/go-yaml/yaml/issues/107/comments","events_url":"https://api.github.com/repos/go-yaml/yaml/issues/107/events","html_url":"https://github.com/go-yaml/yaml/issues/107","id":88437508,"number":107,"title":"Structs that unmarshal themselves don't work","user":{"login":"rojer","id":531307,"avatar_url":"https://avatars.githubusercontent.com/u/531307?v=3","gravatar_id":"","url":"https://api.github.com/users/rojer","html_url":"https://github.com/rojer","followers_url":"https://api.github.com/users/rojer/followers","following_url":"https://api.github.com/users/rojer/following{/other_user}","gists_url":"https://api.github.com/users/rojer/gists{/gist_id}","starred_url":"https://api.github.com/users/rojer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rojer/subscriptions","organizations_url":"https://api.github.com/users/rojer/orgs","repos_url":"https://api.github.com/users/rojer/repos","events_url":"https://api.github.com/users/rojer/events{/privacy}","received_events_url":"https://api.github.com/users/rojer/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-15T14:07:22Z","updated_at":"2015-06-15T20:12:15Z","closed_at":"2015-06-15T20:12:15Z","body":"I'm migrating code from JSON to YAML and have run into an issue with UnmarshalYAML recursing endlessly where UnmarshalJSON works.\r\nI have a struct that has UnmarshalJSON defined and further calls into json.Unmarshal to do most of the unmarshaling, but then does some touch-up/validation afterwards.\r\nNaive implementation does cause endless recursion, but a type alias stops it. See live example here: http://play.golang.org/p/St33ZkWvTN\r\n\r\nnote foo.ok set to true during unmarshaling which was not in the original message. this is convenient.\r\n\r\nnow, when converted to equivalent code for YAML, it goes into a recursion loop, as YAML unmarshaler seems to treat type aliases differently than JSON.\r\n\r\nit would be nice if this use case was supported.\r\nhere's a full test program:\r\n```go\r\npackage main\r\n\r\nimport (\r\n        \"encoding/json\"\r\n        \"fmt\"\r\n        \"os\"\r\n\r\n        yaml \"gopkg.in/yaml.v2\"\r\n)\r\n\r\ntype Foo struct {\r\n        Bar string\r\n        ok  bool\r\n}\r\n\r\ntype fooJSON *Foo\r\n\r\nfunc (f *Foo) UnmarshalJSON(data []byte) error {\r\n        fmt.Printf(\"unmarshal JSON\\n\")\r\n        if err := json.Unmarshal(data, fooJSON(f)); err != nil {\r\n                return err\r\n        }\r\n        f.ok = true\r\n        return nil\r\n}\r\n\r\ntype fooYAML *Foo\r\n\r\nvar n = 0\r\n\r\nfunc (f *Foo) UnmarshalYAML(unmarshal func(interface{}) error) error {\r\n        n += 1\r\n        fmt.Printf(\"unmarshal YAML\\n\")\r\n        if n > 1 {\r\n                os.Exit(1)\r\n        }\r\n        if err := unmarshal(fooYAML(f)); err != nil {\r\n                return err\r\n        }\r\n        f.ok = true\r\n        return nil\r\n}\r\n\r\nfunc main() {\r\n        var foo1, foo2 Foo\r\n        err := json.Unmarshal([]byte(`{\"bar\": \"baz\"}`), &foo1)\r\n        if err != nil {\r\n                panic(err)\r\n        }\r\n        fmt.Printf(\"%+v\\n\", foo1)\r\n        err = yaml.Unmarshal([]byte(`{\"bar\": \"baz\"}`), &foo2)\r\n        if err != nil {\r\n                panic(err)\r\n        }\r\n        fmt.Printf(\"%+v\\n\", foo2)\r\n}\r\n```\r\n\r\nand output it produces:\r\n\r\n```\r\nunmarshal JSON\r\n{Bar:baz ok:true}\r\nunmarshal YAML\r\nunmarshal YAML\r\n```"},"comment":{"url":"https://api.github.com/repos/go-yaml/yaml/issues/comments/112193612","html_url":"https://github.com/go-yaml/yaml/issues/107#issuecomment-112193612","issue_url":"https://api.github.com/repos/go-yaml/yaml/issues/107","id":112193612,"user":{"login":"niemeyer","id":378683,"avatar_url":"https://avatars.githubusercontent.com/u/378683?v=3","gravatar_id":"","url":"https://api.github.com/users/niemeyer","html_url":"https://github.com/niemeyer","followers_url":"https://api.github.com/users/niemeyer/followers","following_url":"https://api.github.com/users/niemeyer/following{/other_user}","gists_url":"https://api.github.com/users/niemeyer/gists{/gist_id}","starred_url":"https://api.github.com/users/niemeyer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niemeyer/subscriptions","organizations_url":"https://api.github.com/users/niemeyer/orgs","repos_url":"https://api.github.com/users/niemeyer/repos","events_url":"https://api.github.com/users/niemeyer/events{/privacy}","received_events_url":"https://api.github.com/users/niemeyer/received_events","type":"User","site_admin":false},"created_at":"2015-06-15T20:12:15Z","updated_at":"2015-06-15T20:12:15Z","body":"There's indeed a difference in behavior, but we cannot simply break compatibility to make the behavior of the yaml package match the json package, as that would break the expectation of existing applications trusting on the implemented behavior.\r\n"}}