| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
) |
| 12 |
|
| 13 |
type A[T any] struct { |
| 14 |
F1 string `json:"t1"` |
| 15 |
F2 T `json:"t2"` |
| 16 |
B B `json:"t3"` |
| 17 |
} |
| 18 |
|
| 19 |
type B struct { |
| 20 |
F4 int `json:"t4"` |
| 21 |
} |
| 22 |
|
| 23 |
func a[T any]() { |
| 24 |
data := `{"t1":"1","t2":2,"t3":{"t4":4}}` |
| 25 |
a1 := A[T]{} |
| 26 |
if err := json.Unmarshal([]byte(data), &a1); err != nil { |
| 27 |
panic(err) |
| 28 |
} |
| 29 |
if bytes, err := json.Marshal(&a1); err != nil { |
| 30 |
panic(err) |
| 31 |
} else if string(bytes) != data { |
| 32 |
panic(string(bytes)) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func main() { |
| 37 |
a[int]() |
| 38 |
} |