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: ignores omitempty when multiple parameters used #14283

Closed
GPHofficial opened this issue Feb 10, 2016 · 2 comments

Comments

Projects
None yet
5 participants
@GPHofficial
Copy link

commented Feb 10, 2016

It's supposed to prioritize omitempty before the string type. Instead, it prioritizes string and attempts to parse the value regardless of whether an empty value

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

@ianlancetaylor ianlancetaylor changed the title json library ignores omitempty when multiple parameters used encoding/json: ignores omitempty when multiple parameters used Feb 10, 2016

@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Feb 10, 2016

@joeshaw

This comment has been minimized.

Copy link
Contributor

commented Feb 23, 2016

omitempty only applies to JSON encoding, not decoding. (That appears to also be the case with encoding/asn1 and encoding/xml)

Go's encoding of that zero-value struct with omitempty is {}. If the Id field still had string set but not omitempty, it encodes as {"id": "0"}. It properly decodes both of those cases.

I'm not sure it's the correct thing to interpret "" as "0" for string-encoded integer fields. I know Go allows for this kind of encoding for 64-bit integers for safety (because JS only has 53-bit integer precision, IIRC), but is it a reasonably well specified practice? It looks like it was introduced in https://golang.org/cl/4918051

@bradfitz

This comment has been minimized.

Copy link
Member

commented Feb 23, 2016

Yeah, it looks like this is working as intended.

@bradfitz bradfitz closed this Feb 23, 2016

@golang golang locked and limited conversation to collaborators Feb 28, 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.