Encode writes the property list encoding of v to the stream.
(v interface{})
| 23 | |
| 24 | // Encode writes the property list encoding of v to the stream. |
| 25 | func (p *Encoder) Encode(v interface{}) (err error) { |
| 26 | defer func() { |
| 27 | if r := recover(); r != nil { |
| 28 | if _, ok := r.(runtime.Error); ok { |
| 29 | panic(r) |
| 30 | } |
| 31 | err = r.(error) |
| 32 | } |
| 33 | }() |
| 34 | |
| 35 | pval := p.marshal(reflect.ValueOf(v)) |
| 36 | if pval == nil { |
| 37 | panic(errors.New("plist: no root element to encode")) |
| 38 | } |
| 39 | |
| 40 | var g generator |
| 41 | switch p.format { |
| 42 | case XMLFormat: |
| 43 | g = newXMLPlistGenerator(p.writer) |
| 44 | case BinaryFormat, AutomaticFormat: |
| 45 | g = newBplistGenerator(p.writer) |
| 46 | case OpenStepFormat, GNUStepFormat: |
| 47 | g = newTextPlistGenerator(p.writer, p.format) |
| 48 | } |
| 49 | g.Indent(p.indent) |
| 50 | g.generateDocument(pval) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | // Indent turns on pretty-printing for the XML and Text property list formats. |
| 55 | // Each element begins on a new line and is preceded by one or more copies of indent according to its nesting depth. |