Skip to content

luismoyano/shiny-json-logic-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

189 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ShinyJsonLogic ✨

Compatibility Gem Version Ruby

The fastest AND most compliant JSONLogic implementation for Ruby. ✨

ShinyJsonLogic is a pure Ruby, zero-dependency JSON Logic implementation β€” the only Ruby gem that passes 100% of the official JSON Logic tests, and faster than all competitors on every Ruby version tested.


Why ShinyJsonLogic?

If you've used JSON Logic in Ruby before, you've probably found that other gems don't behave like their JS or Python counterparts: It's either wrong outputs on edge cases, missing operators or subtle bugs... And you end up patching, spawning a JS process or writing the interpreter yourself. Been there.

ShinyJsonLogic fixes that:

  • πŸš€ Fastest Ruby JSON Logic gem β€” wins across all Ruby versions tested, up to +117% faster than competitors.
  • βœ… 100% spec-compliant β€” the only Ruby gem that passes all official JSON Logic tests.
  • 🧩 Zero runtime dependencies (stdlib-only). Just plug & play.
  • πŸ•°οΈ Ruby 2.4+ compatible β€” one of the lowest minimum versions in the Ruby ecosystem.
  • πŸ”§ Actively maintained and continuously improved.

Try it in the sandbox at jsonlogicruby.com/playground, or see the full benchmark results at jsonlogicruby.com/benchmarks.


Installation

Add it to your Gemfile:

gem "shiny_json_logic"

Then run:

bundle install

Or install it directly:

gem install shiny_json_logic

and require it in your project:

require "shiny_json_logic"

Performance

Benchmarked via jsonlogic_benchmarks β€” automated CI on Linux across all Ruby versions (2.7 through 4.0, with and without YJIT). shiny_json_logic wins across the board, up to +117% faster than the next best gem.

See jsonlogicruby.com/benchmarks for the full interactive results.


Migrating from json-logic-ruby

If you're currently using json-logic-ruby, migration is seamless.

ShinyJsonLogic provides JsonLogic and JSONLogic as aliases, so you only need to swap the gem in your Gemfile:

- gem "json-logic-ruby"
+ gem "shiny_json_logic"

Your existing code will work without changes:

require "shiny_json_logic"

# Both of these work exactly as before:
JsonLogic.apply(rule, data)
JSONLogic.apply(rule, data)

# Or use the new module name:
ShinyJsonLogic.apply(rule, data)

Usage

Basic usage is intentionally simple:

require "shiny_json_logic"

rule = {
  "==" => [
    { "var" => "status" },
    "active"
  ]
}

data = { "status" => "active" }

ShinyJsonLogic.apply(rule, data)
# => true

Nested logic

Rules can be nested arbitrarily:

rule = {
  "if" => [
    { "var" => "financing" },
    { "missing" => ["apr"] },
    []
  ]
}

data = { "financing" => true }

ShinyJsonLogic.apply(rule, data)
# => ["apr"]

Supported operators

Our goal is full JSON Logic coverage.
Currently implemented operators include:

Logic

if, and, or, !, !!, ?:, try✨, throw✨

Comparison

==, ===, !=, !==, >, >=, <, <=

Data access

var, missing, missing_some, val✨, exists✨

Math

+, -, *, /, %, min, max

Strings

cat, substr

Arrays

merge, in, ??✨ (coalesce operator)

Iterable operations

map, reduce, filter, some, all, none

Evaluation

preserve✨

See the spec for the full list of operators and their behavior.


Error Handling

ShinyJsonLogic uses native Ruby exceptions for error handling:

# Unknown operators raise an error
ShinyJsonLogic.apply({ "unknown_op" => [1, 2] }, {})
# => raises ShinyJsonLogic::Errors::UnknownOperator

# Invalid arguments raise an error
ShinyJsonLogic.apply({ "+" => ["not", "numbers"] }, {})
# => raises ShinyJsonLogic::Errors::InvalidArguments

# You can use try/throw for controlled error handling within rules
rule = {
  "try" => [
    { "throw" => "Something went wrong" },
    { "cat" => ["Error: ", { "var" => "type" }] }
  ]
}
ShinyJsonLogic.apply(rule, {})
# => "Error: Something went wrong"

Error classes:

  • ShinyJsonLogic::Errors::UnknownOperator - Unknown operator in rule
  • ShinyJsonLogic::Errors::InvalidArguments - Invalid arguments to operator
  • ShinyJsonLogic::Errors::NotANumber - NaN result in numeric operation

or rescue the ShinyJsonLogic::Errors::Base error class in a single sweep.


Development

After checking out the repo:

bin/setup
bundle exec rspec

Open a console:

bin/console

Install locally:

bundle install

How to run the compatibility test suite:

bin/test.sh

Contributing

Contributions are welcome β€” especially:

  • spec alignment improvements
  • missing operators
  • edge-case tests
  • performance improvements

Please include tests with any change.

Repository:
https://github.com/luismoyano/shiny_json_logic


License

MIT License.

Use it. Fork it. Ship it. (:


Shine bright like a Ruby πŸ’Ž

About

A JSONLogic runtime for Ruby that actually works

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors