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: a way for MarshalJSON to skip marshaling a field? #4554

Closed
minux opened this issue Dec 15, 2012 · 6 comments
Closed

encoding/json: a way for MarshalJSON to skip marshaling a field? #4554

minux opened this issue Dec 15, 2012 · 6 comments
Milestone

Comments

@minux
Copy link
Member

minux commented Dec 15, 2012

for example, one might want to solve issue #4357 with this.

a test program: http://play.golang.org/p/gyk1yehoTp

i think we can treat a nil byte slice as the signal.
@dsymonds
Copy link
Contributor

Comment 1:

issue #4357 is for this, via an explicit IsZero and using omitempty.

Status changed to Duplicate.

Merged into issue #4357.

@minux
Copy link
Member Author

minux commented Dec 15, 2012

Comment 2:

i don't think this is duplicate of issue #4357, because custom JSONMarshaler still need
a way to skip marshaling the field (sort of IsZero and JSONMarshal combined).
I'm just using issue #4357 as an example.

@dsymonds
Copy link
Contributor

Comment 3:

There's no JSON field types that can omit themselves without
omitempty. Either you're proposing to add such a mechanism, or these
two proposals are equivalent.

@minux
Copy link
Member Author

minux commented Dec 15, 2012

Comment 4:

yeah, i propose to allow MarshalJSON to return a nil byte slice to
signify omitting this field. WDYT?

@dsymonds
Copy link
Contributor

Comment 5:

It seems like a bad idea to conflate them. Go typically doesn't use
sentinels. I think the IsZero approach is much clearer, plus you get
the benefit of being able to control the omission with omitempty.

@minux
Copy link
Member Author

minux commented Dec 16, 2012

Comment 6:

you're right. thank you for explanations.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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