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 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

@fraenkel fraenkel commented Oct 10, 2020

see #36225

@zhoujie0101
Copy link
Author

@zhoujie0101 zhoujie0101 commented Oct 10, 2020

see #36225

thanks

@mvdan
Copy link
Member

@mvdan mvdan commented Oct 10, 2020

Closing as a dup.

@mvdan mvdan closed this Oct 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.