1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:24:41 -07:00
Change-Id: I52bc6ba07505f2961e8bcac66d3159b280218960
This commit is contained in:
Mateusz Poliwczak 2024-05-20 20:12:18 +02:00
parent c3644388d4
commit 338a31d721

View File

@ -19,7 +19,7 @@ type Decoder struct {
scanned int64 // amount of data already scanned scanned int64 // amount of data already scanned
scan scanner scan scanner
err error err error
errFromMore bool errFromMore error
tokenState int tokenState int
tokenStack []int tokenStack []int
@ -48,14 +48,14 @@ func (dec *Decoder) DisallowUnknownFields() { dec.d.disallowUnknownFields = true
// See the documentation for [Unmarshal] for details about // See the documentation for [Unmarshal] for details about
// the conversion of JSON into a Go value. // the conversion of JSON into a Go value.
func (dec *Decoder) Decode(v any) error { func (dec *Decoder) Decode(v any) error {
if dec.err != nil { if dec.errFromMore != nil {
err := dec.err err := dec.errFromMore
if dec.errFromMore { dec.errFromMore = nil
dec.err = nil
dec.errFromMore = false
}
return err return err
} }
if dec.err != nil {
return dec.err
}
if err := dec.tokenPrepareForDecode(); err != nil { if err := dec.tokenPrepareForDecode(); err != nil {
return err return err
@ -371,10 +371,9 @@ func (d Delim) String() string {
// to mark the start and end of arrays and objects. // to mark the start and end of arrays and objects.
// Commas and colons are elided. // Commas and colons are elided.
func (dec *Decoder) Token() (Token, error) { func (dec *Decoder) Token() (Token, error) {
if dec.err != nil && dec.errFromMore { if dec.errFromMore != nil {
err := dec.err err := dec.errFromMore
dec.err = nil dec.errFromMore = nil
dec.errFromMore = false
return nil, err return nil, err
} }
@ -496,10 +495,14 @@ func (dec *Decoder) More() bool {
c, err := dec.peek() c, err := dec.peek()
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
// Preserve the io.EOF error, next read calls from the io.Reader
// do not have to return the io.EOF error, but we've made
// a decision based on that error, so it is better to return it from
// next Token or Decode call.
dec.errFromMore = io.EOF
return false return false
} }
dec.err = err dec.errFromMore = err
dec.errFromMore = true
return true return true
} }
return c != ']' && c != '}' return c != ']' && c != '}'