encoding/json: marshaling nil value stored in interface value doesn't omitempty #16750
Labels
Milestone
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. |
jnross
added a commit
to jnross/go
that referenced
this issue
Aug 17, 2016
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 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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: