-Fixed casting dynamic JValue to JToken

This commit is contained in:
James Newton-King
2015-12-13 18:56:10 +13:00
parent 815b283e58
commit 4c256e7afd
4 changed files with 55 additions and 2 deletions
@@ -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<InvalidOperationException>(
() => { JObject token = json.uid; },
"Can not convert from System.Guid to Newtonsoft.Json.Linq.JObject.");
}
}
public class DynamicDictionary : DynamicObject
+1 -1
View File
@@ -9,7 +9,7 @@
/// <summary>
/// Rent a array from the pool. This array must be returned when it is no longer needed.
/// </summary>
/// <param name="minimumLength">The minimum required length of the array. The returned array may be larger.</param>
/// <param name="minimumLength">The minimum required length of the array. The returned array may be longer.</param>
/// <returns>The rented array from the pool. This array must be returned when it is no longer needed.</returns>
T[] Rent(int minimumLength);
+1 -1
View File
@@ -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;
@@ -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);