-Make .NET 4.0 dynamic actually work (oops)

-Added JValue dynamic value conversion
This commit is contained in:
JamesNK
2010-11-25 07:11:38 +00:00
parent 5c0431464b
commit efc2310a3d
10 changed files with 179 additions and 117 deletions
+19 -1
View File
@@ -119,6 +119,18 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(2, memberNames.Count);
Assert.AreEqual("ChildValue", memberNames[0]);
Assert.AreEqual("Hello Joe", memberNames[1]);
o = new JObject(
new JProperty("ChildValue1", "blah blah"),
new JProperty("Hello Joe1", null));
d = o;
memberNames = o.GetDynamicMemberNames().ToList();
Assert.AreEqual(2, memberNames.Count);
Assert.AreEqual("ChildValue1", memberNames[0]);
Assert.AreEqual("Hello Joe1", memberNames[1]);
}
[Test]
@@ -126,11 +138,15 @@ namespace Newtonsoft.Json.Tests.Linq
{
AssertValueConverted<bool>(true);
AssertValueConverted<bool?>(true);
AssertValueConverted<bool?>(false);
AssertValueConverted<byte[]>(null);
AssertValueConverted<byte[]>(Encoding.UTF8.GetBytes("blah"));
AssertValueConverted<DateTime>(new DateTime(2000, 12, 20, 23, 59, 2, DateTimeKind.Utc));
AssertValueConverted<DateTime?>(new DateTime(2000, 12, 20, 23, 59, 2, DateTimeKind.Utc));
AssertValueConverted<DateTime?>(null);
AssertValueConverted<DateTimeOffset>(new DateTimeOffset(2000, 12, 20, 23, 59, 2, TimeSpan.FromHours(1)));
AssertValueConverted<DateTimeOffset?>(new DateTimeOffset(2000, 12, 20, 23, 59, 2, TimeSpan.FromHours(1)));
AssertValueConverted<DateTimeOffset?>(null);
AssertValueConverted<decimal>(99.9m);
AssertValueConverted<decimal?>(99.9m);
AssertValueConverted<double>(99.9);
@@ -150,6 +166,7 @@ namespace Newtonsoft.Json.Tests.Linq
AssertValueConverted<ulong?>(ulong.MaxValue);
AssertValueConverted<ushort>(ushort.MinValue);
AssertValueConverted<ushort?>(ushort.MinValue);
AssertValueConverted<ushort?>(null);
}
private static void AssertValueConverted<T>(T value)
@@ -157,7 +174,8 @@ namespace Newtonsoft.Json.Tests.Linq
JValue v = new JValue(value);
dynamic d = v;
Assert.AreEqual(value, (T)d);
T t = d;
Assert.AreEqual(value, t);
}
}
}
@@ -267,7 +267,6 @@
<Compile Include="Utilities\ReflectionUtilsTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.Config" />
<None Include="LinqToSql\LinqToSqlClasses.dbml">
<Generator>MSLinqToSQLGenerator</Generator>
<LastGenOutput>LinqToSqlClasses.designer.cs</LastGenOutput>
@@ -3827,63 +3827,5 @@ keyword such as type of business.""
});
}
#endif
[Test]
public void SerializingIEnumerableOfTShouldRetainGenericTypeInfo()
{
CustomEnumerable<Product> products = new CustomEnumerable<Product>();
string json = JsonConvert.SerializeObject(products, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
Assert.AreEqual(@"{
""$type"": ""Newtonsoft.Json.Tests.TestObjects.Product[], Newtonsoft.Json.Tests"",
""$values"": []
}", json);
}
public class CustomEnumerable<T> : IEnumerable<T>
{
//NOTE: a simple linked list
private readonly T value;
private readonly CustomEnumerable<T> next;
private readonly int count;
private CustomEnumerable(T value, CustomEnumerable<T> next)
{
this.value = value;
this.next = next;
count = this.next.count + 1;
}
public CustomEnumerable()
{
count = 0;
}
public CustomEnumerable<T> AddFirst(T newVal)
{
return new CustomEnumerable<T>(newVal, this);
}
public IEnumerator<T> GetEnumerator()
{
if (count == 0) // last node
yield break;
yield return value;
var nextInLine = next;
while (nextInLine != null)
{
if (nextInLine.count != 0)
yield return nextInLine.value;
nextInLine = nextInLine.next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
}
@@ -24,6 +24,7 @@
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters;
@@ -640,6 +641,68 @@ namespace Newtonsoft.Json.Tests.Serialization
List<UrlStatus> statues = (List<UrlStatus>) newCollection["List"];
Assert.AreEqual(2, statues.Count);
}
[Test]
public void SerializingIEnumerableOfTShouldRetainGenericTypeInfo()
{
string productClassRef = ReflectionUtils.GetTypeName(typeof(Product[]), FormatterAssemblyStyle.Simple);
CustomEnumerable<Product> products = new CustomEnumerable<Product>();
string json = JsonConvert.SerializeObject(products, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
Assert.AreEqual(@"{
""$type"": """ + productClassRef + @""",
""$values"": []
}", json);
}
public class CustomEnumerable<T> : IEnumerable<T>
{
//NOTE: a simple linked list
private readonly T value;
private readonly CustomEnumerable<T> next;
private readonly int count;
private CustomEnumerable(T value, CustomEnumerable<T> next)
{
this.value = value;
this.next = next;
count = this.next.count + 1;
}
public CustomEnumerable()
{
count = 0;
}
public CustomEnumerable<T> AddFirst(T newVal)
{
return new CustomEnumerable<T>(newVal, this);
}
public IEnumerator<T> GetEnumerator()
{
if (count == 0) // last node
yield break;
yield return value;
var nextInLine = next;
while (nextInLine != null)
{
if (nextInLine.count != 0)
yield return nextInLine.value;
nextInLine = nextInLine.next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
public class Message