plist_lite
plist_lite
is the fastest plist processor for Ruby written in C.
It converts Ruby objects to an XML plist (a.k.a. property list), and vice versa.
Usage
plist_lite
provides 2 APIs:
PlistLite#dump(object)
PlistLite#load(plist_string)
require 'plist_lite'
plist = PlistLite.dump({foo: 'bar', ary: [1,2,3], time: Time.at(0)})
# => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.0\"><dict><key>foo</key><string>bar</string><key>ary</key><array><integer>1</integer><integer>2</integer><integer>3</integer></array><key>time</key><date>1970-01-01T00:00:00Z</date></dict></plist>"
PlistLite.load(plist)
# => {"foo"=>"bar", "ary"=>[1, 2, 3], "time"=>1970-01-01 00:00:00 UTC}
Supported Data Types
Ruby Type | Plist Element |
---|---|
Array |
<array> |
Hash |
<dict> |
Integer |
<integer> |
Float |
<real> |
TrueClass |
<true/> |
FalseClass |
<false/> |
String (binary encoding) |
<data> |
String (other encodings) |
<string> |
Symbol |
<string> |
Time |
<date> |
DateTime |
<date> |
Date |
<date> |
- Avoid using
Date
because it does not have time zone information likeTime
orDateTime
.
Compare to plist
Gem
There is another competitor called plist.
Here are differences between plist_lite
and plist
:
-
plist_lite
is 5 times faster thanplist
, see benchmark. -
plist_lite
knows how to handle encoding whileplist
doesn't.plist
assume all strings are UTF-8 encoded.ruby -rplist -e 'puts Plist::Emit.dump("兜".encode(Encoding::BIG5))'
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <string></string> </plist>
-
plist_lite
treat binary string as binary data whileplist
treatsIO
andStringIO
instances as binary data. The design ofplist
makes little sense. -
plist
usesMarshal#dump
to handle unsupported data types that makes it vulnerable. -
plist_lite
knows how to handle XML encoding whileplist
doesn't.According the the spec, escaping
"
and'
is unnecessary.ruby -rplist -e 'puts Plist::Emit.dump "\""'
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <string>"</string> </plist>
Benchmark
ruby -Ilib benchmark/dump.rb
Rehearsal ----------------------------------------------------
Plist::Emit.dump 3.715427 0.003159 3.718586 ( 3.720294)
PlistLite.dump 0.634391 0.000851 0.635242 ( 0.635718)
------------------------------------------- total: 4.353828sec
user system total real
Plist::Emit.dump 3.853843 0.003882 3.857725 ( 3.860390)
PlistLite.dump 0.700628 0.001118 0.701746 ( 0.702823)
ruby -Ilib benchmark/load.rb
Rehearsal ---------------------------------------------------
Plist.parse_xml 2.220934 1.423194 3.644128 ( 3.645330)
PlistLite.load 0.598344 0.007032 0.605376 ( 0.605603)
------------------------------------------ total: 4.249504sec
user system total real
Plist.parse_xml 2.265274 1.454423 3.719697 ( 3.721122)
PlistLite.load 0.673733 0.005373 0.679106 ( 0.679638)