Source file src/encoding/encoding.go
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 5 // Package encoding defines interfaces shared by other packages that 6 // convert data to and from byte-level and textual representations. 7 // Packages that check for these interfaces include encoding/gob, 8 // encoding/json, and encoding/xml. As a result, implementing an 9 // interface once can make a type useful in multiple encodings. 10 // Standard types that implement these interfaces include time.Time and net.IP. 11 // The interfaces come in pairs that produce and consume encoded data. 12 // 13 // Adding encoding/decoding methods to existing types may constitute a breaking change, 14 // as they can be used for serialization in communicating with programs 15 // written with different library versions. 16 // The policy for packages maintained by the Go project is to only allow 17 // the addition of marshaling functions if no existing, reasonable marshaling exists. 18 package encoding 19 20 // BinaryMarshaler is the interface implemented by an object that can 21 // marshal itself into a binary form. 22 // 23 // MarshalBinary encodes the receiver into a binary form and returns the result. 24 type BinaryMarshaler interface { 25 MarshalBinary() (data []byte, err error) 26 } 27 28 // BinaryUnmarshaler is the interface implemented by an object that can 29 // unmarshal a binary representation of itself. 30 // 31 // UnmarshalBinary must be able to decode the form generated by MarshalBinary. 32 // UnmarshalBinary must copy the data if it wishes to retain the data 33 // after returning. 34 type BinaryUnmarshaler interface { 35 UnmarshalBinary(data []byte) error 36 } 37 38 // TextMarshaler is the interface implemented by an object that can 39 // marshal itself into a textual form. 40 // 41 // MarshalText encodes the receiver into UTF-8-encoded text and returns the result. 42 type TextMarshaler interface { 43 MarshalText() (text []byte, err error) 44 } 45 46 // TextUnmarshaler is the interface implemented by an object that can 47 // unmarshal a textual representation of itself. 48 // 49 // UnmarshalText must be able to decode the form generated by MarshalText. 50 // UnmarshalText must copy the text if it wishes to retain the text 51 // after returning. 52 type TextUnmarshaler interface { 53 UnmarshalText(text []byte) error 54 } 55