Source File
decode.go
Belonging Package
google.golang.org/protobuf/internal/encoding/json
package json
import (
)
var ErrUnexpectedEOF = errors.New("%v", io.ErrUnexpectedEOF)
func ( *Decoder) () (Token, error) {
const = Null | Bool | Number | String
defer func() { .lastCall = readCall }()
if .lastCall == peekCall {
return .lastToken, .lastErr
}
, := .parseNext()
if != nil {
return Token{},
}
switch .kind {
case EOF:
if len(.openStack) != 0 ||
.lastToken.kind&|ObjectClose|ArrayClose == 0 {
return Token{}, ErrUnexpectedEOF
}
case Null:
if !.isValueNext() {
return Token{}, .newSyntaxError(.pos, unexpectedFmt, .RawString())
}
case Bool, Number:
if !.isValueNext() {
return Token{}, .newSyntaxError(.pos, unexpectedFmt, .RawString())
}
case String:
if .isValueNext() {
break
if .lastToken.kind&(ObjectOpen|comma) == 0 {
return Token{}, .newSyntaxError(.pos, unexpectedFmt, .RawString())
}
if len(.in) == 0 {
return Token{}, ErrUnexpectedEOF
}
if := .in[0]; != ':' {
return Token{}, .newSyntaxError(.currPos(), `unexpected character %s, missing ":" after field name`, string())
}
.kind = Name
.consume(1)
case ObjectOpen, ArrayOpen:
if !.isValueNext() {
return Token{}, .newSyntaxError(.pos, unexpectedFmt, .RawString())
}
.openStack = append(.openStack, .kind)
case ObjectClose:
if len(.openStack) == 0 ||
.lastToken.kind == comma ||
.openStack[len(.openStack)-1] != ObjectOpen {
return Token{}, .newSyntaxError(.pos, unexpectedFmt, .RawString())
}
.openStack = .openStack[:len(.openStack)-1]
case ArrayClose:
if len(.openStack) == 0 ||
.lastToken.kind == comma ||
.openStack[len(.openStack)-1] != ArrayOpen {
return Token{}, .newSyntaxError(.pos, unexpectedFmt, .RawString())
}
.openStack = .openStack[:len(.openStack)-1]
case comma:
if len(.openStack) == 0 ||
.lastToken.kind&(|ObjectClose|ArrayClose) == 0 {
return Token{}, .newSyntaxError(.pos, unexpectedFmt, .RawString())
}
}
var errRegexp = regexp.MustCompile(`^([-+._a-zA-Z0-9]{1,32}|.)`)
.consume(0)
:= .in
if len() == 0 {
return .consumeToken(EOF, 0), nil
}
switch [0] {
case 'n':
if := matchWithDelim("null", ); != 0 {
return .consumeToken(Null, ), nil
}
case 't':
if := matchWithDelim("true", ); != 0 {
return .consumeBoolToken(true, ), nil
}
case 'f':
if := matchWithDelim("false", ); != 0 {
return .consumeBoolToken(false, ), nil
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
if , := parseNumber(); {
return .consumeToken(Number, ), nil
}
case '"':
, , := .parseString()
if != nil {
return Token{},
}
return .consumeStringToken(, ), nil
case '{':
return .consumeToken(ObjectOpen, 1), nil
case '}':
return .consumeToken(ObjectClose, 1), nil
case '[':
return .consumeToken(ArrayOpen, 1), nil
case ']':
return .consumeToken(ArrayClose, 1), nil
case ',':
return .consumeToken(comma, 1), nil
}
return Token{}, .newSyntaxError(.currPos(), "invalid value %s", errRegexp.Find())
}
func ( *Decoder) () bool {
if len(.openStack) == 0 {
return .lastToken.kind == 0
}
:= .openStack[len(.openStack)-1]
switch {
case ObjectOpen:
return .lastToken.kind&Name != 0
case ArrayOpen:
return .lastToken.kind&(ArrayOpen|comma) != 0
}
panic(fmt.Sprintf(
"unreachable logic in Decoder.isValueNext, lastToken.kind: %v, openStack: %v",
.lastToken.kind, ))
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |