@@ -45,11 +45,18 @@ func checkValid(data []byte, scan *scanner) error {
45
45
// A SyntaxError is a description of a JSON syntax error.
46
46
// [Unmarshal] will return a SyntaxError if the JSON can't be parsed.
47
47
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
50
52
}
51
53
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
+ }
53
60
54
61
// A scanner is a JSON scanning state machine.
55
62
// Callers call scan.reset and then pass bytes in one at a time
@@ -168,7 +175,7 @@ func (s *scanner) eof() int {
168
175
return scanEnd
169
176
}
170
177
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 }
172
179
}
173
180
return scanError
174
181
}
@@ -590,7 +597,7 @@ func stateError(s *scanner, c byte) int {
590
597
// error records an error and switches to the error state.
591
598
func (s * scanner ) error (c byte , context string ) int {
592
599
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 }
594
601
return scanError
595
602
}
596
603
0 commit comments