尝试对 Microsoft / RulesEngine 进行简单的概念验证,但得到 JsonSerializationException

Attempting simple proof of concept of microsoft / RulesEngine but getting JsonSerializationException

我假设我在这里犯了一个愚蠢的错误,但想知道是否有人可以提供帮助?

我正在试用以下库:

Install-Package RulesEngine -Version 3.2.0

我的rules.json如下:

[
  {
      "WorkflowName": "DeliverToMatchingSuburb",
      "Rules": [
        {
          "RuleName": "MatchingSuburb",
          "Expression": "customer.PostCode == location.PostCode"
        }
      ]
  }
]

我的代码:

    string[] readText = File.ReadAllLines("rules.json");
    Customer c = new Customer();
    Pharmacy p = new Pharmacy();
    var pharmacies = p.GetLocations();
    var customers = c.GenerateCustomers();

    var re = new RulesEngine.RulesEngine(readText, null);

我尝试将 'readText' 传递到 RulesEngine 中,但出现以下异常?

Newtonsoft.Json.JsonSerializationException: 'Unexpected end when reading JSON. Path '', line 1, position 1.'

异常发生在这一行:var re = new RulesEngine.RulesEngine(readText, null);

RulesEngine(string[], ILogger, ReSettings) 构造函数的期望是字符串数组的每个元素都是一个完整的 JSON 对象。在您的例子中,您只为每个数组元素提供了一行。

鉴于您的文本文件已经包含规则集合,您应该自己反序列化它,并将反序列化的集合传递给接受 WorkflowRules[]:

的构造函数
string json = File.ReadAllText("rules.json");
var rules = JsonConvert.DeserializeObject<WorkflowRules[]>(json);
var engine = new RulesEngine.RulesEngine(rules);