From 1e8b1dcfc28b55afd38641f2ce29c54b6d83fada Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Mon, 7 Oct 2024 23:56:14 +0300 Subject: [PATCH 01/24] Win? --- dotnet/src/webdriver/Command.cs | 17 ++++++++++++++++- dotnet/src/webdriver/Response.cs | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index d12274ca90d7a..62f651d620cca 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -101,7 +101,7 @@ public string ParametersAsJsonString string parametersString = string.Empty; if (this.commandParameters != null && this.commandParameters.Count > 0) { - parametersString = JsonSerializer.Serialize(this.commandParameters); + parametersString = JsonSerializer.Serialize(new SerializableCommand() { Data = this.commandParameters }, CommandSerializerContext.Default.SerializableCommand); } if (string.IsNullOrEmpty(parametersString)) @@ -133,4 +133,19 @@ private static Dictionary ConvertParametersFromJson(string value return parameters; } } + + internal class SerializableCommand + { + [JsonExtensionData] + public Dictionary Data { get; set; } + } + + [JsonSerializable(typeof(SerializableCommand))] + + [JsonSerializable(typeof(IList))] + [JsonSerializable(typeof(System.Collections.ObjectModel.ReadOnlyCollection))] + internal partial class CommandSerializerContext : JsonSerializerContext + { + + } } diff --git a/dotnet/src/webdriver/Response.cs b/dotnet/src/webdriver/Response.cs index 674a6488135b4..96bfd73bb5b79 100644 --- a/dotnet/src/webdriver/Response.cs +++ b/dotnet/src/webdriver/Response.cs @@ -21,6 +21,7 @@ using System.Collections.Generic; using System.Globalization; using System.Text.Json; +using System.Text.Json.Serialization; namespace OpenQA.Selenium { @@ -31,7 +32,8 @@ public class Response { private readonly static JsonSerializerOptions s_jsonSerializerOptions = new() { - Converters = { new ResponseValueJsonConverter() } + TypeInfoResolver = ResponseSerializerContext.Default, + Converters = { new ResponseValueJsonConverter() } // we still need it to make `Object` as `Dictionary` }; private object responseValue; @@ -208,4 +210,16 @@ public override string ToString() return string.Format(CultureInfo.InvariantCulture, "({0} {1}: {2})", this.SessionId, this.Status, this.Value); } } + + internal class DeserializableResponse + { + [JsonExtensionData] + public Dictionary Data { get; set; } + } + + [JsonSerializable(typeof(DeserializableResponse))] + internal partial class ResponseSerializerContext : JsonSerializerContext + { + + } } From b927052dae33fdf88384c3589068204eb22fe757 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:28:37 +0300 Subject: [PATCH 02/24] int64? --- dotnet/src/webdriver/Response.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Response.cs b/dotnet/src/webdriver/Response.cs index 96bfd73bb5b79..fb71812c1be58 100644 --- a/dotnet/src/webdriver/Response.cs +++ b/dotnet/src/webdriver/Response.cs @@ -218,6 +218,7 @@ internal class DeserializableResponse } [JsonSerializable(typeof(DeserializableResponse))] + [JsonSerializable(typeof(long))] internal partial class ResponseSerializerContext : JsonSerializerContext { From a0767021e993a1839e9247a05efd75bc6e5b7360 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:50:32 +0300 Subject: [PATCH 03/24] Revert "int64?" This reverts commit b927052dae33fdf88384c3589068204eb22fe757. --- dotnet/src/webdriver/Response.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/dotnet/src/webdriver/Response.cs b/dotnet/src/webdriver/Response.cs index fb71812c1be58..96bfd73bb5b79 100644 --- a/dotnet/src/webdriver/Response.cs +++ b/dotnet/src/webdriver/Response.cs @@ -218,7 +218,6 @@ internal class DeserializableResponse } [JsonSerializable(typeof(DeserializableResponse))] - [JsonSerializable(typeof(long))] internal partial class ResponseSerializerContext : JsonSerializerContext { From 8e9a755112cca31515069bf1dccc6b9285d2e795 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:52:52 +0300 Subject: [PATCH 04/24] Long in command --- dotnet/src/webdriver/Command.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 62f651d620cca..1e88428f6d5f9 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -144,6 +144,8 @@ internal class SerializableCommand [JsonSerializable(typeof(IList))] [JsonSerializable(typeof(System.Collections.ObjectModel.ReadOnlyCollection))] + [JsonSerializable(typeof(long))] + [JsonSerializable(typeof(int))] internal partial class CommandSerializerContext : JsonSerializerContext { From 85c3989b7644c2baa5dab4fd834d591c9c8199cb Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 01:16:43 +0300 Subject: [PATCH 05/24] Boolean! --- dotnet/src/webdriver/Command.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 1e88428f6d5f9..8e645c848d1d3 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -144,6 +144,7 @@ internal class SerializableCommand [JsonSerializable(typeof(IList))] [JsonSerializable(typeof(System.Collections.ObjectModel.ReadOnlyCollection))] + [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(long))] [JsonSerializable(typeof(int))] internal partial class CommandSerializerContext : JsonSerializerContext From 5050151faf6d73469b449f1e2704034e339d2f99 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 01:36:32 +0300 Subject: [PATCH 06/24] char[] --- dotnet/src/webdriver/Command.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 8e645c848d1d3..f6cfacc8db111 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -147,6 +147,7 @@ internal class SerializableCommand [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(long))] [JsonSerializable(typeof(int))] + [JsonSerializable(typeof(char[]))] internal partial class CommandSerializerContext : JsonSerializerContext { From 61e45af405395c600204ad82c389cfea2fbfdb20 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 01:44:10 +0300 Subject: [PATCH 07/24] Double --- dotnet/src/webdriver/Command.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index f6cfacc8db111..eb41c38955c84 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -141,12 +141,14 @@ internal class SerializableCommand } [JsonSerializable(typeof(SerializableCommand))] + [JsonSerializable(typeof(Cookie))] [JsonSerializable(typeof(IList))] [JsonSerializable(typeof(System.Collections.ObjectModel.ReadOnlyCollection))] [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(long))] [JsonSerializable(typeof(int))] + [JsonSerializable(typeof(double))] [JsonSerializable(typeof(char[]))] internal partial class CommandSerializerContext : JsonSerializerContext { From 04168169fd2f7f020277b1358cc3bbf8c70826f5 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 01:53:47 +0300 Subject: [PATCH 08/24] Remove extra --- dotnet/src/webdriver/Command.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index eb41c38955c84..57e9a9dec9d51 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -16,7 +16,6 @@ // limitations under the License. // -using OpenQA.Selenium.Internal; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; @@ -28,11 +27,6 @@ namespace OpenQA.Selenium /// public class Command { - private readonly static JsonSerializerOptions s_jsonSerializerOptions = new() - { - Converters = { new ResponseValueJsonConverter() } - }; - private SessionId commandSessionId; private string commandName; private Dictionary commandParameters = new Dictionary(); @@ -129,7 +123,7 @@ public override string ToString() /// A with a string keys, and an object value. private static Dictionary ConvertParametersFromJson(string value) { - Dictionary parameters = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions); + Dictionary parameters = JsonSerializer.Deserialize(value, CommandSerializerContext.Default.SerializableCommand).Data; return parameters; } } From 8509830151ae2cf3d01c4be4efb2eb81c1a1bcde Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 02:05:52 +0300 Subject: [PATCH 09/24] uint --- dotnet/src/webdriver/Command.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 57e9a9dec9d51..9816a1458336b 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -142,6 +142,7 @@ internal class SerializableCommand [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(long))] [JsonSerializable(typeof(int))] + [JsonSerializable(typeof(uint)] [JsonSerializable(typeof(double))] [JsonSerializable(typeof(char[]))] internal partial class CommandSerializerContext : JsonSerializerContext From 1d781a8268bbdb4537727ec4f0d7cc61501ad445 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 02:09:34 +0300 Subject: [PATCH 10/24] float --- dotnet/src/webdriver/Command.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 9816a1458336b..b81f54c5a5a7c 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -142,7 +142,7 @@ internal class SerializableCommand [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(long))] [JsonSerializable(typeof(int))] - [JsonSerializable(typeof(uint)] + [JsonSerializable(typeof(float)] [JsonSerializable(typeof(double))] [JsonSerializable(typeof(char[]))] internal partial class CommandSerializerContext : JsonSerializerContext From 419e833cd2b3b71c2c4ffc91fa2de3cc2c9865cf Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 11:07:25 +0300 Subject: [PATCH 11/24] Update Command.cs --- dotnet/src/webdriver/Command.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index b81f54c5a5a7c..a1712acbab116 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -142,7 +142,7 @@ internal class SerializableCommand [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(long))] [JsonSerializable(typeof(int))] - [JsonSerializable(typeof(float)] + [JsonSerializable(typeof(float))] [JsonSerializable(typeof(double))] [JsonSerializable(typeof(char[]))] internal partial class CommandSerializerContext : JsonSerializerContext From 01b62a6697e864b33eb42a7499bb5183fddc679f Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 11:26:49 +0300 Subject: [PATCH 12/24] Add uint --- dotnet/src/webdriver/Command.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index a1712acbab116..164a022b90b2f 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -140,8 +140,9 @@ internal class SerializableCommand [JsonSerializable(typeof(IList))] [JsonSerializable(typeof(System.Collections.ObjectModel.ReadOnlyCollection))] [JsonSerializable(typeof(bool))] - [JsonSerializable(typeof(long))] + [JsonSerializable(typeof(uint))] [JsonSerializable(typeof(int))] + [JsonSerializable(typeof(long))] [JsonSerializable(typeof(float))] [JsonSerializable(typeof(double))] [JsonSerializable(typeof(char[]))] From 1b359e2ef89611ea0c32db476fba99683303c3b2 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 13:57:54 +0300 Subject: [PATCH 13/24] Make serialization generic --- dotnet/src/webdriver/Command.cs | 22 +-- .../Internal/ResponseValueJsonConverter.cs | 159 +++++++++++------- 2 files changed, 111 insertions(+), 70 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 164a022b90b2f..ffbf548a05c2e 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -16,6 +16,7 @@ // limitations under the License. // +using OpenQA.Selenium.Internal; using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; @@ -31,6 +32,12 @@ public class Command private string commandName; private Dictionary commandParameters = new Dictionary(); + private readonly static JsonSerializerOptions s_jsonSerializerOptions = new() + { + TypeInfoResolver = CommandSerializerContext.Default, + Converters = { new ResponseValueJsonConverter() } + }; + /// /// Initializes a new instance of the class using a command name and a JSON-encoded string for the parameters. /// @@ -95,7 +102,7 @@ public string ParametersAsJsonString string parametersString = string.Empty; if (this.commandParameters != null && this.commandParameters.Count > 0) { - parametersString = JsonSerializer.Serialize(new SerializableCommand() { Data = this.commandParameters }, CommandSerializerContext.Default.SerializableCommand); + parametersString = JsonSerializer.Serialize(new SerializableCommand() { Data = this.commandParameters }, s_jsonSerializerOptions); } if (string.IsNullOrEmpty(parametersString)) @@ -123,7 +130,7 @@ public override string ToString() /// A with a string keys, and an object value. private static Dictionary ConvertParametersFromJson(string value) { - Dictionary parameters = JsonSerializer.Deserialize(value, CommandSerializerContext.Default.SerializableCommand).Data; + Dictionary parameters = JsonSerializer.Deserialize(value, s_jsonSerializerOptions).Data; return parameters; } } @@ -135,17 +142,6 @@ internal class SerializableCommand } [JsonSerializable(typeof(SerializableCommand))] - [JsonSerializable(typeof(Cookie))] - - [JsonSerializable(typeof(IList))] - [JsonSerializable(typeof(System.Collections.ObjectModel.ReadOnlyCollection))] - [JsonSerializable(typeof(bool))] - [JsonSerializable(typeof(uint))] - [JsonSerializable(typeof(int))] - [JsonSerializable(typeof(long))] - [JsonSerializable(typeof(float))] - [JsonSerializable(typeof(double))] - [JsonSerializable(typeof(char[]))] internal partial class CommandSerializerContext : JsonSerializerContext { diff --git a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs index 9967d790c55c9..e547d18849e99 100644 --- a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs +++ b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs @@ -30,75 +30,120 @@ internal class ResponseValueJsonConverter : JsonConverter { public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - return this.ProcessToken(ref reader, options); + return ProcessReadToken(ref reader, options); } public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) { - JsonSerializer.Serialize(writer, value, options); + switch (value) + { + case null: + writer.WriteNullValue(); + break; + case string str: + writer.WriteStringValue(str); + break; + case bool b: + writer.WriteBooleanValue(b); + break; + case long l: + writer.WriteNumberValue(l); + break; + case double d: + writer.WriteNumberValue(d); + break; + case IEnumerable list: + writer.WriteStartArray(); + foreach (var item in list) + { + Write(writer, item, options); + } + writer.WriteEndArray(); + break; + case IDictionary dictionary: + writer.WriteStartObject(); + foreach (var pair in dictionary) + { + writer.WritePropertyName(pair.Key); + Write(writer, pair.Value, options); + } + writer.WriteEndObject(); + break; + case object obj: + writer.WriteStartObject(); + Write(writer, obj, options); + writer.WriteEndObject(); + break; + default: + throw new JsonException($"Unrecognized '{value.GetType()}' when serializing a command."); + } } - private object ProcessToken(ref Utf8JsonReader reader, JsonSerializerOptions options) + private static object ProcessReadToken(ref Utf8JsonReader reader, JsonSerializerOptions options) { // Recursively processes a token. This is required for elements that next other elements. - object processedObject = null; + object processedObject; - if (reader.TokenType == JsonTokenType.StartObject) + switch (reader.TokenType) { - Dictionary dictionaryValue = new Dictionary(); - while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) - { - string elementKey = reader.GetString(); - reader.Read(); - dictionaryValue.Add(elementKey, this.ProcessToken(ref reader, options)); - } + case JsonTokenType.StartObject: + { + Dictionary dictionaryValue = []; + while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) + { + string elementKey = reader.GetString(); + reader.Read(); + dictionaryValue.Add(elementKey, ProcessReadToken(ref reader, options)); + } - processedObject = dictionaryValue; - } - else if (reader.TokenType == JsonTokenType.StartArray) - { - List arrayValue = new List(); - while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) - { - arrayValue.Add(this.ProcessToken(ref reader, options)); - } + processedObject = dictionaryValue; + break; + } - processedObject = arrayValue.ToArray(); - } - else if (reader.TokenType == JsonTokenType.Null) - { - processedObject = null; - } - else if (reader.TokenType == JsonTokenType.False) - { - processedObject = false; - } - else if (reader.TokenType == JsonTokenType.True) - { - processedObject = true; - } - else if (reader.TokenType == JsonTokenType.String) - { - processedObject = reader.GetString(); - } - else if (reader.TokenType == JsonTokenType.Number) - { - if (reader.TryGetInt64(out long longValue)) - { - processedObject = longValue; - } - else if (reader.TryGetDouble(out double doubleValue)) - { - processedObject = doubleValue; - } - else - { - throw new JsonException($"Unrecognized '{JsonElement.ParseValue(ref reader)}' token as a number value."); - } - } - else - { - throw new JsonException($"Unrecognized '{reader.TokenType}' token type while parsing command response."); + case JsonTokenType.StartArray: + { + List arrayValue = []; + while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) + { + arrayValue.Add(ProcessReadToken(ref reader, options)); + } + + processedObject = arrayValue.ToArray(); + break; + } + + case JsonTokenType.Null: + processedObject = null; + break; + case JsonTokenType.False: + processedObject = false; + break; + case JsonTokenType.True: + processedObject = true; + break; + case JsonTokenType.String: + processedObject = reader.GetString(); + break; + case JsonTokenType.Number: + { + if (reader.TryGetInt64(out long longValue)) + { + processedObject = longValue; + } + else if (reader.TryGetDouble(out double doubleValue)) + { + processedObject = doubleValue; + } + else + { + throw new JsonException($"Unrecognized '{JsonElement.ParseValue(ref reader)}' token as a number value."); + } + + break; + } + + default: + throw new JsonException($"Unrecognized '{reader.TokenType}' token type while parsing command response."); } return processedObject; From 51ae258a3f2b2c8048529aa2057aeb502edae178 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:05:24 +0300 Subject: [PATCH 14/24] Interesting --- dotnet/src/webdriver/Command.cs | 9 +++++++++ .../Internal/ResponseValueJsonConverter.cs | 18 +----------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index ffbf548a05c2e..92cee93594077 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -142,6 +142,15 @@ internal class SerializableCommand } [JsonSerializable(typeof(SerializableCommand))] + [JsonSerializable(typeof(Cookie))] + [JsonSerializable(typeof(char[]))] + [JsonSerializable(typeof(bool))] + [JsonSerializable(typeof(string))] + [JsonSerializable(typeof(uint))] + [JsonSerializable(typeof(int))] + [JsonSerializable(typeof(long))] + [JsonSerializable(typeof(float))] + [JsonSerializable(typeof(double))] internal partial class CommandSerializerContext : JsonSerializerContext { diff --git a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs index e547d18849e99..c820adff6b32e 100644 --- a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs +++ b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs @@ -40,18 +40,6 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp case null: writer.WriteNullValue(); break; - case string str: - writer.WriteStringValue(str); - break; - case bool b: - writer.WriteBooleanValue(b); - break; - case long l: - writer.WriteNumberValue(l); - break; - case double d: - writer.WriteNumberValue(d); - break; case IEnumerable list: writer.WriteStartArray(); foreach (var item in list) @@ -70,12 +58,8 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp writer.WriteEndObject(); break; case object obj: - writer.WriteStartObject(); - Write(writer, obj, options); - writer.WriteEndObject(); + JsonSerializer.Serialize(writer, obj, options.GetTypeInfo(obj.GetType())); break; - default: - throw new JsonException($"Unrecognized '{value.GetType()}' when serializing a command."); } } From 38ad419ac0dd8049f09ddade6c0355d3bff760c5 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:50:49 +0300 Subject: [PATCH 15/24] Remove extra command --- dotnet/src/webdriver/Command.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 92cee93594077..d86832c763f1d 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -102,7 +102,7 @@ public string ParametersAsJsonString string parametersString = string.Empty; if (this.commandParameters != null && this.commandParameters.Count > 0) { - parametersString = JsonSerializer.Serialize(new SerializableCommand() { Data = this.commandParameters }, s_jsonSerializerOptions); + parametersString = JsonSerializer.Serialize(this.commandParameters, s_jsonSerializerOptions); } if (string.IsNullOrEmpty(parametersString)) @@ -130,19 +130,12 @@ public override string ToString() /// A with a string keys, and an object value. private static Dictionary ConvertParametersFromJson(string value) { - Dictionary parameters = JsonSerializer.Deserialize(value, s_jsonSerializerOptions).Data; + Dictionary parameters = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions); return parameters; } } - internal class SerializableCommand - { - [JsonExtensionData] - public Dictionary Data { get; set; } - } - - [JsonSerializable(typeof(SerializableCommand))] - [JsonSerializable(typeof(Cookie))] + [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(char[]))] [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(string))] @@ -151,6 +144,7 @@ internal class SerializableCommand [JsonSerializable(typeof(long))] [JsonSerializable(typeof(float))] [JsonSerializable(typeof(double))] + [JsonSerializable(typeof(Cookie))] internal partial class CommandSerializerContext : JsonSerializerContext { From 658d1d5ca81d1e44552820d86f9eab656a1c0ce4 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:13:59 +0300 Subject: [PATCH 16/24] Remove extra response --- dotnet/src/webdriver/Response.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/dotnet/src/webdriver/Response.cs b/dotnet/src/webdriver/Response.cs index 96bfd73bb5b79..d5f6ae784ce87 100644 --- a/dotnet/src/webdriver/Response.cs +++ b/dotnet/src/webdriver/Response.cs @@ -211,13 +211,7 @@ public override string ToString() } } - internal class DeserializableResponse - { - [JsonExtensionData] - public Dictionary Data { get; set; } - } - - [JsonSerializable(typeof(DeserializableResponse))] + [JsonSerializable(typeof(Dictionary))] internal partial class ResponseSerializerContext : JsonSerializerContext { From f26429bfe5558e2b34d5893194f229920b2a40bb Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:32:41 +0300 Subject: [PATCH 17/24] Add Proxy type as serializable --- dotnet/src/webdriver/Command.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index d86832c763f1d..a66aeeeca7f22 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -145,6 +145,7 @@ private static Dictionary ConvertParametersFromJson(string value [JsonSerializable(typeof(float))] [JsonSerializable(typeof(double))] [JsonSerializable(typeof(Cookie))] + [JsonSerializable(typeof(Proxy))] internal partial class CommandSerializerContext : JsonSerializerContext { From 3d9763b9888a84df0e86b2fe9fe6b1ecdacab210 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:50:16 +0300 Subject: [PATCH 18/24] ulong --- dotnet/src/webdriver/Command.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index a66aeeeca7f22..ef953781d8b3c 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -141,6 +141,7 @@ private static Dictionary ConvertParametersFromJson(string value [JsonSerializable(typeof(string))] [JsonSerializable(typeof(uint))] [JsonSerializable(typeof(int))] + [JsonSerializable(typeof(ulong))] [JsonSerializable(typeof(long))] [JsonSerializable(typeof(float))] [JsonSerializable(typeof(double))] From 8d4506845ff53e5455bf7108d70f662e32d73698 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:50:53 +0300 Subject: [PATCH 19/24] decimal --- dotnet/src/webdriver/Command.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index ef953781d8b3c..3679f534beed0 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -145,6 +145,7 @@ private static Dictionary ConvertParametersFromJson(string value [JsonSerializable(typeof(long))] [JsonSerializable(typeof(float))] [JsonSerializable(typeof(double))] + [JsonSerializable(typeof(decimal))] [JsonSerializable(typeof(Cookie))] [JsonSerializable(typeof(Proxy))] internal partial class CommandSerializerContext : JsonSerializerContext From 5170bc64cf9415d007103dc1629da2e17cc91c32 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 20 Oct 2024 00:28:26 +0300 Subject: [PATCH 20/24] Add all built-in types --- dotnet/src/webdriver/Command.cs | 31 +++++++++++++++++++++---------- dotnet/src/webdriver/Response.cs | 4 ++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 3679f534beed0..236eaa5ebbd15 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -34,7 +34,7 @@ public class Command private readonly static JsonSerializerOptions s_jsonSerializerOptions = new() { - TypeInfoResolver = CommandSerializerContext.Default, + TypeInfoResolver = CommandJsonSerializerContext.Default, Converters = { new ResponseValueJsonConverter() } }; @@ -135,20 +135,31 @@ private static Dictionary ConvertParametersFromJson(string value } } - [JsonSerializable(typeof(Dictionary))] - [JsonSerializable(typeof(char[]))] + // Built-in types [JsonSerializable(typeof(bool))] - [JsonSerializable(typeof(string))] - [JsonSerializable(typeof(uint))] + [JsonSerializable(typeof(byte))] + [JsonSerializable(typeof(sbyte))] + [JsonSerializable(typeof(char))] + [JsonSerializable(typeof(decimal))] + [JsonSerializable(typeof(double))] + [JsonSerializable(typeof(float))] [JsonSerializable(typeof(int))] - [JsonSerializable(typeof(ulong))] + [JsonSerializable(typeof(uint))] + [JsonSerializable(typeof(nint))] + [JsonSerializable(typeof(nuint))] [JsonSerializable(typeof(long))] - [JsonSerializable(typeof(float))] - [JsonSerializable(typeof(double))] - [JsonSerializable(typeof(decimal))] + [JsonSerializable(typeof(ulong))] + [JsonSerializable(typeof(short))] + [JsonSerializable(typeof(ushort))] + + [JsonSerializable(typeof(string))] + + // Selenium WebDriver types + [JsonSerializable(typeof(char[]))] + [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(Cookie))] [JsonSerializable(typeof(Proxy))] - internal partial class CommandSerializerContext : JsonSerializerContext + internal partial class CommandJsonSerializerContext : JsonSerializerContext { } diff --git a/dotnet/src/webdriver/Response.cs b/dotnet/src/webdriver/Response.cs index d5f6ae784ce87..ebfd47e2cfa1b 100644 --- a/dotnet/src/webdriver/Response.cs +++ b/dotnet/src/webdriver/Response.cs @@ -32,7 +32,7 @@ public class Response { private readonly static JsonSerializerOptions s_jsonSerializerOptions = new() { - TypeInfoResolver = ResponseSerializerContext.Default, + TypeInfoResolver = ResponseJsonSerializerContext.Default, Converters = { new ResponseValueJsonConverter() } // we still need it to make `Object` as `Dictionary` }; @@ -212,7 +212,7 @@ public override string ToString() } [JsonSerializable(typeof(Dictionary))] - internal partial class ResponseSerializerContext : JsonSerializerContext + internal partial class ResponseJsonSerializerContext : JsonSerializerContext { } From abdc1af83d06bb49fd4e911e5020d0aba7f481f4 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:06:53 +0300 Subject: [PATCH 21/24] byte[] --- dotnet/src/webdriver/Command.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/webdriver/Command.cs b/dotnet/src/webdriver/Command.cs index 236eaa5ebbd15..409c393860d6c 100644 --- a/dotnet/src/webdriver/Command.cs +++ b/dotnet/src/webdriver/Command.cs @@ -156,6 +156,7 @@ private static Dictionary ConvertParametersFromJson(string value // Selenium WebDriver types [JsonSerializable(typeof(char[]))] + [JsonSerializable(typeof(byte[]))] [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(Cookie))] [JsonSerializable(typeof(Proxy))] From 1d764018171dfe26e1b5e16ca98965a871740566 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:24:07 +0300 Subject: [PATCH 22/24] Serialize Enum as decimal --- dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs index c820adff6b32e..e193133867850 100644 --- a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs +++ b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs @@ -40,6 +40,9 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp case null: writer.WriteNullValue(); break; + case Enum: + writer.WriteNumberValue((decimal)value); + break; case IEnumerable list: writer.WriteStartArray(); foreach (var item in list) From 800a8dde4044f47fd0da90a006e668687cca92dc Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:30:33 +0300 Subject: [PATCH 23/24] Enum as long --- dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs index e193133867850..86bd03cb36f84 100644 --- a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs +++ b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs @@ -41,7 +41,7 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp writer.WriteNullValue(); break; case Enum: - writer.WriteNumberValue((decimal)value); + writer.WriteNumberValue((long)value); break; case IEnumerable list: writer.WriteStartArray(); From 358cba0749a3097958630d014f7f20c7e96bb8cf Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sat, 26 Oct 2024 23:00:59 +0300 Subject: [PATCH 24/24] Fix enum converter to long --- dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs index 86bd03cb36f84..6b888a788236b 100644 --- a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs +++ b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs @@ -41,7 +41,7 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp writer.WriteNullValue(); break; case Enum: - writer.WriteNumberValue((long)value); + writer.WriteNumberValue(Convert.ToInt64(value)); break; case IEnumerable list: writer.WriteStartArray();