encoding.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package encoding defines interfaces shared by other packages that
  5. // convert data to and from byte-level and textual representations.
  6. // Packages that check for these interfaces include encoding/gob,
  7. // encoding/json, and encoding/xml. As a result, implementing an
  8. // interface once can make a type useful in multiple encodings.
  9. // Standard types that implement these interfaces include time.Time and net.IP.
  10. // The interfaces come in pairs that produce and consume encoded data.
  11. package encoding
  12. // BinaryMarshaler is the interface implemented by an object that can
  13. // marshal itself into a binary form.
  14. //
  15. // MarshalBinary encodes the receiver into a binary form and returns the result.
  16. type BinaryMarshaler interface {
  17. MarshalBinary() (data []byte, err error)
  18. }
  19. // BinaryUnmarshaler is the interface implemented by an object that can
  20. // unmarshal a binary representation of itself.
  21. //
  22. // UnmarshalBinary must be able to decode the form generated by MarshalBinary.
  23. // UnmarshalBinary must copy the data if it wishes to retain the data
  24. // after returning.
  25. type BinaryUnmarshaler interface {
  26. UnmarshalBinary(data []byte) error
  27. }
  28. // TextMarshaler is the interface implemented by an object that can
  29. // marshal itself into a textual form.
  30. //
  31. // MarshalText encodes the receiver into UTF-8-encoded text and returns the result.
  32. type TextMarshaler interface {
  33. MarshalText() (text []byte, err error)
  34. }
  35. // TextUnmarshaler is the interface implemented by an object that can
  36. // unmarshal a textual representation of itself.
  37. //
  38. // UnmarshalText must be able to decode the form generated by MarshalText.
  39. // UnmarshalText must copy the text if it wishes to retain the text
  40. // after returning.
  41. type TextUnmarshaler interface {
  42. UnmarshalText(text []byte) error
  43. }