From b48e59dbb749f3e3ab809e370ef40d6afb237605 Mon Sep 17 00:00:00 2001 From: Tao Wen Date: Thu, 1 Dec 2016 13:53:36 +0800 Subject: [PATCH] support array --- jsoniter.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/jsoniter.go b/jsoniter.go index 67b13f7..32b380f 100644 --- a/jsoniter.go +++ b/jsoniter.go @@ -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 +} \ No newline at end of file