{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/11006","labels_url":"https://api.github.com/repos/golang/go/issues/11006/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/11006/comments","events_url":"https://api.github.com/repos/golang/go/issues/11006/events","html_url":"https://github.com/golang/go/issues/11006","id":82876488,"number":11006,"title":"encoding/json: multiple anonymous struct fields shouldn't be ignored","user":{"login":"themihai","id":1800192,"avatar_url":"https://avatars.githubusercontent.com/u/1800192?v=3","gravatar_id":"","url":"https://api.github.com/users/themihai","html_url":"https://github.com/themihai","followers_url":"https://api.github.com/users/themihai/followers","following_url":"https://api.github.com/users/themihai/following{/other_user}","gists_url":"https://api.github.com/users/themihai/gists{/gist_id}","starred_url":"https://api.github.com/users/themihai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/themihai/subscriptions","organizations_url":"https://api.github.com/users/themihai/orgs","repos_url":"https://api.github.com/users/themihai/repos","events_url":"https://api.github.com/users/themihai/events{/privacy}","received_events_url":"https://api.github.com/users/themihai/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":16,"created_at":"2015-05-30T18:11:34Z","updated_at":"2015-05-31T20:22:41Z","closed_at":"2015-05-30T23:26:51Z","body":"Currently if there are duplicate anonymous struct fields they are all ignored. I think this is wrong because the encoding process shouldn't loose information unless the output format is not capable to represent it(which is not the case).\r\nFor example marshalling AllTypes produces an empty output.\r\ntype AllTypes struct {\r\n\tType1\r\n\tType2\r\n}\r\ntype Type1 struct {\r\n\tName    string\r\n\tAddress string\r\n}\r\ntype Type2 struct {\r\n\tName    string\r\n\tAddress string\r\n}\r\nTo overcome this issue an additional step is required to create a boilerplate type specially for the encoding/decoding process. Most likely we end-up with a new type (AllTypes2) as below. This is against the composability of the language and adds additional burden to the developer(look for duplicate fields ->  create a new type). \r\n\r\ntype AllTypes2 struct{\r\n   Type1 Type1\r\n  Type2 Type2\r\n}\r\n\r\nAny fix/convention that preserves the data would be fine to me but I present my proposal below too as starting point. I think it's also backward compatible.\r\n  **Use the same addressing like the native go types**.\r\n If there are duplicate anonymous fields, they should be addressed using the type name as prefix. If the type name is duplicate too both the package name and the type name should be used as prefix. For type AllTypes I would expect the output below:\r\n\r\n{\r\n  \"Type1\": {\"Name\":\"example\", \"Address\": \"example\"},\r\n  \"Type2\": {\"Name\":\"example\", \"Address\": \"example\"}\r\n}\r\n\r\n Using the same addressing as on go types makes the behaviour intuitive and easier to understand too as we have only one familiar rule instead of 3 special rules. \r\n It would make the behaviour more consistent too because the type name is already used as key on certain cases. For example type SomeType is encoded  to {\"MySlice\":[\"somedata\"]}.\r\ntype SomeType struct{\r\n   MySlice\r\n}\r\ntype MySlice []string\r\n"},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/107241544","html_url":"https://github.com/golang/go/issues/11006#issuecomment-107241544","issue_url":"https://api.github.com/repos/golang/go/issues/11006","id":107241544,"user":{"login":"themihai","id":1800192,"avatar_url":"https://avatars.githubusercontent.com/u/1800192?v=3","gravatar_id":"","url":"https://api.github.com/users/themihai","html_url":"https://github.com/themihai","followers_url":"https://api.github.com/users/themihai/followers","following_url":"https://api.github.com/users/themihai/following{/other_user}","gists_url":"https://api.github.com/users/themihai/gists{/gist_id}","starred_url":"https://api.github.com/users/themihai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/themihai/subscriptions","organizations_url":"https://api.github.com/users/themihai/orgs","repos_url":"https://api.github.com/users/themihai/repos","events_url":"https://api.github.com/users/themihai/events{/privacy}","received_events_url":"https://api.github.com/users/themihai/received_events","type":"User","site_admin":false},"created_at":"2015-05-31T20:22:41Z","updated_at":"2015-05-31T20:22:41Z","body":"I can read this[0]  on the current go documentation. I think it makes a clear statement for the fields you want to ignore, such sensitive information. Relaying on an implementation detail to avoid data leaking doesn't sounds right to me nor I believe people responsible with sensitive data do that. \r\n\r\n``Handling of anonymous struct fields is new in Go 1.1. Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of an anonymous struct field in both current and earlier versions, give the field a JSON tag of \"-\".``"}}