jsonlogic: JsonLogic Evaluation

[ json, library, mit ] [ Propose Tags ]

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


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
error

Error on ghc warnings

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.14.3 && <5), containers (>=0.6.5 && <0.7), mtl (>=2.2.2 && <2.3) [details]
License MIT
Author Marien Matser, Gerard van Schie, Jelle Teeuwissen
Maintainer jelleteeuwissen@hotmail.nl
Category JSON
Bug tracker https://github.com/JTeeuwissen/json-logic-haskell
Uploaded by JTeeuwissen at 2022-04-06T08:50:54Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 75 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for jsonlogic-0.1.0.0

[back to package description]

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 **"