From 7819b3c5ef37220dc5e5d2f877921740b67d676c Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Mon, 16 Jan 2017 21:45:43 +0100 Subject: [PATCH] Handle F# lists that implement IReadOnlyCollection (F# 4.1 and above) (#1181) Fixes #1180 --- .../Serialization/JsonArrayContract.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 3a301647..96afac5b 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -191,6 +191,11 @@ namespace Newtonsoft.Json.Serialization _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType); + +#if HAVE_FSHARP_TYPES + StoreFSharpListCreatorIfNecessary(underlyingType); +#endif + IsReadOnlyOrFixedSize = true; canDeserialize = HasParameterizedCreatorInternal; } @@ -207,11 +212,7 @@ namespace Newtonsoft.Json.Serialization _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); #if HAVE_FSHARP_TYPES - if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName) - { - FSharpUtils.EnsureInitialized(underlyingType.Assembly()); - _parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType); - } + StoreFSharpListCreatorIfNecessary(underlyingType); #endif if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) @@ -306,5 +307,16 @@ namespace Newtonsoft.Json.Serialization return (IList)_genericTemporaryCollectionCreator(); } + +#if HAVE_FSHARP_TYPES + private void StoreFSharpListCreatorIfNecessary(Type underlyingType) + { + if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName) + { + FSharpUtils.EnsureInitialized(underlyingType.Assembly()); + _parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType); + } + } +#endif } } \ No newline at end of file