src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

object.go (810B)


      1 package json
      2 
      3 import (
      4 	"bytes"
      5 )
      6 
      7 // Object represents the encoding of a JSON Object type
      8 type Object struct {
      9 	w          *bytes.Buffer
     10 	writeComma bool
     11 	scratch    *[]byte
     12 }
     13 
     14 func newObject(w *bytes.Buffer, scratch *[]byte) *Object {
     15 	w.WriteRune(leftBrace)
     16 	return &Object{w: w, scratch: scratch}
     17 }
     18 
     19 func (o *Object) writeKey(key string) {
     20 	escapeStringBytes(o.w, []byte(key))
     21 	o.w.WriteRune(colon)
     22 }
     23 
     24 // Key adds the given named key to the JSON object.
     25 // Returns a Value encoder that should be used to encode
     26 // a JSON value type.
     27 func (o *Object) Key(name string) Value {
     28 	if o.writeComma {
     29 		o.w.WriteRune(comma)
     30 	} else {
     31 		o.writeComma = true
     32 	}
     33 	o.writeKey(name)
     34 	return newValue(o.w, o.scratch)
     35 }
     36 
     37 // Close encodes the end of the JSON Object
     38 func (o *Object) Close() {
     39 	o.w.WriteRune(rightBrace)
     40 }