44[ ![ Coverage Status] ( https://coveralls.io/repos/github/barweiss/go-tuple/badge.svg )] ( https://coveralls.io/github/barweiss/go-tuple )
55[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/barweiss/go-tuple )] ( https://goreportcard.com/report/github.com/barweiss/go-tuple )
66[ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/barweiss/go-tuple.svg )] ( https://pkg.go.dev/github.com/barweiss/go-tuple )
7- [ ![ Mentioned in Awesome Go] ( https://awesome.re/mentioned-badge.svg )] ( https://github.com/avelino/awesome-go )
7+ [ ![ Mentioned in Awesome Go] ( https://awesome.re/mentioned-badge.svg )] ( https://github.com/avelino/awesome-go )
88
99Go 1.18+ tuple implementation.
1010
@@ -79,6 +79,35 @@ tup := tuple.New2(5, "hi!")
7979a , b := tup.Values ()
8080```
8181
82+ ## JSON Marshalling
83+
84+ Tuples are marshalled and unmarshalled as JSON arrays.
85+
86+ ``` go
87+ type User struct {
88+ Name string ` json:"name"`
89+ Age int ` json:"age,omitempty"`
90+ }
91+
92+ type MyJSON struct {
93+ Users []tuple.T2 [string , User ] ` json:"users"`
94+ }
95+
96+ func main () {
97+ data := MyJSON{
98+ Users: []tuple.T2 [string , User]{
99+ tuple.New2 (" foo" , User{Name: " foo" , Age: 42 }),
100+ tuple.New2 (" bar" , User{Name: " bar" , Age: 21 }),
101+ tuple.New2 (" baz" , User{Name: " baz" }),
102+ },
103+ }
104+
105+ marshalled , _ := json.Marshal (data)
106+ fmt.Printf (" %s \n " , string (marshalled))
107+ // Outputs: {"users":[["foo",{"name":"foo","age":42}],["bar",{"name":"bar","age":21}],["baz",{"name":"baz"}]]}
108+ }
109+ ```
110+
82111## Comparison
83112
84113Tuples are compared from the first element to the last.
@@ -101,6 +130,7 @@ fmt.Println(tups) // [["bar", -4, 43], ["foo", 2, -23], ["foo", 72, 15]].
101130```
102131
103132---
133+
104134** NOTE**
105135
106136In order to compare tuples, all tuple elements must match ` constraints.Ordered ` .
@@ -165,7 +195,7 @@ func main() {
165195}
166196```
167197
168- In order to call the complex types variation of the comparable functions, __ all __ tuple types must match the ` Comparable ` constraint.
198+ In order to call the complex types variation of the comparable functions, ** all ** tuple types must match the ` Comparable ` constraint.
169199
170200While this is not ideal, this a known inconvenience given the current type parameters capabilities in Go.
171201Some solutions have been porposed for this issue ([ lesser] ( https://github.com/lelysses/lesser ) , for example, beatifully articulates the issue),
0 commit comments