json-eval-rs 0.0.98

High-performance JSON Logic evaluator with schema validation and dependency tracking. Built on blazing-fast Rust engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# ParsedSchemaCache - Dependency Injection Web API Guide

Complete guide for using ParsedSchemaCache with ASP.NET Core Web API and modern Program.cs (minimal hosting model).

## Quick Start

### 1. Install NuGet Package

```bash
dotnet add package JsonEvalRs
```

### 2. Create a New Web API Project

```bash
dotnet new webapi -n JsonEvalDemo
cd JsonEvalDemo
dotnet add package JsonEvalRs
```

## Complete Example: Program.cs (Minimal Hosting Model)

### Basic Setup with Global Cache

```csharp
using JsonEvalRs;

var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// ✅ Add JsonEvalRs global cache
builder.Services.AddJsonEvalRsCache();

var app = builder.Build();

// Configure middleware
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();
```

### Advanced Setup with Pre-loaded Schemas

```csharp
using JsonEvalRs;
using System.Text.Json;

var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// ✅ Add JsonEvalRs cache with pre-loaded schemas
builder.Services.AddJsonEvalRsCache(cache =>
{
    var logger = builder.Logging.Services.BuildServiceProvider()
        .GetRequiredService<ILogger<Program>>();
    
    try
    {
        // Load schemas from files
        var schemasPath = Path.Combine(builder.Environment.ContentRootPath, "Schemas");
        
        if (Directory.Exists(schemasPath))
        {
            foreach (var file in Directory.GetFiles(schemasPath, "*.json"))
            {
                var schemaName = Path.GetFileNameWithoutExtension(file);
                var schemaJson = File.ReadAllText(file);
                
                cache.Insert(schemaName, schemaJson);
                logger.LogInformation("Loaded schema: {SchemaName}", schemaName);
            }
            
            logger.LogInformation("✅ Pre-loaded {Count} schemas", cache.Count);
        }
        else
        {
            logger.LogWarning("Schemas directory not found: {Path}", schemasPath);
        }
    }
    catch (Exception ex)
    {
        logger.LogError(ex, "Failed to pre-load schemas");
    }
});

var app = builder.Build();

// ✅ Initialize cache (triggers pre-loading)
app.UseJsonEvalRsCache();

// Configure middleware
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();
```

## Controller Examples

### Basic Controller with Cache Injection

```csharp
using JsonEvalRs;
using Microsoft.AspNetCore.Mvc;

namespace JsonEvalDemo.Controllers;

[ApiController]
[Route("api/[controller]")]
public class EvaluationController : ControllerBase
{
    private readonly ParsedSchemaCache _cache;
    private readonly ILogger<EvaluationController> _logger;

    public EvaluationController(
        ParsedSchemaCache cache,
        ILogger<EvaluationController> logger)
    {
        _cache = cache;
        _logger = logger;
    }

    [HttpGet("cache/stats")]
    public IActionResult GetCacheStats()
    {
        var stats = _cache.GetStats();
        return Ok(new
        {
            entryCount = stats.EntryCount,
            keys = stats.Keys,
            isEmpty = _cache.IsEmpty
        });
    }

    [HttpPost("evaluate")]
    public IActionResult Evaluate([FromBody] EvaluationRequest request)
    {
        try
        {
            // Ensure schema is cached
            if (!_cache.Contains(request.SchemaKey))
            {
                if (string.IsNullOrEmpty(request.Schema))
                {
                    return BadRequest($"Schema '{request.SchemaKey}' not found in cache and no schema provided");
                }
                
                _cache.Insert(request.SchemaKey, request.Schema);
                _logger.LogInformation("Cached new schema: {SchemaKey}", request.SchemaKey);
            }

            // Use the schema (benefits from cached ParsedSchema internally)
            using var eval = new JSONEval(
                request.Schema ?? GetSchemaFromCache(request.SchemaKey),
                request.Context,
                request.Data
            );
            
            eval.Evaluate(request.Data, request.Context);
            var result = eval.GetEvaluatedSchema(skipLayout: false);
            
            return Ok(new
            {
                success = true,
                result = result,
                schemaKey = request.SchemaKey,
                cached = true
            });
        }
        catch (JsonEvalException ex)
        {
            _logger.LogError(ex, "Evaluation failed");
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpPost("validate")]
    public IActionResult Validate([FromBody] ValidationRequest request)
    {
        try
        {
            if (!_cache.Contains(request.SchemaKey))
            {
                return BadRequest($"Schema '{request.SchemaKey}' not found in cache");
            }

            var schema = GetSchemaFromCache(request.SchemaKey);
            using var eval = new JSONEval(schema, request.Context, null);
            
            var validationResult = eval.Validate(request.Data, request.Context, null);
            
            return Ok(new
            {
                hasError = validationResult.HasError,
                errors = validationResult.Errors.Select(e => new
                {
                    path = e.Key,
                    ruleType = e.Value.RuleType,
                    message = e.Value.Message
                })
            });
        }
        catch (JsonEvalException ex)
        {
            _logger.LogError(ex, "Validation failed");
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpDelete("cache/{key}")]
    public IActionResult RemoveFromCache(string key)
    {
        var removed = _cache.Remove(key);
        
        if (removed)
        {
            _logger.LogInformation("Removed schema from cache: {Key}", key);
            return Ok(new { message = $"Schema '{key}' removed from cache" });
        }
        
        return NotFound(new { message = $"Schema '{key}' not found in cache" });
    }

    [HttpDelete("cache")]
    public IActionResult ClearCache()
    {
        var count = _cache.Count;
        _cache.Clear();
        
        _logger.LogInformation("Cleared cache ({Count} entries)", count);
        return Ok(new { message = $"Cleared {count} schemas from cache" });
    }

    private string GetSchemaFromCache(string key)
    {
        // In real app, you'd store the schema JSON alongside the cached ParsedSchema
        // For this example, we assume it's available
        throw new NotImplementedException("Implement schema retrieval logic");
    }
}

// Request models
public class EvaluationRequest
{
    public string SchemaKey { get; set; } = "";
    public string? Schema { get; set; }
    public string Data { get; set; } = "{}";
    public string? Context { get; set; }
}

public class ValidationRequest
{
    public string SchemaKey { get; set; } = "";
    public string Data { get; set; } = "{}";
    public string? Context { get; set; }
}
```

