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: unmarshalling same field into two struct fields #15127
Comments
Hopefully this is fixable without compatibility worries. |
I took a look at this. I don't have a complete answer for it, but want to document my findings here. This behavior is caused by the "annihilation" logic shared between encoding and decoding. For encoding, this makes sense: since the two sources might have different value, we might as well not write anything. One might argue that this should be an error. I'm not sure what the rationale is, but the doc on dominantField function ( https://golang.org/src/encoding/json/encode.go#L1139 ) and a test (TestDuplicatedFieldDisappears) indicate that it's a deliberate decision. Since encode makes a decision to not error, I think we should refrain from returning an error from the decode side. One way to solve this issue is to give decode its own version of dominantField that does "union" instead of "annihilation", setting all "conflicted" fields to the same value. Of course, another way out is to document this behavior and keep the implementation as is. What do you think? (Please let me know if golang-dev is a better place for this discussion.) |
ping @bradfitz . Is this too late for 1.7 freeze? |
Things aren't frozen yet, but this would need to have a decision and an implementation soon. I'm not familiar with any past decisions here. /cc @rsc |
I thought a little more about this. Currently |
The annihilation logic is intentional. I don't think we can change this. This use case is already defined to ignore both. I apologize. |
Please answer these questions before submitting your issue. Thanks!
go version
)?1.6
go env
)?linux, amd64
I'm trying to unmarshal the same JSON field into two struct fields.
http://play.golang.org/p/K86eAWaVyr
Struct fields must have JSON value or json.Unmarshal() method must return an error.
Struct fields are empty and no errors returned.
The text was updated successfully, but these errors were encountered: