aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/encoding/json/decode_test.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2012-02-19 00:27:05 -0500
committerRuss Cox <rsc@golang.org>2012-02-19 00:27:05 -0500
commit990f9f4c007cb8b74fafd5c3d4800de86a7f2295 (patch)
tree2546bdbbbf5db1c3de7d21ca0de4d845daf9a8f0 /src/pkg/encoding/json/decode_test.go
parent89b075cc90f260edaa4973bd25258ee653a37a2f (diff)
downloadgo-990f9f4c007cb8b74fafd5c3d4800de86a7f2295.tar.xz
encoding/json: disable anonymous fields
We should, after Go 1, make them work the same as package xml, that is, make them appear in the outer struct. For now turn them off so that people do not depend on the old behavior. Fixing them is issue 3069. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5656102
Diffstat (limited to 'src/pkg/encoding/json/decode_test.go')
-rw-r--r--src/pkg/encoding/json/decode_test.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/pkg/encoding/json/decode_test.go b/src/pkg/encoding/json/decode_test.go
index 775becfa7c..0eec586a9b 100644
--- a/src/pkg/encoding/json/decode_test.go
+++ b/src/pkg/encoding/json/decode_test.go
@@ -619,3 +619,32 @@ func TestRefUnmarshal(t *testing.T) {
t.Errorf("got %+v, want %+v", got, want)
}
}
+
+// Test that anonymous fields are ignored.
+// We may assign meaning to them later.
+func TestAnonymous(t *testing.T) {
+ type S struct {
+ T
+ N int
+ }
+
+ data, err := Marshal(new(S))
+ if err != nil {
+ t.Fatalf("Marshal: %v", err)
+ }
+ want := `{"N":0}`
+ if string(data) != want {
+ t.Fatalf("Marshal = %#q, want %#q", string(data), want)
+ }
+
+ var s S
+ if err := Unmarshal([]byte(`{"T": 1, "T": {"Y": 1}, "N": 2}`), &s); err != nil {
+ t.Fatalf("Unmarshal: %v", err)
+ }
+ if s.N != 2 {
+ t.Fatal("Unmarshal: did not set N")
+ }
+ if s.T.Y != 0 {
+ t.Fatal("Unmarshal: did set T.Y")
+ }
+}