talira

A fast JSON parser and Object marshaller as a Ruby gem

open sourceMITself-hostablefree

Oj (Optimized JSON) is a JSON parser and serializer for Ruby written as a C extension. Its modes decide how documents map to Ruby: :strict and :null for plain JSON types, :compat to mimic the json gem, :rails to mimic ActiveSupport's encoder, :object to round-trip almost any Ruby object, and :custom. Oj.optimize_rails swaps Oj in for Rails' JSON encoding and the JSON methods, and Oj::Parser (3.13) is a faster parser with per-instance options. Peter Ohler has developed it since 2012; commercial support is sold through Tidelift.

Problems it solves

  • JSON encoding is the slowest part of our Rails API responses
  • parse large JSON payloads in Ruby faster than the standard json library
  • make every to_json and JSON.parse call in my Rails app faster without touching the code
  • serialize arbitrary Ruby objects to JSON and load them back as the same classes
  • stream through a huge JSON file in Ruby without loading it all into memory
  • parse untrusted JSON safely so it can't create Ruby objects of the attacker's choosing
  • Rails-compatible JSON output, as_json and all, but quicker
  • dump JSON with exact control over indentation, float precision and time formats
  • store a serialized column as JSON with a faster coder than the default
  • pull a few values out of a big JSON document without building the whole hash

Features

  • strict, null, compat (json gem), rails, object and custom modes
  • Oj.optimize_rails to replace Rails' JSON encoder and decoder
  • mimic_JSON to take over the JSON constant's methods
  • Oj::Parser, a faster parser with option isolation
  • Oj::Doc for fast access into a parsed document
  • streaming and callback (SAJ, SCP) parsing
  • safe_load and strict_load for untrusted input
  • object mode that round-trips Ruby objects without monkey patching
  • options for indentation, float precision, time formats and more
  • usable as an ActiveRecord serialize coder

Limitations

  • Doesn't run on JRuby: it's a C extension, and JRuby support was removed1

Capabilities

Has an APIplan not known

Alternatives

The same job

These do what Oj does.

  1. json Ruby's JSON parser and generator, shipped with Ruby as a default gem

Compare Oj with the top 3