public
Description: An extremely efficient streaming JSON parsing and encoding library. Ruby C bindings to Yajl
Homepage: http://rdoc.info/projects/brianmario/yajl-ruby
Clone URL: git://github.com/brianmario/yajl-ruby.git
Click here to lend your support to: yajl-ruby and make a donation at www.pledgie.com !
name age message
file .gitignore Thu May 07 21:40:23 -0700 2009 adding my TODO file to gitignore [brianmario]
file CHANGELOG.rdoc Loading commit data...
file MIT-LICENSE Sun Apr 19 17:11:50 -0700 2009 adding license [brianmario]
file README.rdoc
file Rakefile Thu Apr 30 15:46:37 -0700 2009 updates for 0.4.2 release [brianmario]
file VERSION.yml
directory benchmark/
directory ext/
directory lib/
directory spec/ Fri May 08 09:04:11 -0700 2009 Revert "Revert "updated Yajl::Deflate::StreamRe... [brianmario]
file yajl-ruby.gemspec
README.rdoc

YAJL C Bindings for Ruby (work in progress)

This gem (although not in gem form just yet) is a C binding to the excellent YAJL JSON parsing library.

You can read more info at the projects website lloydforge.org/projects/yajl or check out it’s codes at github.com/lloyd/yajl.

Example of use

First, you’re probably gonna want to include it:

 include 'yajl'

Then maybe parse some JSON from:

a File IO

 json_contents = File.new('test.json', 'r')
 hash = Yajl::Stream.parse(json)

or maybe a StringIO

 json_contents = StringIO.new
 hash = Yajl::Stream.parse(json)

or maybe STDIN

 cat someJsonFile.json | ruby -ryajl -e "puts Yajl::Stream.parse(STDIN).inspect"

Or lets say you didn’t have access to the IO object that contained JSON data, but instead only had access to chunks of it at a time. No problem!

(Assume we’re in an EventMachine::Connection instance)

 def object_parsed(obj)
    puts "Sometimes one pays most for the things one gets for nothing. - Albert Einstein"
    puts obj.inspect
  end

 def connection_completed
   # once a full JSON object has been parsed from the stream
   # object_parsed will be called, and passed the constructed object
   Yajl::Chunked.on_parse_complete = method(:object_parsed)
 end

 def receive_data(data)
   # continue passing chunks
   Yajl::Chunked.parse_some(data)

   # Or as an alias, you could have done:
   # Yajl::Chunked << data
 end

There are a lot more possibilities, some of which I’m going to write other gems/plugins for.

Some ideas are:

How to install

First, Yajl uses CMake to build itself (yes, the author realizes this isn’t the norm for open source and is willing and ready to accept patches, fork away kids!) so you’ll need to grab it first from www.cmake.org.

After you’ve got that, grab the latest version (I suggest at least 1.0.4 as it contains fixes for Unicode parsing) of Yajl itself from the Githubs at github.com/lloyd/yajl.

After you have that installed, you should be able to install it like any other gem hosted here like so:

(more instructions here: gems.github.com)

 sudo gem install brianmario-yajl-ruby

Benchmarks

I’ll update this readme with some actual data soon.

After I finished implementation - this library performs close to the same as the current JSON.parse (C gem) does on small/medium files.

But on larger files, and higher amounts of iteration, this library was around 2x faster than JSON.parse.

The main benefit of this library is in it’s memory usage. Since it’s able to parse the stream in chunks, it’s memory requirements are very, very low.

Here’s what parsing a 2.43MB JSON file off the filesystem 20 times looks like:

Memory Usage

Average

  • Yajl::Stream.parse: 32MB
  • JSON.parse: 54MB
  • ActiveSupport::JSON.decode: 63MB

Peak

  • Yajl::Stream.parse: 32MB
  • JSON.parse: 57MB
  • ActiveSupport::JSON.decode: 67MB

Parse Time

  • Yajl::Stream.parse: 4.54s
  • JSON.parse: 5.47s
  • ActiveSupport::JSON.decode: 64.42s

Special Thanks

I’ve had a lot of inspiration, and a lot of help. Thanks to everyone who’s been a part of this and those to come!

  • Lloyd Hilaiel (github.com/lloyd) - for writing Yajl!!
  • Josh Ferguson (github.com/besquared) - for peer-pressuring me into getting back into C; it worked ;) Also tons of support over IM
  • Jonathan Novak (github.com/cypriss) - pointer-hacking help
  • Tom Smith (github.com/rtomsmith) - pointer-hacking help
  • Rick (github.com/technoweenie) - for making an ActiveSupport patch with support for this library and teasing me that it might go into Rails 3. You sure lit a fire under my ass and I got a ton of work done because of it! :)
  • The entire Github Crew - my inspiration, time spent writing this, finding Yajl, So many-MANY other things wouldn’t have been possible without this awesome service. I owe you guys some whiskey at Kilowatt.