Skip to content

Commit 41a2b07

Browse files
authored
Merge pull request #917 from Denny09310/feature/system-text-json
refactor: Migrating from Newtonsoft.Json to System.Text.Json
2 parents fc69598 + 0522bc4 commit 41a2b07

File tree

79 files changed

+700
-916
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+700
-916
lines changed

src/ElectronNET.API/API/ApiBase.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
namespace ElectronNET.API
22
{
3+
using ElectronNET.API.Serialization;
4+
using ElectronNET.Common;
35
using System;
46
using System.Collections.Concurrent;
57
using System.Diagnostics;
68
using System.Runtime.CompilerServices;
9+
using System.Text.Json;
710
using System.Threading.Tasks;
8-
using ElectronNET.Common;
911

1012
public abstract class ApiBase
1113
{
@@ -156,8 +158,19 @@ public PropertyGetter(ApiBase apiBase, string callerName, int timeoutMs)
156158

157159
lock (this)
158160
{
159-
this.tcs?.SetResult(result);
160-
this.tcs = null;
161+
try
162+
{
163+
var value = result;
164+
this.tcs?.SetResult(value);
165+
}
166+
catch (Exception ex)
167+
{
168+
this.tcs?.TrySetException(ex);
169+
}
170+
finally
171+
{
172+
this.tcs = null;
173+
}
161174
}
162175
});
163176

@@ -170,7 +183,7 @@ public PropertyGetter(ApiBase apiBase, string callerName, int timeoutMs)
170183
BridgeConnector.Socket.Emit(messageName);
171184
}
172185

