JSON Serialization
Install the RoslynRules.Json package:
dotnet add package RoslynRules.Json
Save and load rules/workflows from JSON for configuration-driven setups.
using RoslynRules.Json;
Methods
Serialize(Workflow|Rule, JsonSerializerOptions?)
Serializes to JSON string.
var json = JsonRuleLoader.Serialize(workflow);
File.WriteAllText("rules.json", json);
DeserializeWorkflow(string) / DeserializeRule(string)
Deserializes from JSON string.
var workflow = JsonRuleLoader.DeserializeWorkflow(json);
workflow.Validate();
workflow.Compile(new[] { new RuleParameter("customer", typeof(Customer)) });
LoadWorkflowFromFile(string) / LoadRuleFromFile(string)
Loads from file path.
var workflow = JsonRuleLoader.LoadWorkflowFromFile("rules.json");
SaveWorkflowToFile(Workflow, string) / SaveRuleToFile(Rule, string)
Saves to file path.
JsonRuleLoader.SaveWorkflowToFile(workflow, "rules.json");
JSON Options
Custom JsonSerializerOptions with camelCase naming and indented output.
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new JsonStringEnumConverter() }
};
var json = JsonRuleLoader.Serialize(workflow, options);
Related
- Rule — Serialized model
- Workflow — Serialized container
- Rule Templates — Serialize templates before instantiation
- Getting Started
- NuGet Package