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

@@ -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;
}