Skip to content

Commit 71ced8d

Browse files
committed
refactor: Migrated from Newtonsoft.Json to System.Text.Json, missing one test passing
1 parent fc69598 commit 71ced8d

File tree

80 files changed

+715
-873
lines changed

Some content is hidden

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

80 files changed

+715
-873
lines changed

src/ElectronNET.API/API/ApiBase.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
namespace ElectronNET.API
22
{
3+
using ElectronNET.Common;
34
using System;
45
using System.Collections.Concurrent;
56
using System.Diagnostics;
67
using System.Runtime.CompilerServices;
8+
using System.Text.Json;
79
using System.Threading.Tasks;
8-
using ElectronNET.Common;
910

1011
public abstract class ApiBase
1112
{
@@ -150,14 +151,25 @@ public PropertyGetter(ApiBase apiBase, string callerName, int timeoutMs)
150151

151152
var messageName = apiBase.propertyMessageNames.GetOrAdd(callerName, s => apiBase.objectName + s.StripAsync());
152153

153-
BridgeConnector.Socket.On<T>(eventName, (result) =>
154+
BridgeConnector.Socket.On<JsonElement>(eventName, (result) =>
154155
{
155156
BridgeConnector.Socket.Off(eventName);
156157

157158
lock (this)
158159
{
159-
this.tcs?.SetResult(result);
160-
this.tcs = null;
160+
try
161+
{
162+
var value = JsonSerializer.Deserialize<T>(result, Serialization.ElectronJson.Options);
163+
this.tcs?.SetResult(value);
164+
}
165+
catch (Exception ex)
166+
{
167+
this.tcs?.TrySetException(ex);
168+
}
169+
finally
170+
{
171+
this.tcs = null;
172+
}
161173
}
162174
});
163175

@@ -170,7 +182,7 @@ public PropertyGetter(ApiBase apiBase, string callerName, int timeoutMs)
170182
BridgeConnector.Socket.Emit(messageName);
171183
}
172184

173-
System.Threading.Tasks.Task.Delay(ApiBase.PropertyTimeout).ContinueWith(_ =>
185+
System.Threading.Tasks.Task.Delay(PropertyTimeout).ContinueWith(_ =>
174186
{
175187
if (this.tcs != null)
176188
{

src/ElectronNET.API/API/App.cs

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
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.Common;
54
using System;
65
using System.Runtime.InteropServices;
6+
using System.Text.Json;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using ElectronNET.API.Extensions;
10-
using ElectronNET.Common;
119

1210
// ReSharper disable InconsistentNaming
1311

@@ -271,7 +269,7 @@ public event Action WebContentsCreated
271269
/// <returns><see langword="true"/> when Chrome's accessibility support is enabled, <see langword="false"/> otherwise.</returns>
272270
public event Action<bool> AccessibilitySupportChanged
273271
{
274-
add => ApiEventManager.AddEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value, (args) => (bool)args);
272+
add => ApiEventManager.AddEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value, (args) => args.GetBoolean());
275273
remove => ApiEventManager.RemoveEvent("app-accessibility-support-changed", GetHashCode(), _accessibilitySupportChanged, value);
276274
}
277275

@@ -414,10 +412,7 @@ internal static App Instance
414412
private static App _app;
415413
private static object _syncRoot = new object();
416414

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

422417
/// <summary>
423418
/// Try to close all windows. The <see cref="BeforeQuit"/> event will be emitted first. If all windows are successfully
@@ -475,7 +470,7 @@ public void Relaunch()
475470
/// <param name="relaunchOptions">Options for the relaunch.</param>
476471
public void Relaunch(RelaunchOptions relaunchOptions)
477472
{
478-
this.CallMethod1(JObject.FromObject(relaunchOptions, _jsonSerializer));
473+
this.CallMethod1(relaunchOptions);
479474
}
480475

481476
/// <summary>
@@ -495,7 +490,7 @@ public void Focus()
495490
/// </summary>
496491
public void Focus(FocusOptions focusOptions)
497492
{
498-
this.CallMethod1(JObject.FromObject(focusOptions, _jsonSerializer));
493+
this.CallMethod1(focusOptions);
499494
}
500495

501496
/// <summary>
@@ -551,11 +546,11 @@ public async Task<string> GetPathAsync(PathName pathName, CancellationToken canc
551546
var taskCompletionSource = new TaskCompletionSource<string>();
552547
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
553548
{
554-
BridgeConnector.Socket.On("appGetPathCompleted", (path) =>
549+
BridgeConnector.Socket.On<JsonElement>("appGetPathCompleted", (path) =>
555550
{
556551
BridgeConnector.Socket.Off("appGetPathCompleted");
557552

558-
taskCompletionSource.SetResult(path.ToString());
553+
taskCompletionSource.SetResult(path.GetString());
559554
});
560555

561556
BridgeConnector.Socket.Emit("appGetPath", pathName.GetDescription());
@@ -720,10 +715,10 @@ public async Task<bool> SetAsDefaultProtocolClientAsync(string protocol, string
720715
var taskCompletionSource = new TaskCompletionSource<bool>();
721716
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
722717
{
723-
BridgeConnector.Socket.On("appSetAsDefaultProtocolClientCompleted", (success) =>
718+
BridgeConnector.Socket.On<JsonElement>("appSetAsDefaultProtocolClientCompleted", (success) =>
724719
{
725720
BridgeConnector.Socket.Off("appSetAsDefaultProtocolClientCompleted");
726-
taskCompletionSource.SetResult((bool)success);
721+
taskCompletionSource.SetResult(success.GetBoolean());
727722
});
728723

729724
BridgeConnector.Socket.Emit("appSetAsDefaultProtocolClient", protocol, path, args);
@@ -774,10 +769,10 @@ public async Task<bool> RemoveAsDefaultProtocolClientAsync(string protocol, stri
774769
var taskCompletionSource = new TaskCompletionSource<bool>();
775770
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
776771
{
777-
BridgeConnector.Socket.On("appRemoveAsDefaultProtocolClientCompleted", (success) =>
772+
BridgeConnector.Socket.On<JsonElement>("appRemoveAsDefaultProtocolClientCompleted", (success) =>
778773
{
779774
BridgeConnector.Socket.Off("appRemoveAsDefaultProtocolClientCompleted");
780-
taskCompletionSource.SetResult((bool)success);
775+
taskCompletionSource.SetResult(success.GetBoolean());
781776
});
782777

783778
BridgeConnector.Socket.Emit("appRemoveAsDefaultProtocolClient", protocol, path, args);
@@ -846,10 +841,10 @@ public async Task<bool> IsDefaultProtocolClientAsync(string protocol, string pat
846841
var taskCompletionSource = new TaskCompletionSource<bool>();
847842
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
848843
{
849-
BridgeConnector.Socket.On("appIsDefaultProtocolClientCompleted", (success) =>
844+
BridgeConnector.Socket.On<JsonElement>("appIsDefaultProtocolClientCompleted", (success) =>
850845
{
851846
BridgeConnector.Socket.Off("appIsDefaultProtocolClientCompleted");
852-
taskCompletionSource.SetResult((bool)success);
847+
taskCompletionSource.SetResult(success.GetBoolean());
853848
});
854849

855850
BridgeConnector.Socket.Emit("appIsDefaultProtocolClient", protocol, path, args);
@@ -874,13 +869,13 @@ public async Task<bool> SetUserTasksAsync(UserTask[] userTasks, CancellationToke
874869
var taskCompletionSource = new TaskCompletionSource<bool>();
875870
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
876871
{
877-
BridgeConnector.Socket.On("appSetUserTasksCompleted", (success) =>
872+
BridgeConnector.Socket.On<JsonElement>("appSetUserTasksCompleted", (success) =>
878873
{
879874
BridgeConnector.Socket.Off("appSetUserTasksCompleted");
880-
taskCompletionSource.SetResult((bool)success);
875+
taskCompletionSource.SetResult(success.GetBoolean());
881876
});
882877

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

885880
return await taskCompletionSource.Task
886881
.ConfigureAwait(false);
@@ -916,7 +911,7 @@ public async Task<JumpListSettings> GetJumpListSettingsAsync(CancellationToken c
916911
/// <param name="categories">Array of <see cref="JumpListCategory"/> objects.</param>
917912
public void SetJumpList(JumpListCategory[] categories)
918913
{
919-
this.CallMethod1(JArray.FromObject(categories, _jsonSerializer));
914+
this.CallMethod1(categories);
920915
}
921916

922917
/// <summary>
@@ -947,19 +942,21 @@ public async Task<bool> RequestSingleInstanceLockAsync(Action<string[], string>
947942
var taskCompletionSource = new TaskCompletionSource<bool>();
948943
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
949944
{
950-
BridgeConnector.Socket.On("appRequestSingleInstanceLockCompleted", (success) =>
945+
BridgeConnector.Socket.On<JsonElement>("appRequestSingleInstanceLockCompleted", (success) =>
951946
{
952947
BridgeConnector.Socket.Off("appRequestSingleInstanceLockCompleted");
953-
taskCompletionSource.SetResult((bool)success);
948+
taskCompletionSource.SetResult(success.GetBoolean());
954949
});
955950

956951
BridgeConnector.Socket.Off("secondInstance");
957-
BridgeConnector.Socket.On("secondInstance", (result) =>
952+
BridgeConnector.Socket.On<JsonElement>("secondInstance", (result) =>
958953
{
959-
JArray results = (JArray)result;
960-
string[] args = results.First.ToObject<string[]>();
961-
string workingDirectory = results.Last.ToObject<string>();
962-
954+
var arr = result.EnumerateArray();
955+
var e = arr.GetEnumerator();
956+
e.MoveNext();
957+
var args = e.Current.Deserialize<string[]>(JsonSerializerOptions.Default);
958+
e.MoveNext();
959+
var workingDirectory = e.Current.GetString();
963960
newInstanceOpened(args, workingDirectory);
964961
});
965962

@@ -1071,13 +1068,13 @@ public async Task<int> ImportCertificateAsync(ImportCertificateOptions options,
10711068
var taskCompletionSource = new TaskCompletionSource<int>();
10721069
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
10731070
{
1074-
BridgeConnector.Socket.On("appImportCertificateCompleted", (result) =>
1071+
BridgeConnector.Socket.On<JsonElement>("appImportCertificateCompleted", (result) =>
10751072
{
10761073
BridgeConnector.Socket.Off("appImportCertificateCompleted");
1077-
taskCompletionSource.SetResult((int)result);
1074+
taskCompletionSource.SetResult(result.GetInt32());
10781075
});
10791076

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

10821079
return await taskCompletionSource.Task
10831080
.ConfigureAwait(false);
@@ -1127,10 +1124,10 @@ public async Task<bool> SetBadgeCountAsync(int count, CancellationToken cancella
11271124
var taskCompletionSource = new TaskCompletionSource<bool>();
11281125
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
11291126
{
1130-
BridgeConnector.Socket.On("appSetBadgeCountCompleted", (success) =>
1127+
BridgeConnector.Socket.On<JsonElement>("appSetBadgeCountCompleted", (success) =>
11311128
{
11321129
BridgeConnector.Socket.Off("appSetBadgeCountCompleted");
1133-
taskCompletionSource.SetResult((bool)success);
1130+
taskCompletionSource.SetResult(success.GetBoolean());
11341131
});
11351132

11361133
BridgeConnector.Socket.Emit("appSetBadgeCount", count);
@@ -1187,11 +1184,11 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
11871184
var taskCompletionSource = new TaskCompletionSource<LoginItemSettings>();
11881185
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
11891186
{
1190-
BridgeConnector.Socket.On("appGetLoginItemSettingsCompleted", (loginItemSettings) =>
1187+
BridgeConnector.Socket.On<JsonElement>("appGetLoginItemSettingsCompleted", (loginItemSettings) =>
11911188
{
11921189
BridgeConnector.Socket.Off("appGetLoginItemSettingsCompleted");
11931190

1194-
var result = ((JObject)loginItemSettings).ToObject<LoginItemSettings>();
1191+
var result = JsonSerializer.Deserialize<LoginItemSettings>(loginItemSettings, Serialization.ElectronJson.Options);
11951192

11961193
taskCompletionSource.SetResult(result);
11971194
});
@@ -1202,7 +1199,7 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
12021199
}
12031200
else
12041201
{
1205-
BridgeConnector.Socket.Emit("appGetLoginItemSettings", JObject.FromObject(options, _jsonSerializer));
1202+
BridgeConnector.Socket.Emit("appGetLoginItemSettings", options);
12061203
}
12071204

12081205
return await taskCompletionSource.Task
@@ -1218,7 +1215,7 @@ public async Task<LoginItemSettings> GetLoginItemSettingsAsync(LoginItemSettings
12181215
/// <param name="loginSettings"></param>
12191216
public void SetLoginItemSettings(LoginSettings loginSettings)
12201217
{
1221-
this.CallMethod1(JObject.FromObject(loginSettings, _jsonSerializer));
1218+
this.CallMethod1(loginSettings);
12221219
}
12231220

12241221
/// <summary>
@@ -1270,7 +1267,7 @@ public void ShowAboutPanel()
12701267
/// <param name="options">About panel options.</param>
12711268
public void SetAboutPanelOptions(AboutPanelOptions options)
12721269
{
1273-
this.CallMethod1(JObject.FromObject(options, _jsonSerializer));
1270+
this.CallMethod1(options);
12741271
}
12751272

12761273
/// <summary>
@@ -1306,14 +1303,14 @@ public Task<string> UserAgentFallbackAsync
13061303
{
13071304
get
13081305
{
1309-
return Task.Run<string>(() =>
1306+
return Task.Run(() =>
13101307
{
13111308
var taskCompletionSource = new TaskCompletionSource<string>();
13121309

1313-
BridgeConnector.Socket.On("appGetUserAgentFallbackCompleted", (result) =>
1310+
BridgeConnector.Socket.On<JsonElement>("appGetUserAgentFallbackCompleted", (result) =>
13141311
{
13151312
BridgeConnector.Socket.Off("appGetUserAgentFallbackCompleted");
1316-
taskCompletionSource.SetResult((string)result);
1313+
taskCompletionSource.SetResult(result.GetString());
13171314
});
13181315

13191316
BridgeConnector.Socket.Emit("appGetUserAgentFallback");
@@ -1364,4 +1361,4 @@ public void Once(string eventName, Action action)
13641361
public async Task Once(string eventName, Action<object> action)
13651362
=> await Events.Instance.Once(ModuleName, eventName, action).ConfigureAwait(false);
13661363
}
1367-
}
1364+
}

0 commit comments

Comments
 (0)