### Advanced: Service Layer Pattern

```csharp
// ISchemaService.cs
public interface ISchemaService
{
    Task<string> GetSchemaAsync(string key);
    Task CacheSchemaAsync(string key, string schema);
    bool IsCached(string key);
    Task<object> EvaluateAsync(string schemaKey, string data, string? context = null);
}

// SchemaService.cs
public class SchemaService : ISchemaService
{
    private readonly ParsedSchemaCache _cache;
    private readonly ILogger<SchemaService> _logger;
    private readonly Dictionary<string, string> _schemaStore; // Simple in-memory store

    public SchemaService(
        ParsedSchemaCache cache,
        ILogger<SchemaService> logger)
    {
        _cache = cache;
        _logger = logger;
        _schemaStore = new Dictionary<string, string>();
    }

    public Task<string> GetSchemaAsync(string key)
    {
        if (_schemaStore.TryGetValue(key, out var schema))
        {
            return Task.FromResult(schema);
        }
        
        throw new KeyNotFoundException($"Schema '{key}' not found");
    }

    public Task CacheSchemaAsync(string key, string schema)
    {
        _cache.Insert(key, schema);
        _schemaStore[key] = schema;
        _logger.LogInformation("Schema cached: {Key}", key);
        
        return Task.CompletedTask;
    }

    public bool IsCached(string key)
    {
        return _cache.Contains(key);
    }

    public async Task<object> EvaluateAsync(string schemaKey, string data, string? context = null)
    {
        var schema = await GetSchemaAsync(schemaKey);
        
        using var eval = new JSONEval(schema, context, data);
        eval.Evaluate(data, context);
        
        return eval.GetEvaluatedSchema(skipLayout: false);
    }
}

// Program.cs - Register service
builder.Services.AddJsonEvalRsCache();
builder.Services.AddScoped<ISchemaService, SchemaService>();

// Controller using service
[ApiController]
[Route("api/[controller]")]
public class SchemaController : ControllerBase
{
    private readonly ISchemaService _schemaService;

    public SchemaController(ISchemaService schemaService)
    {
        _schemaService = schemaService;
    }

    [HttpPost("evaluate")]
    public async Task<IActionResult> Evaluate([FromBody] EvaluationRequest request)
    {
        try
        {
            if (!_schemaService.IsCached(request.SchemaKey) && !string.IsNullOrEmpty(request.Schema))
            {
                await _schemaService.CacheSchemaAsync(request.SchemaKey, request.Schema);
            }

            var result = await _schemaService.EvaluateAsync(
                request.SchemaKey,
                request.Data,
                request.Context
            );

            return Ok(result);
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }
}
```

## Configuration Patterns

### Pattern 1: Configuration-based Pre-loading

```csharp
// appsettings.json
{
  "JsonEvalRs": {
    "Schemas": {
      "validation": "schemas/validation-schema.json",
      "calculation": "schemas/calculation-schema.json",
      "report": "schemas/report-schema.json"
    }
  }
}

// Program.cs
builder.Services.Configure<JsonEvalRsOptions>(
    builder.Configuration.GetSection("JsonEvalRs"));

builder.Services.AddJsonEvalRsCache(cache =>
{
    var options = builder.Configuration
        .GetSection("JsonEvalRs:Schemas")
        .Get<Dictionary<string, string>>();

    if (options != null)
    {
        foreach (var (key, path) in options)
        {
            var fullPath = Path.Combine(builder.Environment.ContentRootPath, path);
            if (File.Exists(fullPath))
            {
                var schema = File.ReadAllText(fullPath);
                cache.Insert(key, schema);
            }
        }
    }
});
```

### Pattern 2: Database-backed Schemas

```csharp
// Program.cs
builder.Services.AddJsonEvalRsCache(cache =>
{
    var serviceProvider = builder.Services.BuildServiceProvider();
    using var scope = serviceProvider.CreateScope();
    var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    
    var schemas = dbContext.Schemas
        .Where(s => s.IsActive)
        .ToList();
    
    foreach (var schema in schemas)
    {
        cache.Insert(schema.Key, schema.JsonContent);
    }
});
```

