1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00
json-iterator/value_tests/marshaler_test.go

85 lines
1.4 KiB
Go
Raw Normal View History

2018-02-13 23:49:40 +08:00
package test
2018-02-18 21:05:42 +08:00
import (
"encoding"
2018-02-24 22:04:41 +08:00
"encoding/json"
2018-02-18 21:05:42 +08:00
)
2018-02-13 23:49:40 +08:00
func init() {
2018-02-19 22:53:42 +08:00
jm := json.Marshaler(jmOfStruct{})
tm1 := encoding.TextMarshaler(tmOfStruct{})
tm2 := encoding.TextMarshaler(&tmOfStructInt{})
2018-02-13 23:49:40 +08:00
marshalCases = append(marshalCases,
2018-02-19 22:53:42 +08:00
jmOfStruct{},
&jm,
tmOfStruct{},
&tm1,
tmOfStructInt{},
&tm2,
map[tmOfStruct]int{
2018-02-24 22:04:41 +08:00
{}: 100,
2018-02-18 22:49:06 +08:00
},
2018-02-19 22:53:42 +08:00
map[*tmOfStruct]int{
2018-02-24 22:04:41 +08:00
{}: 100,
2018-02-18 22:49:06 +08:00
},
map[encoding.TextMarshaler]int{
2018-02-19 22:53:42 +08:00
tm1: 100,
2018-02-18 22:49:06 +08:00
},
2018-02-13 23:49:40 +08:00
)
2018-02-19 22:53:42 +08:00
unmarshalCases = append(unmarshalCases, unmarshalCase{
2018-02-24 22:04:41 +08:00
ptr: (*tmOfMap)(nil),
2018-02-19 22:53:42 +08:00
input: `"{1:2}"`,
}, unmarshalCase{
2018-02-24 22:04:41 +08:00
ptr: (*tmOfMapPtr)(nil),
2018-02-19 22:53:42 +08:00
input: `"{1:2}"`,
})
2018-02-13 23:49:40 +08:00
}
2018-02-19 22:53:42 +08:00
type jmOfStruct struct {
2018-02-18 21:05:42 +08:00
F2 chan []byte
}
2018-02-19 22:53:42 +08:00
func (q jmOfStruct) MarshalJSON() ([]byte, error) {
2018-02-18 21:05:42 +08:00
return []byte(`""`), nil
}
2018-02-19 22:53:42 +08:00
func (q *jmOfStruct) UnmarshalJSON(value []byte) error {
2018-02-18 21:05:42 +08:00
return nil
}
2018-02-19 22:53:42 +08:00
type tmOfStruct struct {
2018-02-13 23:49:40 +08:00
F2 chan []byte
}
2018-02-19 22:53:42 +08:00
func (q tmOfStruct) MarshalText() ([]byte, error) {
2018-02-13 23:49:40 +08:00
return []byte(`""`), nil
}
2018-02-19 22:53:42 +08:00
func (q *tmOfStruct) UnmarshalText(value []byte) error {
2018-02-13 23:49:40 +08:00
return nil
}
2018-02-18 22:49:06 +08:00
2018-02-19 22:53:42 +08:00
type tmOfStructInt struct {
2018-02-18 22:49:06 +08:00
Field2 int
}
2018-02-19 22:53:42 +08:00
func (q *tmOfStructInt) MarshalText() ([]byte, error) {
2018-02-18 22:49:06 +08:00
return []byte(`"abc"`), nil
}
2018-02-19 22:53:42 +08:00
func (q *tmOfStructInt) UnmarshalText(value []byte) error {
return nil
}
type tmOfMap map[int]int
func (q tmOfMap) UnmarshalText(value []byte) error {
return nil
}
type tmOfMapPtr map[int]int
func (q *tmOfMapPtr) UnmarshalText(value []byte) error {
2018-02-18 22:49:06 +08:00
return nil
}