MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / FromJson

Method FromJson

dotnet/src/webdriver/Response.cs:54–112  ·  view source on GitHub ↗

Returns a new from a JSON-encoded string. The JSON string to deserialize into a . A object described by the JSON string. If is . If <

(string value)

Source from the content-addressed store, hash-verified

52 /// <exception cref="ArgumentNullException">If <paramref name="value"/> is <see langword="null"/>.</exception>
53 /// <exception cref="JsonException">If <paramref name="value"/> is not a valid JSON object.</exception>
54 public static Response FromJson(string value)
55 {
56 JsonObject rawResponse = JsonNode.Parse(value) as JsonObject
57 ?? throw new WebDriverException($"JSON success response did not return a dictionary{Environment.NewLine}{value}");
58
59 JsonNode? contents;
60 string? sessionId = null;
61
62 if (rawResponse.TryGetPropertyValue("sessionId", out JsonNode? s) && s is not null)
63 {
64 sessionId = s.ToString();
65 }
66
67 if (rawResponse.TryGetPropertyValue("value", out JsonNode? valueObj))
68 {
69 contents = valueObj;
70 }
71 else
72 {
73 // If the returned object does *not* have a "value" property
74 // the response value should be the entirety of the response.
75 // TODO: Remove this if statement altogether; there should
76 // never be a spec-compliant response that does not contain a
77 // value property.
78
79 // Special-case for the new session command, where the "capabilities"
80 // property of the response is the actual value we're interested in.
81 if (rawResponse.TryGetPropertyValue("capabilities", out JsonNode? capabilities))
82 {
83 contents = capabilities;
84 }
85 else
86 {
87 contents = rawResponse;
88 }
89 }
90
91 if (contents is JsonObject valueDictionary)
92 {
93 // Special case code for the new session command. If the response contains
94 // sessionId and capabilities properties, fix up the session ID and value members.
95 if (valueDictionary.TryGetPropertyValue("sessionId", out JsonNode? session))
96 {
97 sessionId = session?.ToString();
98 if (valueDictionary.TryGetPropertyValue("capabilities", out JsonNode? capabilities))
99 {
100 contents = capabilities;
101 }
102 else
103 {
104 contents = valueDictionary["value"];
105 }
106 }
107 }
108
109 var contentsDictionary = JsonSerializer.Deserialize(contents, ResponseJsonSerializerContext.Default.Object);
110
111 return new Response(sessionId, contentsDictionary, WebDriverResult.Success);

Callers 1

CreateResponseMethod · 0.45

Calls 2

ToStringMethod · 0.45
DeserializeMethod · 0.45

Tested by

no test coverage detected