]> Cypherpunks repositories - gostls13.git/commitdiff
json: Properly handle nil slices.
authorAlexander Reece <awreece@gmail.com>
Mon, 31 Oct 2011 17:59:23 +0000 (13:59 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 31 Oct 2011 17:59:23 +0000 (13:59 -0400)
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

src/pkg/json/decode.go
src/pkg/json/decode_test.go
src/pkg/json/encode.go
src/pkg/json/encode_test.go

index cd4b5f12c2d04b52dc55c41d56530a1d85b42e6a..800df985abc9212c78ab44b617b697a4f7883d30 100644 (file)
@@ -588,7 +588,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value) {
                switch v.Kind() {
                default:
                        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()))
                }
 
index 6a6c32d292e95844571055c407f087f8676b768b..d745e8dd26f864bbfcff9275e8dd39f626f16df9 100644 (file)
@@ -456,7 +456,7 @@ var allValueIndent = `{
        "PSlice": null,
        "PSliceP": null,
        "EmptySlice": [],
-       "NilSlice": [],
+       "NilSlice": null,
        "StringSlice": [
                "str24",
                "str25",
@@ -528,8 +528,8 @@ var pallValueIndent = `{
        },
        "EmptyMap": null,
        "NilMap": null,
-       "Slice": [],
-       "SliceP": [],
+       "Slice": null,
+       "SliceP": null,
        "PSlice": [
                {
                        "Tag": "tag20"
@@ -547,10 +547,10 @@ var pallValueIndent = `{
                        "Tag": "tag23"
                }
        ],
-       "EmptySlice": [],
-       "NilSlice": [],
-       "StringSlice": [],
-       "ByteSlice": "",
+       "EmptySlice": null,
+       "NilSlice": null,
+       "StringSlice": null,
+       "ByteSlice": null,
        "Small": {
                "Tag": ""
        },
index 46abe4360ed6c59d12baf8df4507ba16abd132e0..ba5c15cc4969e5279edb9fe35e2d83f2ef089da3 100644 (file)
@@ -352,7 +352,15 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) {
                }
                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 {
                        e.WriteByte('"')
                        s := v.Interface().([]byte)
index f85bb6216a276051f299570ae954a0ac90c62a78..92f266aba63819bf87dcb26be26b436f7e9515d0 100644 (file)
@@ -28,7 +28,7 @@ type Optionals struct {
 var optionalsExpected = `{
  "sr": "",
  "omitempty": 0,
- "slr": [],
+ "slr": null,
  "mr": {}
 }`