I want to use json.Encoder to encode a large stream of data without loading all of it into memory at once.
// I want to marshal this
t := struct
You can unpack the channel in the MarshalJSON method in your struct like this:
type S struct {
Foo string
Bar chan string
}
func (s *S) MarshalJSON() (b []byte, err error) {
b, err := json.Marshal(s.Foo)
if err != nil { return nil, err }
for x := range s.Bar {
tmp, err := json.Marshal(x)
if err != nil { return nil, err }
b = append(b, tmp...)
}
return
}