Replace All use of Json.Net with System.Text.Json

This commit is contained in:
弘韬 张 2020-12-30 13:40:42 +08:00
parent 58d281db7a
commit d28b14ff2d
17 changed files with 67 additions and 77 deletions

View file

@ -3,10 +3,10 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Flow.Launcher.Infrastructure.Exception;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin;
@ -65,7 +65,7 @@ namespace Flow.Launcher.Core.Plugin
{
List<Result> results = new List<Result>();
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
JsonRPCQueryResponseModel queryResponseModel = JsonSerializer.Deserialize<JsonRPCQueryResponseModel>(output);
if (queryResponseModel.Result == null) return null;
foreach (JsonRPCResult result in queryResponseModel.Result)
@ -84,7 +84,7 @@ namespace Flow.Launcher.Core.Plugin
else
{
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
JsonRPCRequestModel jsonRpcRequestModel = JsonSerializer.Deserialize<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Flow.Launcher."))

View file

@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin;
using System.Text.Json;
namespace Flow.Launcher.Core.Plugin
{
@ -61,7 +61,7 @@ namespace Flow.Launcher.Core.Plugin
PluginMetadata metadata;
try
{
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
// for plugins which doesn't has ActionKeywords key
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> { metadata.ActionKeyword };

View file

@ -13,7 +13,6 @@ using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System.IO;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using System.Text.Json.Serialization;

View file

@ -1,12 +1,19 @@
using System;
using Newtonsoft.Json.Converters;
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Flow.Launcher.Infrastructure
{
public static class Helper
{
static Helper()
{
jsonFormattedSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}
/// <summary>
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
/// </summary>
@ -65,13 +72,18 @@ namespace Flow.Launcher.Infrastructure
}
}
private static readonly JsonSerializerOptions jsonFormattedSerializerOptions = new JsonSerializerOptions
{
WriteIndented = true
};
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
});
return formatted;
}
}

View file

