Skip to content

Commit b348f1d

Browse files
authored
Merge pull request #253 from taooceros/UpdateJson
Replace Json.Net with System.Text.Json
2 parents 2b8c4c3 + e1f715e commit b348f1d

File tree

19 files changed

+69
-81
lines changed

19 files changed

+69
-81
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Reflection;
6+
using System.Text.Json;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using System.Windows.Forms;
9-
using Newtonsoft.Json;
1010
using Flow.Launcher.Infrastructure.Exception;
1111
using Flow.Launcher.Infrastructure.Logger;
1212
using Flow.Launcher.Plugin;
@@ -65,7 +65,7 @@ private List<Result> DeserializedResult(string output)
6565
{
6666
List<Result> results = new List<Result>();
6767

68-
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
68+
JsonRPCQueryResponseModel queryResponseModel = JsonSerializer.Deserialize<JsonRPCQueryResponseModel>(output);
6969
if (queryResponseModel.Result == null) return null;
7070

7171
foreach (JsonRPCResult result in queryResponseModel.Result)
@@ -84,7 +84,7 @@ private List<Result> DeserializedResult(string output)
8484
else
8585
{
8686
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
87-
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
87+
JsonRPCRequestModel jsonRpcRequestModel = JsonSerializer.Deserialize<JsonRPCRequestModel>(actionReponse);
8888
if (jsonRpcRequestModel != null
8989
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
9090
&& jsonRpcRequestModel.Method.StartsWith("Flow.Launcher."))

Flow.Launcher.Core/Plugin/PluginConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.IO;
5-
using Newtonsoft.Json;
65
using Flow.Launcher.Infrastructure;
76
using Flow.Launcher.Infrastructure.Logger;
87
using Flow.Launcher.Plugin;
8+
using System.Text.Json;
99

1010
namespace Flow.Launcher.Core.Plugin
1111
{
@@ -61,7 +61,7 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
6161
PluginMetadata metadata;
6262
try
6363
{
64-
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
64+
metadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(configPath));
6565
metadata.PluginDirectory = pluginDirectory;
6666
// for plugins which doesn't has ActionKeywords key
6767
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> { metadata.ActionKeyword };

Flow.Launcher.Core/Updater.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Flow.Launcher.Infrastructure;
1414
using Flow.Launcher.Infrastructure.Http;
1515
using Flow.Launcher.Infrastructure.Logger;
16-
using System.IO;
1716
using Flow.Launcher.Infrastructure.UserSettings;
1817
using Flow.Launcher.Plugin;
1918
using System.Text.Json.Serialization;

Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
</ItemGroup>
5050

5151
<ItemGroup>
52-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
5352
<PackageReference Include="NLog.Schema" Version="4.7.0-rc1" />
5453
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
5554
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />

Flow.Launcher.Infrastructure/Helper.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
using System;
22
using System.IO;
3-
using Newtonsoft.Json;
4-
using Newtonsoft.Json.Converters;
3+
using System.Runtime.CompilerServices;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
56

67
namespace Flow.Launcher.Infrastructure
78
{
89
public static class Helper
910
{
11+
static Helper()
12+
{
13+
jsonFormattedSerializerOptions.Converters.Add(new JsonStringEnumConverter());
14+
}
15+
1016
/// <summary>
1117
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
1218
/// </summary>
@@ -65,13 +71,18 @@ public static void ValidateDirectory(string path)
6571
}
6672
}
6773

74+
private static readonly JsonSerializerOptions jsonFormattedSerializerOptions = new JsonSerializerOptions
75+
{
76+
WriteIndented = true
77+
};
78+
6879
public static string Formatted<T>(this T t)
6980
{
70-
var formatted = JsonConvert.SerializeObject(
71-
t,
72-
Formatting.Indented,
73-
new StringEnumConverter()
74-
);
81+
var formatted = JsonSerializer.Serialize(t, new JsonSerializerOptions
82+
{
83+
WriteIndented = true
84+
});
85+
7586
return formatted;
7687
}
7788
}

Flow.Launcher.Infrastructure/Logger/Log.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static void LogInternal(string message, LogLevel level)
128128
public static void Exception(string message, System.Exception e)
129129
{
130130
#if DEBUG
131-
throw e;
131+
throw e;
132132
#else
133133
if (FormatValid(message))
134134
{

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Globalization;
33
using System.IO;
4-
using Newtonsoft.Json;
4+
using System.Text.Json;
55
using Flow.Launcher.Infrastructure.Logger;
66

77
namespace Flow.Launcher.Infrastructure.Storage
@@ -11,7 +11,7 @@ namespace Flow.Launcher.Infrastructure.Storage
1111
/// </summary>
1212
public class JsonStrorage<T>
1313
{
14-
private readonly JsonSerializerSettings _serializerSettings;
14+
private readonly JsonSerializerOptions _serializerSettings;
1515
private T _data;
1616
// need a new directory name
1717
public const string DirectoryName = "Settings";
@@ -24,10 +24,9 @@ internal JsonStrorage()
2424
{
2525
// use property initialization instead of DefaultValueAttribute
2626
// easier and flexible for default value of object
27-
_serializerSettings = new JsonSerializerSettings
27+
_serializerSettings = new JsonSerializerOptions
2828
{
29-
ObjectCreationHandling = ObjectCreationHandling.Replace,
30-
NullValueHandling = NullValueHandling.Ignore
29+
IgnoreNullValues = false
3130
};
3231
}
3332

@@ -56,7 +55,7 @@ private void Deserialize(string searlized)
5655
{
5756
try
5857
{
59-
_data = JsonConvert.DeserializeObject<T>(searlized, _serializerSettings);
58+
_data = JsonSerializer.Deserialize<T>(searlized, _serializerSettings);
6059
}
6160
catch (JsonException e)
6261
{
@@ -77,7 +76,7 @@ private void LoadDefault()
7776
BackupOriginFile();
7877
}
7978

80-
_data = JsonConvert.DeserializeObject<T>("{}", _serializerSettings);
79+
_data = JsonSerializer.Deserialize<T>("{}", _serializerSettings);
8180
Save();
8281
}
8382

@@ -94,7 +93,8 @@ private void BackupOriginFile()
9493

9594
public void Save()
9695
{
97-
string serialized = JsonConvert.SerializeObject(_data, Formatting.Indented);
96+
string serialized = JsonSerializer.Serialize(_data, new JsonSerializerOptions() { WriteIndented = true });
97+
9898
File.WriteAllText(FilePath, serialized);
9999
}
100100
}

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using System;
22
using System.Collections.ObjectModel;
33
using System.Drawing;
4-
using Newtonsoft.Json;
5-
using Newtonsoft.Json.Converters;
4+
using System.Text.Json.Serialization;
65
using Flow.Launcher.Plugin;
76

87
namespace Flow.Launcher.Infrastructure.UserSettings
@@ -16,7 +15,8 @@ public class Settings : BaseModel
1615
public bool ShowOpenResultHotkey { get; set; } = true;
1716
public string Language
1817
{
19-
get => language; set {
18+
get => language; set
19+
{
2020
language = value;
2121
OnPropertyChanged();
2222
}
@@ -73,9 +73,7 @@ public string QuerySearchPrecisionString
7373
public int MaxResultsToShow { get; set; } = 5;
7474
public int ActivateTimes { get; set; }
7575

76-
// Order defaults to 0 or -1, so 1 will let this property appear last
77-
[JsonProperty(Order = 1)]
78-
public PluginsSettings PluginSettings { get; set; } = new PluginsSettings();
76+
7977
public ObservableCollection<CustomPluginHotkey> CustomPluginHotkeys { get; set; } = new ObservableCollection<CustomPluginHotkey>();
8078

8179
public bool DontPromptUpdateMsg { get; set; }
@@ -100,8 +98,12 @@ public bool HideNotifyIcon
10098

10199
public HttpProxy Proxy { get; set; } = new HttpProxy();
102100

103-
[JsonConverter(typeof(StringEnumConverter))]
101+
[JsonConverter(typeof(JsonStringEnumConverter))]
104102
public LastQueryMode LastQueryMode { get; set; } = LastQueryMode.Selected;
103+
104+
105+
// This needs to be loaded last by staying at the bottom
106+
public PluginsSettings PluginSettings { get; set; } = new PluginsSettings();
105107
}
106108

107109
public enum LastQueryMode

Flow.Launcher.Plugin/Flow.Launcher.Plugin.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
<ItemGroup>
6363
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
6464
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
65-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
6665
</ItemGroup>
6766

6867
</Project>

Flow.Launcher.Plugin/PluginMetadata.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using Newtonsoft.Json;
4+
using System.Text.Json.Serialization;
55

66
namespace Flow.Launcher.Plugin
77
{
8-
[JsonObject(MemberSerialization.OptOut)]
98
public class PluginMetadata : BaseModel
109
{
1110
private string _pluginDirectory;

0 commit comments

Comments
 (0)