Skip to content

Commit

Permalink
Refactoring JSON serialization of cookies in .NET
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Apr 8, 2014
1 parent 17799b3 commit e4e825d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 121 deletions.
30 changes: 29 additions & 1 deletion dotnet/src/webdriver/Cookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

using System;
using System.Globalization;
using Newtonsoft.Json;

namespace OpenQA.Selenium
{
/// <summary>
/// Represents a cookie in the browser.
/// </summary>
[Serializable]
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class Cookie
{
private string cookieName;
Expand Down Expand Up @@ -128,6 +130,7 @@ public Cookie(string name, string value)
/// <summary>
/// Gets the name of the cookie.
/// </summary>
[JsonProperty("name")]
public string Name
{
get { return this.cookieName; }
Expand All @@ -136,6 +139,7 @@ public string Name
/// <summary>
/// Gets the value of the cookie.
/// </summary>
[JsonProperty("value")]
public string Value
{
get { return this.cookieValue; }
Expand All @@ -144,14 +148,16 @@ public string Value
/// <summary>
/// Gets the domain of the cookie.
/// </summary>
[JsonProperty("domain")]
public string Domain
{
get { return this.cookieDomain; }
get { return string.IsNullOrEmpty(this.cookieDomain) ? string.Empty : this.cookieDomain; }
}

/// <summary>
/// Gets the path of the cookie.
/// </summary>
[JsonProperty("path")]
public virtual string Path
{
get { return this.cookiePath; }
Expand All @@ -160,6 +166,7 @@ public virtual string Path
/// <summary>
/// Gets a value indicating whether the cookie is secure.
/// </summary>
[JsonProperty("secure")]
public virtual bool Secure
{
get { return false; }
Expand All @@ -173,6 +180,27 @@ public virtual bool Secure
get { return this.cookieExpiry; }
}

/// <summary>
/// Gets the cookie expiration date in seconds from the defined zero date (01 January 1970 00:00:00 UTC).
/// </summary>
/// <remarks>This property only exists so that the JSON serializer can serialize a
/// cookie without resorting to a custom converter.</remarks>
[JsonProperty("expiry", NullValueHandling = NullValueHandling.Ignore)]
internal long? ExpirySeconds
{
get
{
if (this.cookieExpiry == null)
{
return null;
}

DateTime zeroDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = this.cookieExpiry.Value.ToUniversalTime().Subtract(zeroDate);
return Convert.ToInt64(span.TotalSeconds);
}
}

/// <summary>
/// Creates and returns a string representation of the cookie.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Remote/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public string ParametersAsJsonString
string parametersString = string.Empty;
if (this.commandParameters != null && this.commandParameters.Count > 0)
{
parametersString = JsonConvert.SerializeObject(this.commandParameters, new JsonConverter[] { new CookieJsonConverter(), new DesiredCapabilitiesJsonConverter() });
parametersString = JsonConvert.SerializeObject(this.commandParameters, new JsonConverter[] { new DesiredCapabilitiesJsonConverter() });
}

return parametersString;
Expand Down
116 changes: 0 additions & 116 deletions dotnet/src/webdriver/Remote/JsonConverters/CookieJsonConverter.cs

This file was deleted.

2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Remote/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static Response FromJson(string value)
/// <returns>A JSON-encoded string representing this <see cref="Response"/> object.</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, new CookieJsonConverter());
return JsonConvert.SerializeObject(this);
}

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions dotnet/src/webdriver/WebDriver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@
<Compile Include="PhantomJS\PhantomJSOptions.cs" />
<Compile Include="PhantomJS\PhantomJSWebElement.cs" />
<Compile Include="Remote\ICommandServer.cs" />
<Compile Include="Remote\JsonConverters\CharArrayJsonConverter.cs" />
<Compile Include="Remote\JsonConverters\CookieJsonConverter.cs" />
<Compile Include="Remote\JsonConverters\DesiredCapabilitiesJsonConverter.cs" />
<Compile Include="Remote\JsonConverters\PlatformJsonConverter.cs" />
<Compile Include="Remote\JsonConverters\ResponseValueJsonConverter.cs" />
Expand Down

0 comments on commit e4e825d

Please sign in to comment.