@ -128,7 +128,7 @@ namespace Flow.Launcher.Infrastructure.Logger
public static void Exception(string message, System.Exception e)
{
#if DEBUG
throw e;
throw e;
#else
if (FormatValid(message))
{

View file

@ -1,7 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;
using System.Text.Json;
using Flow.Launcher.Infrastructure.Logger;
namespace Flow.Launcher.Infrastructure.Storage
@ -11,7 +11,7 @@ namespace Flow.Launcher.Infrastructure.Storage
/// </summary>
public class JsonStrorage<T>
{
private readonly JsonSerializerSettings _serializerSettings;
private readonly JsonSerializerOptions _serializerSettings;
private T _data;
// need a new directory name
public const string DirectoryName = "Settings";
@ -24,10 +24,9 @@ namespace Flow.Launcher.Infrastructure.Storage
{
// use property initialization instead of DefaultValueAttribute
// easier and flexible for default value of object
_serializerSettings = new JsonSerializerSettings
_serializerSettings = new JsonSerializerOptions
{
ObjectCreationHandling = ObjectCreationHandling.Replace,
NullValueHandling = NullValueHandling.Ignore
IgnoreNullValues = false
};
}
@ -56,7 +55,7 @@ namespace Flow.Launcher.Infrastructure.Storage
{
try
{
_data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
_data = JsonSerializer.Deserialize<T>(searlized, _serializerSettings);
}
catch (JsonException e)
{
@ -77,7 +76,7 @@ namespace Flow.Launcher.Infrastructure.Storage
BackupOriginFile();
}
_data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
_data = JsonSerializer.Deserialize<T>("{}", _serializerSettings);
Save();
}
@ -94,7 +93,8 @@ namespace Flow.Launcher.Infrastructure.Storage
public void Save()
{
string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);
string serialized = JsonSerializer.Serialize(_data, new JsonSerializerOptions() { WriteIndented = true });
File.WriteAllText(FilePath, serialized);
}
}

View file

@ -1,8 +1,7 @@
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json.Serialization;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Infrastructure.UserSettings
@ -16,7 +15,8 @@ namespace Flow.Launcher.Infrastructure.UserSettings
public bool ShowOpenResultHotkey { get; set; } = true;
public string Language
{
get => language; set {
get => language; set
{
language = value;
OnPropertyChanged();
}
@ -73,9 +73,7 @@ namespace Flow.Launcher.Infrastructure.UserSettings
public int MaxResultsToShow { get; set; } = 5;
public int ActivateTimes { get; set; }
// Order defaults to 0 or -1, so 1 will let this property appear last
[JsonProperty(Order = 1)]
public PluginsSettings PluginSettings { get; set; } = new PluginsSettings();
public ObservableCollection<CustomPluginHotkey> CustomPluginHotkeys { get; set; } = new ObservableCollection<CustomPluginHotkey>();
public bool DontPromptUpdateMsg { get; set; }
@ -100,8 +98,12 @@ namespace Flow.Launcher.Infrastructure.UserSettings
public HttpProxy Proxy { get; set; } = new HttpProxy();
[JsonConverter(typeof(StringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter))]
public LastQueryMode LastQueryMode { get; set; } = LastQueryMode.Selected;
// Order defaults to 0 or -1, so 1 will let this property appear last
public PluginsSettings PluginSettings { get; set; } = new PluginsSettings();
}
public enum LastQueryMode

View file

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace Flow.Launcher.Plugin
{
[JsonObject(MemberSerialization.OptOut)]
public class PluginMetadata : BaseModel
{
private string _pluginDirectory;

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Storage

View file

@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Text.Json;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Storage
@ -8,7 +8,6 @@ namespace Flow.Launcher.Storage
// todo this class is not thread safe.... but used from multiple threads.
public class TopMostRecord
{
[JsonProperty]
private Dictionary<string, Record> records = new Dictionary<string, Record>();
internal bool IsTopMost(Result result)

View file

@ -1,5 +1,4 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Plugin;
@ -7,7 +6,6 @@ namespace Flow.Launcher.Storage
{
public class UserSelectedRecord
{
[JsonProperty]
private Dictionary<string, int> records = new Dictionary<string, int>();
public void Add(Result result)

View file

@ -1,15 +1,15 @@
using Newtonsoft.Json;
using System;
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Flow.Launcher.Plugin.Explorer.Search.FolderLinks
{
[JsonObject(MemberSerialization.OptIn)]
public class FolderLink
{
[JsonProperty]
public string Path { get; set; }
[JsonIgnore]
public string Nickname
{
get

View file

@ -1,28 +1,22 @@
using Flow.Launcher.Plugin.Explorer.Search;
using Flow.Launcher.Plugin.Explorer.Search.FolderLinks;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Flow.Launcher.Plugin.Explorer
{
public class Settings
{
[JsonProperty]
public int MaxResult { get; set; } = 100;
[JsonProperty]
public List<FolderLink> QuickFolderAccessLinks { get; set; } = new List<FolderLink>();
[JsonProperty]
public bool UseWindowsIndexForDirectorySearch { get; set; } = true;
[JsonProperty]
public List<FolderLink> IndexSearchExcludedSubdirectoryPaths { get; set; } = new List<FolderLink>();
[JsonProperty]
public string SearchActionKeyword { get; set; } = Query.GlobalPluginWildcardSign;
[JsonProperty]
public string FileContentSearchActionKeyword { get; set; } = Constants.DefaultContentSearchActionKeyword;
}
}

View file

@ -1,10 +1,10 @@
using System.IO;
using System.Windows.Media;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure;
using System.Reflection;
using System.Text.Json.Serialization;
namespace Flow.Launcher.Plugin.WebSearch
{

View file

@ -1,6 +1,6 @@
using System;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Flow.Launcher.Plugin.WebSearch.SuggestionSources;
namespace Flow.Launcher.Plugin.WebSearch

View file

@ -2,10 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System.Net.Http;
@ -35,25 +34,20 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
Match match = _reg.Match(result);
if (match.Success)
{
JContainer json;
JsonDocument json;
try
{
json = JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
json = JsonDocument.Parse(match.Groups[1].Value);
}
catch (JsonSerializationException e)
catch(JsonException e)
{
Log.Exception("|Baidu.Suggestions|can't parse suggestions", e);
return new List<string>();
}
if (json != null)
{
var results = json["s"] as JArray;
if (results != null)
{
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
}
}
var results = json?.RootElement.GetProperty("s");
return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List<string>();
}
return new List<string>();

View file

@ -3,11 +3,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System.Net.Http;
using System.Text.Json;
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
{
@ -27,25 +26,20 @@ namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
return new List<string>();
}
if (string.IsNullOrEmpty(result)) return new List<string>();
JContainer json;
JsonDocument json;
try
{
json = JsonConvert.DeserializeObject(result) as JContainer;
json = JsonDocument.Parse(result);
}
catch (JsonSerializationException e)
catch (JsonException e)
{
Log.Exception("|Google.Suggestions|can't parse suggestions", e);
return new List<string>();
}
if (json != null)
{
var results = json[1] as JContainer;
if (results != null)
{
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
}
}
return new List<string>();
var results = json?.RootElement.EnumerateArray().ElementAt(1);
return results?.EnumerateArray().Select(o => o.GetString()).ToList() ?? new List<string>();
}
public override string ToString()