MCPcopy Create free account
hub / github.com/blues/note / Encode

Method Encode

jsonxt/stream.go:199–234  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

197// See the documentation for Marshal for details about the
198// conversion of Go values to JSON.
199func (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).

Callers 1

encodeByteSliceFunction · 0.45

Calls 5

newEncodeStateFunction · 0.85
IndentFunction · 0.85
marshalMethod · 0.80
ResetMethod · 0.80
PutMethod · 0.80

Tested by

no test coverage detected