mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
add map key example
This commit is contained in:
parent
0ac74bba4a
commit
a377e2656b
@ -3,6 +3,7 @@ package jsoniter
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExampleMarshal() {
|
||||
@ -93,3 +94,30 @@ func ExampleGet() {
|
||||
// Output:
|
||||
// Crimson
|
||||
}
|
||||
|
||||
func ExampleMapKey() {
|
||||
hello := MyKey("hello")
|
||||
output, _ := Marshal(map[*MyKey]string{&hello: "world"})
|
||||
fmt.Println(string(output))
|
||||
obj := map[*MyKey]string{}
|
||||
Unmarshal(output, &obj)
|
||||
for k, v := range obj {
|
||||
fmt.Println(*k, v)
|
||||
}
|
||||
// Output:
|
||||
// {"Hello":"world"}
|
||||
// Hel world
|
||||
}
|
||||
|
||||
type MyKey string
|
||||
|
||||
|
||||
func (m *MyKey) MarshalText() ([]byte, error) {
|
||||
return []byte(strings.Replace(string(*m), "h", "H", -1)), nil
|
||||
}
|
||||
|
||||
func (m *MyKey) UnmarshalText(text []byte) error {
|
||||
*m = MyKey(text[:3])
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user