{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/11940","repository_url":"https://api.github.com/repos/golang/go","labels_url":"https://api.github.com/repos/golang/go/issues/11940/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/11940/comments","events_url":"https://api.github.com/repos/golang/go/issues/11940/events","html_url":"https://github.com/golang/go/issues/11940","id":98195219,"number":11940,"title":"proposal: encoding/json: Encode channel as array","user":{"login":"lukescott","id":486224,"avatar_url":"https://avatars.githubusercontent.com/u/486224?v=3","gravatar_id":"","url":"https://api.github.com/users/lukescott","html_url":"https://github.com/lukescott","followers_url":"https://api.github.com/users/lukescott/followers","following_url":"https://api.github.com/users/lukescott/following{/other_user}","gists_url":"https://api.github.com/users/lukescott/gists{/gist_id}","starred_url":"https://api.github.com/users/lukescott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukescott/subscriptions","organizations_url":"https://api.github.com/users/lukescott/orgs","repos_url":"https://api.github.com/users/lukescott/repos","events_url":"https://api.github.com/users/lukescott/events{/privacy}","received_events_url":"https://api.github.com/users/lukescott/received_events","type":"User","site_admin":false},"labels":[{"id":246350325,"url":"https://api.github.com/repos/golang/go/labels/Proposal-Declined","name":"Proposal-Declined","color":"eb6420","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/golang/go/milestones/30","html_url":"https://github.com/golang/go/milestone/30","labels_url":"https://api.github.com/repos/golang/go/milestones/30/labels","id":1373555,"number":30,"title":"Proposal","description":"Proposals that are pending (not yet accepted/rejected).\r\n","creator":{"login":"rsc","id":104030,"avatar_url":"https://avatars.githubusercontent.com/u/104030?v=3","gravatar_id":"","url":"https://api.github.com/users/rsc","html_url":"https://github.com/rsc","followers_url":"https://api.github.com/users/rsc/followers","following_url":"https://api.github.com/users/rsc/following{/other_user}","gists_url":"https://api.github.com/users/rsc/gists{/gist_id}","starred_url":"https://api.github.com/users/rsc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsc/subscriptions","organizations_url":"https://api.github.com/users/rsc/orgs","repos_url":"https://api.github.com/users/rsc/repos","events_url":"https://api.github.com/users/rsc/events{/privacy}","received_events_url":"https://api.github.com/users/rsc/received_events","type":"User","site_admin":false},"open_issues":33,"closed_issues":94,"state":"open","created_at":"2015-10-24T01:40:49Z","updated_at":"2016-12-12T21:53:32Z","due_on":null,"closed_at":null},"comments":8,"created_at":"2015-07-30T15:16:46Z","updated_at":"2016-12-12T22:09:06Z","closed_at":"2016-12-12T21:31:19Z","body":"I'm currency doing something like this to encode a channel:\n\n``` go\nif _, err = w.Write([]byte{'['}); err != nil {\n    return err\n}\nenc := json.NewEncoder(w)\nif err = enc.Encode(<-objects); err != nil {\n    return err\n}\nfor o := range objects {\n    if _, err = w.Write([]byte{','}); err != nil {\n        return err\n    }\n    if err = enc.Encode(o); err != nil {\n        return err\n    }\n}\nif _, err = w.Write([]byte{']'}); err != nil {\n    return err\n}\n```\n\nI have a couple issues with this approach:\n\n1 - It's verbose\n2 - enc.Encode terminates each object with a newline character, so I end up with something like:\n\n``` json\n[{}\n,{}\n,{}]\n```\n\nInstead of:\n\n``` json\n[{},{},{}]\n```\n\nWhich is a bit odd because json.Marshal does not terminate with a newline character. I would have expected them both to do the same thing, just Marshal allocates a writer/buffer for you.\n"},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/266568717","html_url":"https://github.com/golang/go/issues/11940#issuecomment-266568717","issue_url":"https://api.github.com/repos/golang/go/issues/11940","id":266568717,"user":{"login":"rsc","id":104030,"avatar_url":"https://avatars.githubusercontent.com/u/104030?v=3","gravatar_id":"","url":"https://api.github.com/users/rsc","html_url":"https://github.com/rsc","followers_url":"https://api.github.com/users/rsc/followers","following_url":"https://api.github.com/users/rsc/following{/other_user}","gists_url":"https://api.github.com/users/rsc/gists{/gist_id}","starred_url":"https://api.github.com/users/rsc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsc/subscriptions","organizations_url":"https://api.github.com/users/rsc/orgs","repos_url":"https://api.github.com/users/rsc/repos","events_url":"https://api.github.com/users/rsc/events{/privacy}","received_events_url":"https://api.github.com/users/rsc/received_events","type":"User","site_admin":false},"created_at":"2016-12-12T22:09:06Z","updated_at":"2016-12-12T22:09:06Z","body":"Re uses: What if the channel is in the struct because it is for sending to, not receiving from? What if there are a known number of values to receive over the channel, and it will never be closed? Lots of things that could reasonably go wrong. Better to reject channels by default: an error message is better than a deadlock because the channel is being used wrong.\r\n\r\nAs for json streaming, the current encoder does not write any part of the JSON response to the underlying writer until the entire JSON is constructed. (Otherwise it might encounter some problem midway and return an error but have written a half-JSON to the underlying writer. It avoids this by buffering until it knows the JSON conversion has succeeded.)\r\n\r\nSome people do want streaming, of course, and if we add streaming we should also add some way to stream during a custom encoding method. But that makes streaming a non-argument for built-in channel handling."}}
{"actor":{"display_login":"rsc"}}