### Pattern 3: Lazy Loading with Factory

```csharp
// SchemaFactory.cs
public class SchemaFactory
{
    private readonly ParsedSchemaCache _cache;
    private readonly IConfiguration _configuration;

    public SchemaFactory(ParsedSchemaCache cache, IConfiguration configuration)
    {
        _cache = cache;
        _configuration = configuration;
    }

    public string GetOrLoadSchema(string key)
    {
        if (!_cache.Contains(key))
        {
            var schemaPath = _configuration[$"Schemas:{key}"];
            if (!string.IsNullOrEmpty(schemaPath))
            {
                var schema = File.ReadAllText(schemaPath);
                _cache.Insert(key, schema);
            }
        }
        
        // Return schema from your storage
        return LoadSchemaFromStorage(key);
    }

    private string LoadSchemaFromStorage(string key)
    {
        // Implement your storage logic
        throw new NotImplementedException();
    }
}

// Program.cs
builder.Services.AddJsonEvalRsCache();
builder.Services.AddSingleton<SchemaFactory>();
```

## Testing

### Unit Test Example

```csharp
using Xunit;
using JsonEvalRs;

public class SchemaServiceTests
{
    [Fact]
    public void CacheService_ShouldStoreAndRetrieve()
    {
        // Arrange
        using var cache = new ParsedSchemaCache();
        var schemaJson = @"{
            ""type"": ""object"",
            ""properties"": {
                ""name"": { ""type"": ""string"" }
            }
        }";

        // Act
        cache.Insert("test-schema", schemaJson);

        // Assert
        Assert.True(cache.Contains("test-schema"));
        Assert.Equal(1, cache.Count);
    }

    [Fact]
    public void GlobalCache_ShouldBeShared()
    {
        // Arrange & Act
        ParsedSchemaCache.Global.Insert("shared-schema", "{}");

        // Assert
        Assert.True(ParsedSchemaCache.Global.Contains("shared-schema"));
    }
}
```

### Integration Test Example

```csharp
public class EvaluationControllerTests : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly HttpClient _client;

    public EvaluationControllerTests(WebApplicationFactory<Program> factory)
    {
        _client = factory.CreateClient();
    }

    [Fact]
    public async Task CacheStats_ShouldReturnStats()
    {
        // Act
        var response = await _client.GetAsync("/api/evaluation/cache/stats");

        // Assert
        response.EnsureSuccessStatusCode();
        var content = await response.Content.ReadAsStringAsync();
        Assert.Contains("entryCount", content);
    }
}
```

## Project Structure

```
JsonEvalDemo/
├── Program.cs              # Minimal hosting setup
├── appsettings.json        # Configuration
├── Controllers/
│   ├── EvaluationController.cs
│   └── SchemaController.cs
├── Services/
│   ├── ISchemaService.cs
│   └── SchemaService.cs
├── Models/
│   ├── EvaluationRequest.cs
│   └── ValidationRequest.cs
└── Schemas/                # Pre-loaded schema files
    ├── validation-schema.json
    ├── calculation-schema.json
    └── report-schema.json
```

## Best Practices

1. **Use Global Cache for Shared Schemas**
   - One cache instance across entire application
   - Schemas shared between all requests

2. **Pre-load on Startup**
   - Load frequently-used schemas during app initialization
   - Reduces first-request latency

3. **Monitor Cache Usage**
   - Expose cache stats via health check endpoint
   - Log cache operations for debugging

4. **Handle Cache Misses Gracefully**
   - Fall back to loading schema if not cached
   - Cache automatically on first use

5. **Clear Cache Strategically**
   - Only clear when schemas are updated
   - Consider selective removal instead of full clear

6. **Use Scoped Services**
   - Schema service as scoped for per-request isolation
   - Cache as singleton for shared state

## Performance Metrics

With ParsedSchemaCache:
- **~100x faster** schema initialization (after first parse)
-**~10x less memory** (Arc-based sharing)
-**Zero parsing overhead** for cached schemas
-**Thread-safe** concurrent access

## Troubleshooting

### Schema Not Found

```csharp
if (!cache.Contains("my-schema"))
{
    var keys = cache.GetKeys();
    _logger.LogWarning("Schema not found. Available: {Keys}", string.Join(", ", keys));
}
```

### Cache Not Initializing

Ensure `UseJsonEvalRsCache()` is called in Program.cs:

```csharp
var app = builder.Build();
app.UseJsonEvalRsCache(); // ← Required for pre-loading
```

### Native Library Not Found

Ensure the JsonEvalRs NuGet package includes native libraries for your platform.

## Summary

ParsedSchemaCache provides:
- **Simple DI integration** via `AddJsonEvalRsCache()`
-**Automatic pre-loading** on startup
-**Thread-safe** global or local instances
-**Performance** optimized for high-throughput scenarios
-**Flexible** configuration and usage patterns

Perfect for microservices, APIs, and high-performance applications! 🚀