mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
add map key example
This commit is contained in:
parent
0ac74bba4a
commit
a377e2656b
@ -3,6 +3,7 @@ package jsoniter
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleMarshal() {
|
func ExampleMarshal() {
|
||||||
@ -93,3 +94,30 @@ func ExampleGet() {
|
|||||||
// Output:
|
// Output:
|
||||||
// Crimson
|
// 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…
x
Reference in New Issue
Block a user