Skip to content
github-actions[bot] edited this page Nov 29, 2026 · 2 revisions

Celly.Json

Celly.Json is a high-performance, minimal-allocation System.Text.Json adapter, JSON Schema static type checker, and result serializer for Celly — Google's Common Expression Language (CEL) for .NET.


Why Celly.Json?

Core Celly evaluates CEL expressions in ~125 ns, but passing raw JSON payloads into it by eagerly converting JsonDocument into nested Dictionary<string, object?> and List<object?> trees allocates tens of kilobytes per request and slows evaluation down by 50–100×.

Celly.Json eliminates that bottleneck:

Workload Naive Eager Dictionary Celly.Json Improvement
Policy rule on 15 KB JSON (JsonDocument) 19,375 ns / 85,416 B 269 ns / 384 B 72× faster, 222× less alloc
Policy rule on 15 KB JSON (JsonObject) 19,375 ns / 85,416 B 187 ns / 296 B 103× faster, 288× less alloc
100-item filter comprehension 55,508 ns / 214,680 B 8,504 ns / 14,992 B 6.5× faster, 14.3× less alloc

Key Capabilities

  • Zero eager materialization — Evaluates directly over JsonElement, JsonDocument, JsonObject, and JsonArray, decoding only the fields an expression actually reads.
  • JSON Schema static type checking — Compile Draft-07 / Draft 2020-12 JSON Schemas (or .NET types via JsonSchemaEnv.Create<T>()) into Celly static type declarations to catch typos and type mismatches before runtime.
  • Schema-driven static cost estimation — Feed JSON Schema maxLength / maxItems / maxProperties bounds directly into CelEnv.EstimateCost(...) to sandbox untrusted user rules.
  • In-expression JSON helpers — JsonLibrary adds json.parse(string) and json.stringify(dyn) inside CEL expressions.
  • Direct CEL → JSON serialization — Write CelValue results straight to Utf8JsonWriter, JSON strings, UTF-8 bytes, or JsonNode.
  • Zero extra dependencies & NativeAOT ready — Targets .NET 10, depends only on Celly and the BCL (System.Text.Json).

30-Second Example

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

// Compile once, evaluate millions of times
CelProgram rule = CelEnv.Default.Compile("user.age >= 18 && 'admin' in user.roles");

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

bool allowed = rule.EvalJsonBool(doc); // true (~270 ns, zero Dictionary allocations)

Where to Go Next

Clone this wiki locally