brianmario/yajl-ruby logo

brianmario/yajl-ruby

Free

A streaming JSON parsing and encoding library for Ruby (C bindings to yajl)

FreeFree tier
Type
Open Source

About brianmario/yajl-ruby

yajl-ruby is a Ruby gem providing C bindings to the YAJL (Yet Another JSON Library) for high-performance streaming JSON parsing and generation. It enables parsing and encoding directly from/to IO streams (files, sockets, strings) and supports compressed streams (Bzip2, Gzip, Deflate). The library can handle multiple JSON objects from a single stream or string, and offers a JSON gem compatibility API for drop-in replacement. It also includes a basic HTTP client for GET requests that streams JSON parsing from the response body. Benchmarks show it is approximately 3.5x faster than JSON.generate, 1.9x faster than JSON.parse, and significantly faster than YAML and Marshal operations.

Key Features

JSON parsing and encoding directly to/from IO streams (file, socket, string)
Compressed stream parsing and encoding for Bzip2, Gzip, and Deflate
Parse and encode multiple JSON objects from streams or strings continuously
JSON gem compatibility API for drop-in replacement of the JSON gem
Basic HTTP client for GET requests with streaming JSON parsing from response body
Significantly faster than standard Ruby JSON, YAML, and Marshal libraries (e.g., ~3.5x faster than JSON.generate, ~1.9x faster than JSON.parse)

Pros & Cons

Pros
  • High performance due to C bindings to YAJL
  • Supports streaming parsing and encoding for memory efficiency
  • Handles compressed JSON streams natively
  • Easy installation via `gem install yajl-ruby`
  • Open source with an active GitHub repository (1.5k stars)
Cons
  • Only supports HTTP GET requests in the built-in HTTP client (no POST/PUT)
  • Requires compilation of native C extension during installation
  • Limited to JSON format; no support for other data interchange formats
  • May have edge cases with non-standard JSON extensions

Best For

Parsing large JSON files or streams without loading entire data into memoryStreaming JSON parsing from network sockets or HTTP responsesParsing JSON from STDIN for command-line processingReplacing the standard JSON gem with a faster alternative in Ruby applicationsProcessing compressed JSON data (Bzip2, Gzip, Deflate)

FAQ

How do I install yajl-ruby?
Install it via RubyGems: `gem install yajl-ruby`. Or add `gem 'yajl-ruby', require: 'yajl'` to your Gemfile.
Can I parse JSON from a file or socket?
Yes, yajl-ruby supports parsing JSON directly from any IO object, including files, sockets, and strings. For example: `Yajl::Parser.parse(File.new('test.json', 'r'))`.
Does yajl-ruby support streaming JSON?
Yes, it supports streaming parsing. You can pass chunks of data to the parser via `<<` and set a callback (`on_parse_complete`) to receive parsed objects as they are completed.
What compression formats are supported?
yajl-ruby supports Bzip2, Gzip, and Deflate compressed streams for both parsing and encoding.
Is it compatible with the standard JSON gem?
Yes, yajl-ruby provides a compatibility API that allows it to be used as a drop-in replacement for the JSON gem.