marshaler_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package test
  2. import (
  3. "encoding"
  4. "encoding/json"
  5. )
  6. func init() {
  7. jm := json.Marshaler(jmOfStruct{})
  8. tm1 := encoding.TextMarshaler(tmOfStruct{})
  9. tm2 := encoding.TextMarshaler(&tmOfStructInt{})
  10. marshalCases = append(marshalCases,
  11. jmOfStruct{},
  12. &jm,
  13. tmOfStruct{},
  14. &tm1,
  15. tmOfStructInt{},
  16. &tm2,
  17. map[tmOfStruct]int{
  18. {}: 100,
  19. },
  20. map[*tmOfStruct]int{
  21. {}: 100,
  22. },
  23. map[encoding.TextMarshaler]int{
  24. tm1: 100,
  25. },
  26. )
  27. unmarshalCases = append(unmarshalCases, unmarshalCase{
  28. ptr: (*tmOfMap)(nil),
  29. input: `"{1:2}"`,
  30. }, unmarshalCase{
  31. ptr: (*tmOfMapPtr)(nil),
  32. input: `"{1:2}"`,
  33. })
  34. }
  35. type jmOfStruct struct {
  36. F2 chan []byte
  37. }
  38. func (q jmOfStruct) MarshalJSON() ([]byte, error) {
  39. return []byte(`""`), nil
  40. }
  41. func (q *jmOfStruct) UnmarshalJSON(value []byte) error {
  42. return nil
  43. }
  44. type tmOfStruct struct {
  45. F2 chan []byte
  46. }
  47. func (q tmOfStruct) MarshalText() ([]byte, error) {
  48. return []byte(`""`), nil
  49. }
  50. func (q *tmOfStruct) UnmarshalText(value []byte) error {
  51. return nil
  52. }
  53. type tmOfStructInt struct {
  54. Field2 int
  55. }
  56. func (q *tmOfStructInt) MarshalText() ([]byte, error) {
  57. return []byte(`"abc"`), nil
  58. }
  59. func (q *tmOfStructInt) UnmarshalText(value []byte) error {
  60. return nil
  61. }
  62. type tmOfMap map[int]int
  63. func (q tmOfMap) UnmarshalText(value []byte) error {
  64. return nil
  65. }
  66. type tmOfMapPtr map[int]int
  67. func (q *tmOfMapPtr) UnmarshalText(value []byte) error {
  68. return nil
  69. }