-Fixed deserializing non-generic IReadOnlyCollection<T> implementations

This commit is contained in:
James Newton-King
2014-05-12 13:40:48 +12:00
parent 7a8f5e43bc
commit 017a38c65b
5 changed files with 56 additions and 4 deletions
@@ -71,7 +71,9 @@ namespace Newtonsoft.Json.Tests
jsonWriter.WriteEndObject(); jsonWriter.WriteEndObject();
} }
string json = Encoding.UTF8.GetString(ms.ToArray()); byte[] data = ms.ToArray();
string json = Encoding.UTF8.GetString(data, 0, data.Length);
Assert.AreEqual(@"{" + '\n' + @" ""prop"": true" + '\n' + "}", json); Assert.AreEqual(@"{" + '\n' + @" ""prop"": true" + '\n' + "}", json);
} }
@@ -75,4 +75,4 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.3.17309")] [assembly: AssemblyFileVersion("6.0.3.17312")]
@@ -1488,8 +1488,58 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(2, products.Count); Assert.AreEqual(2, products.Count);
Assert.AreEqual("Product 1", products[0].Name); Assert.AreEqual("Product 1", products[0].Name);
} }
#if !(NET40 || NET35 || NET20 || PORTABLE40)
[Test]
public void ReadOnlyIntegerList()
{
ReadOnlyIntegerList l = new ReadOnlyIntegerList(new List<int>
{
1,
2,
3,
int.MaxValue
});
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
Assert.AreEqual(@"[
1,
2,
3,
2147483647
]", json);
}
#endif
} }
#if !(NET40 || NET35 || NET20 || PORTABLE40)
public class ReadOnlyIntegerList : IReadOnlyCollection<int>
{
private readonly List<int> _list;
public ReadOnlyIntegerList(List<int> l)
{
_list = l;
}
public int Count
{
get { return _list.Count; }
}
public IEnumerator<int> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
#endif
public class Array2D public class Array2D
{ {
public string Before { get; set; } public string Before { get; set; }
@@ -91,5 +91,5 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")] [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.3.17309")] [assembly: AssemblyFileVersion("6.0.3.17312")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
@@ -127,7 +127,7 @@ namespace Newtonsoft.Json.Serialization
#if !(NET40 || NET35 || NET20 || PORTABLE40) #if !(NET40 || NET35 || NET20 || PORTABLE40)
else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>), out tempCollectionType)) else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>), out tempCollectionType))
{ {
CollectionItemType = underlyingType.GetGenericArguments()[0]; CollectionItemType = tempCollectionType.GetGenericArguments()[0];
if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>)) if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection<>))
|| ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList<>))) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList<>)))