Skip to content

Commit e4e825d

Browse files
committed
Refactoring JSON serialization of cookies in .NET
1 parent 17799b3 commit e4e825d

File tree

5 files changed

+31
-121
lines changed

5 files changed

+31
-121
lines changed

Diff for: dotnet/src/webdriver/Cookie.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
using System;
2020
using System.Globalization;
21+
using Newtonsoft.Json;
2122

2223
namespace OpenQA.Selenium
2324
{
2425
/// <summary>
2526
/// Represents a cookie in the browser.
2627
/// </summary>
2728
[Serializable]
29+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
2830
public class Cookie
2931
{
3032
private string cookieName;
@@ -128,6 +130,7 @@ public Cookie(string name, string value)
128130
/// <summary>
129131
/// Gets the name of the cookie.
130132
/// </summary>
133+
[JsonProperty("name")]
131134
public string Name
132135
{
133136
get { return this.cookieName; }
@@ -136,6 +139,7 @@ public string Name
136139
/// <summary>
137140
/// Gets the value of the cookie.
138141
/// </summary>
142+
[JsonProperty("value")]
139143
public string Value
140144
{
141145
get { return this.cookieValue; }
@@ -144,14 +148,16 @@ public string Value
144148
/// <summary>
145149
/// Gets the domain of the cookie.
146150
/// </summary>
151+
[JsonProperty("domain")]
147152
public string Domain
148153
{
149-
get { return this.cookieDomain; }
154+
get { return string.IsNullOrEmpty(this.cookieDomain) ? string.Empty : this.cookieDomain; }
150155
}
151156

152157
/// <summary>
153158
/// Gets the path of the cookie.
154159
/// </summary>
160+
[JsonProperty("path")]
155161
public virtual string Path
156162
{
157163
get { return this.cookiePath; }
@@ -160,6 +166,7 @@ public virtual string Path
160166
/// <summary>
161167
/// Gets a value indicating whether the cookie is secure.
162168
/// </summary>
169+
[JsonProperty("secure")]
163170
public virtual bool Secure
164171
{
165172
get { return false; }
@@ -173,6 +180,27 @@ public DateTime? Expiry
173180
get { return this.cookieExpiry; }
174181
}
175182

183+
/// <summary>
184+
/// Gets the cookie expiration date in seconds from the defined zero date (01 January 1970 00:00:00 UTC).
185+
/// </summary>
186+
/// <remarks>This property only exists so that the JSON serializer can serialize a
187+
/// cookie without resorting to a custom converter.</remarks>
188+
[JsonProperty("expiry", NullValueHandling = NullValueHandling.Ignore)]
189+
internal long? ExpirySeconds
190+
{
191+
get
192+
{
193+
if (this.cookieExpiry == null)
194+
{
195+
return null;
196+
}
197+
198+
DateTime zeroDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
199+
TimeSpan span = this.cookieExpiry.Value.ToUniversalTime().Subtract(zeroDate);
200+
return Convert.ToInt64(span.TotalSeconds);
201+
}
202+
}
203+
176204
/// <summary>
177205
/// Creates and returns a string representation of the cookie.
178206
/// </summary>

Diff for: dotnet/src/webdriver/Remote/Command.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public string ParametersAsJsonString
9494
string parametersString = string.Empty;
9595
if (this.commandParameters != null && this.commandParameters.Count > 0)
9696
{
97-
parametersString = JsonConvert.SerializeObject(this.commandParameters, new JsonConverter[] { new CookieJsonConverter(), new DesiredCapabilitiesJsonConverter() });
97+
parametersString = JsonConvert.SerializeObject(this.commandParameters, new JsonConverter[] { new DesiredCapabilitiesJsonConverter() });
9898
}
9999

100100
return parametersString;

Diff for: dotnet/src/webdriver/Remote/JsonConverters/CookieJsonConverter.cs

-116
This file was deleted.

Diff for: dotnet/src/webdriver/Remote/Response.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static Response FromJson(string value)
9696
/// <returns>A JSON-encoded string representing this <see cref="Response"/> object.</returns>
9797
public string ToJson()
9898
{
99-
return JsonConvert.SerializeObject(this, new CookieJsonConverter());
99+
return JsonConvert.SerializeObject(this);
100100
}
101101

102102
/// <summary>

Diff for: dotnet/src/webdriver/WebDriver.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@
107107
<Compile Include="PhantomJS\PhantomJSOptions.cs" />
108108
<Compile Include="PhantomJS\PhantomJSWebElement.cs" />
109109
<Compile Include="Remote\ICommandServer.cs" />
110-
<Compile Include="Remote\JsonConverters\CharArrayJsonConverter.cs" />
111-
<Compile Include="Remote\JsonConverters\CookieJsonConverter.cs" />
112110
<Compile Include="Remote\JsonConverters\DesiredCapabilitiesJsonConverter.cs" />
113111
<Compile Include="Remote\JsonConverters\PlatformJsonConverter.cs" />
114112
<Compile Include="Remote\JsonConverters\ResponseValueJsonConverter.cs" />

0 commit comments

Comments
 (0)