Skip to content

Commit

Permalink
Merge pull request #41 from shiftkey/testing-uri-parsing
Browse files Browse the repository at this point in the history
Bugfix - Uris are being deserialized like strings
  • Loading branch information
prabirshrestha committed Nov 4, 2013
2 parents 59ebd6b + 10425d4 commit 77ec61f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/SimpleJson.Tests/DeserializeObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,26 @@ public void DeserializeDoubleQuotesCorrectly()
Assert.AreEqual("Hi \"Prabir\"", result["message"]);
}

class ClassWithUri { public Uri url { get; set; } }

[TestMethod]
public void DeserializeUriCorrectly()
{
var json = "{\"url\":\"https://github.com/shiftkey/simple-json/issues/1\"}";
var result = SimpleJson.DeserializeObject<ClassWithUri>(json);

Assert.AreEqual(new Uri("https://github.com/shiftkey/simple-json/issues/1"), result.url);
}

[TestMethod]
public void DeserializeInvalidUriCorrectly()
{
var json = "{\"url\":\"this is a broken uri\"}";
var result = SimpleJson.DeserializeObject<ClassWithUri>(json);

Assert.IsNull(result.url);
}

[TestMethod]
public void DeserializeUnknownProperty()
{
Expand Down
8 changes: 8 additions & 0 deletions src/SimpleJson/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,14 @@ public virtual object DeserializeObject(object value, Type type)
return DateTimeOffset.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
if (type == typeof(Guid) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)))
return new Guid(str);
if (type == typeof(Uri))
{
bool isValid = Uri.IsWellFormedUriString(str, UriKind.RelativeOrAbsolute);

Uri result;
if (isValid && Uri.TryCreate(str, UriKind.RelativeOrAbsolute, out result))
return result;
}
return str;
}
else
Expand Down

0 comments on commit 77ec61f

Please sign in to comment.