Documentation
¶
Index ¶
- type Reader
- type TokenType
- type Writer
- func (w *Writer) BeginArray()
- func (w *Writer) BeginObject()
- func (w *Writer) EmptyArray()
- func (w *Writer) EmptyObject()
- func (w *Writer) EndArray()
- func (w *Writer) EndObject()
- func (w *Writer) Flush() error
- func (w *Writer) WriteBool(val bool)
- func (w *Writer) WriteFalse()
- func (w *Writer) WriteName(name string)
- func (w *Writer) WriteNull()
- func (w *Writer) WriteNumber(num float64)
- func (w *Writer) WriteRaw(raw string)
- func (w *Writer) WriteString(s string)
- func (w *Writer) WriteTrue()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
Buf []byte
// Current parsed trunk start position
Start int
// Current parsed trunk end position, Buf[Start:End] is the current parsed trunk
End int
// Last parsed string/property name.
Str []byte
// contains filtered or unexported fields
}
Read json string as a sequence of Json Token. After create Reader struct, must call Init() method.
func (*Reader) ExpectName ¶
If next token is not PropertyName, or its name not expected, return non-nil error
func (*Reader) Next ¶
Parse next json token, return token type. If json string is invalid, Next() return non-nil error.
func (*Reader) ReadNumber ¶
ReadNumber return next float value, return non-nil error if next token not number.
func (*Reader) ReadString ¶
ReadString return next string value, return non-nil error if next token not string.
type TokenType ¶
type TokenType int
Json Token type
const ( Null TokenType Bool // If the token is number, the number value not parsed, caller should parse // itself from Buf[Start:End]. Why Reader not parse number as double? Because: // // 1. Caller may parse number in different way, such as parse it as decimal, // parse to double, then decimal will lose precision. // 2. Performance, strconv.ParseFloat() is expensive, caller can use cheaper // parse function such as ParseInt() or just treat it as string because // she knows the underlay type exactly. // // And also means, the number format maybe invalid even the Reader report it // as a valid Number, because Reader use a very simple way to parse number // string. Caller should process the error, such as check the err result of // ParseFloat(). Number // If the token is string or property name, Buf[Start:End] contains the raw // json string trunk include `"` quote. The parsed value stores in Buf.Str field. String BeginObject PropertyName // Json object name EndObject BeginArray EndArray EOF )
type Writer ¶
type Writer struct {
// use bufio.Writer because it will cache writer error, and do not need check error
// on every Write().
*bufio.Writer
// contains filtered or unexported fields
}
Writer write json encoded string.
Writer stores error occurred during write, WriteXXX() functions do not return error. If an error stored in Writer, all WriteXXX() functions quit immediately. Must call .Flush() periodically to check does error have occurred.
func NewWriter ¶
Create a new writer that output json string to a IO Writer. Argument `w' must also implement io.ByteWriter interface.
func (*Writer) BeginArray ¶
func (w *Writer) BeginArray()
func (*Writer) BeginObject ¶
func (w *Writer) BeginObject()
func (*Writer) EmptyArray ¶
func (w *Writer) EmptyArray()
func (*Writer) EmptyObject ¶
func (w *Writer) EmptyObject()
func (*Writer) Flush ¶
Flush written content to underlay writer, and report whether an underlay error happened?
func (*Writer) WriteFalse ¶
func (w *Writer) WriteFalse()