From 48def517c649faaa36cb25ccfe3a7917495dcc75 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Tue, 12 Nov 2019 13:15:10 -0800 Subject: [PATCH] Provide more specific exception message when an enumerable or dictionary cannot be instantiated (dotnet/corefx#42497) * 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 --- .../Text/Json/Serialization/ReadStackFrame.cs | 30 ++++++++-------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStackFrame.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStackFrame.cs index f9091d3..6418552 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStackFrame.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStackFrame.cs @@ -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() -- 2.7.4