Skip to content

Commit ba108c9

Browse files
rentziassianlancetaylor
authored andcommitted
encoding/json: add Decoder.InputOffset for offset access
This makes Decoder.offset public while renaming it to Decoder.InputOffset to match encoding/xml Decoder API Code changes made by Adam Stankiewicz [[email protected]] Fixes #29688 Change-Id: I86dbfd2b2da80160846e92bfa580c53d8d45e2db Reviewed-on: https://go-review.googlesource.com/c/go/+/200677 Run-TryBot: Johan Brandhorst <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent ac8966a commit ba108c9

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/encoding/json/stream.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (dec *Decoder) Decode(v interface{}) error {
5656
}
5757

5858
if !dec.tokenValueAllowed() {
59-
return &SyntaxError{msg: "not at beginning of value", Offset: dec.offset()}
59+
return &SyntaxError{msg: "not at beginning of value", Offset: dec.InputOffset()}
6060
}
6161

6262
// Read whole value into buffer.
@@ -314,7 +314,7 @@ func (dec *Decoder) tokenPrepareForDecode() error {
314314
return err
315315
}
316316
if c != ',' {
317-
return &SyntaxError{"expected comma after array element", dec.offset()}
317+
return &SyntaxError{"expected comma after array element", dec.InputOffset()}
318318
}
319319
dec.scanp++
320320
dec.tokenState = tokenArrayValue
@@ -324,7 +324,7 @@ func (dec *Decoder) tokenPrepareForDecode() error {
324324
return err
325325
}
326326
if c != ':' {
327-
return &SyntaxError{"expected colon after object key", dec.offset()}
327+
return &SyntaxError{"expected colon after object key", dec.InputOffset()}
328328
}
329329
dec.scanp++
330330
dec.tokenState = tokenObjectValue
@@ -477,7 +477,7 @@ func (dec *Decoder) tokenError(c byte) (Token, error) {
477477
case tokenObjectComma:
478478
context = " after object key:value pair"
479479
}
480-
return nil, &SyntaxError{"invalid character " + quoteChar(c) + context, dec.offset()}
480+
return nil, &SyntaxError{"invalid character " + quoteChar(c) + context, dec.InputOffset()}
481481
}
482482

483483
// More reports whether there is another element in the
@@ -506,6 +506,9 @@ func (dec *Decoder) peek() (byte, error) {
506506
}
507507
}
508508

509-
func (dec *Decoder) offset() int64 {
509+
// InputOffset returns the input stream byte offset of the current decoder position.
510+
// The offset gives the location of the end of the most recently returned token
511+
// and the beginning of the next token.
512+
func (dec *Decoder) InputOffset() int64 {
510513
return dec.scanned + int64(dec.scanp)
511514
}

0 commit comments

Comments
 (0)