array.go

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