Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v. See the documentation for [Unmarshal] for details about the conversion of JSON into a Go value.
(v any)
| 46 | // See the documentation for [Unmarshal] for details about |
| 47 | // the conversion of JSON into a Go value. |
| 48 | func (dec *Decoder) Decode(v any) error { |
| 49 | if dec.err != nil { |
| 50 | return dec.err |
| 51 | } |
| 52 | |
| 53 | if err := dec.tokenPrepareForDecode(); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | if !dec.tokenValueAllowed() { |
| 58 | return &SyntaxError{msg: "not at beginning of value", Offset: dec.InputOffset()} |
| 59 | } |
| 60 | |
| 61 | // Read whole value into buffer. |
| 62 | n, err := dec.readValue() |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | dec.d.init(dec.buf[dec.scanp : dec.scanp+n]) |
| 67 | dec.scanp += n |
| 68 | |
| 69 | // Don't save err from unmarshal into dec.err: |
| 70 | // the connection is still usable since we read a complete JSON |
| 71 | // object from it before the error happened. |
| 72 | err = dec.d.unmarshal(v) |
| 73 | |
| 74 | // fixup token streaming state |
| 75 | dec.tokenValueEnd() |
| 76 | |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | // Buffered returns a reader of the data remaining in the Decoder's |
| 81 | // buffer. The reader is valid until the next call to [Decoder.Decode]. |
no test coverage detected