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

@@ -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();