encoder.go 594 B

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