array.go 657 B

1234567891011121314151617181920212223242526272829303132333435
  1. package json
  2. import (
  3. "bytes"
  4. )
  5. // Array represents the encoding of a JSON Array
  6. type Array struct {
  7. w *bytes.Buffer
  8. writeComma bool
  9. scratch *[]byte
  10. }
  11. func newArray(w *bytes.Buffer, scratch *[]byte) *Array {
  12. w.WriteRune(leftBracket)
  13. return &Array{w: w, scratch: scratch}
  14. }
  15. // Value adds a new element to the JSON Array.
  16. // Returns a Value type that is used to encode
  17. // the array element.
  18. func (a *Array) Value() Value {
  19. if a.writeComma {
  20. a.w.WriteRune(comma)
  21. } else {
  22. a.writeComma = true
  23. }
  24. return newValue(a.w, a.scratch)
  25. }
  26. // Close encodes the end of the JSON Array
  27. func (a *Array) Close() {
  28. a.w.WriteRune(rightBracket)
  29. }