]> Cypherpunks repositories - gostls13.git/commitdiff
undo CL 11161044 / ba455262a9db
authorRuss Cox <rsc@golang.org>
Fri, 12 Jul 2013 21:42:01 +0000 (17:42 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 12 Jul 2013 21:42:01 +0000 (17:42 -0400)
I want to think more carefully about this.

We put this in because Marshal encoded named []byte but Unmarshal rejected them.
And we noticed that Marshal's behavior was undocumented so we documented it.
But I am starting to think the docs and Unmarshal were correct and Marshal's
behavior was the problem.

Rolling back to give us more time to think.

««« original CL description
json: unmarshal types that are byte slices.

The json package cheerfully would marshal

        type S struct {
                IP net.IP
        }

but would give an error when unmarshalling.  This change allows any
type whose concrete type is a byte slice to be unmarshalled from a
string.

Fixes #5086.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/11161044

»»»

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/11042046

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

index e608ef4a61dc49d2a20719bdf1e37e224240c39a..62ac294b89f3bd113c6b63f6974a1c4f7d147dcb 100644 (file)
@@ -660,7 +660,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
                default:
                        d.saveError(&UnmarshalTypeError{"string", v.Type()})
                case reflect.Slice:
-                       if v.Type().Elem().Kind() != reflect.Uint8 {
+                       if v.Type() != byteSliceType {
                                d.saveError(&UnmarshalTypeError{"string", v.Type()})
                                break
                        }
index dfc688cdc4e9e203f1c4edd7f55bb96a07793c46..65e6d6ec38243867d79a76d20eeb293d18744818 100644 (file)
@@ -1186,32 +1186,3 @@ func TestSkipArrayObjects(t *testing.T) {
                t.Errorf("got error %q, want nil", err)
        }
 }
-
-// Test that types of byte slices (such as net.IP) both
-// marshal and unmarshal.
-func TestByteSliceType(t *testing.T) {
-       type A []byte
-       type S struct {
-               A A
-       }
-
-       for x, in := range []S{
-               S{},
-               S{A: []byte{'1'}},
-               S{A: []byte{'1', '2', '3', '4', '5'}},
-       } {
-               data, err := Marshal(&in)
-               if err != nil {
-                       t.Errorf("#%d: got Marshal error %q, want nil", x, err)
-                       continue
-               }
-               var out S
-               err = Unmarshal(data, &out)
-               if err != nil {
-                       t.Fatalf("#%d: got Unmarshal error %q, want nil", x, err)
-               }
-               if !reflect.DeepEqual(&out, &in) {
-                       t.Fatalf("#%d: got %v, want %v", x, &out, &in)
-               }
-       }
-}
index 7cc9398c97ab2ea413aa75cb7e5666cd428ea788..be4a7b1fa66c82b7650863fbe498628f7acf6427 100644 (file)
@@ -44,8 +44,8 @@ import (
 // The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e"
 // to keep some browsers from misinterpreting JSON output as HTML.
 //
-// Array and slice values encode as JSON arrays, except that a slice of
-// bytes encodes as a base64-encoded string, and a nil slice
+// Array and slice values encode as JSON arrays, except that
+// []byte encodes as a base64-encoded string, and a nil slice
 // encodes as the null JSON object.
 //
 // Struct values encode as JSON objects. Each exported struct field