173-
System.Threading.Tasks.Task.Delay(ApiBase.PropertyTimeout).ContinueWith(_ =>
186+
System.Threading.Tasks.Task.Delay(PropertyTimeout).ContinueWith(_ =>
174187
{
175188
if (this.tcs != null)
176189
{

src/ElectronNET.API/API/App.cs

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using ElectronNET.API.Entities;
2-
using Newtonsoft.Json;
3-
using Newtonsoft.Json.Linq;
4-
using Newtonsoft.Json.Serialization;
1+
using ElectronNET.API.Entities;
2+
using ElectronNET.API.Extensions;
3+
using ElectronNET.API.Serialization;
4+
using ElectronNET.Common;
55
using System;
66
using System.Runtime.InteropServices;
7+
using System.Text.Json;
78
using System.Threading;
89
using System.Threading.Tasks;
9-
using ElectronNET.API.Extensions;
10-
using ElectronNET.Common;
1110

1211
// ReSharper disable InconsistentNaming
1312

@@ -271,7 +270,7 @@ public event Action WebContentsCreated
271270
/// <returns><see langword="true"/> when Chrome's accessibility support is enabled, <see langword="false"/> otherwise.</returns>
272271
public event Action<bool> AccessibilitySupportChanged
273272
{
274-
add => ApiEventManager.AddEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value, (args) => (bool)args);
273+
add => ApiEventManager.AddEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value, (args) => args.GetBoolean());
275274
remove => ApiEventManager.RemoveEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value);
276275
}
277276

@@ -414,10 +413,7 @@ internal static App Instance
414413
private static App _app;
415414
private static object _syncRoot = new object();
416415

417-
private readonly JsonSerializer _jsonSerializer = new JsonSerializer()
418-
{
419-
ContractResolver = new CamelCasePropertyNamesContractResolver()
420-
};
416+
421417

422418
/// <summary>
423419
/// Try to close all windows. The <see cref="BeforeQuit"/> event will be emitted first. If all windows are successfully
@@ -475,7 +471,7 @@ public void Relaunch()
475471
/// <param name="relaunchOptions">Options for the relaunch.</param>
476472
public void Relaunch(RelaunchOptions relaunchOptions)
477473
{
478-
this.CallMethod1(JObject.FromObject(relaunchOptions, _jsonSerializer));
474+
this.CallMethod1(relaunchOptions);
479475
}
480476

481477
/// <summary>
@@ -495,7 +491,7 @@ public void Focus()
495491
/// </summary>
496492
public void Focus(FocusOptions focusOptions)
497493
{
498-
this.CallMethod1(JObject.FromObject(focusOptions, _jsonSerializer));
494+
this.CallMethod1(focusOptions);
499495
}
500496

501497
/// <summary>
@@ -551,11 +547,11 @@ public async Task<string> GetPathAsync(PathName pathName, CancellationToken canc
551547
var taskCompletionSource = new TaskCompletionSource<string>();
552548
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
553549
{
554-
BridgeConnector.Socket.On("appGetPathCompleted", (path) =>
550+
BridgeConnector.Socket.On<string>("appGetPathCompleted", (path) =>
555551
{
556552
BridgeConnector.Socket.Off("appGetPathCompleted");
557553

558-
taskCompletionSource.SetResult(path.ToString());
554+
taskCompletionSource.SetResult(path);
559555
});
560556

561557
BridgeConnector.Socket.Emit("appGetPath", pathName.GetDescription());
@@ -720,10 +716,10 @@ public async Task<bool> SetAsDefaultProtocolClientAsync(string protocol, string
720716
var taskCompletionSource = new TaskCompletionSource<bool>();
721717
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
722718
{
723-
BridgeConnector.Socket.On("appSetAsDefaultProtocolClientCompleted", (success) =>
719+
BridgeConnector.Socket.On<bool>("appSetAsDefaultProtocolClientCompleted", (success) =>
724720
{
725721
BridgeConnector.Socket.Off("appSetAsDefaultProtocolClientCompleted");
726-
taskCompletionSource.SetResult((bool)success);
722+
taskCompletionSource.SetResult(success);
727723
});
728724

729725
BridgeConnector.Socket.Emit("appSetAsDefaultProtocolClient", protocol, path, args);
@@ -774,10 +770,10 @@ public async Task<bool> RemoveAsDefaultProtocolClientAsync(string protocol, stri
774770
var taskCompletionSource = new TaskCompletionSource<bool>();
775771
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
776772
{
777-
BridgeConnector.Socket.On("appRemoveAsDefaultProtocolClientCompleted", (success) =>
773+
BridgeConnector.Socket.On<bool>("appRemoveAsDefaultProtocolClientCompleted", (success) =>
778774
{
779775
BridgeConnector.Socket.Off("appRemoveAsDefaultProtocolClientCompleted");
780-
taskCompletionSource.SetResult((bool)success);
776+
taskCompletionSource.SetResult(success);
781777
});
782778

783779
BridgeConnector.Socket.Emit("appRemoveAsDefaultProtocolClient", protocol, path, args);
@@ -846,10 +842,10 @@ public async Task<bool> IsDefaultProtocolClientAsync(string protocol, string pat
846842
var taskCompletionSource = new TaskCompletionSource<bool>();
847843
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
848844
{
849-
BridgeConnector.Socket.On("appIsDefaultProtocolClientCompleted", (success) =>
845+
BridgeConnector.Socket.On<bool>("appIsDefaultProtocolClientCompleted", (success) =>
850846
{
851847
BridgeConnector.Socket.Off("appIsDefaultProtocolClientCompleted");
852-
taskCompletionSource.SetResult((bool)success);
848+
taskCompletionSource.SetResult(success);
853849
});
854850

855851
BridgeConnector.Socket.Emit("appIsDefaultProtocolClient", protocol, path, args);
@@ -874,13 +870,13 @@ public async Task<bool> SetUserTasksAsync(UserTask[] userTasks, CancellationToke
874870
var taskCompletionSource = new TaskCompletionSource<bool>();
875871
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
876872
{
877-
BridgeConnector.Socket.On("appSetUserTasksCompleted", (success) =>
873+
BridgeConnector.Socket.On<bool>("appSetUserTasksCompleted", (success) =>
878874
{
879875
BridgeConnector.Socket.Off("appSetUserTasksCompleted");
880-
taskCompletionSource.SetResult((bool)success);
876+
taskCompletionSource.SetResult(success);
881877
});
882878

883-
BridgeConnector.Socket.Emit("appSetUserTasks", JArray.FromObject(userTasks, _jsonSerializer));
879+
BridgeConnector.Socket.Emit("appSetUserTasks", userTasks);
884880

885881
return await taskCompletionSource.Task
886882
.ConfigureAwait(false);
@@ -916,7 +912,7 @@ public async Task<JumpListSettings> GetJumpListSettingsAsync(CancellationToken c
916912
/// <param name="categories">Array of <see cref="JumpListCategory"/> objects.</param>
917913
public void SetJumpList(JumpListCategory[] categories)
918914
{
919-
this.CallMethod1(JArray.FromObject(categories, _jsonSerializer));
915+
this.CallMethod1(categories);
920916
}
921917

922918
/// <summary>
@@ -947,19 +943,21 @@ public async Task<bool> RequestSingleInstanceLockAsync(Action<string[], string>
947943
var taskCompletionSource = new TaskCompletionSource<bool>();
948944
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
949945
{
950-
BridgeConnector.Socket.On("appRequestSingleInstanceLockCompleted", (success) =>
946+
BridgeConnector.Socket.On<bool>("appRequestSingleInstanceLockCompleted", (success) =>
951947
{
952948
BridgeConnector.Socket.Off("appRequestSingleInstanceLockCompleted");
953-
taskCompletionSource.SetResult((bool)success);
949+
taskCompletionSource.SetResult(success);
954950
});
955951

956952
BridgeConnector.Socket.Off("secondInstance");
957-
BridgeConnector.Socket.On("secondInstance", (result) =>
953+
BridgeConnector.Socket.On<JsonElement>("secondInstance", (result) =>
958954
{
959-
JArray results = (JArray)result;
960-
string[] args = results.First.ToObject<string[]>();
961-
string workingDirectory = results.Last.ToObject<string>();
962-
955+
var arr = result.EnumerateArray();
956+
var e = arr.GetEnumerator();
957+
e.MoveNext();
958+
var args = e.Current.Deserialize<string[]>(JsonSerializerOptions.Default);
959+
e.MoveNext();
960+
var workingDirectory = e.Current.GetString();
963961
newInstanceOpened(args, workingDirectory);
964962
});
965963

@@ -1071,13 +1069,13 @@ public async Task<int> ImportCertificateAsync(ImportCertificateOptions options,
10711069
var taskCompletionSource = new TaskCompletionSource<int>();
10721070
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
10731071
{
1074-
BridgeConnector.Socket.On("appImportCertificateCompleted", (result) =>
1072+
BridgeConnector.Socket.On<int>("appImportCertificateCompleted", (result) =>
10751073
{
10761074
BridgeConnector.Socket.Off("appImportCertificateCompleted");
1077-
taskCompletionSource.SetResult((int)result);
1075+
taskCompletionSource.SetResult(result);
10781076
});
10791077

1080-
BridgeConnector.Socket.Emit("appImportCertificate", JObject.FromObject(options, _jsonSerializer));
1078+
BridgeConnector.Socket.Emit("appImportCertificate", options);
10811079

10821080
return await taskCompletionSource.Task
10831081
.ConfigureAwait(false);
@@ -1127,10 +1125,10 @@ public async Task<bool> SetBadgeCountAsync(int count, CancellationToken cancella
11271125
var taskCompletionSource = new TaskCompletionSource<bool>();
11281126
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
11291127
{
1130-
BridgeConnector.Socket.On("appSetBadgeCountCompleted", (success) =>
1128+
BridgeConnector.Socket.On<bool>("appSetBadgeCountCompleted", (success) =>
11311129
{
11321130
BridgeConnector.Socket.Off("appSetBadgeCountCompleted");
1133-
taskCompletionSource.SetResult((bool)success);
1131+
taskCompletionSource.SetResult(success);
11341132
});
11351133

11361134
BridgeConnector.Socket.Emit("appSetBadgeCount", count);
@@ -1187,12 +1185,9 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
11871185
var taskCompletionSource = new TaskCompletionSource<LoginItemSettings>();
11881186
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
11891187
{
1190-
BridgeConnector.Socket.On("appGetLoginItemSettingsCompleted", (loginItemSettings) =>
1188+
BridgeConnector.Socket.On<LoginItemSettings>("appGetLoginItemSettingsCompleted", (result) =>
11911189
{
11921190
BridgeConnector.Socket.Off("appGetLoginItemSettingsCompleted");
1193-
1194-
var result = ((JObject)loginItemSettings).ToObject<LoginItemSettings>();
1195-
11961191
taskCompletionSource.SetResult(result);
11971192
});
11981193

@@ -1202,7 +1197,7 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
12021197
}
12031198
else
12041199
{
1205-
BridgeConnector.Socket.Emit("appGetLoginItemSettings", JObject.FromObject(options, _jsonSerializer));
1200+
BridgeConnector.Socket.Emit("appGetLoginItemSettings", options);
12061201
}
12071202

12081203
return await taskCompletionSource.Task
@@ -1218,7 +1213,7 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
12181213
/// <param name="loginSettings"></param>
12191214
public void SetLoginItemSettings(LoginSettings loginSettings)
12201215
{
1221-
this.CallMethod1(JObject.FromObject(loginSettings, _jsonSerializer));
1216+
this.CallMethod1(loginSettings);
12221217
}
12231218

12241219
/// <summary>
@@ -1270,7 +1265,7 @@ public void ShowAboutPanel()
12701265
/// <param name="options">About panel options.</param>
12711266
public void SetAboutPanelOptions(AboutPanelOptions options)
12721267
{
1273-
this.CallMethod1(JObject.FromObject(options, _jsonSerializer));
1268+
this.CallMethod1(options);
12741269
}
12751270

12761271
/// <summary>
@@ -1306,14 +1301,14 @@ public Task<string> UserAgentFallbackAsync
13061301
{
13071302
get
13081303
{
1309-
return Task.Run<string>(() =>
1304+
return Task.Run(() =>
13101305
{
13111306
var taskCompletionSource = new TaskCompletionSource<string>();
13121307

1313-
BridgeConnector.Socket.On("appGetUserAgentFallbackCompleted", (result) =>
1308+
BridgeConnector.Socket.On<string>("appGetUserAgentFallbackCompleted", (result) =>
13141309
{
13151310
BridgeConnector.Socket.Off("appGetUserAgentFallbackCompleted");
1316-
taskCompletionSource.SetResult((string)result);
1311+
taskCompletionSource.SetResult(result);
13171312
});
13181313

13191314
BridgeConnector.Socket.Emit("appGetUserAgentFallback");
@@ -1364,4 +1359,4 @@ public void Once(string eventName, Action action)
13641359
public async Task Once(string eventName, Action<object> action)
13651360
=> await Events.Instance.Once(ModuleName, eventName, action).ConfigureAwait(false);
13661361
}
1367-
}
1362+
}

0 commit comments

Comments
 (0)