Encode writes the JSON encoding of v to the stream, followed by a newline character. See the documentation for Marshal for details about the conversion of Go values to JSON.
(v interface{})
| 197 | // See the documentation for Marshal for details about the |
| 198 | // conversion of Go values to JSON. |
| 199 | func (enc *Encoder) Encode(v interface{}) error { |
| 200 | if enc.err != nil { |
| 201 | return enc.err |
| 202 | } |
| 203 | e := newEncodeState() |
| 204 | err := e.marshal(v, encOpts{escapeHTML: enc.escapeHTML}) |
| 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | |
| 209 | // Terminate each value with a newline. |
| 210 | // This makes the output look a little nicer |
| 211 | // when debugging, and some kind of space |
| 212 | // is required if the encoded value was a number, |
| 213 | // so that the reader knows there aren't more |
| 214 | // digits coming. |
| 215 | e.WriteByte('\n') |
| 216 | |
| 217 | b := e.Bytes() |
| 218 | if enc.indentPrefix != "" || enc.indentValue != "" { |
| 219 | if enc.indentBuf == nil { |
| 220 | enc.indentBuf = new(bytes.Buffer) |
| 221 | } |
| 222 | enc.indentBuf.Reset() |
| 223 | err = Indent(enc.indentBuf, b, enc.indentPrefix, enc.indentValue) |
| 224 | if err != nil { |
| 225 | return err |
| 226 | } |
| 227 | b = enc.indentBuf.Bytes() |
| 228 | } |
| 229 | if _, err = enc.w.Write(b); err != nil { |
| 230 | enc.err = err |
| 231 | } |
| 232 | encodeStatePool.Put(e) |
| 233 | return err |
| 234 | } |
| 235 | |
| 236 | // SetIndent instructs the encoder to format each subsequent encoded |
| 237 | // value as if indented by the package-level function Indent(dst, src, prefix, indent). |
no test coverage detected