1
0
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:
Tao Wen 2018-02-23 18:20:14 +08:00
parent 0ac74bba4a
commit a377e2656b

View File

@ -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
}