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

encoding/json: unmarshal conversion of map with some invalid fields breaks valid fields #17425

Closed
cdelorme opened this issue Oct 13, 2016 · 2 comments

Comments

@cdelorme
Copy link

cdelorme commented Oct 13, 2016

Please answer these questions before submitting your issue. Thanks!

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

go1.7 (installed via gvm)

What operating system and processor architecture are you using (go env)?

osx (el capitan) amd64, debian linux amd64, also go playground

What did you do?

https://play.golang.org/p/om2yJ89fb_

What did you expect to see?

I expect to see "yarp" as the value in String.

What did you see instead?

When I supply specific combinations of valid json with values that do not match my structure String is empty instead of "yarp".

@spenczar
Copy link
Contributor

json.Unmarshal returns an error, which you are ignoring, which is why you're getting the zero value of the data struct. See https://play.golang.org/p/iMGK8M12Pm.

@cdelorme
Copy link
Author

Thanks for the answer; while I don't disagree that I should be catching the error the code I was originally working with had a composite struct with three properties and which correctly populated 2/3 fields in spite of the error.

Is the solution then to make sure you are only doing json unmarshal on a temporary structure that you can discard in the event of errors, or should an error prevent modifying the structure passed in?

@mikioh mikioh changed the title json unmarshal conversion of map with some invalid fields breaks valid fields encoding/json: unmarshal conversion of map with some invalid fields breaks valid fields Oct 15, 2016
@golang golang locked and limited conversation to collaborators Oct 15, 2017
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