From 4c256e7afd46197a6e40127f77dff18e8f044b8c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 13 Dec 2015 18:56:10 +1300 Subject: [PATCH] -Fixed casting dynamic JValue to JToken --- .../Linq/DynamicTests.cs | 51 +++++++++++++++++++ Src/Newtonsoft.Json/IArrayPool.cs | 2 +- Src/Newtonsoft.Json/Linq/JValue.cs | 2 +- Src/Newtonsoft.Json/Utilities/ConvertUtils.cs | 2 + 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs b/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs index 36f11127..7d70012d 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs @@ -909,6 +909,57 @@ namespace Newtonsoft.Json.Tests.Linq ""StockValue"": 22050.00 }", json); } + + [Test] + public void DynamicAccess_ToJToken_ShouldNotFail() + { + Guid g = Guid.NewGuid(); + dynamic json = JObject.FromObject(new { uid = g }); + JToken token = json.uid; + + Assert.AreEqual(g, ((JValue)token).Value); + } + + [Test] + public void DynamicAccess_ToJTokenExplicit_ShouldNotFail() + { + Guid g = Guid.NewGuid(); + dynamic json = JObject.FromObject(new { uid = g }); + JToken token = (JToken)json.uid; + + Assert.AreEqual(g, ((JValue)token).Value); + } + + [Test] + public void DynamicAccess_ToJTokenSafeCast_ShouldNotFail() + { + Guid g = Guid.NewGuid(); + dynamic json = JObject.FromObject(new { uid = g }); + JToken token = json.uid as JToken; + + Assert.AreEqual(g, ((JValue)token).Value); + } + + [Test] + public void IndexAccess_ToJToken_ShouldNotFail() + { + Guid g = Guid.NewGuid(); + JObject json = JObject.FromObject(new { uid = g }); + JToken token = json["uid"]; + + Assert.AreEqual(g, ((JValue)token).Value); + } + + [Test] + public void DynamicAccess_ToJToken_ShouldFail() + { + Guid g = Guid.NewGuid(); + dynamic json = JObject.FromObject(new { uid = g }); + + ExceptionAssert.Throws( + () => { JObject token = json.uid; }, + "Can not convert from System.Guid to Newtonsoft.Json.Linq.JObject."); + } } public class DynamicDictionary : DynamicObject diff --git a/Src/Newtonsoft.Json/IArrayPool.cs b/Src/Newtonsoft.Json/IArrayPool.cs index 25e27bcb..3d5037e5 100644 --- a/Src/Newtonsoft.Json/IArrayPool.cs +++ b/Src/Newtonsoft.Json/IArrayPool.cs @@ -9,7 +9,7 @@ /// /// Rent a array from the pool. This array must be returned when it is no longer needed. /// - /// The minimum required length of the array. The returned array may be larger. + /// The minimum required length of the array. The returned array may be longer. /// The rented array from the pool. This array must be returned when it is no longer needed. T[] Rent(int minimumLength); diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index c2c6ccb8..0736e60f 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -985,7 +985,7 @@ namespace Newtonsoft.Json.Linq { public override bool TryConvert(JValue instance, ConvertBinder binder, out object result) { - if (binder.Type == typeof(JValue)) + if (binder.Type == typeof(JValue) || binder.Type == typeof(JToken)) { result = instance; return true; diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index 77f413ce..4749eb0b 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -931,7 +931,9 @@ namespace Newtonsoft.Json.Utilities // GUID has to have format 00000000-0000-0000-0000-000000000000 #if NET20 || NET35 if (s == null) + { throw new ArgumentNullException("s"); + } Regex format = new Regex("^[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}$"); Match match = format.Match(s);