code.dwrz.net

Go monorepo.
Log | Files | Refs

array.go (657B)


      1 package json
      2 
      3 import (
      4 	"bytes"
      5 )
      6 
      7 // Array represents the encoding of a JSON Array
      8 type Array struct {
      9 	w          *bytes.Buffer
     10 	writeComma bool
     11 	scratch    *[]byte
     12 }
     13 
     14 func 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.
     22 func (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
     33 func (a *Array) Close() {
     34 	a.w.WriteRune(rightBracket)
     35 }