You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	support array
This commit is contained in:
		
							
								
								
									
										44
									
								
								jsoniter.go
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								jsoniter.go
									
									
									
									
									
								
							| @@ -15,27 +15,39 @@ type Iterator struct { | |||||||
| } | } | ||||||
|  |  | ||||||
| func Parse(reader io.Reader, bufSize int) *Iterator { | func Parse(reader io.Reader, bufSize int) *Iterator { | ||||||
| 	return &Iterator{ | 	iter := &Iterator{ | ||||||
| 		reader: reader, | 		reader: reader, | ||||||
| 		buf: make([]byte, bufSize), | 		buf: make([]byte, bufSize), | ||||||
| 		head: 0, | 		head: 0, | ||||||
| 		tail: 0, | 		tail: 0, | ||||||
| 	} | 	} | ||||||
|  | 	iter.skipWhitespaces() | ||||||
|  | 	return iter | ||||||
| } | } | ||||||
|  |  | ||||||
| func ParseBytes(input []byte) *Iterator { | func ParseBytes(input []byte) *Iterator { | ||||||
| 	return &Iterator{ | 	iter := &Iterator{ | ||||||
| 		reader: nil, | 		reader: nil, | ||||||
| 		buf: input, | 		buf: input, | ||||||
| 		head: 0, | 		head: 0, | ||||||
| 		tail: len(input), | 		tail: len(input), | ||||||
| 	} | 	} | ||||||
|  | 	iter.skipWhitespaces() | ||||||
|  | 	return iter | ||||||
| } | } | ||||||
|  |  | ||||||
| func ParseString(input string) *Iterator { | func ParseString(input string) *Iterator { | ||||||
| 	return ParseBytes([]byte(input)) | 	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) { | 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])) | 	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 | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user