encoder.go raw

   1  package json
   2  
   3  import (
   4  	"bytes"
   5  )
   6  
   7  // Encoder is JSON encoder that supports construction of JSON values
   8  // using methods.
   9  type Encoder struct {
  10  	w *bytes.Buffer
  11  	Value
  12  }
  13  
  14  // NewEncoder returns a new JSON encoder
  15  func NewEncoder() *Encoder {
  16  	writer := bytes.NewBuffer(nil)
  17  	scratch := make([]byte, 64)
  18  
  19  	return &Encoder{w: writer, Value: newValue(writer, &scratch)}
  20  }
  21  
  22  // String returns the String output of the JSON encoder
  23  func (e Encoder) String() string {
  24  	return e.w.String()
  25  }
  26  
  27  // Bytes returns the []byte slice of the JSON encoder
  28  func (e Encoder) Bytes() []byte {
  29  	return e.w.Bytes()
  30  }
  31