Tao Wen пре 7 година
родитељ
комит
a377e2656b
1 измењених фајлова са 28 додато и 0 уклоњено
  1. 28 0
      example_test.go

+ 28 - 0
example_test.go

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