Skip to content

Instantly share code, notes, and snippets.

@swaters86
Created January 22, 2026 17:17
Show Gist options
  • Select an option

  • Save swaters86/e3b2fdf32c09419dfb75db019b658670 to your computer and use it in GitHub Desktop.

Select an option

Save swaters86/e3b2fdf32c09419dfb75db019b658670 to your computer and use it in GitHub Desktop.
Basic example
using System.Text.Json;
using RulesEngine.Models;
using RulesEngine;
using System.Collections;
// ----- Incoming JSON (could vary by source) -----
var json = """
{
"id": "tx-001",
"amount": 12.50,
"source": "ehrA"
}
""";
// ----- Rules (hard-coded for demo) -----
// NOTE: We treat input1 as Dictionary<string, object> so we can safely check keys.
var workflows = new[]
{
new Workflow
{
WorkflowName = "Tx",
Rules = new List<Rule>
{
new Rule
{
RuleName = "HighCharge",
// Safe even if "amount" is missing:
Expression = "input1.ContainsKey(\"amount\") && Convert.ToDecimal(input1[\"amount\"]) > 10"
}
}
}
};
var re = new RulesEngine.RulesEngine(workflows);
// Parse JSON into Dictionary<string, object> (no DTO)
var record = (Dictionary<string, object>)ToObject(JsonDocument.Parse(json).RootElement)!;
// Run
var results = await re.ExecuteAllRulesAsync(
"Tx",
new RuleParameter("input1", record)
);
// Output
var r = results[0];
Console.WriteLine(r.IsSuccess ? "SUCCESS" : "FAIL");
if (!r.IsSuccess && r.ExceptionMessage is { Length: > 0 })
Console.WriteLine(r.ExceptionMessage);
// ----- Minimal JSON -> object converter (Dictionary/List/primitives) -----
static object? ToObject(JsonElement el) =>
el.ValueKind switch
{
JsonValueKind.Object => el.EnumerateObject()
.ToDictionary(p => p.Name, p => ToObject(p.Value)!),
JsonValueKind.Array => el.EnumerateArray()
.Select(ToObject).ToList()!,
JsonValueKind.String => el.GetString(),
JsonValueKind.Number => el.TryGetInt64(out var l) ? l : el.GetDecimal(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Null => null,
_ => null
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment