Closed dataBaseError closed 4 years ago
With the first two examples:
data = Example{
Data: []string{"a", "b", "c"},
}
and
data = Example{
Data: []string{},
}
The builtin encoding/json
is interpreting these as non-nil slices and thus writing out []
instead of null
.
If you use:
data = Example{
Data: null,
}
data = Example{}
You'll see that the builting Go json encoder and sjson both work the same.
Here's another example
My mistake, I didn't correctly initialise my example using the native library. Thank you for correcting me.
When an slice is not initialised empty (e.g.
var list []string
) the value is set asnull
instead of[]
. However if we initialised the slice (e.g.var list := []string{}
) the value is set as[]
Is this the expected behaviour or a bug? It is inconsistent with the
encoding/json
which in both cases will produce[]
.Below is a simple program that highlights this issue including the output on my system. The output
test3
is the one of note, where the array is output asnull
.Output: