Skip to content

Commit 95c6340

Browse files
madhonYogeshPraj
andauthored
move from newtonsoft to system.text.json (#599)
Co-authored-by: YogeshPraj <[email protected]>
1 parent 6a747b3 commit 95c6340

File tree

9 files changed

+51
-39
lines changed

9 files changed

+51
-39
lines changed

benchmark/RulesEngineBenchmark/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
using BenchmarkDotNet.Attributes;
55
using BenchmarkDotNet.Running;
6-
using Newtonsoft.Json;
76
using RulesEngine.Models;
87
using System;
98
using System.Collections.Generic;
109
using System.IO;
1110

1211
namespace RulesEngineBenchmark
1312
{
13+
using System.Text.Json;
14+
1415
[MemoryDiagnoser]
1516
public class REBenchmark
1617
{
@@ -34,7 +35,7 @@ public REBenchmark()
3435
}
3536

3637
var fileData = File.ReadAllText(files[0]);
37-
workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
38+
workflow = JsonSerializer.Deserialize<List<Workflow>>(fileData);
3839

3940
rulesEngine = new RulesEngine.RulesEngine(workflow.ToArray(), new ReSettings {
4041
EnableFormattedErrorMessage = false,

demo/DemoApp/EFDemo.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the MIT License.
33

44
using DemoApp.EFDataExample;
5-
using Newtonsoft.Json;
6-
using Newtonsoft.Json.Converters;
75
using RulesEngine.Models;
86
using System;
97
using System.Collections.Generic;
@@ -15,6 +13,8 @@
1513

1614
namespace DemoApp
1715
{
16+
using System.Text.Json;
17+
1818
public class EFDemo
1919
{
2020
public void Run()
@@ -24,11 +24,9 @@ public void Run()
2424
var orderInfo = "{\"totalOrders\": 5,\"recurringItems\": 2}";
2525
var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}";
2626

27-
var converter = new ExpandoObjectConverter();
28-
29-
dynamic input1 = JsonConvert.DeserializeObject<ExpandoObject>(basicInfo, converter);
30-
dynamic input2 = JsonConvert.DeserializeObject<ExpandoObject>(orderInfo, converter);
31-
dynamic input3 = JsonConvert.DeserializeObject<ExpandoObject>(telemetryInfo, converter);
27+
dynamic input1 = JsonSerializer.Deserialize<ExpandoObject>(basicInfo);
28+
dynamic input2 = JsonSerializer.Deserialize<ExpandoObject>(orderInfo);
29+
dynamic input3 = JsonSerializer.Deserialize<ExpandoObject>(telemetryInfo);
3230

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

4442
var fileData = File.ReadAllText(files[0]);
45-
var workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
43+
var workflow = JsonSerializer.Deserialize<List<Workflow>>(fileData);
4644

4745
RulesEngineDemoContext db = new RulesEngineDemoContext();
4846
if (db.Database.EnsureCreated())

demo/DemoApp/JSONDemo.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Newtonsoft.Json;
5-
using Newtonsoft.Json.Converters;
64
using RulesEngine.Models;
75
using System;
86
using System.Collections.Generic;
@@ -12,6 +10,8 @@
1210

1311
namespace DemoApp
1412
{
13+
using System.Text.Json;
14+
1515
public class JSONDemo
1616
{
1717
public void Run()
@@ -21,11 +21,11 @@ public void Run()
2121
var orderInfo = "{\"totalOrders\": 5,\"recurringItems\": 2}";
2222
var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}";
2323

24-
var converter = new ExpandoObjectConverter();
2524

26-
dynamic input1 = JsonConvert.DeserializeObject<ExpandoObject>(basicInfo, converter);
27-
dynamic input2 = JsonConvert.DeserializeObject<ExpandoObject>(orderInfo, converter);
28-
dynamic input3 = JsonConvert.DeserializeObject<ExpandoObject>(telemetryInfo, converter);
25+
26+
dynamic input1 = JsonSerializer.Deserialize<ExpandoObject>(basicInfo);
27+
dynamic input2 = JsonSerializer.Deserialize<ExpandoObject>(orderInfo);
28+
dynamic input3 = JsonSerializer.Deserialize<ExpandoObject>(telemetryInfo);
2929

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

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

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

demo/DemoApp/NestedInputDemo.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Newtonsoft.Json;
54
using RulesEngine.Extensions;
65
using RulesEngine.Models;
76
using System;
@@ -10,6 +9,8 @@
109

1110
namespace DemoApp
1211
{
12+
using System.Text.Json;
13+
1314
internal class ListItem
1415
{
1516
public int Id { get; set; }
@@ -49,7 +50,7 @@ public void Run()
4950
}
5051

5152
var fileData = File.ReadAllText(files[0]);
52-
var Workflows = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
53+
var Workflows = JsonSerializer.Deserialize<List<Workflow>>(fileData);
5354

5455
var bre = new RulesEngine.RulesEngine(Workflows.ToArray(), null);
5556
foreach (var workflow in Workflows)

src/RulesEngine/Actions/ActionContext.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Newtonsoft.Json;
54
using RulesEngine.Models;
65
using System;
76
using System.Collections.Generic;
87

98
namespace RulesEngine.Actions
109
{
10+
using System.Text.Json;
11+
1112
public class ActionContext
1213
{
1314
private readonly IDictionary<string, string> _context;
@@ -27,7 +28,7 @@ public ActionContext(IDictionary<string, object> context, RuleResultTree parentR
2728
value = kv.Value.ToString();
2829
break;
2930
default:
30-
value = JsonConvert.SerializeObject(kv.Value);
31+
value = JsonSerializer.Serialize(kv.Value);
3132
break;
3233

3334
}
@@ -70,7 +71,7 @@ public T GetContext<T>(string name)
7071
{
7172
return (T)Convert.ChangeType(_context[name], typeof(T));
7273
}
73-
return JsonConvert.DeserializeObject<T>(_context[name]);
74+
return JsonSerializer.Deserialize<T>(_context[name]);
7475
}
7576
catch (KeyNotFoundException)
7677
{

src/RulesEngine/Models/Rule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Newtonsoft.Json;
5-
using Newtonsoft.Json.Converters;
64
using System.Collections.Generic;
75
using System.Diagnostics.CodeAnalysis;
86

97
namespace RulesEngine.Models
108
{
9+
using System.Text.Json.Serialization;
10+
1111
/// <summary>
1212
/// Rule class
1313
/// </summary>
@@ -33,7 +33,7 @@ public class Rule
3333
/// </summary>
3434
public bool Enabled { get; set; } = true;
3535

36-
[JsonConverter(typeof(StringEnumConverter))]
36+
[JsonConverter (typeof(JsonStringEnumConverter))]
3737
public RuleExpressionType RuleExpressionType { get; set; } = RuleExpressionType.LambdaExpression;
3838
public IEnumerable<string> WorkflowsToInject { get; set; }
3939
public IEnumerable<Rule> Rules { get; set; }

src/RulesEngine/Models/Workflow.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Newtonsoft.Json.Converters;
5-
using Newtonsoft.Json;
64
using System;
75
using System.Collections.Generic;
86
using System.Diagnostics.CodeAnalysis;

src/RulesEngine/RulesEngine.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the MIT License.
33

44
using FluentValidation;
5-
using Newtonsoft.Json;
6-
using Newtonsoft.Json.Linq;
75
using RulesEngine.Actions;
86
using RulesEngine.Exceptions;
97
using RulesEngine.ExpressionBuilders;
@@ -20,10 +18,13 @@
2018

2119
namespace RulesEngine
2220
{
21+
using System.Text.Json;
22+
using System.Text.Json.Nodes;
23+
2324
/// <summary>
2425
///
2526
/// </summary>
26-
/// <seealso cref="RulesEngine.Interfaces.IRulesEngine" />
27+
/// <seealso cref="IRulesEngine" />
2728
public class RulesEngine : IRulesEngine
2829
{
2930
#region Variables
@@ -38,7 +39,7 @@ public class RulesEngine : IRulesEngine
3839
#region Constructor
3940
public RulesEngine(string[] jsonConfig, ReSettings reSettings = null) : this(reSettings)
4041
{
41-
var workflow = jsonConfig.Select(item => JsonConvert.DeserializeObject<Workflow>(item)).ToArray();
42+
var workflow = jsonConfig.Select(item => JsonSerializer.Deserialize<Workflow>(item)).ToArray();
4243
AddWorkflow(workflow);
4344
}
4445

@@ -403,7 +404,7 @@ private IEnumerable<RuleResultTree> FormatErrorMessages(IEnumerable<RuleResultTr
403404
{
404405
var arrParams = inputs?.Select(c => new { Name = c.Key, c.Value });
405406
var model = arrParams?.Where(a => string.Equals(a.Name, property))?.FirstOrDefault();
406-
var value = model?.Value != null ? JsonConvert.SerializeObject(model?.Value) : null;
407+
var value = model?.Value != null ? JsonSerializer.Serialize(model?.Value) : null;
407408
errorMessage = errorMessage?.Replace($"$({property})", value ?? $"$({property})");
408409
}
409410
}
@@ -430,10 +431,10 @@ private static string UpdateErrorMessage(string errorMessage, IDictionary<string
430431
var model = arrParams?.Where(a => string.Equals(a.Name, typeName))?.FirstOrDefault();
431432
if (model != null)
432433
{
433-
var modelJson = JsonConvert.SerializeObject(model?.Value);
434-
var jObj = JObject.Parse(modelJson);
435-
JToken jToken = null;
436-
var val = jObj?.TryGetValue(propertyName, StringComparison.OrdinalIgnoreCase, out jToken);
434+
var modelJson = JsonSerializer.Serialize(model?.Value);
435+
var jObj = JsonObject.Parse(modelJson).AsObject();
436+
JsonNode jToken = null;
437+
var val = jObj?.TryGetPropertyValue(propertyName, out jToken);
437438
errorMessage = errorMessage.Replace($"$({property})", jToken != null ? jToken?.ToString() : $"({property})");
438439
}
439440

src/RulesEngine/RulesEngine.csproj

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,27 @@
3333
</ItemGroup>
3434

3535
<ItemGroup>
36-
<PackageReference Include="FastExpressionCompiler" Version="4.0.1" />
36+
<PackageReference Include="FastExpressionCompiler" Version="4.1.0" />
3737
<PackageReference Include="FluentValidation" Version="11.9.0" />
38-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
38+
3939
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.4.3" />
40+
4041
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
4142
</ItemGroup>
4243

44+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
45+
<PackageReference Include="System.Text.Json" Version="6.0.9" />
46+
</ItemGroup>
47+
48+
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
49+
<PackageReference Include="System.Text.Json" Version="8.0.0" />
50+
</ItemGroup>
51+
4352
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
44-
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
53+
54+
<PackageReference Include="System.Text.Json" Version="8.0.0" />
55+
<PackageReference Include="Microsoft.CSharp" Version="4.7.0"/>
56+
4557
</ItemGroup>
4658

4759
</Project>

0 commit comments

Comments
 (0)