Skip to content

Commit

Permalink
contentjson120: Add support for tailing comma
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed May 27, 2024
1 parent e146493 commit bb38f5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion common/json/internal/contextjson_120/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func stateEndValue(s *scanner, c byte) int {
case parseObjectValue:
if c == ',' {
s.parseState[n-1] = parseObjectKey
s.step = stateBeginString
s.step = stateBeginStringOrEmpty
return scanObjectValue
}
if c == '}' {
Expand Down
22 changes: 21 additions & 1 deletion common/json/internal/contextjson_120/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ func (dec *Decoder) Token() (Token, error) {

case '}':
if dec.tokenState != tokenObjectStart && dec.tokenState != tokenObjectComma {
if dec.tokenState != tokenObjectStart && dec.tokenState != tokenObjectComma && dec.tokenState != tokenObjectKey {
return dec.tokenError(c)
}
dec.scanp++
Expand Down Expand Up @@ -487,7 +488,26 @@ func (dec *Decoder) tokenError(c byte) (Token, error) {
// current array or object being parsed.
func (dec *Decoder) More() bool {
c, err := dec.peek()
return err == nil && c != ']' && c != '}'
//return err == nil && c != ']' && c != '}'
if err != nil {
return false
}
if c == ']' || c == '}' {
return false
}
if c == ',' {
scanp := dec.scanp
dec.scanp++
c, err = dec.peek()
dec.scanp = scanp
if err != nil {
return false
}
if c == ']' || c == '}' {
return false
}
}
return true
}

func (dec *Decoder) peek() (byte, error) {
Expand Down

0 comments on commit bb38f5c

Please sign in to comment.