-
Notifications
You must be signed in to change notification settings - Fork 136
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
Encode nil slices as empty arrays #62
Comments
Thanks for posting this issue @lukechampine. Definitely something we need to fix (marking this as a bug)! |
Read more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By default, the
json
package marshals zero-valued (nil) slices asnull
, whereas merely empty slices are marshalled as[]
:If the JSON is later consumed by a Go client, this generally isn't an issue, since both
null
and[]
will be decoded as a slice with no elements, and Go code typically doesn't distinguish between nil and empty slices. However, clients in other languages may choke onnull
, e.g.:To avoid this, zero-valued slices should be replaced with empty slices prior to encoding. There are a few ways to do this, but unfortunately none of them are trivial. Perhaps the best approach for Rosetta would be to use a JSON codegen tool such as
easyjson
, which supports aNilSliceAsEmpty
option. This would also improve encoding/decoding performance.The text was updated successfully, but these errors were encountered: