github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/value_tests/marshaler_test.go (about) 1 package test 2 3 import ( 4 "encoding" 5 "encoding/json" 6 ) 7 8 func init() { 9 jm := json.Marshaler(jmOfStruct{}) 10 tm1 := encoding.TextMarshaler(tmOfStruct{}) 11 tm2 := encoding.TextMarshaler(&tmOfStructInt{}) 12 marshalCases = append(marshalCases, 13 jmOfStruct{}, 14 &jm, 15 tmOfStruct{}, 16 &tm1, 17 tmOfStructInt{}, 18 &tm2, 19 map[tmOfStruct]int{ 20 {}: 100, 21 }, 22 map[*tmOfStruct]int{ 23 {}: 100, 24 }, 25 map[encoding.TextMarshaler]int{ 26 tm1: 100, 27 }, 28 ) 29 unmarshalCases = append(unmarshalCases, unmarshalCase{ 30 ptr: (*tmOfMap)(nil), 31 input: `"{1:2}"`, 32 }, unmarshalCase{ 33 ptr: (*tmOfMapPtr)(nil), 34 input: `"{1:2}"`, 35 }) 36 } 37 38 type jmOfStruct struct { 39 F2 chan []byte 40 } 41 42 func (q jmOfStruct) MarshalJSON() ([]byte, error) { 43 return []byte(`""`), nil 44 } 45 46 func (q *jmOfStruct) UnmarshalJSON(value []byte) error { 47 return nil 48 } 49 50 type tmOfStruct struct { 51 F2 chan []byte 52 } 53 54 func (q tmOfStruct) MarshalText() ([]byte, error) { 55 return []byte(`""`), nil 56 } 57 58 func (q *tmOfStruct) UnmarshalText(value []byte) error { 59 return nil 60 } 61 62 type tmOfStructInt struct { 63 Field2 int 64 } 65 66 func (q *tmOfStructInt) MarshalText() ([]byte, error) { 67 return []byte(`"abc"`), nil 68 } 69 70 func (q *tmOfStructInt) UnmarshalText(value []byte) error { 71 return nil 72 } 73 74 type tmOfMap map[int]int 75 76 func (q tmOfMap) UnmarshalText(value []byte) error { 77 return nil 78 } 79 80 type tmOfMapPtr map[int]int 81 82 func (q *tmOfMapPtr) UnmarshalText(value []byte) error { 83 return nil 84 }