diff options
author | Eirik Tsarpalis <eirik.tsarpalis@gmail.com> | 2022-07-11 17:56:29 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-11 17:56:29 +0300 |
commit | 005f654c31896de7b1f7b8ef94363a49d9779a56 (patch) | |
tree | 8c42efe1752790ef4f8e69ad95b69c46c4a86915 | |
parent | 35d3f2b7e2568ae39d030c6098c5806a7d89b719 (diff) |
Improve JsonSerializerContext error messages in combined contexts. (#71936)
* Improve JsonSerializerContext error messages in combined contexts.
* Update src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Co-authored-by: Krzysztof Wicher <mordotymoja@gmail.com>
Co-authored-by: Krzysztof Wicher <mordotymoja@gmail.com>
5 files changed, 13 insertions, 11 deletions
diff --git a/src/libraries/System.Text.Json/src/Resources/Strings.resx b/src/libraries/System.Text.Json/src/Resources/Strings.resx index ed5b4f159a5..34166a7016e 100644 --- a/src/libraries/System.Text.Json/src/Resources/Strings.resx +++ b/src/libraries/System.Text.Json/src/Resources/Strings.resx @@ -603,7 +603,7 @@ <value>Invalid configuration provided for 'propInitFunc', 'ctorParamInitFunc' and 'serializeFunc'.</value> </data> <data name="NoMetadataForTypeProperties" xml:space="preserve"> - <value>'JsonSerializerContext' '{0}' did not provide property metadata for type '{1}'.</value> + <value>TypeInfoResolver '{0}' did not provide property metadata for type '{1}'.</value> </data> <data name="NoDefaultOptionsForContext" xml:space="preserve"> <value>To specify a serialization implementation for type '{0}'', context '{0}' must specify default options.</value> @@ -618,7 +618,7 @@ <value>F# discriminated union serialization is not supported. Consider authoring a custom converter for the type.</value> </data> <data name="NoMetadataForTypeCtorParams" xml:space="preserve"> - <value>'JsonSerializerContext' '{0}' did not provide constructor parameter metadata for type '{1}'.</value> + <value>TypeInfoResolver '{0}' did not provide constructor parameter metadata for type '{1}'.</value> </data> <data name="Polymorphism_BaseConverterDoesNotSupportMetadata" xml:space="preserve"> <value>The converter for polymorphic type '{0}' does not support metadata writes or reads.</value> diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs index 1fd7d2c452b..17e88162bf1 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs @@ -180,6 +180,9 @@ namespace System.Text.Json } } + // Needed since public property is RequiresUnreferencedCode. + internal IJsonTypeInfoResolver? TypeInfoResolverSafe => _typeInfoResolver; + /// <summary> /// Defines whether an extra comma at the end of a list of JSON values in an object or array /// is allowed (and ignored) within the JSON payload being deserialized. diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs index 1d3ecb5f6c7..689ea6bedf2 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs @@ -205,7 +205,7 @@ namespace System.Text.Json.Serialization.Metadata { if (ThrowOnDeserialize) { - ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeProperties(Options.SerializerContext, Type); + ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeProperties(Options.TypeInfoResolverSafe, Type); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs index 5edb7b5de37..139cd781cd6 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs @@ -91,11 +91,10 @@ namespace System.Text.Json.Serialization.Metadata internal override JsonParameterInfoValues[] GetParameterInfoValues() { - JsonSerializerContext? context = Options.SerializerContext; JsonParameterInfoValues[] array; if (CtorParamInitFunc == null || (array = CtorParamInitFunc()) == null) { - ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeCtorParams(context, Type); + ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeCtorParams(Options.TypeInfoResolverSafe, Type); return null!; } @@ -127,13 +126,13 @@ namespace System.Text.Json.Serialization.Metadata return; } - if (SerializeHandler != null && Options.SerializerContext?.CanUseSerializationLogic == true) + if (SerializeHandler != null && context?.CanUseSerializationLogic == true) { ThrowOnDeserialize = true; return; } - ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeProperties(context, Type); + ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeProperties(Options.TypeInfoResolverSafe, Type); return; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs index cb15bfe034f..3719e49c031 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs @@ -668,14 +668,14 @@ namespace System.Text.Json throw new InvalidOperationException(SR.Format(SR.MetadataInitFuncsNull)); } - public static void ThrowInvalidOperationException_NoMetadataForTypeProperties(JsonSerializerContext? context, Type type) + public static void ThrowInvalidOperationException_NoMetadataForTypeProperties(IJsonTypeInfoResolver? resolver, Type type) { - throw new InvalidOperationException(SR.Format(SR.NoMetadataForTypeProperties, context?.GetType().FullName ?? "<null>", type)); + throw new InvalidOperationException(SR.Format(SR.NoMetadataForTypeProperties, resolver?.GetType().FullName ?? "<null>", type)); } - public static void ThrowInvalidOperationException_NoMetadataForTypeCtorParams(JsonSerializerContext? context, Type type) + public static void ThrowInvalidOperationException_NoMetadataForTypeCtorParams(IJsonTypeInfoResolver? resolver, Type type) { - throw new InvalidOperationException(SR.Format(SR.NoMetadataForTypeCtorParams, context?.GetType().FullName ?? "<null>", type)); + throw new InvalidOperationException(SR.Format(SR.NoMetadataForTypeCtorParams, resolver?.GetType().FullName ?? "<null>", type)); } public static void ThrowInvalidOperationException_NoDefaultOptionsForContext(JsonSerializerContext context, Type type) |