From e4e825d06584c158bd785a7b0f1d6cb468f0df24 Mon Sep 17 00:00:00 2001 From: Jim Evans Date: Tue, 8 Apr 2014 18:57:51 -0400 Subject: [PATCH] Refactoring JSON serialization of cookies in .NET --- dotnet/src/webdriver/Cookie.cs | 30 ++++- dotnet/src/webdriver/Remote/Command.cs | 2 +- .../JsonConverters/CookieJsonConverter.cs | 116 ------------------ dotnet/src/webdriver/Remote/Response.cs | 2 +- dotnet/src/webdriver/WebDriver.csproj | 2 - 5 files changed, 31 insertions(+), 121 deletions(-) delete mode 100644 dotnet/src/webdriver/Remote/JsonConverters/CookieJsonConverter.cs diff --git a/dotnet/src/webdriver/Cookie.cs b/dotnet/src/webdriver/Cookie.cs index bfa22db51ec12..55b3026bf1419 100644 --- a/dotnet/src/webdriver/Cookie.cs +++ b/dotnet/src/webdriver/Cookie.cs @@ -18,6 +18,7 @@ using System; using System.Globalization; +using Newtonsoft.Json; namespace OpenQA.Selenium { @@ -25,6 +26,7 @@ namespace OpenQA.Selenium /// Represents a cookie in the browser. /// [Serializable] + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] public class Cookie { private string cookieName; @@ -128,6 +130,7 @@ public Cookie(string name, string value) /// /// Gets the name of the cookie. /// + [JsonProperty("name")] public string Name { get { return this.cookieName; } @@ -136,6 +139,7 @@ public string Name /// /// Gets the value of the cookie. /// + [JsonProperty("value")] public string Value { get { return this.cookieValue; } @@ -144,14 +148,16 @@ public string Value /// /// Gets the domain of the cookie. /// + [JsonProperty("domain")] public string Domain { - get { return this.cookieDomain; } + get { return string.IsNullOrEmpty(this.cookieDomain) ? string.Empty : this.cookieDomain; } } /// /// Gets the path of the cookie. /// + [JsonProperty("path")] public virtual string Path { get { return this.cookiePath; } @@ -160,6 +166,7 @@ public virtual string Path /// /// Gets a value indicating whether the cookie is secure. /// + [JsonProperty("secure")] public virtual bool Secure { get { return false; } @@ -173,6 +180,27 @@ public virtual bool Secure get { return this.cookieExpiry; } } + /// + /// Gets the cookie expiration date in seconds from the defined zero date (01 January 1970 00:00:00 UTC). + /// + /// This property only exists so that the JSON serializer can serialize a + /// cookie without resorting to a custom converter. + [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); + } + } + /// /// Creates and returns a string representation of the cookie. /// diff --git a/dotnet/src/webdriver/Remote/Command.cs b/dotnet/src/webdriver/Remote/Command.cs index 2ea6892410bfc..f9b5bd6b6c322 100644 --- a/dotnet/src/webdriver/Remote/Command.cs +++ b/dotnet/src/webdriver/Remote/Command.cs @@ -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; diff --git a/dotnet/src/webdriver/Remote/JsonConverters/CookieJsonConverter.cs b/dotnet/src/webdriver/Remote/JsonConverters/CookieJsonConverter.cs deleted file mode 100644 index 00a01a45bb437..0000000000000 --- a/dotnet/src/webdriver/Remote/JsonConverters/CookieJsonConverter.cs +++ /dev/null @@ -1,116 +0,0 @@ -// -// Copyright 2007-2011 WebDriver committers -// Copyright 2007-2011 Google Inc. -// Portions copyright 2011 Software Freedom Conservancy -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System; -using System.Globalization; -using Newtonsoft.Json; - -namespace OpenQA.Selenium.Remote -{ - /// - /// Provides a way to convert Cookies to JSON and back - /// - internal class CookieJsonConverter : JsonConverter - { - /// - /// Checks if the object can be converted - /// - /// Type of the object - /// A value indicating if it can be converted - public override bool CanConvert(Type objectType) - { - return objectType != null && objectType.IsAssignableFrom(typeof(Cookie)); - } - - /// - /// Get the platform from the JSON reader - /// - /// JSON Reader instance - /// Object type being read - /// The existing value of the object - /// JSON Serializer instance - /// Platform from JSON reader - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - Platform platformValue = null; - if (reader != null) - { - if (reader.TokenType == JsonToken.String) - { - PlatformType platformTypeValue = (PlatformType)Enum.Parse(objectType, reader.Value.ToString(), true); - platformValue = new Platform(platformTypeValue); - } - } - - return platformValue; - } - - /// - /// Created a cookie from the JSON string - /// - /// The JSON writer with a string - /// Value of the string - /// JSON serializer instance - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - if (writer != null) - { - Cookie cookieValue = value as Cookie; - if (cookieValue != null) - { - writer.WriteStartObject(); - writer.WritePropertyName("name"); - writer.WriteValue(cookieValue.Name); - writer.WritePropertyName("value"); - writer.WriteValue(cookieValue.Value); - writer.WritePropertyName("path"); - if (!string.IsNullOrEmpty(cookieValue.Path)) - { - writer.WriteValue(cookieValue.Path); - } - else - { - writer.WriteValue(string.Empty); - } - - writer.WritePropertyName("domain"); - if (!string.IsNullOrEmpty(cookieValue.Domain)) - { - writer.WriteValue(cookieValue.Domain); - } - else - { - writer.WriteValue(string.Empty); - } - - if (cookieValue.Expiry != null) - { - writer.WritePropertyName("expiry"); - DateTime zeroDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - TimeSpan span = cookieValue.Expiry.Value.ToUniversalTime().Subtract(zeroDate); - writer.WriteValue(Convert.ToInt64(span.TotalSeconds)); - } - - writer.WritePropertyName("secure"); - writer.WriteValue(cookieValue.Secure); - writer.WriteEndObject(); - } - } - } - } -} diff --git a/dotnet/src/webdriver/Remote/Response.cs b/dotnet/src/webdriver/Remote/Response.cs index 51681c57a85b2..46baee761617e 100644 --- a/dotnet/src/webdriver/Remote/Response.cs +++ b/dotnet/src/webdriver/Remote/Response.cs @@ -96,7 +96,7 @@ public static Response FromJson(string value) /// A JSON-encoded string representing this object. public string ToJson() { - return JsonConvert.SerializeObject(this, new CookieJsonConverter()); + return JsonConvert.SerializeObject(this); } /// diff --git a/dotnet/src/webdriver/WebDriver.csproj b/dotnet/src/webdriver/WebDriver.csproj index 48e077c7493b7..49206326720d7 100644 --- a/dotnet/src/webdriver/WebDriver.csproj +++ b/dotnet/src/webdriver/WebDriver.csproj @@ -107,8 +107,6 @@ - -