Skip to content

A Swiss Army knife for developers.

Switch from Newtonsoft.Json to System.Text.Json

Etienne Baudoux2c4730c4bc51
diff --git a/THIRD-PARTY-NOTICES.md b/THIRD-PARTY-NOTICES.md
index 396f2da18..7e02f861c 100644
--- a/THIRD-PARTY-NOTICES.md
+++ b/THIRD-PARTY-NOTICES.md
@@ -3,7 +3,6 @@
This project incorporates components from the projects listed below. The original copyright notices and the licenses under which Etienne Baudoux received such components are set forth below. Etienne Baudoux reserves all rights not expressly granted herein, whether by implication, estoppel or otherwise.
1. MudBlazor's Popover
-1. [Newtonsoft.Json](#newtonsoftjson) (<https://github.com/JamesNK/Newtonsoft.Json>)
2. [GitInfo](#gitinfo) (<https://github.com/devlooped/GitInfo>)
3. [Windows Community Toolkit](#windows-community-toolkit) (<https://github.com/CommunityToolkit/WindowsCommunityToolkit>)
4. [YamlDotNet](#yamldotnet) (<https://github.com/aaubry/YamlDotNet>)
@@ -20,30 +19,6 @@ This project incorporates components from the projects listed below. The origina
15. [Cronos](#cronos) (<https://github.com/HangfireIO/Cronos>)
16. [BouncyCastle](#bouncy-castle) (<https://www.bouncycastle.org/>)
-Newtonsoft.Json
-=========================================
-
-The MIT License (MIT)
-
-Copyright (c) 2007 James Newton-King
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of
-this software and associated documentation files (the "Software"), to deal in
-the Software without restriction, including without limitation the rights to
-use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
-the Software, and to permit persons to whom the Software is furnished to do so,
-subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
-FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
-IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
GitInfo
=========================================
diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props
index fb21448bb..2a1d03a2e 100644
--- a/src/Directory.Packages.props
+++ b/src/Directory.Packages.props
@@ -24,7 +24,7 @@
<PackageVersion Include="Microsoft.TypeScript.MSBuild" Version="5.1.5" />
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.3.18-beta"/>
<PackageVersion Include="Moq" Version="4.18.4" />
- <PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
+ <PackageVersion Include="System.Text.Json" Version="$(DotNetVersion)" />
<PackageVersion Include="Nuke.Common" Version="7.0.2" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.ComponentModel.Composition" Version="$(DotNetVersion)" />
diff --git a/src/app/dev/DevToys.Business/DevToys.Business.csproj b/src/app/dev/DevToys.Business/DevToys.Business.csproj
index 6b292de52..00f868b75 100644
--- a/src/app/dev/DevToys.Business/DevToys.Business.csproj
+++ b/src/app/dev/DevToys.Business/DevToys.Business.csproj
@@ -13,7 +13,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" />
<PackageReference Include="FuzzySharp" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
- <PackageReference Include="Newtonsoft.Json" />
+ <PackageReference Include="System.Text.Json" />
<PackageReference Include="System.ComponentModel.Composition" />
<PackageReference Include="Uno.Core.Extensions.Logging" />
<PackageReference Include="Uno.Core.Extensions.Logging.Singleton" />
diff --git a/src/app/dev/DevToys.Core/DevToys.Core.csproj b/src/app/dev/DevToys.Core/DevToys.Core.csproj
index 544968672..4693a0a44 100644
--- a/src/app/dev/DevToys.Core/DevToys.Core.csproj
+++ b/src/app/dev/DevToys.Core/DevToys.Core.csproj
@@ -12,7 +12,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" />
<PackageReference Include="FuzzySharp" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
- <PackageReference Include="Newtonsoft.Json" />
+ <PackageReference Include="System.Text.Json" />
<PackageReference Include="System.ComponentModel.Composition" />
<PackageReference Include="Uno.Core.Extensions.Logging" />
<PackageReference Include="Uno.Core.Extensions.Logging.Singleton" />
diff --git a/src/app/dev/DevToys.Core/Settings/SettingsProvider.cs b/src/app/dev/DevToys.Core/Settings/SettingsProvider.cs
index 0960750f3..a54559e78 100644
--- a/src/app/dev/DevToys.Core/Settings/SettingsProvider.cs
+++ b/src/app/dev/DevToys.Core/Settings/SettingsProvider.cs
@@ -1,7 +1,7 @@
using System.Collections;
using System.Globalization;
+using System.Text.Json;
using Microsoft.Extensions.Logging;
-using Newtonsoft.Json;
using Uno.Extensions;
namespace DevToys.Core.Settings;
@@ -31,7 +31,12 @@ internal sealed partial class SettingsProvider : ISettingsProvider
}
else if (typeof(IList).IsAssignableFrom(typeof(T)))
{
- return JsonConvert.DeserializeObject<T>(settingValue?.ToString() ?? string.Empty)!;
+ return JsonSerializer.Deserialize<T>(
+ settingValue?.ToString() ?? string.Empty,
+ new JsonSerializerOptions
+ {
+ PropertyNameCaseInsensitive = true
+ })!;
}
object? result = Convert.ChangeType(settingValue, typeof(T), CultureInfo.InvariantCulture);
@@ -53,7 +58,14 @@ internal sealed partial class SettingsProvider : ISettingsProvider
}
else if (value is IList list)
{
- valueToSave = JsonConvert.SerializeObject(list, Formatting.None);
+ valueToSave
+ = JsonSerializer.Serialize(
+ list,
+ new JsonSerializerOptions
+ {
+ PropertyNameCaseInsensitive = true,
+ WriteIndented = false,
+ });
}
_settingsStorage.WriteSetting(settingDefinition.Name, valueToSave);
diff --git a/src/app/dev/DevToys.Localization/DevToys.Localization.csproj b/src/app/dev/DevToys.Localization/DevToys.Localization.csproj
index 5886ddb95..42784f72b 100644
--- a/src/app/dev/DevToys.Localization/DevToys.Localization.csproj
+++ b/src/app/dev/DevToys.Localization/DevToys.Localization.csproj
@@ -12,7 +12,7 @@
<PackageReference Include="CommunityToolkit.Common" />
<PackageReference Include="CommunityToolkit.Diagnostics" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
- <PackageReference Include="Newtonsoft.Json" />
+ <PackageReference Include="System.Text.Json" />
<PackageReference Include="System.ComponentModel.Composition" />
</ItemGroup>
<ItemGroup>
diff --git a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj b/src/app/dev/DevToys.Tools/DevToys.Tools.csproj
index 89072ef2f..68c64533f 100644
--- a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj
+++ b/src/app/dev/DevToys.Tools/DevToys.Tools.csproj
@@ -44,7 +44,7 @@
<ItemGroup>
<PackageReference Include="Uno.Core.Extensions.Logging.Singleton" />
- <PackageReference Include="Newtonsoft.Json" />
+ <PackageReference Include="System.Text.Json" />
</ItemGroup>
<Target Name="CopyPluginAfterBuild" AfterTargets="Build">
diff --git a/src/app/dev/DevToys.Tools/SmartDetection/JsonDataTypeDetector.cs b/src/app/dev/DevToys.Tools/SmartDetection/JsonDataTypeDetector.cs
index 6f0e12f15..07600066d 100644
--- a/src/app/dev/DevToys.Tools/SmartDetection/JsonDataTypeDetector.cs
+++ b/src/app/dev/DevToys.Tools/SmartDetection/JsonDataTypeDetector.cs
@@ -1,6 +1,6 @@
-using Microsoft.Extensions.Logging;
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using Microsoft.Extensions.Logging;
using Uno.Extensions;
namespace DevToys.Tools.SmartDetection;
@@ -25,13 +25,13 @@ internal sealed partial class JsonDataTypeDetector : IDataTypeDetector
{
try
{
- var jtoken = JToken.Parse(dataString);
- if (jtoken is not null)
+ var jsonNode = JsonNode.Parse(dataString);
+ if (jsonNode is not null)
{
- return ValueTask.FromResult(new DataDetectionResult(Success: true, Data: new Tuple<JToken, string>(jtoken, dataString)));
+ return ValueTask.FromResult(new DataDetectionResult(Success: true, Data: new Tuple<JsonNode, string>(jsonNode, dataString)));
}
}
- catch (JsonReaderException)
+ catch (JsonException)
{
// Exception in parsing json. It likely mean the text isn't a JSON.
}
diff --git a/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs b/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs
index 07a527f68..7e447e6ff 100644
--- a/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs
@@ -1,5 +1,5 @@
-using Microsoft.Extensions.Logging;
-using Newtonsoft.Json.Linq;
+using System.Text.Json.Nodes;
+using Microsoft.Extensions.Logging;
namespace DevToys.Tools.Tools.Sample;
@@ -113,7 +113,7 @@ internal sealed class SampleGuiTool : IGuiTool
public void OnDataReceived(string dataTypeName, object? parsedData)
{
- if (dataTypeName == PredefinedCommonDataTypeNames.Json && parsedData is Tuple<JToken, string> strongTypedParsedData)
+ if (dataTypeName == PredefinedCommonDataTypeNames.Json && parsedData is Tuple<JsonNode, string> strongTypedParsedData)
{
//_editor.Text(strongTypedParsedData.Item2);
//_editor.Highlight(new TextSpan(3, 6));
diff --git a/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
index 13639a991..2bc51094e 100644
--- a/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
@@ -1,5 +1,5 @@
#if DEBUG
-using Newtonsoft.Json.Linq;
+using System.Text.Json.Nodes;
namespace DevToys.Tools.Tools.Test;
@@ -199,7 +199,7 @@ internal sealed class TestGuiTool : IGuiTool
// Smart detection handler.
public void OnDataReceived(string dataTypeName, object? parsedData)
{
- if (dataTypeName == PredefinedCommonDataTypeNames.Json && parsedData is Tuple<JToken, string> strongTypedParsedData)
+ if (dataTypeName == PredefinedCommonDataTypeNames.Json && parsedData is Tuple<JsonNode, string> strongTypedParsedData)
{
// Pass the JSON from Smart Detection into the Input text editor.
_inputJsonEditor.Text(strongTypedParsedData.Item2);
diff --git a/src/app/tests/DevToys.UnitTests/Mocks/Tools/MockDataTypeDetector.cs b/src/app/tests/DevToys.UnitTests/Mocks/Tools/MockDataTypeDetector.cs
index 0ba3e0e6b..8df04ac03 100644
--- a/src/app/tests/DevToys.UnitTests/Mocks/Tools/MockDataTypeDetector.cs
+++ b/src/app/tests/DevToys.UnitTests/Mocks/Tools/MockDataTypeDetector.cs
@@ -1,7 +1,7 @@
using System.ComponentModel.Composition;
+using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
-using Newtonsoft.Json.Linq;
namespace DevToys.UnitTests.Mocks.Tools;
@@ -17,10 +17,10 @@ internal class MockJsonDataTypeDetector : IDataTypeDetector
{
try
{
- var jtoken = JToken.Parse(dataString);
+ var jtoken = JsonNode.Parse(dataString);
if (jtoken is not null)
{
- return ValueTask.FromResult(new DataDetectionResult(Success: true, Data: new Tuple<JToken, string>(jtoken, dataString)));
+ return ValueTask.FromResult(new DataDetectionResult(Success: true, Data: new Tuple<JsonNode, string>(jtoken, dataString)));
}
}
catch