Skip to content

Auto-detect binary or XML #17

@jpap

Description

@jpap

Thanks for a great package!

I end up needing to have an external func to automatically decode a binary or XML plist, see below.

It would be nice to replace the deprecated plist.NewDecoder func with this. Happy to provide a PR on your suggestion.

// NewPListDecoder returns a new Property List decoder, auto-detecting
// whether it is binary or XML text.
func NewPListDecoder(r io.ReadSeeker) *plist.Decoder {
	// Detect if it XML or Binary
	magic := make([]byte, 5)
	n, err := r.Read(magic)
	if err != nil {
		return nil
	}
	_, err = r.Seek(0, 0)
	if err != nil {
		return nil
	}
	switch string(magic[:n]) {
	case "<?xml":
		return plist.NewXMLDecoder(r)
	case "bplis":
		return plist.NewBinaryDecoder(r)
	}
	return nil
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions