diff --git a/dotnet/src/webdriver/Response.cs b/dotnet/src/webdriver/Response.cs index 0af9b181779b3..abe4bb2a9fcdd 100644 --- a/dotnet/src/webdriver/Response.cs +++ b/dotnet/src/webdriver/Response.cs @@ -56,19 +56,29 @@ public Response(SessionId sessionId) } } - private Response(Dictionary rawResponse) + /// + /// Returns a new from a JSON-encoded string. + /// + /// The JSON string to deserialize into a . + /// A object described by the JSON string. + public static Response FromJson(string value) { + Dictionary rawResponse = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions) + ?? throw new WebDriverException("JSON success response returned \"null\" value"); + + var response = new Response(); + if (rawResponse.ContainsKey("sessionId")) { if (rawResponse["sessionId"] != null) { - this.SessionId = rawResponse["sessionId"].ToString(); + response.SessionId = rawResponse["sessionId"].ToString(); } } - if (rawResponse.TryGetValue("value", out object value)) + if (rawResponse.TryGetValue("value", out object valueObj)) { - this.Value = value; + response.Value = valueObj; } // If the returned object does *not* have a "value" property @@ -76,37 +86,39 @@ private Response(Dictionary rawResponse) // TODO: Remove this if statement altogether; there should // never be a spec-compliant response that does not contain a // value property. - if (!rawResponse.ContainsKey("value") && this.Value == null) + if (!rawResponse.ContainsKey("value") && response.Value == null) { // Special-case for the new session command, where the "capabilities" // property of the response is the actual value we're interested in. if (rawResponse.ContainsKey("capabilities")) { - this.Value = rawResponse["capabilities"]; + response.Value = rawResponse["capabilities"]; } else { - this.Value = rawResponse; + response.Value = rawResponse; } } - if (this.Value is Dictionary valueDictionary) + if (response.Value is Dictionary valueDictionary) { // Special case code for the new session command. If the response contains // sessionId and capabilities properties, fix up the session ID and value members. if (valueDictionary.ContainsKey("sessionId")) { - this.SessionId = valueDictionary["sessionId"].ToString(); + response.SessionId = valueDictionary["sessionId"].ToString(); if (valueDictionary.TryGetValue("capabilities", out object capabilities)) { - this.Value = capabilities; + response.Value = capabilities; } else { - this.Value = valueDictionary["value"]; + response.Value = valueDictionary["value"]; } } } + + return response; } /// @@ -124,18 +136,6 @@ private Response(Dictionary rawResponse) /// public WebDriverResult Status { get; set; } - /// - /// Returns a new from a JSON-encoded string. - /// - /// The JSON string to deserialize into a . - /// A object described by the JSON string. - public static Response FromJson(string value) - { - Dictionary deserializedResponse = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions) - ?? throw new WebDriverException("JSON success response returned \"null\" value"); - - return new Response(deserializedResponse); - } /// /// Returns a new from a JSON-encoded string.