package jx
import (
"bytes"
"io"
)
type Writer struct {
Buf []byte
stream *streamState
}
func (w *Writer ) Write (p []byte ) (n int , err error ) {
if w .stream != nil {
return 0 , errStreaming
}
w .Buf = append (w .Buf , p ...)
return len (p ), nil
}
func (w *Writer ) WriteTo (t io .Writer ) (n int64 , err error ) {
if w .stream != nil {
return 0 , errStreaming
}
wrote , err := t .Write (w .Buf )
return int64 (wrote ), err
}
func (w Writer ) String () string {
w .stream .mustNotBeStreaming ()
return string (w .Buf )
}
func (w *Writer ) Reset () {
w .Buf = w .Buf [:0 ]
w .stream = nil
}
func (w *Writer ) ResetWriter (out io .Writer ) {
w .Buf = w .Buf [:0 ]
if w .stream == nil {
w .stream = newStreamState (out )
}
w .stream .Reset (out )
}
func (w *Writer ) Grow (n int ) {
buf := bytes .NewBuffer (w .Buf )
buf .Grow (n )
w .Buf = buf .Bytes ()
}
func (w *Writer ) byte (c byte ) (fail bool ) {
if w .stream == nil {
w .Buf = append (w .Buf , c )
return false
}
return writeStreamBytes (w , c )
}
func (w *Writer ) twoBytes (c1 , c2 byte ) bool {
if w .stream == nil {
w .Buf = append (w .Buf , c1 , c2 )
return false
}
return writeStreamBytes (w , c1 , c2 )
}
func (w *Writer ) RawStr (v string ) bool {
return w .rawStr (v )
}
func (w *Writer ) rawStr (v string ) bool {
return writeStreamByteseq (w , v )
}
func (w *Writer ) Raw (b []byte ) bool {
return writeStreamByteseq (w , b )
}
func (w *Writer ) Null () bool {
return writeStreamByteseq (w , "null" )
}
func (w *Writer ) True () bool {
return writeStreamByteseq (w , "true" )
}
func (w *Writer ) False () bool {
return writeStreamByteseq (w , "false" )
}
func (w *Writer ) Bool (v bool ) bool {
if v {
return w .True ()
}
return w .False ()
}
func (w *Writer ) ObjStart () bool {
return w .byte ('{' )
}
func (w *Writer ) FieldStart (field string ) bool {
return w .Str (field ) ||
w .byte (':' )
}
func (w *Writer ) ObjEnd () bool {
return w .byte ('}' )
}
func (w *Writer ) ArrStart () bool {
return w .byte ('[' )
}
func (w *Writer ) ArrEnd () bool {
return w .byte (']' )
}
func (w *Writer ) Comma () bool {
return w .byte (',' )
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .