diff options
author | JamesNK <james@newtonking.com> | 2010-11-17 10:31:20 +0300 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2010-11-17 10:31:20 +0300 |
commit | 5c0431464be353671d2c3b9607018fbf34045657 (patch) | |
tree | bc8296bbc496f6c98800536ceb471551e302b994 /Src/Newtonsoft.Json | |
parent | 15ea91fb63f40e7ea0a1dfb1057b949d424bb63c (diff) |
-Fix to preserve collection type information in certain situations when serializing
-Add missing Newtonsoft.Json.WindowsPhone.sln file
Diffstat (limited to 'Src/Newtonsoft.Json')
-rw-r--r-- | Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 8137531..01aede8 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -77,19 +77,40 @@ namespace Newtonsoft.Json.Serialization if (_genericCollectionDefinitionType != null)
{
- if (_genericWrapperType == null)
+ EnsureGenericWrapperCreator();
+ return (IWrappedCollection) _genericWrapperCreator(null, list);
+ }
+ else
+ {
+ IList values = ((IEnumerable) list).Cast<object>().ToList();
+
+ if (CollectionItemType != null)
{
- _genericWrapperType = ReflectionUtils.MakeGenericType(typeof (CollectionWrapper<>), CollectionItemType);
+ Array array = Array.CreateInstance(CollectionItemType, values.Count);
+ for (int i = 0; i < values.Count; i++)
+ {
+ array.SetValue(values[i], i);
+ }
- ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] {_genericCollectionDefinitionType});
- _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
+ values = array;
}
- return (IWrappedCollection) _genericWrapperCreator(null, list);
+ return new CollectionWrapper<object>(values);
}
- else
+ }
+
+ private void EnsureGenericWrapperCreator()
+ {
+ if (_genericWrapperType == null)
{
- return new CollectionWrapper<object>((IList<object>)((IEnumerable)list).Cast<object>().ToList());
+ _genericWrapperType = ReflectionUtils.MakeGenericType(typeof (CollectionWrapper<>), CollectionItemType);
+
+ Type constructorArgument = (ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof (List<>)))
+ ? ReflectionUtils.MakeGenericType(typeof (ICollection<>), CollectionItemType)
+ : _genericCollectionDefinitionType;
+
+ ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { constructorArgument });
+ _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(genericWrapperConstructor);
}
}
|