Skip to content

Commit

Permalink
Removing attribute-based JSON deserialization for .NET responses
Browse files Browse the repository at this point in the history
This allows the Response object to be flexible enough to handle both the
open-source and W3C dialects of the JSON wire protocol.
  • Loading branch information
jimevans committed Sep 21, 2015
1 parent e790265 commit 00c9b0a
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions dotnet/src/webdriver/Remote/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

using System.Globalization;
using Newtonsoft.Json;
using System.Collections.Generic;
using System;

namespace OpenQA.Selenium.Remote
{
Expand Down Expand Up @@ -49,11 +51,27 @@ public Response(SessionId sessionId)
}
}

private Response(Dictionary<string, object> rawResponse)
{
if (rawResponse.ContainsKey("sessionId"))
{
this.responseSessionId = rawResponse["sessionId"].ToString();
}

if (rawResponse.ContainsKey("status"))
{
this.responseStatus = (WebDriverResult)Convert.ToInt32(rawResponse["status"]);
}

if (rawResponse.ContainsKey("value"))
{
this.responseValue = rawResponse["value"];
}
}

/// <summary>
/// Gets or sets the value from JSON.
/// </summary>
[JsonConverter(typeof(ResponseValueJsonConverter))]
[JsonProperty("value")]
public object Value
{
get { return this.responseValue; }
Expand All @@ -63,7 +81,6 @@ public object Value
/// <summary>
/// Gets or sets the session ID.
/// </summary>
[JsonProperty("sessionId")]
public string SessionId
{
get { return this.responseSessionId; }
Expand All @@ -73,7 +90,6 @@ public string SessionId
/// <summary>
/// Gets or sets the status value of the response.
/// </summary>
[JsonProperty("status")]
public WebDriverResult Status
{
get { return this.responseStatus; }
Expand All @@ -87,9 +103,9 @@ public WebDriverResult Status
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
public static Response FromJson(string value)
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.None;
return JsonConvert.DeserializeObject<Response>(value, settings);
Dictionary<string, object> deserializedResponse = JsonConvert.DeserializeObject<Dictionary<string, object>>(value, new ResponseValueJsonConverter());
Response response = new Response(deserializedResponse);
return response;
}

/// <summary>
Expand Down

0 comments on commit 00c9b0a

Please sign in to comment.