GithubHelp home page GithubHelp logo

ayushmaanbhav / json-logic-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jamsesso/json-logic-java

0.0 0.0 0.0 157 KB

A pure Java implementation of JsonLogic without using the Nashorn JS engine

License: MIT License

Java 100.00%

json-logic-java's Introduction

json-logic-java

This parser accepts JsonLogic rules and executes them in Java without Nashorn.

The JsonLogic format is designed to allow you to share rules (logic) between front-end and back-end code (regardless of language difference), even to store logic along with a record in a database. JsonLogic is documented extensively at JsonLogic.com, including examples of every supported operation and a place to try out rules in your browser.

Installation

<dependency>
  <groupId>io.github.jamsesso</groupId>
  <artifactId>json-logic-java</artifactId>
  <version>1.0.7</version>
</dependency>

Examples

The public API for json-logic-java attempts to mimic the public API of the original Javascript implementation as close as possible. For this reason, the API is loosely typed in many places. This implementation relies on duck-typing for maps/dictionaries and arrays: if it looks and feels like an array, we treat it like an array.

// Create a new JsonLogic instance. JsonLogic is thread safe.
JsonLogic jsonLogic = new JsonLogic();

// Set up some JSON and some data.
String expression = "{\"*\": [{\"var\": \"x\"}, 2]}";
Map<String, Integer> data = new HashMap<>();
data.put("x", 10);

// Evaluate the result.
double result = (double) jsonLogic.apply(expression, data);
assert result == 20.0;

You can add your own operations like so:

// Register an operation.
jsonLogic.addOperation("greet", (args) -> "Hello, " + args[0] + "!");

// Evaluate the result.
String result = (String) jsonLogic.apply("{\"greet\": [\"Sam\"]}", null);
assert "Hello, Sam!".equals(result);

There is a truthy static method that mimics the truthy-ness rules of Javascript:

assert JsonLogic.truthy(0) == false;
assert JsonLogic.truthy(1) == true;
assert JsonLogic.truthy("") == false;
assert JsonLogic.truthy("Hello world!") == true;

// etc...

Building a Release

For now, there is no CI pipeline. Changes do not happen frequently enough to require them.

  1. Remove the -SNAPSHOT from the version in build.gradle.
  2. Update the README.md installation instructions to use the new version.
  3. Add and commit the changes.
  4. Create a git tag (git tag v{VERSION}) and push: git push && git push --tags.
  5. Run ./gradlew clean build uploadArchives to push to Sonatype.
  6. Bump the version in the build.gradle file and add -SNAPSHOT to it.
  7. Add and commit the changes.

json-logic-java's People

Contributors

jamsesso avatar magnarn avatar iliecirciumaru avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.