Liking cljdoc? Tell your friends :D


Clojars Project Build Status

A jsonlogic implementation for Clojure.


[com.nytimes/jsonlogic "1.0.0"]


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

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

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.


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

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close