You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, the json package marshals zero-valued (nil) slices as null, whereas merely empty slices are marshalled as []:
vars []intjson.Marshal(s) // -> nulls= []int{}
json.Marshal(s) // -> []
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 on null, e.g.:
letarr=request(endpoint)letlen=arr.length// invalid if arr === null
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 a NilSliceAsEmpty option. This would also improve encoding/decoding performance.
The text was updated successfully, but these errors were encountered:
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: