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.
---
## Related
- [Rule](/RoslynRules/api/rule.html) — Serialized model
- [Workflow](/RoslynRules/api/workflow.html) — Serialized container
- [Rule Templates](/RoslynRules/api/rule-templates.html) — Serialize templates before instantiation
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new JsonStringEnumConverter() }
};
var json = JsonRuleLoader.Serialize(workflow, options);