Provide more specific exception message when an enumerable or dictionary cannot be...
authorLayomi Akinrinade <laakinri@microsoft.com>
Tue, 12 Nov 2019 21:15:10 +0000 (13:15 -0800)
committerGitHub <noreply@github.com>
Tue, 12 Nov 2019 21:15:10 +0000 (13:15 -0800)
* Provide more specific exception message when an enumerable or dictionary cannot be instantiated

* Address review feedback

Commit migrated from https://github.com/dotnet/corefx/commit/961bb667a056bb3b9a5f32e22aed8bb64eaddcad

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStackFrame.cs

index f9091d3..6418552 100644 (file)
@@ -213,18 +213,13 @@ namespace System.Text.Json
             }
 
             JsonClassInfo runtimeClassInfo = jsonPropertyInfo.RuntimeClassInfo;
-            if (runtimeClassInfo.CreateObject != null)
-            {
-                return runtimeClassInfo.CreateObject();
-            }
-            else
+
+            if (runtimeClassInfo.CreateObject == null)
             {
-                // Could not create an instance to be returned. For derived types, this means there is no parameterless ctor.
-                throw ThrowHelper.GetNotSupportedException_SerializationNotSupportedCollection(
-                    jsonPropertyInfo.DeclaredPropertyType,
-                    jsonPropertyInfo.ParentClassType,
-                    jsonPropertyInfo.PropertyInfo);
+                ThrowHelper.ThrowNotSupportedException_DeserializeCreateObjectDelegateIsNull(jsonPropertyInfo.DeclaredPropertyType);
             }
+
+            return runtimeClassInfo.CreateObject();
         }
 
         public static object CreateDictionaryValue(ref ReadStack state)
@@ -257,18 +252,13 @@ namespace System.Text.Json
             }
 
             JsonClassInfo runtimeClassInfo = jsonPropertyInfo.RuntimeClassInfo;
-            if (runtimeClassInfo.CreateObject != null)
-            {
-                return runtimeClassInfo.CreateObject();
-            }
-            else
+
+            if (runtimeClassInfo.CreateObject == null)
             {
-                // Could not create an instance to be returned. For derived types, this means there is no parameterless ctor.
-                throw ThrowHelper.GetNotSupportedException_SerializationNotSupportedCollection(
-                    jsonPropertyInfo.DeclaredPropertyType,
-                    jsonPropertyInfo.ParentClassType,
-                    jsonPropertyInfo.PropertyInfo);
+                ThrowHelper.ThrowNotSupportedException_DeserializeCreateObjectDelegateIsNull(jsonPropertyInfo.DeclaredPropertyType);
             }
+
+            return runtimeClassInfo.CreateObject();
         }
 
         public Type GetElementType()