c# - 尝试简单的 microsoft/RulesEngine 概念证明但得到 JsonSerializationException

标签 c# .net-core rule-engine jsonserializer

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

我正在尝试以下库:

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”传递到规则引擎中,但出现以下异常?

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

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

  • 我已使用在线 JSON 检查 JSON 是否有效 验证器。
  • 我还尝试在顶部添加花括号 底部。
  • 我什至尝试过从以下位置复制/粘贴示例规则 JSON 文档在这里 https://github.com/microsoft/RulesEngine#how-to-use-it (会心 它不会工作,但至少应该通过创建的步骤 RulesEngine),但也失败并出现相同的错误。

最佳答案

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

鉴于您的文本文件已包含规则集合,您应该自行反序列化它,并将反序列化的集合传递到接受 WorkflowRules[] 的构造函数中:

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

关于c# - 尝试简单的 microsoft/RulesEngine 概念证明但得到 JsonSerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68053336/

相关文章:

c# - 为什么我不能从 'System.IO.StreamWriter'转换为 'CsvHelper.ISerializer'?

java - 流口水为什么 StatefulKnowledgeSession 有不同的知识库

java - drools 包名称(Fluent api)

c# - 如何在 C# 中将空值设置为 int?

c# - HashSet 上的 LINQ 的 ForEach?

c# - 如何从 wpf 中的方法调用返回格式化文本

macos - 为什么在 MacOS 上部署 ASP.NET-Core 应用程序时会收到错误消息 "Locking/unlocking file regions is not supported on this platform."?

c# - 是否可以使用 SqlCommand 强制执行只读行为?

c# - 进行通用 MVC API 查询

java - 使用 Drools 的动态规则