Skip to content

Commit c8aa015

Browse files
[dotnet] Switch DevTools response JSON parsing to JsonElement (#14990)
1 parent 335c14c commit c8aa015

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

dotnet/src/webdriver/DevTools/DevToolsCommandData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System.Text.Json;
2021
using System.Text.Json.Nodes;
2122
using System.Text.Json.Serialization;
2223
using System.Threading;
@@ -90,7 +91,7 @@ public DevToolsCommandData(long commandId, string sessionId, string commandName,
9091
/// Get or sets the result of the command execution.
9192
/// </summary>
9293
[JsonIgnore]
93-
public JsonNode Result { get; set; }
94+
public JsonElement Result { get; set; }
9495

9596
/// <summary>
9697
/// Gets or sets a value indicating whether the command resulted in an error response.

dotnet/src/webdriver/DevTools/DevToolsSession.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
168168
throw new InvalidOperationException($"Type {command.GetType()} does not correspond to a known command response type.");
169169
}
170170

171-
return result.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
171+
return result.Value.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
172172
}
173173

174174
/// <summary>
@@ -201,7 +201,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
201201
throw new InvalidOperationException($"Type {typeof(TCommand)} does not correspond to a known command response type.");
202202
}
203203

204-
return result.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
204+
return result.Value.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
205205
}
206206

207207
/// <summary>
@@ -230,7 +230,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
230230
return default(TCommandResponse);
231231
}
232232

233-
return result.Deserialize<TCommandResponse>();
233+
return result.Value.Deserialize<TCommandResponse>();
234234
}
235235

236236
/// <summary>
@@ -243,7 +243,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
243243
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
244244
/// <returns>The command response object implementing the <see cref="ICommandResponse{T}"/> interface.</returns>
245245
//[DebuggerStepThrough]
246-
public async Task<JsonNode> SendCommand(string commandName, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
246+
public async Task<JsonElement?> SendCommand(string commandName, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
247247
{
248248
if (this.attachedTargetId == null)
249249
{
@@ -265,7 +265,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
265265
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
266266
/// <returns>The command response object implementing the <see cref="ICommandResponse{T}"/> interface.</returns>
267267
//[DebuggerStepThrough]
268-
public async Task<JsonNode> SendCommand(string commandName, string sessionId, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
268+
public async Task<JsonElement?> SendCommand(string commandName, string sessionId, JsonNode commandParameters, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
269269
{
270270
if (millisecondsTimeout.HasValue == false)
271271
{
@@ -298,8 +298,8 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
298298
{
299299
if (modified.IsError)
300300
{
301-
var errorMessage = modified.Result["message"].GetValue<string>();
302-
var errorData = modified.Result["data"]?.GetValue<string>();
301+
var errorMessage = modified.Result.GetProperty("message").GetString();
302+
var errorData = modified.Result.TryGetProperty("data", out var data) ? data.GetString() : null;
303303

304304
var exceptionMessage = $"{commandName}: {errorMessage}";
305305
if (!string.IsNullOrWhiteSpace(errorData))
@@ -310,7 +310,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
310310
LogTrace("Recieved Error Response {0}: {1} {2}", modified.CommandId, message, errorData);
311311
throw new CommandResponseException(exceptionMessage)
312312
{
313-
Code = modified.Result["code"]?.GetValue<long>() ?? -1
313+
Code = modified.Result.TryGetProperty("code", out var code) ? code.GetInt64() : -1
314314
};
315315
}
316316

@@ -559,23 +559,27 @@ private void ProcessMessage(string message)
559559
logger.Trace($"CDP RCV << {message}");
560560
}
561561

562-
var messageObject = JsonObject.Parse(message).AsObject();
562+
JsonElement messageObject;
563+
using (var doc = JsonDocument.Parse(message))
564+
{
565+
messageObject = doc.RootElement.Clone();
566+
}
563567

564-
if (messageObject.TryGetPropertyValue("id", out var idProperty))
568+
if (messageObject.TryGetProperty("id", out var idProperty))
565569
{
566-
long commandId = (long)idProperty;
570+
long commandId = idProperty.GetInt64();
567571

568572
DevToolsCommandData commandInfo;
569573
if (this.pendingCommands.TryGetValue(commandId, out commandInfo))
570574
{
571-
if (messageObject.TryGetPropertyValue("error", out var errorProperty))
575+
if (messageObject.TryGetProperty("error", out var errorProperty))
572576
{
573577
commandInfo.IsError = true;
574578
commandInfo.Result = errorProperty;
575579
}
576580
else
577581
{
578-
commandInfo.Result = messageObject["result"];
582+
commandInfo.Result = messageObject.GetProperty("result");
579583
LogTrace("Recieved Response {0}: {1}", commandId, commandInfo.Result.ToString());
580584
}
581585

@@ -594,11 +598,11 @@ private void ProcessMessage(string message)
594598
return;
595599
}
596600

597-
if (messageObject.TryGetPropertyValue("method", out var methodProperty))
601+
if (messageObject.TryGetProperty("method", out var methodProperty))
598602
{
599-
var method = (string)methodProperty;
603+
var method = methodProperty.GetString();
600604
var methodParts = method.Split(new char[] { '.' }, 2);
601-
var eventData = messageObject["params"];
605+
var eventData = messageObject.GetProperty("params");
602606

603607
LogTrace("Recieved Event {0}: {1}", method, eventData.ToString());
604608

dotnet/src/webdriver/DevTools/DevToolsSessionEventReceivedEventArgs.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using System;
21+
using System.Text.Json;
2122
using System.Text.Json.Nodes;
2223

2324
namespace OpenQA.Selenium.DevTools
@@ -33,7 +34,7 @@ public class DevToolsEventReceivedEventArgs : EventArgs
3334
/// <param name="domainName">The domain on which the event is to be raised.</param>
3435
/// <param name="eventName">The name of the event to be raised.</param>
3536
/// <param name="eventData">The data for the event to be raised.</param>
36-
public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonNode eventData)
37+
public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonElement eventData)
3738
{
3839
DomainName = domainName;
3940
EventName = eventName;
@@ -53,6 +54,6 @@ public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonN
5354
/// <summary>
5455
/// Gets the data with which the event is to be raised.
5556
/// </summary>
56-
public JsonNode EventData { get; private set; }
57+
public JsonElement EventData { get; private set; }
5758
}
5859
}

dotnet/src/webdriver/DevTools/IDevToolsSession.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using System;
21+
using System.Text.Json;
2122
using System.Text.Json.Nodes;
2223
using System.Threading;
2324
using System.Threading.Tasks;
@@ -84,6 +85,6 @@ Task<TCommandResponse> SendCommand<TCommand, TCommandResponse>(TCommand command,
8485
/// <param name="millisecondsTimeout">The execution timeout of the command in milliseconds.</param>
8586
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
8687
/// <returns>The command response object implementing the <see cref="ICommandResponse{T}"/> interface.</returns>
87-
Task<JsonNode> SendCommand(string commandName, JsonNode @params, CancellationToken cancellationToken, int? millisecondsTimeout, bool throwExceptionIfResponseNotReceived);
88+
Task<JsonElement?> SendCommand(string commandName, JsonNode @params, CancellationToken cancellationToken, int? millisecondsTimeout, bool throwExceptionIfResponseNotReceived);
8889
}
8990
}

0 commit comments

Comments
 (0)