-Fixed List<T> silly buggers with nullable types - http://tinyurl.com/yagsptj - That was tough!

This commit is contained in:
JamesNK
2009-10-03 07:54:47 +00:00
parent be8d951c03
commit a5b7507d17
5 changed files with 31 additions and 12 deletions
@@ -2227,5 +2227,16 @@ keyword such as type of business.""
list l = JsonConvert.DeserializeObject<list>(json);
}
[Test]
public void sdfsdf()
{
//List<decimal?> l = new List<decimal?>();
//l.Add(null);
//IList il = l;
//il.Add(null);
JsonConvert.DeserializeObject<List<decimal?>>("[ 3.3, null, 1.1 ] ");
}
}
}
@@ -502,12 +502,14 @@ namespace Newtonsoft.Json.Serialization
throw new JsonSerializationException("Cannot call OnError on an array or readonly list: {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
#endif
PopulateList(l, reader, reference, contract);
PopulateList(CollectionUtils.CreateCollectionWrapper(l), reader, reference, contract);
});
}
private IList PopulateList(IList list, JsonReader reader, string reference, JsonArrayContract contract)
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract)
{
object list = wrappedList.UnderlyingCollection;
if (reference != null)
_serializer.ReferenceResolver.AddReference(reference, list);
@@ -522,7 +524,7 @@ namespace Newtonsoft.Json.Serialization
case JsonToken.EndArray:
contract.InvokeOnDeserialized(list);
return list;
return wrappedList.UnderlyingCollection;
case JsonToken.Comment:
break;
default:
@@ -530,12 +532,12 @@ namespace Newtonsoft.Json.Serialization
{
object value = CreateValue(reader, contract.CollectionItemType, null, null);
list.Add(value);
wrappedList.Add(value);
}
catch (Exception ex)
{
#if !PocketPC && !SILVERLIGHT && !NET20
ErrorContext errorContext = GetErrorContext(list, list.Count, ex);
ErrorContext errorContext = GetErrorContext(list, wrappedList.Count, ex);
contract.InvokeOnError(list, errorContext);
if (errorContext.Handled)
@@ -525,7 +525,7 @@ namespace Newtonsoft.Json.Utilities
// can't add or modify a readonly list
// use List<T> and convert once populated
list = (IList)CreateGenericList(readOnlyCollectionContentsType);
list = CreateGenericList(readOnlyCollectionContentsType);
isReadOnlyOrFixedSize = true;
}
else if (typeof(IList).IsAssignableFrom(listType))
@@ -537,9 +537,9 @@ namespace Newtonsoft.Json.Utilities
else
list = null;
}
else if (listType.IsGenericType && listType.GetGenericTypeDefinition() == typeof(IList<>))
else if (ReflectionUtils.ImplementsGenericDefinition(listType, typeof(IList<>)))
{
list = CollectionUtils.CreateGenericList(ReflectionUtils.GetCollectionItemType(listType));
list = CreateGenericList(ReflectionUtils.GetCollectionItemType(listType));
}
else
{
@@ -48,7 +48,10 @@ namespace Newtonsoft.Json.Utilities
{
ValidationUtils.ArgumentNotNull(list, "list");
_list = list;
if (list is ICollection<T>)
_genericCollection = (ICollection<T>)list;
else
_list = list;
}
public CollectionWrapper(ICollection<T> list)
@@ -248,7 +251,7 @@ namespace Newtonsoft.Json.Utilities
private static bool IsCompatibleObject(object value)
{
if (!(value is T) && (value != null || typeof(T).IsValueType))
if (!(value is T) && (value != null || (typeof(T).IsValueType && !ReflectionUtils.IsNullableType(typeof(T)))))
return false;
return true;
+5 -2
View File
@@ -48,7 +48,10 @@ namespace Newtonsoft.Json.Utilities
{
ValidationUtils.ArgumentNotNull(list, "list");
_list = list;
if (list is IList<T>)
_genericList = (IList<T>) list;
else
_list = list;
}
public ListWrapper(IList<T> list)
@@ -267,7 +270,7 @@ namespace Newtonsoft.Json.Utilities
private static bool IsCompatibleObject(object value)
{
if (!(value is T) && (value != null || typeof(T).IsValueType))
if (!(value is T) && (value != null || (typeof(T).IsValueType && !ReflectionUtils.IsNullableType(typeof(T)))))
return false;
return true;