using System.Collections.Generic;
using System.Text.Json.Serialization;
var baseRule = JsonSerializer.Deserialize<BaseRule>(
new JsonSerializerOptions(JsonSerializerDefaults.Web)
public class RuleGroup : BaseRule
public string Combinator { get; set; }
public bool Not { get; set; }
public List<BaseRule> Criteria { get; set; }
public class RuleCriteria : BaseRule
public string Field { get; set; }
public string Operator { get; set; }
public string Key { get; set; }
public string Value { get; set; }
[JsonConverter(typeof(RuleConverter))]
public class RuleConverter : JsonConverter<BaseRule>
public override BaseRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
using var doc = JsonDocument.ParseValue(ref reader);
if (doc.RootElement.TryGetProperty("combinator", out _))
return doc.Deserialize<RuleGroup>(options);
return doc.Deserialize<RuleCriteria>(options);
public override void Write(Utf8JsonWriter writer, BaseRule value, JsonSerializerOptions options)
=> throw new NotImplementedException();