/tomlib

Tomlib is a fast and standards-compliant TOML parser and generator for Ruby.

Primary LanguageCMIT LicenseMIT

Tomlib

Tomlib is a TOML parser and generator for Ruby. It is fast and standards-compliant by relying on native tomlc99 parser.

Tomlib is TOML v1.0 compliant.

Installation

Tomlib supports Ruby (MRI) 2.7+

Add this line to your application's Gemfile:

gem 'tomlib'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install tomlib

Usage

To parse a TOML document use:

require 'tomlib'

Tomlib.load(<<~TOML)
firstName = "John"
lastName = "Doe"
hobbies = [ "Singing", "Dancing" ]

[address]
city = "London"
zip = "E1 6AN"

[address.street]
name = "Oxford Street"
TOML

# =>
#
# {
#   "firstName" => "John",
#   "lastName" => "Doe",
#   "hobbies" => ["Singing", "Dancing"],
#   "address" => {
#     "city"=>"London",
#     "zip"=>"E1 6AN",
#     "street"=>{ "name"=>"Oxford Street" }
#   }
# }

To generate a TOML document from Ruby Hash use:

require 'tomlib'

Tomlib.dump({
  "firstName" => "John",
  "lastName" => "Doe",
  "hobbies" => ["Singing", "Dancing"],
  "address" => {
    "city"=>"London",
    "zip"=>"E1 6AN",
    "street"=>{ "name"=>"Oxford Street" }
  }
})

# =>
#
# firstName = "John"
# lastName = "Doe"
# hobbies = [ "Singing", "Dancing" ]
#
# [address]
# city = "London"
# zip = "E1 6AN"
#
#   [address.street]
#   name = "Oxford Street"

If you don't need indentation use:

require 'tomlib'

Tomlib.dump(hash, indent: false)

# =>
#
# firstName = "John"
# lastName = "Doe"
# hobbies = [ "Singing", "Dancing" ]
#
# [address]
# city = "London"
# zip = "E1 6AN"
#
# [address.street]
# name = "Oxford Street"

Performance

Tomlib parsing is ~300x faster than toml-rb and ~15x faster than Tomlrb for usual use case (~5KB TOML document size).

Generating TOML document is about 1.7x faster than toml-rb.

For full comparison take a look at benchmarks

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kgiszczak/tomlib.

License

The gem is available as open source under the terms of the MIT License.