go-ipfs-cmds icon indicating copy to clipboard operation
go-ipfs-cmds copied to clipboard

Sanitize null arrays to []

Open Kubuxu opened this issue 6 years ago • 3 comments

Currently, depending on circumstances arrays can be either Marshalled to JSON as null or []. It would be best if we send just one format, possibly the [].

Kubuxu avatar Mar 16 '18 14:03 Kubuxu

As a consumer of the HTTP API, I would greatly prefer to receive an empty array compared to null, since you don't really have to have any special logic, you just can treat it as an array while forEach with null would fail for example.

victorb avatar Mar 16 '18 14:03 victorb

I agree that this would be preferable, but unfortunately I don't really know how to do that without either (a) initializing all nil slices to empty slices or (b) using custom types for all slices with MarshalJSON functions that encode nil values to '[]'. Both of these can not be fixed in this library, but on the code that determines which values to be sent (i.e. go-ipfs).

I'll try to find all instances in this package, though.

keks avatar Apr 10 '18 09:04 keks

@keks I've filed a feature request to always encode slices/maps to [] and {} with refmt (a marshaling library we're planning on using for IPLD stuff): https://github.com/polydawn/refmt/issues/25

If that works out, we can just replace the json library with that one.

Stebalien avatar Apr 18 '18 04:04 Stebalien