MyNixOS website logo
Description

JsonLogic Evaluation.

JsonLogic is a library for evaluating JSON logic. It can be extended with additional operations

Core

The core JsonLogic evaluation package. Allows for creating and adding custom operations to the evaluator in a pure and IO context. See the example below for more information.

Example


-- | The main function
-- Perform simple power function
main :: IO ()
main = do
  putStrLn "First number:"
  first <- getLine
  putStrLn "Second number:"
  second <- getLine
  print $ evaluate (read first) (read second)

-- | Evaluate two numbers with pow operation using json logic.
-- The two numbers are placed into an data object and given to the evaluator with the following logic:
-- {"**":[{"var":"base"}, {"var":"exp"}]}
-- 
-- >>> evaluate (read "3") (read "4")
-- Right 81.0
evaluate :: Json -> Json -> Result Json
evaluate base expo = applyWithPow (read "{\"**\":[{\"var\":\"base\"}, {\"var\":\"exp\"}]}") (JsonObject [("base", base), ("exp", expo)])

-- | An evaluator that can evaluate operations with power (**).
applyWithPow :: Rule -> Data -> Result Json
applyWithPow = apply [powOperation]

-- | The power operation.
-- Takes the power function and adds a name to it to create an operation.
powOperation :: Operation
powOperation = ("**", powFunction)

-- | The power function.
-- Takes an subevaluator, function arguments (in this case just a list) and data to pass through.
-- 1. tries to evaluate the arguments to double values
--  (as they might be json logic evaluating to doubles, instead of direct numbers).
-- 2. if successful, returns the result of the power operation
powFunction :: Function Json
powFunction evaluator (JsonArray [base', expo']) vars = do
  base <- evaluateDouble evaluator base' vars
  expo <- evaluateDouble evaluator expo' vars
  return $ JsonNumber $ base ** expo
powFunction _ _ _ = throw "Wrong number of arguments for **"
Metadata

Version

0.1.0.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows