Skip to content

nytimes/jsonlogic

Repository files navigation

JsonLogic

Clojars Project Build Status

A jsonlogic implementation for Clojure.

Installation

[com.nytimes/jsonlogic "1.0.0"]

Usage

(require '[com.nytimes.jsonlogic :as jsonlogic])

(jsonlogic/apply {:if [true "Foo" "Bar"]})
"Foo"

This library supports encoding the operators as keywords, string, or symbols. All of these values are coerced to symbols for evalution..

(jsonlogic/apply {"+" [1 2]})
;;=> 3
(jsonlogic/apply {:+ [1 2]})
;;=> 3
(jsonlogic/apply {'+ [1 2]})
;;=> 3

Extending JsonLogic

You can add custom JsonLogic operations by extending the operate multi-method.

Arguments

Operate receives two arguments, a map of the operation and the data provided to apply. For example, given this invocation:

(jsonlogic/apply {"*" [{"var" "x"} {"var" "x"}]}
                 {"x" 2})

The arguments to * are:

  1. {* [{var "x"} {var "x"}]}
  2. {"x" 2}

A Custom Operator Example

Here is an example of adding a custom power function.

(require '[com.nytimes.jsonlogic :as json])
(require '[clojure.math :as math])

(defmethod jsonlogic/operate '**
  [{args '**} _env]
  (if (not= 2 (count args))
    (throw (ex-info "** requires exactly two arguments" {:args args}))
    (let [[base exponent] args]
      (math/pow base exponent))))

(jsonlogic/apply {'** [2 3]})
;;=> 8.0

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published