-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: Marshaller omits false booleans as if they were nil #13284
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
Comments
This is working as intended. |
Actually that's slightly misleading: it's not that
|
If you need to make a distinction between a non-existent boolean and false boolean, use a *bool in your struct, like this:
You'll have to do the extra work of checking that t.Autoplay is not nil, but that's unavoidable - it sounds like you want this ternary logic. |
@cespare Considering golang's design where primitive types are always initialized to empty values, your comment makes perfect sense. So I realize this is not a bug, but it's also somewhat confusing and perhaps even unexpected. When writing @spenczar that's a great idea, thanks. That's exactly what I was after. |
A boolean only has two values. You can either send false or not, depending on what tag you put on the variable. If you need a three-valued boolean, that's not a boolean. As @spenczar suggested, *bool is a good choice in this case. |
fixes the issue where the client would send an empty request on `deis routing:disable`. see golang/go#13284
fixes the issue where the client would send an empty request on `deis routing:disable`. see golang/go#13284
It appears that the
Marshal
function fromencoding/json
treats false booleans as if they werenil
values, and thus it omits them from the resulted json. I believe this behavior is a bug. I couldn't find reference in the json spec, but it has caused some issues with my client code, that makes a distinction between a non-existent boolean and a false boolean.See example here: http://play.golang.org/p/i3cNXqorif
The text was updated successfully, but these errors were encountered: