diff --git a/benchmark/RulesEngineBenchmark/Program.cs b/benchmark/RulesEngineBenchmark/Program.cs index 6d611abe..e8aeb7a7 100644 --- a/benchmark/RulesEngineBenchmark/Program.cs +++ b/benchmark/RulesEngineBenchmark/Program.cs @@ -3,7 +3,6 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; -using Newtonsoft.Json; using RulesEngine.Models; using System; using System.Collections.Generic; @@ -11,6 +10,8 @@ namespace RulesEngineBenchmark { + using System.Text.Json; + [MemoryDiagnoser] public class REBenchmark { @@ -34,7 +35,7 @@ public REBenchmark() } var fileData = File.ReadAllText(files[0]); - workflow = JsonConvert.DeserializeObject>(fileData); + workflow = JsonSerializer.Deserialize>(fileData); rulesEngine = new RulesEngine.RulesEngine(workflow.ToArray(), new ReSettings { EnableFormattedErrorMessage = false, diff --git a/demo/DemoApp/EFDemo.cs b/demo/DemoApp/EFDemo.cs index a02f5a3f..4f196696 100644 --- a/demo/DemoApp/EFDemo.cs +++ b/demo/DemoApp/EFDemo.cs @@ -2,8 +2,6 @@ // Licensed under the MIT License. using DemoApp.EFDataExample; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using RulesEngine.Models; using System; using System.Collections.Generic; @@ -15,6 +13,8 @@ namespace DemoApp { + using System.Text.Json; + public class EFDemo { public void Run() @@ -24,11 +24,9 @@ public void Run() var orderInfo = "{\"totalOrders\": 5,\"recurringItems\": 2}"; var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}"; - var converter = new ExpandoObjectConverter(); - - dynamic input1 = JsonConvert.DeserializeObject(basicInfo, converter); - dynamic input2 = JsonConvert.DeserializeObject(orderInfo, converter); - dynamic input3 = JsonConvert.DeserializeObject(telemetryInfo, converter); + dynamic input1 = JsonSerializer.Deserialize(basicInfo); + dynamic input2 = JsonSerializer.Deserialize(orderInfo); + dynamic input3 = JsonSerializer.Deserialize(telemetryInfo); var inputs = new dynamic[] { @@ -42,7 +40,7 @@ public void Run() throw new Exception("Rules not found."); var fileData = File.ReadAllText(files[0]); - var workflow = JsonConvert.DeserializeObject>(fileData); + var workflow = JsonSerializer.Deserialize>(fileData); RulesEngineDemoContext db = new RulesEngineDemoContext(); if (db.Database.EnsureCreated()) diff --git a/demo/DemoApp/JSONDemo.cs b/demo/DemoApp/JSONDemo.cs index 88f9ad95..d3b8897d 100644 --- a/demo/DemoApp/JSONDemo.cs +++ b/demo/DemoApp/JSONDemo.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using RulesEngine.Models; using System; using System.Collections.Generic; @@ -12,6 +10,8 @@ namespace DemoApp { + using System.Text.Json; + public class JSONDemo { public void Run() @@ -21,11 +21,11 @@ public void Run() var orderInfo = "{\"totalOrders\": 5,\"recurringItems\": 2}"; var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}"; - var converter = new ExpandoObjectConverter(); - dynamic input1 = JsonConvert.DeserializeObject(basicInfo, converter); - dynamic input2 = JsonConvert.DeserializeObject(orderInfo, converter); - dynamic input3 = JsonConvert.DeserializeObject(telemetryInfo, converter); + + dynamic input1 = JsonSerializer.Deserialize(basicInfo); + dynamic input2 = JsonSerializer.Deserialize(orderInfo); + dynamic input3 = JsonSerializer.Deserialize(telemetryInfo); var inputs = new dynamic[] { @@ -39,7 +39,7 @@ public void Run() throw new Exception("Rules not found."); var fileData = File.ReadAllText(files[0]); - var workflow = JsonConvert.DeserializeObject>(fileData); + var workflow = JsonSerializer.Deserialize>(fileData); var bre = new RulesEngine.RulesEngine(workflow.ToArray(), null); diff --git a/demo/DemoApp/NestedInputDemo.cs b/demo/DemoApp/NestedInputDemo.cs index 9427af14..62b62fc3 100644 --- a/demo/DemoApp/NestedInputDemo.cs +++ b/demo/DemoApp/NestedInputDemo.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using Newtonsoft.Json; using RulesEngine.Extensions; using RulesEngine.Models; using System; @@ -10,6 +9,8 @@ namespace DemoApp { + using System.Text.Json; + internal class ListItem { public int Id { get; set; } @@ -49,7 +50,7 @@ public void Run() } var fileData = File.ReadAllText(files[0]); - var Workflows = JsonConvert.DeserializeObject>(fileData); + var Workflows = JsonSerializer.Deserialize>(fileData); var bre = new RulesEngine.RulesEngine(Workflows.ToArray(), null); foreach (var workflow in Workflows) diff --git a/src/RulesEngine/Actions/ActionContext.cs b/src/RulesEngine/Actions/ActionContext.cs index 1c86778d..44116679 100644 --- a/src/RulesEngine/Actions/ActionContext.cs +++ b/src/RulesEngine/Actions/ActionContext.cs @@ -1,13 +1,14 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using Newtonsoft.Json; using RulesEngine.Models; using System; using System.Collections.Generic; namespace RulesEngine.Actions { + using System.Text.Json; + public class ActionContext { private readonly IDictionary _context; @@ -27,7 +28,7 @@ public ActionContext(IDictionary context, RuleResultTree parentR value = kv.Value.ToString(); break; default: - value = JsonConvert.SerializeObject(kv.Value); + value = JsonSerializer.Serialize(kv.Value); break; } @@ -70,7 +71,7 @@ public T GetContext(string name) { return (T)Convert.ChangeType(_context[name], typeof(T)); } - return JsonConvert.DeserializeObject(_context[name]); + return JsonSerializer.Deserialize(_context[name]); } catch (KeyNotFoundException) { diff --git a/src/RulesEngine/Models/Rule.cs b/src/RulesEngine/Models/Rule.cs index 13bf225b..04ceebf1 100644 --- a/src/RulesEngine/Models/Rule.cs +++ b/src/RulesEngine/Models/Rule.cs @@ -1,13 +1,13 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace RulesEngine.Models { + using System.Text.Json.Serialization; + /// /// Rule class /// @@ -33,7 +33,7 @@ public class Rule /// public bool Enabled { get; set; } = true; - [JsonConverter(typeof(StringEnumConverter))] + [JsonConverter (typeof(JsonStringEnumConverter))] public RuleExpressionType RuleExpressionType { get; set; } = RuleExpressionType.LambdaExpression; public IEnumerable WorkflowsToInject { get; set; } public IEnumerable Rules { get; set; } diff --git a/src/RulesEngine/Models/Workflow.cs b/src/RulesEngine/Models/Workflow.cs index 91d6bdba..10d64aec 100644 --- a/src/RulesEngine/Models/Workflow.cs +++ b/src/RulesEngine/Models/Workflow.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using Newtonsoft.Json.Converters; -using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; diff --git a/src/RulesEngine/RulesEngine.cs b/src/RulesEngine/RulesEngine.cs index 66cd352b..b28e0da7 100644 --- a/src/RulesEngine/RulesEngine.cs +++ b/src/RulesEngine/RulesEngine.cs @@ -2,8 +2,6 @@ // Licensed under the MIT License. using FluentValidation; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using RulesEngine.Actions; using RulesEngine.Exceptions; using RulesEngine.ExpressionBuilders; @@ -20,10 +18,13 @@ namespace RulesEngine { + using System.Text.Json; + using System.Text.Json.Nodes; + /// /// /// - /// + /// public class RulesEngine : IRulesEngine { #region Variables @@ -38,7 +39,7 @@ public class RulesEngine : IRulesEngine #region Constructor public RulesEngine(string[] jsonConfig, ReSettings reSettings = null) : this(reSettings) { - var workflow = jsonConfig.Select(item => JsonConvert.DeserializeObject(item)).ToArray(); + var workflow = jsonConfig.Select(item => JsonSerializer.Deserialize(item)).ToArray(); AddWorkflow(workflow); } @@ -403,7 +404,7 @@ private IEnumerable FormatErrorMessages(IEnumerable new { Name = c.Key, c.Value }); var model = arrParams?.Where(a => string.Equals(a.Name, property))?.FirstOrDefault(); - var value = model?.Value != null ? JsonConvert.SerializeObject(model?.Value) : null; + var value = model?.Value != null ? JsonSerializer.Serialize(model?.Value) : null; errorMessage = errorMessage?.Replace($"$({property})", value ?? $"$({property})"); } } @@ -430,10 +431,10 @@ private static string UpdateErrorMessage(string errorMessage, IDictionary string.Equals(a.Name, typeName))?.FirstOrDefault(); if (model != null) { - var modelJson = JsonConvert.SerializeObject(model?.Value); - var jObj = JObject.Parse(modelJson); - JToken jToken = null; - var val = jObj?.TryGetValue(propertyName, StringComparison.OrdinalIgnoreCase, out jToken); + var modelJson = JsonSerializer.Serialize(model?.Value); + var jObj = JsonObject.Parse(modelJson).AsObject(); + JsonNode jToken = null; + var val = jObj?.TryGetPropertyValue(propertyName, out jToken); errorMessage = errorMessage.Replace($"$({property})", jToken != null ? jToken?.ToString() : $"({property})"); } diff --git a/src/RulesEngine/RulesEngine.csproj b/src/RulesEngine/RulesEngine.csproj index 6c223d0f..1fa1c2c3 100644 --- a/src/RulesEngine/RulesEngine.csproj +++ b/src/RulesEngine/RulesEngine.csproj @@ -33,15 +33,27 @@ - + - + + + + + + + + + + - + + + +