You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
715 B

  1. package msgpack_test
  2. import (
  3. "fmt"
  4. "github.com/vmihailenco/msgpack"
  5. )
  6. type customStruct struct {
  7. S string
  8. N int
  9. }
  10. var _ msgpack.CustomEncoder = (*customStruct)(nil)
  11. var _ msgpack.CustomDecoder = (*customStruct)(nil)
  12. func (s *customStruct) EncodeMsgpack(enc *msgpack.Encoder) error {
  13. return enc.Encode(s.S, s.N)
  14. }
  15. func (s *customStruct) DecodeMsgpack(dec *msgpack.Decoder) error {
  16. return dec.Decode(&s.S, &s.N)
  17. }
  18. func ExampleCustomEncoder() {
  19. b, err := msgpack.Marshal(&customStruct{S: "hello", N: 42})
  20. if err != nil {
  21. panic(err)
  22. }
  23. var v customStruct
  24. err = msgpack.Unmarshal(b, &v)
  25. if err != nil {
  26. panic(err)
  27. }
  28. fmt.Printf("%#v", v)
  29. // Output: msgpack_test.customStruct{S:"hello", N:42}
  30. }