1818// </copyright>
1919
2020using OpenQA . Selenium . Internal ;
21+ using System ;
2122using System . Collections . Generic ;
2223using System . Text . Json ;
2324using System . Text . Json . Serialization ;
2425using System . Text . Json . Serialization . Metadata ;
2526
27+ #nullable enable
28+
2629namespace OpenQA . Selenium
2730{
2831 /// <summary>
2932 /// Provides a way to send commands to the remote server
3033 /// </summary>
3134 public class Command
3235 {
33- private SessionId commandSessionId ;
34- private string commandName ;
35- private Dictionary < string , object > commandParameters = new Dictionary < string , object > ( ) ;
36-
3736 private readonly static JsonSerializerOptions s_jsonSerializerOptions = new ( )
3837 {
3938 TypeInfoResolver = JsonTypeInfoResolver . Combine ( CommandJsonSerializerContext . Default , new DefaultJsonTypeInfoResolver ( ) ) ,
@@ -56,43 +55,30 @@ public Command(string name, string jsonParameters)
5655 /// <param name="sessionId">Session ID the driver is using</param>
5756 /// <param name="name">Name of the command</param>
5857 /// <param name="parameters">Parameters for that command</param>
59- public Command ( SessionId sessionId , string name , Dictionary < string , object > parameters )
58+ public Command ( SessionId ? sessionId , string name , Dictionary < string , object > ? parameters )
6059 {
61- this . commandSessionId = sessionId ;
62- if ( parameters != null )
63- {
64- this . commandParameters = parameters ;
65- }
66-
67- this . commandName = name ;
60+ this . SessionId = sessionId ;
61+ this . Parameters = parameters ?? new Dictionary < string , object > ( ) ;
62+ this . Name = name ;
6863 }
6964
7065 /// <summary>
7166 /// Gets the SessionID of the command
7267 /// </summary>
7368 [ JsonPropertyName ( "sessionId" ) ]
74- public SessionId SessionId
75- {
76- get { return this . commandSessionId ; }
77- }
69+ public SessionId ? SessionId { get ; }
7870
7971 /// <summary>
8072 /// Gets the command name
8173 /// </summary>
8274 [ JsonPropertyName ( "name" ) ]
83- public string Name
84- {
85- get { return this . commandName ; }
86- }
75+ public string Name { get ; }
8776
8877 /// <summary>
8978 /// Gets the parameters of the command
9079 /// </summary>
9180 [ JsonPropertyName ( "parameters" ) ]
92- public Dictionary < string , object > Parameters
93- {
94- get { return this . commandParameters ; }
95- }
81+ public Dictionary < string , object > Parameters { get ; }
9682
9783 /// <summary>
9884 /// Gets the parameters of the command as a JSON-encoded string.
@@ -101,13 +87,12 @@ public string ParametersAsJsonString
10187 {
10288 get
10389 {
104- string parametersString = string . Empty ;
105- if ( this . commandParameters != null && this . commandParameters . Count > 0 )
90+ string parametersString ;
91+ if ( this . Parameters != null && this . Parameters . Count > 0 )
10692 {
107- parametersString = JsonSerializer . Serialize ( this . commandParameters , s_jsonSerializerOptions ) ;
93+ parametersString = JsonSerializer . Serialize ( this . Parameters , s_jsonSerializerOptions ) ;
10894 }
109-
110- if ( string . IsNullOrEmpty ( parametersString ) )
95+ else
11196 {
11297 parametersString = "{}" ;
11398 }
@@ -130,9 +115,11 @@ public override string ToString()
130115 /// </summary>
131116 /// <param name="value">The JSON-encoded string representing the command parameters.</param>
132117 /// <returns>A <see cref="Dictionary{K, V}"/> with a string keys, and an object value. </returns>
133- private static Dictionary < string , object > ConvertParametersFromJson ( string value )
118+ /// <exception cref="JsonException">If <paramref name="value"/> is not a JSON object.</exception>
119+ /// <exception cref="ArgumentNullException">If <paramref name="value"/> is <see langword="null"/>.</exception>
120+ private static Dictionary < string , object > ? ConvertParametersFromJson ( string value )
134121 {
135- Dictionary < string , object > parameters = JsonSerializer . Deserialize < Dictionary < string , object > > ( value , s_jsonSerializerOptions ) ;
122+ Dictionary < string , object > ? parameters = JsonSerializer . Deserialize < Dictionary < string , object > > ( value , s_jsonSerializerOptions ) ;
136123 return parameters ;
137124 }
138125 }
0 commit comments