Skip to content

Getting Started

github-actions[bot] edited this page Nov 29, 2026 · 1 revision

Getting Started

Requirements

  • .NET 10.0 or later (net10.0)
  • Fully compatible with NativeAOT and IL trimming

Installation

dotnet add package Celly.Json

Celly.Json transitively references Celly and uses only System.Text.Json from the .NET BCL — no additional third-party dependencies.


Your First Evaluation

Compile your CEL expression once and reuse the resulting CelProgram across requests:

using System.Text.Json;
using Celly;
using Celly.Json;

CelProgram program = CelEnv.Default.Compile("user.active && user.age >= 18 && 'admin' in user.roles");

using JsonDocument doc = JsonDocument.Parse("""
    {
      "user": {
        "active": true,
        "age": 32,
        "roles": ["viewer", "admin"]
      }
    }
    """);

// Typed convenience helper (throws CelEvaluationException on CEL error or non-bool)
bool allowed = program.EvalJsonBool(doc);

// Or inspect the raw CelValue directly
var rawResult = program.EvalJson(doc);

Supported Input Shapes

Input Type Usage Notes
JsonDocument / JsonElement program.EvalJson(doc) Fastest for read-only payloads; zero dictionary allocation.
JsonObject program.EvalJson(jsonObj) Fastest overall (~187 ns) when you already have a JsonNode DOM.
string / ReadOnlyMemory<byte> program.EvalJson(jsonString) Parses, evaluates, and safely detaches any JSON-backed return value before disposing the JsonDocument.
Reusable JsonElementActivation activation.Reset(doc.RootElement) Best for tight loops and high-throughput request pipelines.

Typed Evaluation Helpers

Instead of manually casting CelValue and checking IsError, use the typed helpers:

bool isAllowed = program.EvalJsonBool(doc);
long count     = countProgram.EvalJsonInt64(doc);
double total   = totalProgram.EvalJsonDouble(doc);
string label   = labelProgram.EvalJsonString(doc);

Each helper throws CelEvaluationException with a clear message if the expression evaluates to a CEL ErrorValue (e.g., division by zero or missing key) or returns a different CEL type.


Next Steps

Clone this wiki locally