Skip to content

Commit 493e3ca

Browse files
author
fadhly.permata
committed
Refactor JTokenComparer and update documentation
Moves JTokenComparer to its own file in the Utilities namespace for better code organization. Updates the README to improve documentation clarity and user examples, and increments the package version to 1.0.10. The changes include: - Relocating JTokenComparer class to Core/Utilities with proper namespace - Adding suppression for SonarLint rule S4055 for the Compare method - Enhancing README with more detailed usage examples and better formatting - Updating roadmap and feature status tables for better clarity - Incrementing package version in the project file
1 parent 05f6500 commit 493e3ca

File tree

5 files changed

+220
-133
lines changed

5 files changed

+220
-133
lines changed

Core/JsonQueryEngine.cs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using JQL.Net.Core.Utilities;
12
using JQL.Net.Exceptions;
23
using Newtonsoft.Json.Linq;
34

@@ -691,59 +692,3 @@ private static bool IsAggregateQuery(JsonQueryRequest request)
691692
);
692693
}
693694
}
694-
695-
internal class JTokenComparer : IComparer<JToken?>
696-
{
697-
public int Compare(JToken? x, JToken? y)
698-
{
699-
if (ReferenceEquals(objA: x, objB: y))
700-
return 0;
701-
if (x == null)
702-
return -1;
703-
if (y == null)
704-
return 1;
705-
706-
if (x is JValue vx && y is JValue vy)
707-
{
708-
var valX = vx.Value;
709-
var valY = vy.Value;
710-
711-
if (valX == null && valY == null)
712-
return 0;
713-
if (valX == null)
714-
return -1;
715-
if (valY == null)
716-
return 1;
717-
718-
if (IsNumeric(obj: valX) && IsNumeric(obj: valY))
719-
return Convert
720-
.ToDouble(value: valX)
721-
.CompareTo(value: Convert.ToDouble(value: valY));
722-
723-
return string.Compare(
724-
strA: valX.ToString(),
725-
strB: valY.ToString(),
726-
comparisonType: StringComparison.OrdinalIgnoreCase
727-
);
728-
}
729-
return string.Compare(
730-
strA: x.ToString(),
731-
strB: y.ToString(),
732-
comparisonType: StringComparison.OrdinalIgnoreCase
733-
);
734-
}
735-
736-
private static bool IsNumeric(object obj) =>
737-
obj
738-
is sbyte
739-
or byte
740-
or short
741-
or ushort
742-
or int
743-
or uint
744-
or long
745-
or ulong
746-
or float
747-
or double
748-
or decimal;
749-
}

Core/Utilities/JTokenComparer.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace JQL.Net.Core.Utilities;
4+
5+
internal class JTokenComparer : IComparer<JToken?>
6+
{
7+
public int Compare(JToken? x, JToken? y)
8+
{
9+
if (ReferenceEquals(objA: x, objB: y))
10+
return 0;
11+
if (x == null)
12+
return -1;
13+
if (y == null)
14+
return 1;
15+
16+
if (x is not JValue vx || y is not JValue vy)
17+
return string.Compare(
18+
strA: x.ToString(),
19+
strB: y.ToString(),
20+
comparisonType: StringComparison.OrdinalIgnoreCase
21+
);
22+
23+
var valX = vx.Value;
24+
var valY = vy.Value;
25+
26+
switch (valX)
27+
{
28+
case null when valY == null:
29+
return 0;
30+
case null:
31+
return -1;
32+
}
33+
34+
if (valY == null)
35+
return 1;
36+
37+
if (IsNumeric(obj: valX) && IsNumeric(obj: valY))
38+
return Convert.ToDouble(value: valX).CompareTo(value: Convert.ToDouble(value: valY));
39+
40+
return string.Compare(
41+
strA: valX.ToString(),
42+
strB: valY.ToString(),
43+
comparisonType: StringComparison.OrdinalIgnoreCase
44+
);
45+
}
46+
47+
private static bool IsNumeric(object obj) =>
48+
obj
49+
is sbyte
50+
or byte
51+
or short
52+
or ushort
53+
or int
54+
or uint
55+
or long
56+
or ulong
57+
or float
58+
or double
59+
or decimal;
60+
}

GlobalSuppressions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@
77
Scope = "member",
88
Target = "~M:JQL.Net.JsonQueryRequest.Parse(System.String)"
99
)]
10+
11+
[assembly: SuppressMessage(
12+
category: "SonarLint",
13+
checkId: "S4055",
14+
Justification = "Static method required for IComparer implementation",
15+
Scope = "member",
16+
Target = "~M:JQL.Net.Core.Utilities.JTokenComparer.Compare(Newtonsoft.Json.Linq.JToken,Newtonsoft.Json.Linq.JToken)"
17+
)]

JQL.Net.csproj

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>net8.0</TargetFramework>
44
<PackageId>JQL.Net</PackageId>
5-
<Version>1.0.6</Version>
5+
<Version>1.0.10</Version>
66
<Authors>Fadhly Permata</Authors>
77
<Company>Fadhly Permata</Company>
88
<Description>JQL.Net: A lightweight SQL-inspired engine to query, join, and aggregate JSON in .NET. No database? No problem!</Description>
@@ -13,20 +13,18 @@
1313
<Nullable>enable</Nullable>
1414
<NoWarn>$(NoWarn);S1192</NoWarn>
1515
</PropertyGroup>
16-
17-
<PropertyGroup>
16+
<!-- <PropertyGroup>
1817
<PackageId>JQL.Net</PackageId>
19-
<Version>1.0.0</Version> <Authors>Fadhly Permata</Authors>
18+
<Version>1.0.0</Version>
19+
<Authors>Fadhly Permata</Authors>
2020
<Description>Lightweight SQL-inspired engine to query JSON in .NET</Description>
2121
<PackageReadmeFile>README.md</PackageReadmeFile>
2222
<RepositoryUrl>https://github.com/fadhly-permata/JQL.Net</RepositoryUrl>
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>
24-
</PropertyGroup>
25-
24+
</PropertyGroup> -->
2625
<ItemGroup>
2726
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
2827
</ItemGroup>
29-
3028
<ItemGroup>
3129
<None Include="README.md" Pack="true" PackagePath="\" />
3230
<None Include="LICENSE" Pack="true" PackagePath="\" />

0 commit comments

Comments
 (0)