Skip to content

Commit

Permalink
Update DebuggerDisplay for metadata types & JsonSerializerOptions (#7…
Browse files Browse the repository at this point in the history
…2524)

* Update DebuggerDisplay for metadata types & JsonSerializerOptions

* address feedback
  • Loading branch information
eiriktsarpalis authored Jul 20, 2022
1 parent ddded2f commit 9b1f2e2
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;

namespace System.Text.Json.Serialization
{
Expand All @@ -20,7 +18,7 @@ public ConfigurationList(IList<TItem>? source = null)
_list = source is null ? new List<TItem>() : new List<TItem>(source);
}

protected abstract bool IsLockedInstance { get; }
protected abstract bool IsImmutable { get; }
protected abstract void VerifyMutable();
protected virtual void OnAddingElement(TItem item) { }

Expand All @@ -45,7 +43,7 @@ public TItem this[int index]

public int Count => _list.Count;

public bool IsReadOnly => IsLockedInstance;
public bool IsReadOnly => IsImmutable;

public void Add(TItem item)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static JsonTypeInfo GetTypeInfo(JsonSerializerOptions? options, Type run

options ??= JsonSerializerOptions.Default;

if (!options.IsLockedInstance || !DefaultJsonTypeInfoResolver.IsDefaultInstanceRooted)
if (!options.IsImmutable || !DefaultJsonTypeInfoResolver.IsDefaultInstanceRooted)
{
options.InitializeForReflectionSerializer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public abstract partial class JsonSerializerContext : IJsonTypeInfoResolver
/// </remarks>
public JsonSerializerOptions Options
{
get => _options ??= new JsonSerializerOptions { TypeInfoResolver = this, IsLockedInstance = true };
get => _options ??= new JsonSerializerOptions { TypeInfoResolver = this, IsImmutable = true };

internal set
{
Debug.Assert(!value.IsLockedInstance);
Debug.Assert(!value.IsImmutable);
value.TypeInfoResolver = this;
value.IsLockedInstance = true;
value.IsImmutable = true;
_options = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal JsonTypeInfo GetTypeInfoInternal(Type type, bool ensureConfigured = tru
{
JsonTypeInfo? typeInfo = null;

if (IsLockedInstance)
if (IsImmutable)
{
typeInfo = GetCachingContext()?.GetOrAddJsonTypeInfo(type);
if (ensureConfigured)
Expand Down Expand Up @@ -115,7 +115,7 @@ internal void ClearCaches()

private CachingContext? GetCachingContext()
{
Debug.Assert(IsLockedInstance);
Debug.Assert(IsImmutable);

return _cachingContext ??= TrackedCachingContexts.GetOrCreate(this);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ internal static class TrackedCachingContexts

public static CachingContext GetOrCreate(JsonSerializerOptions options)
{
Debug.Assert(options.IsLockedInstance, "Cannot create caching contexts for mutable JsonSerializerOptions instances");
Debug.Assert(options.IsImmutable, "Cannot create caching contexts for mutable JsonSerializerOptions instances");
Debug.Assert(options._typeInfoResolver != null);

ConcurrentDictionary<JsonSerializerOptions, WeakReference<CachingContext>> cache = s_cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace System.Text.Json
/// <summary>
/// Provides options to be used with <see cref="JsonSerializer"/>.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed partial class JsonSerializerOptions
{
internal const int BufferSizeDefault = 16 * 1024;
Expand Down Expand Up @@ -73,7 +74,7 @@ public static JsonSerializerOptions Default
private bool _propertyNameCaseInsensitive;
private bool _writeIndented;

private volatile bool _isLockedInstance;
private volatile bool _isImmutable;

/// <summary>
/// Constructs a new <see cref="JsonSerializerOptions"/> instance.
Expand Down Expand Up @@ -606,14 +607,14 @@ public ReferenceHandler? ReferenceHandler
new ReflectionMemberAccessor();
#endif

internal bool IsLockedInstance
internal bool IsImmutable
{
get => _isLockedInstance;
get => _isImmutable;
set
{
Debug.Assert(value, "cannot unlock options instances");
Debug.Assert(_typeInfoResolver != null, "cannot lock without a resolver.");
_isLockedInstance = true;
_isImmutable = true;
}
}

Expand All @@ -628,7 +629,7 @@ internal void InitializeForReflectionSerializer()
// the default resolver to gain access to the default converters.
DefaultJsonTypeInfoResolver defaultResolver = DefaultJsonTypeInfoResolver.RootDefaultInstance();
_typeInfoResolver ??= defaultResolver;
IsLockedInstance = true;
IsImmutable = true;
}

internal void InitializeForMetadataGeneration()
Expand All @@ -638,7 +639,7 @@ internal void InitializeForMetadataGeneration()
ThrowHelper.ThrowInvalidOperationException_JsonTypeInfoUsedButTypeInfoResolverNotSet();
}

IsLockedInstance = true;
IsImmutable = true;
}

private JsonTypeInfo? GetTypeInfoNoCaching(Type type)
Expand Down Expand Up @@ -704,7 +705,7 @@ internal JsonWriterOptions GetWriterOptions()

internal void VerifyMutable()
{
if (_isLockedInstance)
if (_isImmutable)
{
ThrowHelper.ThrowInvalidOperationException_SerializerOptionsImmutable(_typeInfoResolver as JsonSerializerContext);
}
Expand All @@ -720,7 +721,7 @@ public ConverterList(JsonSerializerOptions options, IList<JsonConverter>? source
_options = options;
}

protected override bool IsLockedInstance => _options.IsLockedInstance;
protected override bool IsImmutable => _options.IsImmutable;
protected override void VerifyMutable() => _options.VerifyMutable();
}

Expand All @@ -731,10 +732,12 @@ private static JsonSerializerOptions GetOrCreateDefaultOptionsInstance()
var options = new JsonSerializerOptions
{
TypeInfoResolver = DefaultJsonTypeInfoResolver.RootDefaultInstance(),
IsLockedInstance = true
IsImmutable = true
};

return Interlocked.CompareExchange(ref s_defaultOptions, options, null) ?? options;
}

private string DebuggerDisplay => $"TypeInfoResolver = {TypeInfoResolver?.GetType()?.Name}, IsImmutable = {IsImmutable}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public ModifierCollection(DefaultJsonTypeInfoResolver resolver)
_resolver = resolver;
}

protected override bool IsLockedInstance => !_resolver._mutable;
protected override bool IsImmutable => !_resolver._mutable;
protected override void VerifyMutable()
{
if (!_resolver._mutable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public DerivedTypeList(JsonPolymorphismOptions parent)
_parent = parent;
}

protected override bool IsLockedInstance => _parent.DeclaringTypeInfo?.IsConfigured == true;
protected override bool IsImmutable => _parent.DeclaringTypeInfo?.IsConfigured == true;
protected override void VerifyMutable() => _parent.DeclaringTypeInfo?.VerifyMutable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,6 @@ public JsonNumberHandling? NumberHandling
internal abstract object? DefaultValue { get; }

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"MemberInfo={AttributeProvider as MemberInfo}";
private string DebuggerDisplay => $"PropertyType = {PropertyType}, Name = {Name}, DeclaringType = {DeclaringType}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ internal void Configure()
{
Debug.Assert(Monitor.IsEntered(_configureLock), "Configure called directly, use EnsureConfigured which locks this method");

if (!Options.IsLockedInstance)
if (!Options.IsImmutable)
{
Options.InitializeForMetadataGeneration();
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public JsonPropertyInfoList(JsonTypeInfo jsonTypeInfo)
_jsonTypeInfo = jsonTypeInfo;
}

protected override bool IsLockedInstance => _jsonTypeInfo.IsConfigured || _jsonTypeInfo.Kind != JsonTypeInfoKind.Object;
protected override bool IsImmutable => _jsonTypeInfo.IsConfigured || _jsonTypeInfo.Kind != JsonTypeInfoKind.Object;
protected override void VerifyMutable()
{
_jsonTypeInfo.VerifyMutable();
Expand All @@ -1071,6 +1071,6 @@ protected override void OnAddingElement(JsonPropertyInfo item)
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"Type:{Type.Name}, TypeInfoKind:{Kind}";
private string DebuggerDisplay => $"Type = {Type.Name}, Kind = {Kind}";
}
}

0 comments on commit 9b1f2e2

Please sign in to comment.