原文
func (d *Decoder) stateObjectString(tok []byte) ([]byte, error) {
switch tok[0] {
case '}':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = stateEnd
case inObj:
d.state = stateObjectComma
case !inObj:
d.state = stateArrayComma
}
return tok, nil
case '"':
d.state = stateObjectColon
return tok, nil
default:
return nil, fmt.Errorf("stateObjectString: missing string key")
}
}
func (d *Decoder) stateObjectColon(tok []byte) ([]byte, error) {
switch tok[0] {
case Colon:
d.state = stateObjectValue
return d.NextToken()
default:
return tok, fmt.Errorf("stateObjectColon: expecting colon")
}
}
func (d *Decoder) stateObjectValue(tok []byte) ([]byte, error) {
switch tok[0] {
case '{':
d.state = stateObjectString
d.push(true)
return tok, nil
case '[':
d.state = stateArrayValue
d.push(false)
return tok, nil
default:
d.state = stateObjectComma
return tok, nil
}
}
func (d *Decoder) stateObjectComma(tok []byte) ([]byte, error) {
switch tok[0] {
case '}':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = stateEnd
case inObj:
d.state = stateObjectComma
case !inObj:
d.state = stateArrayComma
}
return tok, nil
case Comma:
d.state = stateObjectString
return d.NextToken()
default:
return tok, fmt.Errorf("stateObjectComma: expecting comma")
}
}
func (d *Decoder) stateArrayValue(tok []byte) ([]byte, error) {
switch tok[0] {
case '{':
d.state = stateObjectString
d.push(true)
return tok, nil
case '[':
d.state = stateArrayValue
d.push(false)
return tok, nil
case ']':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = stateEnd
case inObj:
d.state = stateObjectComma
case !inObj:
d.state = stateArrayComma
}
return tok, nil
case ',':
return nil, fmt.Errorf("stateArrayValue: unexpected comma")
default:
d.state = stateArrayComma
return tok, nil
}
}
func (d *Decoder) stateArrayComma(tok []byte) ([]byte, error) {
switch tok[0] {
case ']':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = stateEnd
case inObj:
d.state = stateObjectComma
case !inObj:
d.state = stateArrayComma
}
return tok, nil
case Comma:
d.state = stateArrayValue
return d.NextToken()
default:
return nil, fmt.Errorf("stateArrayComma: expected comma, %v", d.stack)
}
}
func (d *Decoder) stateValue(tok []byte) ([]byte, error) {
switch tok[0] {
case '{':
d.state = stateObjectString
d.push(true)
return tok, nil
case '[':
d.state = stateArrayValue
d.push(false)
return tok, nil
case ',':
return nil, fmt.Errorf("stateValue: unexpected comma")
default:
d.state = stateEnd
return tok, nil
}
}