gojson: added MarshalSafeCollections
This commit is contained in:
@@ -1237,3 +1237,49 @@ func TestMarshalerError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalSafeCollections(t *testing.T) {
|
||||
var (
|
||||
nilSlice []interface{}
|
||||
pNilSlice *[]interface{}
|
||||
nilMap map[string]interface{}
|
||||
pNilMap *map[string]interface{}
|
||||
)
|
||||
|
||||
type (
|
||||
nilSliceStruct struct {
|
||||
NilSlice []interface{} `json:"nil_slice"`
|
||||
}
|
||||
nilMapStruct struct {
|
||||
NilMap map[string]interface{} `json:"nil_map"`
|
||||
}
|
||||
)
|
||||
|
||||
tests := []struct {
|
||||
in interface{}
|
||||
want string
|
||||
}{
|
||||
{nilSlice, "[]"},
|
||||
{[]interface{}{}, "[]"},
|
||||
{make([]interface{}, 0), "[]"},
|
||||
{[]int{1, 2, 3}, "[1,2,3]"},
|
||||
{pNilSlice, "null"},
|
||||
{nilSliceStruct{}, "{\"nil_slice\":[]}"},
|
||||
{nilMap, "{}"},
|
||||
{map[string]interface{}{}, "{}"},
|
||||
{make(map[string]interface{}, 0), "{}"},
|
||||
{map[string]interface{}{"1": 1, "2": 2, "3": 3}, "{\"1\":1,\"2\":2,\"3\":3}"},
|
||||
{pNilMap, "null"},
|
||||
{nilMapStruct{}, "{\"nil_map\":{}}"},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
b, err := MarshalSafeCollections(tt.in, true, true)
|
||||
if err != nil {
|
||||
t.Errorf("test %d, unexpected failure: %v", i, err)
|
||||
}
|
||||
if got := string(b); got != tt.want {
|
||||
t.Errorf("test %d, Marshal(%#v) = %q, want %q", i, tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user