Skip to content

Commit 19c9367

Browse files
committed
encoding/json: improve decoder alloc count
1 parent 4f48ad5 commit 19c9367

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/encoding/json/scanner.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@ func checkValid(data []byte, scan *scanner) error {
4545
// A SyntaxError is a description of a JSON syntax error.
4646
// [Unmarshal] will return a SyntaxError if the JSON can't be parsed.
4747
type SyntaxError struct {
48-
msg string // description of error
49-
Offset int64 // error occurred after reading Offset bytes
48+
msg string // description of error
49+
Offset int64 // error occurred after reading Offset bytes
50+
invalidCharContext string // invalid character error context
51+
invalidChar byte // the invalid character
5052
}
5153

52-
func (e *SyntaxError) Error() string { return e.msg }
54+
func (e *SyntaxError) Error() string {
55+
if e.invalidCharContext != "" {
56+
return "invalid character " + quoteChar(e.invalidChar) + " " + e.invalidCharContext
57+
}
58+
return e.msg
59+
}
5360

5461
// A scanner is a JSON scanning state machine.
5562
// Callers call scan.reset and then pass bytes in one at a time
@@ -168,7 +175,7 @@ func (s *scanner) eof() int {
168175
return scanEnd
169176
}
170177
if s.err == nil {
171-
s.err = &SyntaxError{"unexpected end of JSON input", s.bytes}
178+
s.err = &SyntaxError{"unexpected end of JSON input", s.bytes, "", 0}
172179
}
173180
return scanError
174181
}
@@ -590,7 +597,7 @@ func stateError(s *scanner, c byte) int {
590597
// error records an error and switches to the error state.
591598
func (s *scanner) error(c byte, context string) int {
592599
s.step = stateError
593-
s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes}
600+
s.err = &SyntaxError{invalidCharContext: context, invalidChar: c, Offset: s.bytes}
594601
return scanError
595602
}
596603

src/encoding/json/stream.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (dec *Decoder) tokenPrepareForDecode() error {
312312
return err
313313
}
314314
if c != ',' {
315-
return &SyntaxError{"expected comma after array element", dec.InputOffset()}
315+
return &SyntaxError{"expected comma after array element", dec.InputOffset(), "", 0}
316316
}
317317
dec.scanp++
318318
dec.tokenState = tokenArrayValue
@@ -322,7 +322,7 @@ func (dec *Decoder) tokenPrepareForDecode() error {
322322
return err
323323
}
324324
if c != ':' {
325-
return &SyntaxError{"expected colon after object key", dec.InputOffset()}
325+
return &SyntaxError{"expected colon after object key", dec.InputOffset(), "", 0}
326326
}
327327
dec.scanp++
328328
dec.tokenState = tokenObjectValue
@@ -475,7 +475,7 @@ func (dec *Decoder) tokenError(c byte) (Token, error) {
475475
case tokenObjectComma:
476476
context = " after object key:value pair"
477477
}
478-
return nil, &SyntaxError{"invalid character " + quoteChar(c) + context, dec.InputOffset()}
478+
return nil, &SyntaxError{invalidChar: c, invalidCharContext: context, Offset: dec.InputOffset()}
479479
}
480480

481481
// More reports whether there is another element in the

0 commit comments

Comments
 (0)