func stateEndTop(s *Scanner, c int) int {
	if c != ' ' && c != '\t' && c != '\r' && c != '\n' {
		s.error(c, "after top-level value")
	}
	return ScanEnd
}
stateEndTop is the state after finishing the top-level value,
such as after reading `{}` or `[1,2,3]`.
Only space characters should be seen now.
package json
import "strconv"
func checkValid(data []byte, scan *Scanner) error {
	scan.Reset()
	for _, c := range data {
		scan.bytes++
		if scan.Step(scan, int(c)) == ScanError {
			return scan.Err
		}
	}
	if scan.EOF() == ScanError {
		return scan.Err
	}
	return nil
}
func Validate(data []byte) error {
	s := &Scanner{}
	return checkValid(data, s)
}
func NextValue(data []byte, scan *Scanner) (value, rest []byte, err error) {
	scan.Reset()
	for i, c := range data {
		v := scan.Step(scan, int(c))
		if v >= ScanEnd {
			switch v {
			case ScanError:
				return nil, nil, scan.Err
			case ScanEnd:
				return data[0:i], data[i:], nil
			}
		}
	}
	if scan.EOF() == ScanError {
		return nil, nil, scan.Err
	}
	return data, nil, nil
}
type SyntaxError struct {
	msg    string  
	Offset int64   
}
func (e *SyntaxError) Error() string { return e.msg }
type Scanner struct {
	Step func(*Scanner, int) int
	endTop bool
	parseState []int
	Err error
	redo      bool
	redoCode  int
	redoState func(*Scanner, int) int
	bytes int64
}
const (
	ScanContinue     = iota  
	ScanBeginLiteral         
	ScanBeginObject          
	ScanObjectKey            
	ScanObjectValue          
	ScanEndObject            
	ScanBeginArray           
	ScanArrayValue           
	ScanEndArray             
	ScanSkipSpace            
	ScanEnd    
	ScanError  
)
const (
	parseObjectKey   = iota  
	parseObjectValue         
	parseArrayValue          
)
func (s *Scanner) Reset() {
	s.Step = stateBeginValue
	s.parseState = s.parseState[0:0]
	s.Err = nil
	s.redo = false
	s.endTop = false
}
func (s *Scanner) EOF() int {
	if s.Err != nil {
		return ScanError
	}
	if s.endTop {
		return ScanEnd
	}
	s.Step(s, ' ')
	if s.endTop {
		return ScanEnd
	}
	if s.Err == nil {
		s.Err = &SyntaxError{"unexpected end of JSON input", s.bytes}
	}
	return ScanError
}
func (s *Scanner) pushParseState(p int) {
	s.parseState = append(s.parseState, p)
}
func (s *Scanner) popParseState() {
	n := len(s.parseState) - 1
	s.parseState = s.parseState[0:n]
	s.redo = false
	if n == 0 {
		s.Step = stateEndTop
		s.endTop = true
	} else {
		s.Step = stateEndValue
	}
}
func isSpace(c rune) bool {
	return c == ' ' || c == '\t' || c == '\r' || c == '\n'
}
func stateBeginValueOrEmpty(s *Scanner, c int) int {
	if c <= ' ' && isSpace(rune(c)) {
		return ScanSkipSpace
	}
	if c == ']' {
		return stateEndValue(s, c)
	}
	return stateBeginValue(s, c)
}
func stateBeginValue(s *Scanner, c int) int {
	if c <= ' ' && isSpace(rune(c)) {
		return ScanSkipSpace
	}
	switch c {
	case '{':
		s.Step = stateBeginStringOrEmpty
		s.pushParseState(parseObjectKey)
		return ScanBeginObject
	case '[':
		s.Step = stateBeginValueOrEmpty
		s.pushParseState(parseArrayValue)
		return ScanBeginArray
	case '"':
		s.Step = stateInString
		return ScanBeginLiteral
	case '-':
		s.Step = stateNeg
		return ScanBeginLiteral
	case '0':  
		s.Step = state0
		return ScanBeginLiteral
	case 't':  
		s.Step = stateT
		return ScanBeginLiteral
	case 'f':  
		s.Step = stateF
		return ScanBeginLiteral
	case 'n':  
		s.Step = stateN
		return ScanBeginLiteral
	}
	if '1' <= c && c <= '9' {  
		s.Step = state1
		return ScanBeginLiteral
	}
	return s.error(c, "looking for beginning of value")
}
func stateBeginStringOrEmpty(s *Scanner, c int) int {
	if c <= ' ' && isSpace(rune(c)) {
		return ScanSkipSpace
	}
	if c == '}' {
		n := len(s.parseState)
		s.parseState[n-1] = parseObjectValue
		return stateEndValue(s, c)
	}
	return stateBeginString(s, c)
}
func stateBeginString(s *Scanner, c int) int {
	if c <= ' ' && isSpace(rune(c)) {
		return ScanSkipSpace
	}
	if c == '"' {
		s.Step = stateInString
		return ScanBeginLiteral
	}
	return s.error(c, "looking for beginning of object key string")
}
func stateEndValue(s *Scanner, c int) int {
	n := len(s.parseState)
	if n == 0 {
		s.Step = stateEndTop
		s.endTop = true
		return stateEndTop(s, c)
	}
	if c <= ' ' && isSpace(rune(c)) {
		s.Step = stateEndValue
		return ScanSkipSpace
	}
	ps := s.parseState[n-1]
	switch ps {
	case parseObjectKey:
		if c == ':' {
			s.parseState[n-1] = parseObjectValue
			s.Step = stateBeginValue
			return ScanObjectKey
		}
		return s.error(c, "after object key")
	case parseObjectValue:
		if c == ',' {
			s.parseState[n-1] = parseObjectKey
			s.Step = stateBeginString
			return ScanObjectValue
		}
		if c == '}' {
			s.popParseState()
			return ScanEndObject
		}
		return s.error(c, "after object key:value pair")
	case parseArrayValue:
		if c == ',' {
			s.Step = stateBeginValue
			return ScanArrayValue
		}
		if c == ']' {
			s.popParseState()
			return ScanEndArray
		}
		return s.error(c, "after array element")
	}
	return s.error(c, "")
}