From 53abac4b59f77861ba48ed56140118bfe38f9af3 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Tue, 20 Nov 2012 17:25:08 +1300 Subject: [PATCH] -Tests --- Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs | 49 ++++++++++++++++++- Src/Newtonsoft.Json/Linq/JToken.cs | 13 +++-- Src/Newtonsoft.Json/Linq/JValue.cs | 2 +- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs index 2659b503..67f0bdda 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs @@ -306,6 +306,7 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(1m, (decimal)(new JValue(true))); Assert.AreEqual(TimeSpan.FromMinutes(1), (TimeSpan)(new JValue(TimeSpan.FromMinutes(1)))); Assert.AreEqual("00:01:00", (string)(new JValue(TimeSpan.FromMinutes(1)))); + Assert.AreEqual(TimeSpan.FromMinutes(1), (TimeSpan)(new JValue("00:01:00"))); Assert.AreEqual("46efe013-b56a-4e83-99e4-4dce7678a5bc", (string)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC")))); Assert.AreEqual("http://www.google.com/", (string)(new JValue(new Uri("http://www.google.com")))); Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid)(new JValue("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"))); @@ -345,6 +346,51 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(5, (int)(new JValue(StringComparison.OrdinalIgnoreCase))); } + [Test] + public void FailedCasting() + { + ExceptionAssert.Throws("Can not convert Boolean to DateTime.", () => { var i = (DateTime)new JValue(true); }); + ExceptionAssert.Throws("Can not convert Integer to DateTime.", () => { var i = (DateTime)new JValue(1); }); + ExceptionAssert.Throws("Can not convert Float to DateTime.", () => { var i = (DateTime)new JValue(1.1); }); + ExceptionAssert.Throws("Can not convert Float to DateTime.", () => { var i = (DateTime)new JValue(1.1m); }); + ExceptionAssert.Throws("Can not convert TimeSpan to DateTime.", () => { var i = (DateTime)new JValue(TimeSpan.Zero); }); + ExceptionAssert.Throws("Can not convert Uri to DateTime.", () => { var i = (DateTime)new JValue(new Uri("http://www.google.com")); }); + ExceptionAssert.Throws("Can not convert Null to DateTime.", () => { var i = (DateTime)new JValue((object)null); }); + ExceptionAssert.Throws("Can not convert Guid to DateTime.", () => { var i = (DateTime)new JValue(Guid.NewGuid()); }); + + ExceptionAssert.Throws("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); }); + ExceptionAssert.Throws("Can not convert Integer to Uri.", () => { var i = (Uri)new JValue(1); }); + ExceptionAssert.Throws("Can not convert Float to Uri.", () => { var i = (Uri)new JValue(1.1); }); + ExceptionAssert.Throws("Can not convert Float to Uri.", () => { var i = (Uri)new JValue(1.1m); }); + ExceptionAssert.Throws("Can not convert TimeSpan to Uri.", () => { var i = (Uri)new JValue(TimeSpan.Zero); }); + ExceptionAssert.Throws("Can not convert Guid to Uri.", () => { var i = (Uri)new JValue(Guid.NewGuid()); }); + ExceptionAssert.Throws("Can not convert Date to Uri.", () => { var i = (Uri)new JValue(DateTime.Now); }); + ExceptionAssert.Throws("Can not convert Date to Uri.", () => { var i = (Uri)new JValue(DateTimeOffset.Now); }); + + ExceptionAssert.Throws("Can not convert Boolean to TimeSpan.", () => { var i = (TimeSpan)new JValue(true); }); + ExceptionAssert.Throws("Can not convert Integer to TimeSpan.", () => { var i = (TimeSpan)new JValue(1); }); + ExceptionAssert.Throws("Can not convert Float to TimeSpan.", () => { var i = (TimeSpan)new JValue(1.1); }); + ExceptionAssert.Throws("Can not convert Float to TimeSpan.", () => { var i = (TimeSpan)new JValue(1.1m); }); + ExceptionAssert.Throws("Can not convert Null to TimeSpan.", () => { var i = (TimeSpan)new JValue((object)null); }); + ExceptionAssert.Throws("Can not convert Guid to TimeSpan.", () => { var i = (TimeSpan)new JValue(Guid.NewGuid()); }); + ExceptionAssert.Throws("Can not convert Date to TimeSpan.", () => { var i = (TimeSpan)new JValue(DateTime.Now); }); + ExceptionAssert.Throws("Can not convert Date to TimeSpan.", () => { var i = (TimeSpan)new JValue(DateTimeOffset.Now); }); + ExceptionAssert.Throws("Can not convert Uri to TimeSpan.", () => { var i = (TimeSpan)new JValue(new Uri("http://www.google.com")); }); + + ExceptionAssert.Throws("Can not convert Boolean to Guid.", () => { var i = (Guid)new JValue(true); }); + ExceptionAssert.Throws("Can not convert Integer to Guid.", () => { var i = (Guid)new JValue(1); }); + ExceptionAssert.Throws("Can not convert Float to Guid.", () => { var i = (Guid)new JValue(1.1); }); + ExceptionAssert.Throws("Can not convert Float to Guid.", () => { var i = (Guid)new JValue(1.1m); }); + ExceptionAssert.Throws("Can not convert Null to Guid.", () => { var i = (Guid)new JValue((object)null); }); + ExceptionAssert.Throws("Can not convert Date to Guid.", () => { var i = (Guid)new JValue(DateTime.Now); }); + ExceptionAssert.Throws("Can not convert Date to Guid.", () => { var i = (Guid)new JValue(DateTimeOffset.Now); }); + ExceptionAssert.Throws("Can not convert TimeSpan to Guid.", () => { var i = (Guid)new JValue(TimeSpan.FromMinutes(1)); }); + ExceptionAssert.Throws("Can not convert Uri to Guid.", () => { var i = (Guid)new JValue(new Uri("http://www.google.com")); }); + + ExceptionAssert.Throws("Can not convert Boolean to DateTimeOffset.", () => { var i = (DateTimeOffset)new JValue(true); }); + ExceptionAssert.Throws("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); }); + } + [Test] public void ToObject() { @@ -442,7 +488,8 @@ namespace Newtonsoft.Json.Tests.Linq Assert.IsTrue(JToken.DeepEquals(new JValue(TimeSpan.FromMinutes(1)), (JValue)(TimeSpan?)TimeSpan.FromMinutes(1))); Assert.IsTrue(JToken.DeepEquals(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC")), (JValue)new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"))); Assert.IsTrue(JToken.DeepEquals(new JValue(new Uri("http://www.google.com")), (JValue)new Uri("http://www.google.com"))); - + Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(Uri)null)); + Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(Guid?)null)); } [Test] diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 1579c0fc..e2bcd84d 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -64,6 +64,9 @@ namespace Newtonsoft.Json.Linq private static readonly JTokenType[] BooleanTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean }; private static readonly JTokenType[] NumberTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean }; private static readonly JTokenType[] StringTypes = new[] { JTokenType.Date, JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean, JTokenType.Bytes, JTokenType.Guid, JTokenType.TimeSpan, JTokenType.Uri }; + private static readonly JTokenType[] GuidTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Guid }; + private static readonly JTokenType[] TimeSpanTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.TimeSpan }; + private static readonly JTokenType[] UriTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Uri }; private static readonly JTokenType[] CharTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw }; private static readonly JTokenType[] DateTimeTypes = new[] { JTokenType.Date, JTokenType.String, JTokenType.Comment, JTokenType.Raw }; private static readonly JTokenType[] BytesTypes = new[] { JTokenType.Bytes, JTokenType.String, JTokenType.Comment, JTokenType.Raw }; @@ -895,7 +898,7 @@ namespace Newtonsoft.Json.Linq public static explicit operator Guid(JToken value) { JValue v = EnsureValue(value); - if (v == null || !ValidateToken(v, StringTypes, false)) + if (v == null || !ValidateToken(v, GuidTypes, false)) throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); @@ -912,7 +915,7 @@ namespace Newtonsoft.Json.Linq return null; JValue v = EnsureValue(value); - if (v == null || !ValidateToken(v, StringTypes, true)) + if (v == null || !ValidateToken(v, GuidTypes, true)) throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); if (v.Value == null) @@ -929,7 +932,7 @@ namespace Newtonsoft.Json.Linq public static explicit operator TimeSpan(JToken value) { JValue v = EnsureValue(value); - if (v == null || !ValidateToken(v, StringTypes, false)) + if (v == null || !ValidateToken(v, TimeSpanTypes, false)) throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); return (v.Value is TimeSpan) ? (TimeSpan)v.Value : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); @@ -946,7 +949,7 @@ namespace Newtonsoft.Json.Linq return null; JValue v = EnsureValue(value); - if (v == null || !ValidateToken(v, StringTypes, true)) + if (v == null || !ValidateToken(v, TimeSpanTypes, true)) throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); if (v.Value == null) @@ -966,7 +969,7 @@ namespace Newtonsoft.Json.Linq return null; JValue v = EnsureValue(value); - if (v == null || !ValidateToken(v, StringTypes, true)) + if (v == null || !ValidateToken(v, UriTypes, true)) throw new ArgumentException("Can not convert {0} to Uri.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); if (v.Value == null) diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index 49a52e30..0fb2f67e 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -144,7 +144,7 @@ namespace Newtonsoft.Json.Linq /// /// The value. public JValue(Uri value) - : this(value, JTokenType.Uri) + : this(value, (value != null) ? JTokenType.Uri : JTokenType.Null) { }