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

Projects
None yet
4 participants
@cdelorme
Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Oct 14, 2016

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.

@bradfitz bradfitz closed this Oct 14, 2016

@cdelorme

This comment has been minimized.

Copy link
Author

commented Oct 14, 2016

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.
You can’t perform that action at this time.