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,34 @@ 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 ;
60+ this . SessionId = sessionId ;
6261 if ( parameters != null )
6362 {
64- this . commandParameters = parameters ;
63+ this . Parameters = parameters ;
6564 }
6665
67- this . commandName = name ;
66+ this . Name = name ;
6867 }
6968
7069 /// <summary>
7170 /// Gets the SessionID of the command
7271 /// </summary>
7372 [ JsonPropertyName ( "sessionId" ) ]
74- public SessionId SessionId
75- {
76- get { return this . commandSessionId ; }
77- }
73+ public SessionId ? SessionId { get ; }
7874
7975 /// <summary>
8076 /// Gets the command name
8177 /// </summary>
8278 [ JsonPropertyName ( "name" ) ]
83- public string Name
84- {
85- get { return this . commandName ; }
86- }
79+ public string Name { get ; }
8780
8881 /// <summary>
8982 /// Gets the parameters of the command
9083 /// </summary>
9184 [ JsonPropertyName ( "parameters" ) ]
92- public Dictionary < string , object > Parameters
93- {
94- get { return this . commandParameters ; }
95- }
85+ public Dictionary < string , object > Parameters { get ; } = new Dictionary < string , object > ( ) ;
9686
9787 /// <summary>
9888 /// Gets the parameters of the command as a JSON-encoded string.
@@ -101,13 +91,12 @@ public string ParametersAsJsonString
10191 {
10292 get
10393 {
104- string parametersString = string . Empty ;
105- if ( this . commandParameters != null && this . commandParameters . Count > 0 )
94+ string parametersString ;
95+ if ( this . Parameters != null && this . Parameters . Count > 0 )
10696 {
107- parametersString = JsonSerializer . Serialize ( this . commandParameters , s_jsonSerializerOptions ) ;
97+ parametersString = JsonSerializer . Serialize ( this . Parameters , s_jsonSerializerOptions ) ;
10898 }
109-
110- if ( string . IsNullOrEmpty ( parametersString ) )
99+ else
111100 {
112101 parametersString = "{}" ;
113102 }
@@ -130,9 +119,11 @@ public override string ToString()
130119 /// </summary>
131120 /// <param name="value">The JSON-encoded string representing the command parameters.</param>
132121 /// <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 )
122+ /// <exception cref="JsonException">If <paramref name="value"/> is not a JSON object.</exception>
123+ /// <exception cref="ArgumentNullException">If <paramref name="value"/> is <see langword="null"/>.</exception>
124+ private static Dictionary < string , object > ? ConvertParametersFromJson ( string value )
134125 {
135- Dictionary < string , object > parameters = JsonSerializer . Deserialize < Dictionary < string , object > > ( value , s_jsonSerializerOptions ) ;
126+ Dictionary < string , object > ? parameters = JsonSerializer . Deserialize < Dictionary < string , object > > ( value , s_jsonSerializerOptions ) ;
136127 return parameters ;
137128 }
138129 }
0 commit comments