diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs index 988f78426d52a0..54c488b2c72e5e 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs @@ -27,7 +27,8 @@ internal static JsonPropertyInfo LookupProperty( #if DEBUG if (state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy != ConverterStrategy.Object) { - Debug.Fail(GetLookupPropertyDebugInfo(obj, unescapedPropertyName, ref state)); + string objTypeName = obj?.GetType().FullName ?? ""; + Debug.Fail($"obj.GetType() => {objTypeName}; {state.Current.JsonTypeInfo.GetPropertyDebugInfo(unescapedPropertyName)}"); } #endif @@ -68,16 +69,6 @@ internal static JsonPropertyInfo LookupProperty( return jsonPropertyInfo; } -#if DEBUG - private static string GetLookupPropertyDebugInfo(object? obj, ReadOnlySpan unescapedPropertyName, ref ReadStack state) - { - JsonTypeInfo jti = state.Current.JsonTypeInfo; - string objTypeName = obj?.GetType().FullName ?? ""; - string propertyName = JsonHelpers.Utf8GetString(unescapedPropertyName); - return $"ConverterStrategy is {jti.PropertyInfoForTypeInfo.ConverterStrategy}. propertyName = {propertyName}; obj.GetType() => {objTypeName}; DebugInfo={jti.GetDebugInfo()}"; - } -#endif - [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ReadOnlySpan GetPropertyName( ref ReadStack state, diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs index 9b8f548c7a1f3c..2a9c4a01c8b28b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs @@ -172,17 +172,22 @@ internal JsonPropertyInfo GetProperty( } // No cached item was found. Try the main dictionary which has all of the properties. - Debug.Assert(PropertyCache != null); +#if DEBUG + if (PropertyCache == null) + { + Debug.Fail($"Property cache is null. {GetPropertyDebugInfo(propertyName)}"); + } +#endif - if (PropertyCache.TryGetValue(JsonHelpers.Utf8GetString(propertyName), out JsonPropertyInfo? info)) + if (PropertyCache!.TryGetValue(JsonHelpers.Utf8GetString(propertyName), out JsonPropertyInfo? info)) { - Debug.Assert(info != null); + Debug.Assert(info != null, "PropertyCache contains null JsonPropertyInfo"); if (Options.PropertyNameCaseInsensitive) { if (propertyName.SequenceEqual(info.NameAsUtf8Bytes)) { - Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan())); + Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()), "key does not match re-computed value for the same sequence (case-insensitive)"); // Use the existing byte[] reference instead of creating another one. utf8PropertyName = info.NameAsUtf8Bytes!; @@ -195,7 +200,7 @@ internal JsonPropertyInfo GetProperty( } else { - Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan())); + Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()), "key does not match re-computed value for the same sequence (case-sensitive)"); utf8PropertyName = info.NameAsUtf8Bytes; } } 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 af841f1e0c4f69..40f4713dd30592 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 @@ -241,6 +241,12 @@ internal virtual void Configure() } #if DEBUG + internal string GetPropertyDebugInfo(ReadOnlySpan unescapedPropertyName) + { + string propertyName = JsonHelpers.Utf8GetString(unescapedPropertyName); + return $"propertyName = {propertyName}; DebugInfo={GetDebugInfo()}"; + } + internal string GetDebugInfo() { ConverterStrategy strat = PropertyInfoForTypeInfo.ConverterStrategy;