Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json.NewDecoder Decode not return err when input invalid #41899

Closed
zhoujie0101 opened this issue Oct 10, 2020 · 3 comments
Closed

json.NewDecoder Decode not return err when input invalid #41899

zhoujie0101 opened this issue Oct 10, 2020 · 3 comments

Comments

@zhoujie0101
Copy link

zhoujie0101 commented Oct 10, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.2 darwin/amd64

What did you do?

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
)

func main() {
	p := "404 not found"
	var m interface{}
	dec := json.NewDecoder(bytes.NewBuffer([]byte(p)))
	dec.UseNumber()
	err := dec.Decode(&m)
        // the err is nil
	if err != nil {
		fmt.Println(err)
	}
        // 404
	fmt.Println(m)

	err = json.Unmarshal([]byte(p), &m)
        // the err is not nil
	if err != nil {
		fmt.Println(err)
	}
        // 404
	fmt.Println(m)
}

What did you expect to see?

both m is 404, but the first err is nil, and the second err is "invalid character 'n' after top-level value". Maybe both should reutrn error?

@fraenkel
Copy link
Contributor

see #36225

@zhoujie0101
Copy link
Author

see #36225

thanks

@mvdan
Copy link
Member

mvdan commented Oct 10, 2020

Closing as a dup.

@mvdan mvdan closed this as completed Oct 10, 2020
@golang golang locked and limited conversation to collaborators Oct 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants