Skip to content

Commit

Permalink
move from newtonsoft to system.text.json (#599)
Browse files Browse the repository at this point in the history
Co-authored-by: YogeshPraj <[email protected]>
  • Loading branch information
madhon and YogeshPraj authored Aug 23, 2024
1 parent 6a747b3 commit 95c6340
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 39 deletions.
5 changes: 3 additions & 2 deletions benchmark/RulesEngineBenchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Newtonsoft.Json;
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.IO;

namespace RulesEngineBenchmark
{
using System.Text.Json;

[MemoryDiagnoser]
public class REBenchmark
{
Expand All @@ -34,7 +35,7 @@ public REBenchmark()
}

var fileData = File.ReadAllText(files[0]);
workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
workflow = JsonSerializer.Deserialize<List<Workflow>>(fileData);

rulesEngine = new RulesEngine.RulesEngine(workflow.ToArray(), new ReSettings {
EnableFormattedErrorMessage = false,
Expand Down
14 changes: 6 additions & 8 deletions demo/DemoApp/EFDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +13,8 @@

namespace DemoApp
{
using System.Text.Json;

public class EFDemo
{
public void Run()
Expand All @@ -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<ExpandoObject>(basicInfo, converter);
dynamic input2 = JsonConvert.DeserializeObject<ExpandoObject>(orderInfo, converter);
dynamic input3 = JsonConvert.DeserializeObject<ExpandoObject>(telemetryInfo, converter);
dynamic input1 = JsonSerializer.Deserialize<ExpandoObject>(basicInfo);
dynamic input2 = JsonSerializer.Deserialize<ExpandoObject>(orderInfo);
dynamic input3 = JsonSerializer.Deserialize<ExpandoObject>(telemetryInfo);

var inputs = new dynamic[]
{
Expand All @@ -42,7 +40,7 @@ public void Run()
throw new Exception("Rules not found.");

var fileData = File.ReadAllText(files[0]);
var workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
var workflow = JsonSerializer.Deserialize<List<Workflow>>(fileData);

RulesEngineDemoContext db = new RulesEngineDemoContext();
if (db.Database.EnsureCreated())
Expand Down
14 changes: 7 additions & 7 deletions demo/DemoApp/JSONDemo.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +10,8 @@

namespace DemoApp
{
using System.Text.Json;

public class JSONDemo
{
public void Run()
Expand All @@ -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<ExpandoObject>(basicInfo, converter);
dynamic input2 = JsonConvert.DeserializeObject<ExpandoObject>(orderInfo, converter);
dynamic input3 = JsonConvert.DeserializeObject<ExpandoObject>(telemetryInfo, converter);

dynamic input1 = JsonSerializer.Deserialize<ExpandoObject>(basicInfo);
dynamic input2 = JsonSerializer.Deserialize<ExpandoObject>(orderInfo);
dynamic input3 = JsonSerializer.Deserialize<ExpandoObject>(telemetryInfo);

var inputs = new dynamic[]
{
Expand All @@ -39,7 +39,7 @@ public void Run()
throw new Exception("Rules not found.");

var fileData = File.ReadAllText(files[0]);
var workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
var workflow = JsonSerializer.Deserialize<List<Workflow>>(fileData);

var bre = new RulesEngine.RulesEngine(workflow.ToArray(), null);

Expand Down
5 changes: 3 additions & 2 deletions demo/DemoApp/NestedInputDemo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Newtonsoft.Json;
using RulesEngine.Extensions;
using RulesEngine.Models;
using System;
Expand All @@ -10,6 +9,8 @@

namespace DemoApp
{
using System.Text.Json;

internal class ListItem
{
public int Id { get; set; }
Expand Down Expand Up @@ -49,7 +50,7 @@ public void Run()
}

var fileData = File.ReadAllText(files[0]);
var Workflows = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
var Workflows = JsonSerializer.Deserialize<List<Workflow>>(fileData);

var bre = new RulesEngine.RulesEngine(Workflows.ToArray(), null);
foreach (var workflow in Workflows)
Expand Down
7 changes: 4 additions & 3 deletions src/RulesEngine/Actions/ActionContext.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> _context;
Expand All @@ -27,7 +28,7 @@ public ActionContext(IDictionary<string, object> context, RuleResultTree parentR
value = kv.Value.ToString();
break;
default:
value = JsonConvert.SerializeObject(kv.Value);
value = JsonSerializer.Serialize(kv.Value);
break;

}
Expand Down Expand Up @@ -70,7 +71,7 @@ public T GetContext<T>(string name)
{
return (T)Convert.ChangeType(_context[name], typeof(T));
}
return JsonConvert.DeserializeObject<T>(_context[name]);
return JsonSerializer.Deserialize<T>(_context[name]);
}
catch (KeyNotFoundException)
{
Expand Down
6 changes: 3 additions & 3 deletions src/RulesEngine/Models/Rule.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Rule class
/// </summary>
Expand All @@ -33,7 +33,7 @@ public class Rule
/// </summary>
public bool Enabled { get; set; } = true;

[JsonConverter(typeof(StringEnumConverter))]
[JsonConverter (typeof(JsonStringEnumConverter))]
public RuleExpressionType RuleExpressionType { get; set; } = RuleExpressionType.LambdaExpression;
public IEnumerable<string> WorkflowsToInject { get; set; }
public IEnumerable<Rule> Rules { get; set; }
Expand Down
2 changes: 0 additions & 2 deletions src/RulesEngine/Models/Workflow.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
19 changes: 10 additions & 9 deletions src/RulesEngine/RulesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,10 +18,13 @@

namespace RulesEngine
{
using System.Text.Json;
using System.Text.Json.Nodes;

/// <summary>
///
/// </summary>
/// <seealso cref="RulesEngine.Interfaces.IRulesEngine" />
/// <seealso cref="IRulesEngine" />
public class RulesEngine : IRulesEngine
{
#region Variables
Expand All @@ -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<Workflow>(item)).ToArray();
var workflow = jsonConfig.Select(item => JsonSerializer.Deserialize<Workflow>(item)).ToArray();
AddWorkflow(workflow);
}

Expand Down Expand Up @@ -403,7 +404,7 @@ private IEnumerable<RuleResultTree> FormatErrorMessages(IEnumerable<RuleResultTr
{
var arrParams = inputs?.Select(c => 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})");
}
}
Expand All @@ -430,10 +431,10 @@ private static string UpdateErrorMessage(string errorMessage, IDictionary<string
var model = arrParams?.Where(a => 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})");
}

Expand Down
18 changes: 15 additions & 3 deletions src/RulesEngine/RulesEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FastExpressionCompiler" Version="4.0.1" />
<PackageReference Include="FastExpressionCompiler" Version="4.1.0" />
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

<PackageReference Include="System.Linq.Dynamic.Core" Version="1.4.3" />

<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Text.Json" Version="6.0.9" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />

<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0"/>

</ItemGroup>

</Project>

0 comments on commit 95c6340

Please sign in to comment.