Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nytimes/jsonlogic
https://github.com/nytimes/jsonlogic
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/nytimes/jsonlogic
- Owner: nytimes
- License: other
- Created: 2022-01-20T18:43:49.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-07T18:51:39.000Z (over 2 years ago)
- Last Synced: 2024-10-12T06:28:51.128Z (2 months ago)
- Language: Clojure
- Size: 34.2 KB
- Stars: 21
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# JsonLogic
[![Clojars Project](https://img.shields.io/clojars/v/com.nytimes/jsonlogic.svg)](https://clojars.org/com.nytimes/jsonlogic)
[![Build Status](https://cloud.drone.io/api/badges/nytimes/jsonlogic/status.svg?ref=refs/heads/main)](https://cloud.drone.io/nytimes/jsonlogic)A [jsonlogic][1] implementation for Clojure.
## Installation
```clojure
[com.nytimes/jsonlogic "1.0.0"]
```## Usage
```clojure
(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..```clojure
(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:```clojure
(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.
```clojure
(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
```[1]: https://jsonlogic.com/