Skip to content

Inconsistent error message for unmarshalling #183

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

Closed
sudo-suhas opened this issue Oct 4, 2017 · 3 comments
Closed

Inconsistent error message for unmarshalling #183

sudo-suhas opened this issue Oct 4, 2017 · 3 comments

Comments

@sudo-suhas
Copy link

Here's some code which demonstrates the inconsistency for error message between encoding/json and jsoniter:

package main

import (
	"encoding/json"
	"fmt"

	jsoniter "github.com/json-iterator/go"
)

type myStruct struct {
	MyField string `json:"myField"`
}

func main() {
	bytes := []byte(`{ "myField": 0 }`)

	var v myStruct
	err := json.Unmarshal(bytes, &v)
	fmt.Printf("Error from encoding/json: \n\t%+v \n", err)

	err = jsoniter.Unmarshal(bytes, &v)
	fmt.Printf("Error from jsoniter.Unmarshal: \n\t%+v \n", err)

	err = jsoniter.ConfigFastest.Unmarshal(bytes, &v)
	fmt.Printf("Error from jsoniter.ConfigFastest.Unmarshal: \n\t%+v \n", err)
}

Output:

Error from encoding/json: 
	json: cannot unmarshal number into Go struct field myStruct.myField of type string 
Error from jsoniter.Unmarshal: 
	main.myStruct: MyField: ReadString: expects " or n, parsing 14 ...yField": 0... at { "myField": 0 } 
Error from jsoniter.ConfigFastest.Unmarshal: 
	main.myStruct: MyField: ReadString: expects " or n, parsing 14 ...yField": 0... at { "myField": 0 }

It would be ideal if I could get the error message same as encoding/json but I am fine with slight difference in the error message. However, the error from jsoniter includes the entire JSON string. In my case this string was quite large and I had difficulty in understanding the source of the problem.

@taowen
Copy link
Contributor

taowen commented Oct 7, 2017

so the problem to fix is this string was quite large and I had difficulty in understanding the source of the problem? How about only output the buffer around the error position?

@sudo-suhas
Copy link
Author

Yes, that should do it. Thanks!

@sudo-suhas
Copy link
Author

One more thing. The wording for the error is a little cryptic right now. If you could change it to something similar to the one from encoding/json that would be great.

taowen added a commit that referenced this issue Oct 9, 2017
@taowen taowen closed this as completed in 0149a5c Oct 9, 2017
zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants