Standardize .NET JSON on System.Text.Json (#12805)

* Implement System.Text.Json for Community.PowerToys.Run.Plugin.VSCodeWorkspaces (#11697)

* Implement System.Text.Json for Community.PowerToys.Run.Plugin.VSCodeWorkspaces

* Cleanup property names

* Implement System.Text.Json for Microsoft.PowerToys.Settings.UI (#11702)

* Implement System.Text.Json for Powerlauncher (#11699)

* Implement System.Text.Json for Wox.Infrastructure

* Implement System.Text.Json for Powerlauncher

* Implement System.Text.Json for Microsoft.Plugin.Folder

* Implement System.Text.Json for Wox.Plugin

* Remove Newtonsoft.Json from launcherInstallComponent

* Update properties with private setter
Format JSON output

* Serialize Get with private set property

* Implement System.Text.Json for ImageResizerUI (#11847)

* Implement System.Text.Json for ImageRezierUI

* Change Newtonsoft.Json.dll to System.Text.Json in ImageResizer

* Add  writefile to spelling whitelist

* Fix installer

* Fix bad merge

Co-authored-by: mykhailopylyp <17161067+mykhailopylyp@users.noreply.github.com>
This commit is contained in:
Roy
2021-08-20 15:36:29 +02:00
committed by GitHub
parent 44ef29ca39
commit ea25bd91b0
29 changed files with 241 additions and 113 deletions

View File

@@ -5,9 +5,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.SshConfigParser;
using Community.PowerToys.Run.Plugin.VSCodeWorkspaces.VSCodeHelper;
using Newtonsoft.Json;
using Wox.Plugin.Logger;
namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.RemoteMachinesHelper
@@ -35,13 +35,14 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.RemoteMachinesHelper
try
{
dynamic vscodeSettingsFile = JsonConvert.DeserializeObject<dynamic>(fileContent);
if (vscodeSettingsFile.ContainsKey("remote.SSH.configFile"))
JsonElement vscodeSettingsFile = JsonSerializer.Deserialize<JsonElement>(fileContent);
if (vscodeSettingsFile.TryGetProperty("remote.SSH.configFile", out var pathElement))
{
var path = vscodeSettingsFile["remote.SSH.configFile"];
if (File.Exists(path.Value))
var path = pathElement.GetString();
if (File.Exists(path))
{
foreach (SshHost h in SshConfig.ParseFile(path.Value))
foreach (SshHost h in SshConfig.ParseFile(path))
{
var machine = new VSCodeRemoteMachine();
machine.Host = h.Host;

View File

@@ -3,13 +3,16 @@
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
{
public class OpenedPathsList
{
[JsonPropertyName("workspaces3")]
public List<dynamic> Workspaces3 { get; set; }
[JsonPropertyName("entries")]
public List<VSCodeWorkspaceEntry> Entries { get; set; }
}
}

View File

@@ -2,10 +2,13 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json.Serialization;
namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
{
public class VSCodeStorageFile
{
[JsonPropertyName("openedPathsList")]
public OpenedPathsList OpenedPathsList { get; set; }
}
}

View File

@@ -2,12 +2,16 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json.Serialization;
namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
{
public class VSCodeWorkspaceEntry
{
[JsonPropertyName("folderUri")]
public string FolderUri { get; set; }
[JsonPropertyName("label")]
public string Label { get; set; }
}
}

View File

@@ -67,12 +67,7 @@ namespace Community.PowerToys.Run.Plugin.VSCodeWorkspaces.WorkspacesHelper
try
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
VSCodeStorageFile vscodeStorageFile = JsonSerializer.Deserialize<VSCodeStorageFile>(fileContent, options);
VSCodeStorageFile vscodeStorageFile = JsonSerializer.Deserialize<VSCodeStorageFile>(fileContent);
if (vscodeStorageFile != null)
{

View File

@@ -4,16 +4,15 @@
using System;
using System.Linq;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace Microsoft.Plugin.Folder
{
[JsonObject(MemberSerialization.OptIn)]
public class FolderLink
{
[JsonProperty]
public string Path { get; set; }
[JsonIgnore]
public string Nickname =>
Path.Split(new[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.None)
.Last()

View File

@@ -3,19 +3,15 @@
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Microsoft.Plugin.Folder
{
public class FolderSettings
{
[JsonProperty]
public List<FolderLink> FolderLinks { get; } = new List<FolderLink>();
[JsonProperty]
public int MaxFolderResults { get; set; } = 50;
[JsonProperty]
public int MaxFileResults { get; set; } = 50;
}
}

View File

@@ -77,7 +77,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.IO.Abstractions" Version="12.2.5" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
</ItemGroup>

View File

@@ -7,7 +7,7 @@ using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using System.Text.Json;
using Wox.Plugin;
using Wox.Plugin.Logger;
@@ -79,7 +79,7 @@ namespace PowerLauncher.Plugin
PluginMetadata metadata;
try
{
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
}
catch (Exception e)

View File

@@ -6,9 +6,9 @@ using System;
using System.IO;
using System.IO.Abstractions;
using System.Reflection;
using System.Text.Json;
using System.Windows;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using Wox.Plugin;
using Wox.Plugin.Logger;
@@ -117,7 +117,7 @@ namespace PowerLauncher.Plugin
try
{
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
}
catch (Exception e)

View File

@@ -100,7 +100,6 @@
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="6.1.1" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.19" />
<PackageReference Include="ModernWpfUI" Version="0.9.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NuGet.CommandLine" Version="5.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -5,12 +5,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace PowerLauncher.Storage
{
public class QueryHistory
{
public List<HistoryItem> Items { get; } = new List<HistoryItem>();
[JsonInclude]
public List<HistoryItem> Items { get; private set; } = new List<HistoryItem>();
private readonly int _maxHistory = 300;

View File

@@ -4,15 +4,15 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Wox.Plugin;
namespace PowerLauncher.Storage
{
public class UserSelectedRecord
{
[JsonProperty]
private readonly Dictionary<string, int> records = new Dictionary<string, int>();
[JsonInclude]
public Dictionary<string, int> Records { get; private set; } = new Dictionary<string, int>();
public void Add(Result result)
{
@@ -22,13 +22,13 @@ namespace PowerLauncher.Storage
}
var key = result.ToString();
if (records.TryGetValue(key, out int value))
if (Records.TryGetValue(key, out int value))
{
records[key] = value + 1;
Records[key] = value + 1;
}
else
{
records.Add(key, 1);
Records.Add(key, 1);
}
}
@@ -39,7 +39,7 @@ namespace PowerLauncher.Storage
throw new ArgumentNullException(nameof(result));
}
if (result != null && records.TryGetValue(result.ToString(), out int value))
if (result != null && Records.TryGetValue(result.ToString(), out int value))
{
return value;
}

View File

@@ -7,8 +7,8 @@ using System.ComponentModel;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json;
using System.Text.Json.Serialization;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure
@@ -81,7 +81,14 @@ namespace Wox.Infrastructure
public static string Formatted<T>(this T t)
{
var formatted = JsonConvert.SerializeObject(t, Formatting.Indented, new StringEnumConverter());
var formatted = JsonSerializer.Serialize(t, new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new JsonStringEnumConverter(),
},
});
return formatted;
}

View File

@@ -6,7 +6,7 @@ using System;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using Newtonsoft.Json;
using System.Text.Json;
using Wox.Plugin.Logger;
namespace Wox.Infrastructure.Storage
@@ -20,7 +20,16 @@ namespace Wox.Infrastructure.Storage
private static readonly IPath Path = FileSystem.Path;
private static readonly IFile File = FileSystem.File;
private readonly JsonSerializerSettings _serializerSettings;
// use property initialization instead of DefaultValueAttribute
// easier and flexible for default value of object
private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
{
IgnoreNullValues = true,
IncludeFields = true,
PropertyNameCaseInsensitive = true,
WriteIndented = true,
};
private T _data;
// need a new directory name
@@ -35,17 +44,6 @@ namespace Wox.Infrastructure.Storage
private const int _jsonStorage = 1;
private StoragePowerToysVersionInfo _storageHelper;
internal JsonStorage()
{
// use property initialization instead of DefaultValueAttribute
// easier and flexible for default value of object
_serializerSettings = new JsonSerializerSettings
{
ObjectCreationHandling = ObjectCreationHandling.Replace,
NullValueHandling = NullValueHandling.Ignore,
};
}
public T Load()
{
_storageHelper = new StoragePowerToysVersionInfo(FilePath, _jsonStorage);
@@ -84,7 +82,7 @@ namespace Wox.Infrastructure.Storage
{
try
{
_data = JsonConvert.DeserializeObject<T>(serialized, _serializerSettings);
_data = JsonSerializer.Deserialize<T>(serialized, _serializerOptions);
}
catch (JsonException e)
{
@@ -105,7 +103,7 @@ namespace Wox.Infrastructure.Storage
BackupOriginFile();
}
_data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
_data = JsonSerializer.Deserialize<T>("{}", _serializerOptions);
Save();
}
@@ -126,7 +124,7 @@ namespace Wox.Infrastructure.Storage
{
try
{
string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);
string serialized = JsonSerializer.Serialize(_data, _serializerOptions);
File.WriteAllText(FilePath, serialized);
_storageHelper.Close();

View File

@@ -1,13 +1,12 @@
// Copyright (c) Microsoft Corporation
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Text.Json.Serialization;
using ManagedCommon;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Wox.Plugin;
namespace Wox.Infrastructure.UserSettings
@@ -177,7 +176,7 @@ namespace Wox.Infrastructure.UserSettings
public HttpProxy Proxy { get; set; } = new HttpProxy();
[JsonConverter(typeof(StringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter))]
public LastQueryMode LastQueryMode { get; set; } = LastQueryMode.Selected;
}

View File

@@ -6,11 +6,10 @@ using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace Wox.Plugin
{
[JsonObject(MemberSerialization.OptOut)]
public class PluginMetadata : BaseModel
{
private static readonly IFileSystem FileSystem = new FileSystem();
@@ -36,6 +35,7 @@ namespace Wox.Plugin
public bool Disabled { get; set; }
[JsonInclude]
public string ExecuteFilePath { get; private set; }
public string ExecuteFileName { get; set; }

View File

@@ -69,7 +69,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Mono.Cecil" Version="0.11.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.5" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
</ItemGroup>