1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-08 13:06:29 +02:00

support array

This commit is contained in:
Tao Wen 2016-12-01 13:53:36 +08:00
parent 5488b122cd
commit b48e59dbb7

View File

@ -15,27 +15,39 @@ type Iterator struct {
}
func Parse(reader io.Reader, bufSize int) *Iterator {
return &Iterator{
iter := &Iterator{
reader: reader,
buf: make([]byte, bufSize),
head: 0,
tail: 0,
}
iter.skipWhitespaces()
return iter
}
func ParseBytes(input []byte) *Iterator {
return &Iterator{
iter := &Iterator{
reader: nil,
buf: input,
head: 0,
tail: len(input),
}
iter.skipWhitespaces()
return iter
}
func ParseString(input string) *Iterator {
return ParseBytes([]byte(input))
}
func (iter *Iterator) skipWhitespaces() {
c := iter.readByte()
for c == ' ' {
c = iter.readByte()
}
iter.unreadByte()
}
func (iter *Iterator) ReportError(operation string, msg string) {
iter.Error = fmt.Errorf("%s: %s, parsing %v at %s", operation, msg, iter.head, string(iter.buf[0:iter.tail]))
}
@ -292,3 +304,31 @@ func appendRune(p []byte, r rune) []byte {
}
}
func (iter *Iterator) ReadArray() bool {
iter.skipWhitespaces()
c := iter.readByte()
if iter.Error != nil {
return false
}
if c == '[' {
iter.skipWhitespaces()
c = iter.readByte()
if iter.Error != nil {
iter.ReportError("ReadArray", "eof after [")
return false
}
if c == ']' {
return false
} else {
iter.unreadByte()
return true
}
}
if c == ']' {
return false
} else if c == ',' {
return true
}
iter.ReportError("ReadArray", "expect [ or ,")
return false
}