mirror of
https://github.com/golang/go
synced 2024-11-12 03:50:21 -07:00
json: Properly handle nil slices.
Marshal nil slices as null and parse null value as a nil slice. Fixes #2278. R=rsc CC=golang-dev https://golang.org/cl/5257053
This commit is contained in:
parent
288dacd016
commit
48c75c5f9c
@ -588,7 +588,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value) {
|
|||||||
switch v.Kind() {
|
switch v.Kind() {
|
||||||
default:
|
default:
|
||||||
d.saveError(&UnmarshalTypeError{"null", v.Type()})
|
d.saveError(&UnmarshalTypeError{"null", v.Type()})
|
||||||
case reflect.Interface, reflect.Ptr, reflect.Map:
|
case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
|
||||||
v.Set(reflect.Zero(v.Type()))
|
v.Set(reflect.Zero(v.Type()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -456,7 +456,7 @@ var allValueIndent = `{
|
|||||||
"PSlice": null,
|
"PSlice": null,
|
||||||
"PSliceP": null,
|
"PSliceP": null,
|
||||||
"EmptySlice": [],
|
"EmptySlice": [],
|
||||||
"NilSlice": [],
|
"NilSlice": null,
|
||||||
"StringSlice": [
|
"StringSlice": [
|
||||||
"str24",
|
"str24",
|
||||||
"str25",
|
"str25",
|
||||||
@ -528,8 +528,8 @@ var pallValueIndent = `{
|
|||||||
},
|
},
|
||||||
"EmptyMap": null,
|
"EmptyMap": null,
|
||||||
"NilMap": null,
|
"NilMap": null,
|
||||||
"Slice": [],
|
"Slice": null,
|
||||||
"SliceP": [],
|
"SliceP": null,
|
||||||
"PSlice": [
|
"PSlice": [
|
||||||
{
|
{
|
||||||
"Tag": "tag20"
|
"Tag": "tag20"
|
||||||
@ -547,10 +547,10 @@ var pallValueIndent = `{
|
|||||||
"Tag": "tag23"
|
"Tag": "tag23"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"EmptySlice": [],
|
"EmptySlice": null,
|
||||||
"NilSlice": [],
|
"NilSlice": null,
|
||||||
"StringSlice": [],
|
"StringSlice": null,
|
||||||
"ByteSlice": "",
|
"ByteSlice": null,
|
||||||
"Small": {
|
"Small": {
|
||||||
"Tag": ""
|
"Tag": ""
|
||||||
},
|
},
|
||||||
|
@ -352,7 +352,15 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) {
|
|||||||
}
|
}
|
||||||
e.WriteByte('}')
|
e.WriteByte('}')
|
||||||
|
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Slice:
|
||||||
|
if v.IsNil() {
|
||||||
|
e.WriteString("null")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Slices can be marshalled as nil, but otherwise are handled
|
||||||
|
// as arrays.
|
||||||
|
fallthrough
|
||||||
|
case reflect.Array:
|
||||||
if v.Type() == byteSliceType {
|
if v.Type() == byteSliceType {
|
||||||
e.WriteByte('"')
|
e.WriteByte('"')
|
||||||
s := v.Interface().([]byte)
|
s := v.Interface().([]byte)
|
||||||
|
@ -28,7 +28,7 @@ type Optionals struct {
|
|||||||
var optionalsExpected = `{
|
var optionalsExpected = `{
|
||||||
"sr": "",
|
"sr": "",
|
||||||
"omitempty": 0,
|
"omitempty": 0,
|
||||||
"slr": [],
|
"slr": null,
|
||||||
"mr": {}
|
"mr": {}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user