summaryrefslogtreecommitdiff
path: root/decode.go
diff options
context:
space:
mode:
Diffstat (limited to 'decode.go')
-rw-r--r--decode.go38
1 files changed, 16 insertions, 22 deletions
diff --git a/decode.go b/decode.go
index 9c05860..1e90bb1 100644
--- a/decode.go
+++ b/decode.go
@@ -122,33 +122,27 @@ func Decode(r interface {
}
// DecodeThenEOF is like decode, but emits an error if there is extra data after the JSON.
-func DecodeThenEOF(r interface {
- io.Reader
- io.RuneScanner
-}, ptr any) error {
- dec := NewDecoder(r)
+func DecodeThenEOF(r io.Reader, ptr any) error {
+ return NewDecoder(r).DecodeThenEOF(ptr)
+}
+
+func (dec *Decoder) DecodeThenEOF(ptr any) (err error) {
if err := dec.Decode(ptr); err != nil {
return err
}
- for {
- c, s, t, _ := dec.io.ReadRuneType()
- switch t {
- case RuneTypeSpace:
- // do nothing
- case RuneTypeEOF:
- if s > 0 {
- return &DecodeError{
- Err: &DecodeSyntaxError{
- Err: fmt.Errorf("invalid character %q after top-level value", c),
- Offset: dec.InputOffset(),
- },
- }
- }
- return nil
- default:
- panic("should not happen")
+ c, s, t, _ := dec.io.ReadRuneType()
+ if t != RuneTypeEOF {
+ panic("should not happen")
+ }
+ if s > 0 {
+ return &DecodeError{
+ Err: &DecodeSyntaxError{
+ Err: fmt.Errorf("invalid character %q after top-level value", c),
+ Offset: dec.InputOffset(),
+ },
}
}
+ return nil
}
func (dec *Decoder) Decode(ptr any) (err error) {