-Fixed List<T> silly buggers with nullable types - http://tinyurl.com/yagsptj - That was tough!
This commit is contained in:
@@ -2227,5 +2227,16 @@ keyword such as type of business.""
|
|||||||
|
|
||||||
list l = JsonConvert.DeserializeObject<list>(json);
|
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));
|
throw new JsonSerializationException("Cannot call OnError on an array or readonly list: {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
|
||||||
#endif
|
#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)
|
if (reference != null)
|
||||||
_serializer.ReferenceResolver.AddReference(reference, list);
|
_serializer.ReferenceResolver.AddReference(reference, list);
|
||||||
|
|
||||||
@@ -522,7 +524,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
case JsonToken.EndArray:
|
case JsonToken.EndArray:
|
||||||
contract.InvokeOnDeserialized(list);
|
contract.InvokeOnDeserialized(list);
|
||||||
|
|
||||||
return list;
|
return wrappedList.UnderlyingCollection;
|
||||||
case JsonToken.Comment:
|
case JsonToken.Comment:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -530,12 +532,12 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
object value = CreateValue(reader, contract.CollectionItemType, null, null);
|
object value = CreateValue(reader, contract.CollectionItemType, null, null);
|
||||||
|
|
||||||
list.Add(value);
|
wrappedList.Add(value);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||||
ErrorContext errorContext = GetErrorContext(list, list.Count, ex);
|
ErrorContext errorContext = GetErrorContext(list, wrappedList.Count, ex);
|
||||||
contract.InvokeOnError(list, errorContext);
|
contract.InvokeOnError(list, errorContext);
|
||||||
|
|
||||||
if (errorContext.Handled)
|
if (errorContext.Handled)
|
||||||
|
|||||||
@@ -525,7 +525,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
// can't add or modify a readonly list
|
// can't add or modify a readonly list
|
||||||
// use List<T> and convert once populated
|
// use List<T> and convert once populated
|
||||||
list = (IList)CreateGenericList(readOnlyCollectionContentsType);
|
list = CreateGenericList(readOnlyCollectionContentsType);
|
||||||
isReadOnlyOrFixedSize = true;
|
isReadOnlyOrFixedSize = true;
|
||||||
}
|
}
|
||||||
else if (typeof(IList).IsAssignableFrom(listType))
|
else if (typeof(IList).IsAssignableFrom(listType))
|
||||||
@@ -537,9 +537,9 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
else
|
else
|
||||||
list = null;
|
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
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -48,7 +48,10 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(list, "list");
|
ValidationUtils.ArgumentNotNull(list, "list");
|
||||||
|
|
||||||
_list = list;
|
if (list is ICollection<T>)
|
||||||
|
_genericCollection = (ICollection<T>)list;
|
||||||
|
else
|
||||||
|
_list = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CollectionWrapper(ICollection<T> list)
|
public CollectionWrapper(ICollection<T> list)
|
||||||
@@ -248,7 +251,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
private static bool IsCompatibleObject(object value)
|
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 false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -48,7 +48,10 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(list, "list");
|
ValidationUtils.ArgumentNotNull(list, "list");
|
||||||
|
|
||||||
_list = list;
|
if (list is IList<T>)
|
||||||
|
_genericList = (IList<T>) list;
|
||||||
|
else
|
||||||
|
_list = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ListWrapper(IList<T> list)
|
public ListWrapper(IList<T> list)
|
||||||
@@ -267,7 +270,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
private static bool IsCompatibleObject(object value)
|
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 false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user