-
Notifications
You must be signed in to change notification settings - Fork 17.4k
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: marshaling nil value stored in interface value doesn't omitempty #16750
Comments
This looks like fallout from https://golang.org/doc/faq#nil_error |
Yes, that must be the case. Perhaps the JSON marshaller should use reflect.ValueOf(res.Err).IsNil() to determine whether an interface is empty and should be omitted per the annotation. |
This appears to resolve the issue: jnross@36aa259 I'll see about contributing this patch directly per the instructions here: https://golang.org/doc/contribute.html |
Working as intended. If you have
Then if I = 0 it gets omitted, or if S is "" it gets omitted, or if J is nil it gets omitted. But if J is 0 or "", it does not get omitted. That's not considered "empty" for an interface. |
No, this issue still occurs as originally reported. See my example: https://play.golang.org/p/uw6jvKN-qX |
go version
)?1.7, also verified with 1.6.3
go env
)?darwin/amd64
I have a struct "result" defined with one of the members "Err" as an interface type "error". The json annotation specifies "omitempty". I define another struct type concreteError which implements this interface on the pointer receiver (i.e. *concreteError implements the interface).
If I assign a nil pointer variable to result.Err, the field is not omitted from the marshaled JSON as expected. Instead the field is marshaled as a JSON null.
If I assign the nil literal to result.Err, the field is omitted from the marshaled JSON as expected
This issue is clearly demonstrated here: https://play.golang.org/p/uw6jvKN-qX
A nil interface value should be omitted from the marshaled JSON when the field is annotated with "omitempty"
The nil interface value was not omitted.
The text was updated successfully, but these errors were encountered: