Skip to content

[dotnet] [bidi] Avoid intermediate JsonDocument allocation to determine unordered discriminator #15555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication.Json.Internal;
using OpenQA.Selenium.BiDi.Modules.Script;
using System;
using System.Text.Json;
Expand All @@ -29,12 +30,10 @@ internal class EvaluateResultConverter : JsonConverter<EvaluateResult>
{
public override EvaluateResult? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);

return jsonDocument.RootElement.GetProperty("type").ToString() switch
return reader.GetDiscriminator("type") switch
{
"success" => jsonDocument.Deserialize<EvaluateResultSuccess>(options),
"exception" => jsonDocument.Deserialize<EvaluateResultException>(options),
"success" => JsonSerializer.Deserialize<EvaluateResultSuccess>(ref reader, options),
"exception" => JsonSerializer.Deserialize<EvaluateResultException>(ref reader, options),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication.Json.Internal;
using OpenQA.Selenium.BiDi.Modules.Log;
using System;
using System.Text.Json;
Expand All @@ -29,12 +30,10 @@ internal class LogEntryConverter : JsonConverter<Modules.Log.LogEntry>
{
public override Modules.Log.LogEntry? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);

return jsonDocument.RootElement.GetProperty("type").ToString() switch
return reader.GetDiscriminator("type") switch
{
"console" => jsonDocument.Deserialize<ConsoleLogEntry>(options),
"javascript" => jsonDocument.Deserialize<JavascriptLogEntry>(options),
"console" => JsonSerializer.Deserialize<ConsoleLogEntry>(ref reader, options),
"javascript" => JsonSerializer.Deserialize<JavascriptLogEntry>(ref reader, options),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication.Json.Internal;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -28,13 +29,11 @@ internal class MessageConverter : JsonConverter<Message>
{
public override Message? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);

return jsonDocument.RootElement.GetProperty("type").ToString() switch
return reader.GetDiscriminator("type") switch
{
"success" => jsonDocument.Deserialize<MessageSuccess>(options),
"error" => jsonDocument.Deserialize<MessageError>(options),
"event" => jsonDocument.Deserialize<MessageEvent>(options),
"success" => JsonSerializer.Deserialize<MessageSuccess>(ref reader, options),
"error" => JsonSerializer.Deserialize<MessageError>(ref reader, options),
"event" => JsonSerializer.Deserialize<MessageEvent>(ref reader, options),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication.Json.Internal;
using OpenQA.Selenium.BiDi.Modules.Script;
using System;
using System.Text.Json;
Expand All @@ -29,18 +30,16 @@ internal class RealmInfoConverter : JsonConverter<RealmInfo>
{
public override RealmInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);

return jsonDocument.RootElement.GetProperty("type").ToString() switch
return reader.GetDiscriminator("type") switch
{
"window" => jsonDocument.Deserialize<WindowRealmInfo>(options),
"dedicated-worker" => jsonDocument.Deserialize<DedicatedWorkerRealmInfo>(options),
"shared-worker" => jsonDocument.Deserialize<SharedWorkerRealmInfo>(options),
"service-worker" => jsonDocument.Deserialize<ServiceWorkerRealmInfo>(options),
"worker" => jsonDocument.Deserialize<WorkerRealmInfo>(options),
"paint-worklet" => jsonDocument.Deserialize<PaintWorkletRealmInfo>(options),
"audio-worklet" => jsonDocument.Deserialize<AudioWorkletRealmInfo>(options),
"worklet" => jsonDocument.Deserialize<WorkletRealmInfo>(options),
"window" => JsonSerializer.Deserialize<WindowRealmInfo>(ref reader, options),
"dedicated-worker" => JsonSerializer.Deserialize<DedicatedWorkerRealmInfo>(ref reader, options),
"shared-worker" => JsonSerializer.Deserialize<SharedWorkerRealmInfo>(ref reader, options),
"service-worker" => JsonSerializer.Deserialize<ServiceWorkerRealmInfo>(ref reader, options),
"worker" => JsonSerializer.Deserialize<WorkerRealmInfo>(ref reader, options),
"paint-worklet" => JsonSerializer.Deserialize<PaintWorkletRealmInfo>(ref reader, options),
"audio-worklet" => JsonSerializer.Deserialize<AudioWorkletRealmInfo>(ref reader, options),
"worklet" => JsonSerializer.Deserialize<WorkletRealmInfo>(ref reader, options),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication.Json.Internal;
using OpenQA.Selenium.BiDi.Modules.Script;
using System;
using System.Text.Json;
Expand All @@ -29,41 +30,39 @@ internal class RemoteValueConverter : JsonConverter<RemoteValue>
{
public override RemoteValue? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);

if (jsonDocument.RootElement.ValueKind == JsonValueKind.String)
if (reader.TokenType == JsonTokenType.String)
{
return new StringRemoteValue(jsonDocument.RootElement.GetString()!);
return new StringRemoteValue(reader.GetString()!);
}

return jsonDocument.RootElement.GetProperty("type").ToString() switch
return reader.GetDiscriminator("type") switch
{
"number" => jsonDocument.Deserialize<NumberRemoteValue>(options),
"boolean" => jsonDocument.Deserialize<BooleanRemoteValue>(options),
"bigint" => jsonDocument.Deserialize<BigIntRemoteValue>(options),
"string" => jsonDocument.Deserialize<StringRemoteValue>(options),
"null" => jsonDocument.Deserialize<NullRemoteValue>(options),
"undefined" => jsonDocument.Deserialize<UndefinedRemoteValue>(options),
"symbol" => jsonDocument.Deserialize<SymbolRemoteValue>(options),
"array" => jsonDocument.Deserialize<ArrayRemoteValue>(options),
"object" => jsonDocument.Deserialize<ObjectRemoteValue>(options),
"function" => jsonDocument.Deserialize<FunctionRemoteValue>(options),
"regexp" => jsonDocument.Deserialize<RegExpRemoteValue>(options),
"date" => jsonDocument.Deserialize<DateRemoteValue>(options),
"map" => jsonDocument.Deserialize<MapRemoteValue>(options),
"set" => jsonDocument.Deserialize<SetRemoteValue>(options),
"weakmap" => jsonDocument.Deserialize<WeakMapRemoteValue>(options),
"weakset" => jsonDocument.Deserialize<WeakSetRemoteValue>(options),
"generator" => jsonDocument.Deserialize<GeneratorRemoteValue>(options),
"error" => jsonDocument.Deserialize<ErrorRemoteValue>(options),
"proxy" => jsonDocument.Deserialize<ProxyRemoteValue>(options),
"promise" => jsonDocument.Deserialize<PromiseRemoteValue>(options),
"typedarray" => jsonDocument.Deserialize<TypedArrayRemoteValue>(options),
"arraybuffer" => jsonDocument.Deserialize<ArrayBufferRemoteValue>(options),
"nodelist" => jsonDocument.Deserialize<NodeListRemoteValue>(options),
"htmlcollection" => jsonDocument.Deserialize<HtmlCollectionRemoteValue>(options),
"node" => jsonDocument.Deserialize<NodeRemoteValue>(options),
"window" => jsonDocument.Deserialize<WindowProxyRemoteValue>(options),
"number" => JsonSerializer.Deserialize<NumberRemoteValue>(ref reader, options),
"boolean" => JsonSerializer.Deserialize<BooleanRemoteValue>(ref reader, options),
"bigint" => JsonSerializer.Deserialize<BigIntRemoteValue>(ref reader, options),
"string" => JsonSerializer.Deserialize<StringRemoteValue>(ref reader, options),
"null" => JsonSerializer.Deserialize<NullRemoteValue>(ref reader, options),
"undefined" => JsonSerializer.Deserialize<UndefinedRemoteValue>(ref reader, options),
"symbol" => JsonSerializer.Deserialize<SymbolRemoteValue>(ref reader, options),
"array" => JsonSerializer.Deserialize<ArrayRemoteValue>(ref reader, options),
"object" => JsonSerializer.Deserialize<ObjectRemoteValue>(ref reader, options),
"function" => JsonSerializer.Deserialize<FunctionRemoteValue>(ref reader, options),
"regexp" => JsonSerializer.Deserialize<RegExpRemoteValue>(ref reader, options),
"date" => JsonSerializer.Deserialize<DateRemoteValue>(ref reader, options),
"map" => JsonSerializer.Deserialize<MapRemoteValue>(ref reader, options),
"set" => JsonSerializer.Deserialize<SetRemoteValue>(ref reader, options),
"weakmap" => JsonSerializer.Deserialize<WeakMapRemoteValue>(ref reader, options),
"weakset" => JsonSerializer.Deserialize<WeakSetRemoteValue>(ref reader, options),
"generator" => JsonSerializer.Deserialize<GeneratorRemoteValue>(ref reader, options),
"error" => JsonSerializer.Deserialize<ErrorRemoteValue>(ref reader, options),
"proxy" => JsonSerializer.Deserialize<ProxyRemoteValue>(ref reader, options),
"promise" => JsonSerializer.Deserialize<PromiseRemoteValue>(ref reader, options),
"typedarray" => JsonSerializer.Deserialize<TypedArrayRemoteValue>(ref reader, options),
"arraybuffer" => JsonSerializer.Deserialize<ArrayBufferRemoteValue>(ref reader, options),
"nodelist" => JsonSerializer.Deserialize<NodeListRemoteValue>(ref reader, options),
"htmlcollection" => JsonSerializer.Deserialize<HtmlCollectionRemoteValue>(ref reader, options),
"node" => JsonSerializer.Deserialize<NodeRemoteValue>(ref reader, options),
"window" => JsonSerializer.Deserialize<WindowProxyRemoteValue>(ref reader, options),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// <copyright file="JsonExtensions.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System.Text.Json;

namespace OpenQA.Selenium.BiDi.Communication.Json.Internal;

internal static class JsonExtensions
{
public static string GetDiscriminator(this ref Utf8JsonReader reader, string name)
{
Utf8JsonReader readerClone = reader;

if (readerClone.TokenType != JsonTokenType.StartObject)
throw new JsonException($"Cannot determine the discriminator of {readerClone.TokenType} token type while supported is {JsonTokenType.StartObject} only.");

string? discriminator = null;

readerClone.Read();
while (readerClone.TokenType == JsonTokenType.PropertyName)
{
string? propertyName = readerClone.GetString();
readerClone.Read();

if (propertyName == name)
{
discriminator = readerClone.GetString();
break;
}

readerClone.Skip();
readerClone.Read();
}

return discriminator ?? throw new JsonException($"Couldn't determine '{name}' descriminator.");
}
}