| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/netip" |
| 7 | ) |
| 8 | |
| 9 | type Slice[T any] struct { |
| 10 | x []T |
| 11 | } |
| 12 | |
| 13 | type IPPrefixSlice struct { |
| 14 | x Slice[netip.Prefix] |
| 15 | } |
| 16 | |
| 17 | func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.x) } |
| 18 | |
| 19 | // MarshalJSON implements json.Marshaler. |
| 20 | func (v IPPrefixSlice) MarshalJSON() ([]byte, error) { |
| 21 | return v.x.MarshalJSON() |
| 22 | } |
| 23 | |
| 24 | func main() { |
| 25 | t := IPPrefixSlice{} |
| 26 | fmt.Println(t) |
| 27 | b, e := t.MarshalJSON() |
| 28 | fmt.Println(string(b), e) |
| 29 | } |
| 30 | |
| 31 | // Output: |
| 32 | // {{[]}} |
| 33 | // null <nil> |