Skip to content

Commit

Permalink
Rename ClassType to ConverterStrategy and clean up usage
Browse files Browse the repository at this point in the history
  • Loading branch information
layomia committed Apr 14, 2021
1 parent 5e72369 commit 3c06307
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.cs" />
<Compile Include="System\Text\Json\Serialization\ReferenceEqualsWrapper.cs" />
<Compile Include="System\Text\Json\Serialization\ClassType.cs" />
<Compile Include="System\Text\Json\Serialization\ConverterStrategy.cs" />
<Compile Include="System\Text\Json\Serialization\ConverterList.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Collection\ArrayConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Collection\ConcurrentQueueOfTConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ namespace System.Text.Json
/// Determines how a given class is treated when it is (de)serialized.
/// </summary>
/// <remarks>
/// Although bit flags are used, a given ClassType can only be one value.
/// Although bit flags are used, a given ConverterStrategy can only be one value.
/// Bit flags are used to efficiently compare against more than one value.
/// </remarks>
internal enum ClassType : byte
internal enum ConverterStrategy : byte
{
// Default - no class type.
None = 0x0,
// JsonObjectConverter<> - objects with properties.
Object = 0x1,
// JsonConverter<> - simple values.
Value = 0x2,
// JsonValueConverter<> - simple values that need to re-enter the serializer such as KeyValuePair<TKey, TValue>.
NewValue = 0x4,
// JsonIEnumerableConverter<> - all enumerable collections except dictionaries.
Enumerable = 0x8,
// JsonDictionaryConverter<,> - dictionary types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ internal sealed override bool OnTryRead(
{
state.Current.PropertyState = StackFramePropertyState.ReadValue;

if (!SingleValueReadWithReadAhead(_valueConverter.ClassType, ref reader, ref state))
if (!SingleValueReadWithReadAhead(_valueConverter.ConverterStrategy, ref reader, ref state))
{
state.Current.DictionaryKey = key;
value = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ internal override bool OnTryRead(
{
state.Current.PropertyState = StackFramePropertyState.ReadValue;

if (!SingleValueReadWithReadAhead(elementConverter.ClassType, ref reader, ref state))
if (!SingleValueReadWithReadAhead(elementConverter.ConverterStrategy, ref reader, ref state))
{
value = default;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Text.Json.Serialization
/// </summary>
internal abstract class JsonCollectionConverter<TCollection, TElement> : JsonResumableConverter<TCollection>
{
internal sealed override ClassType ClassType => ClassType.Enumerable;
internal sealed override ConverterStrategy ConverterStrategy => ConverterStrategy.Enumerable;
internal override Type ElementType => typeof(TElement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Text.Json.Serialization
/// </summary>
internal abstract class JsonDictionaryConverter<T> : JsonResumableConverter<T>
{
internal sealed override ClassType ClassType => ClassType.Dictionary;
internal sealed override ConverterStrategy ConverterStrategy => ConverterStrategy.Dictionary;

protected internal abstract bool OnWriteResume(Utf8JsonWriter writer, T dictionary, JsonSerializerOptions options, ref WriteStack state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Text.Json.Serialization
/// </summary>
internal abstract class JsonObjectConverter<T> : JsonResumableConverter<T>
{
internal sealed override ClassType ClassType => ClassType.Object;
internal sealed override ConverterStrategy ConverterStrategy => ConverterStrategy.Object;
internal sealed override Type? ElementType => null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected override bool TryLookupConstructorParameter(
JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;
ArgumentState? argState = state.Current.CtorArgumentState;

Debug.Assert(typeInfo.ClassType == ClassType.Object);
Debug.Assert(typeInfo.PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object);
Debug.Assert(argState != null);
Debug.Assert(_keyName != null);
Debug.Assert(_valueName != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ internal override bool OnTryWrite(
{
if (!jsonPropertyInfo.GetMemberAndWriteJson(objectValue, ref state, writer))
{
Debug.Assert(jsonPropertyInfo.ConverterBase.ClassType != ClassType.Value);
Debug.Assert(jsonPropertyInfo.ConverterBase.ConverterStrategy != ConverterStrategy.Value);
return false;
}
}
Expand Down Expand Up @@ -328,7 +328,7 @@ internal override bool OnTryWrite(
{
if (!jsonPropertyInfo.GetMemberAndWriteJson(objectValue!, ref state, writer))
{
Debug.Assert(jsonPropertyInfo.ConverterBase.ClassType != ClassType.Value);
Debug.Assert(jsonPropertyInfo.ConverterBase.ConverterStrategy != ConverterStrategy.Value);
return false;
}
}
Expand Down Expand Up @@ -393,15 +393,15 @@ protected bool ReadAheadPropertyValue(ref ReadStack state, ref Utf8JsonReader re

if (!state.Current.UseExtensionProperty)
{
if (!SingleValueReadWithReadAhead(jsonPropertyInfo.ConverterBase.ClassType, ref reader, ref state))
if (!SingleValueReadWithReadAhead(jsonPropertyInfo.ConverterBase.ConverterStrategy, ref reader, ref state))
{
return false;
}
}
else
{
// The actual converter is JsonElement, so force a read-ahead.
if (!SingleValueReadWithReadAhead(ClassType.Value, ref reader, ref state))
if (!SingleValueReadWithReadAhead(ConverterStrategy.Value, ref reader, ref state))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private bool HandleConstructorArgumentWithContinuation(
// Returning false below will cause the read-ahead functionality to finish the read.
state.Current.PropertyState = StackFramePropertyState.ReadValue;

if (!SingleValueReadWithReadAhead(jsonParameterInfo.ConverterBase.ClassType, ref reader, ref state))
if (!SingleValueReadWithReadAhead(jsonParameterInfo.ConverterBase.ConverterStrategy, ref reader, ref state))
{
return false;
}
Expand Down Expand Up @@ -464,7 +464,7 @@ protected virtual bool TryLookupConstructorParameter(
JsonSerializerOptions options,
out JsonParameterInfo? jsonParameterInfo)
{
Debug.Assert(state.Current.JsonTypeInfo.ClassType == ClassType.Object);
Debug.Assert(state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object);

ReadOnlySpan<byte> unescapedPropertyName = JsonSerializer.GetPropertyName(ref state, ref reader, options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public abstract partial class JsonConverter
// AggressiveInlining used since this method is on a hot path and short. The optionally called
// method DoSingleValueReadWithReadAhead is not inlined.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool SingleValueReadWithReadAhead(ClassType classType, ref Utf8JsonReader reader, ref ReadStack state)
internal static bool SingleValueReadWithReadAhead(ConverterStrategy converterStrategy, ref Utf8JsonReader reader, ref ReadStack state)
{
bool readAhead = (state.ReadAhead && (classType & (ClassType.Value | ClassType.NewValue)) != 0);
bool readAhead = state.ReadAhead && converterStrategy == ConverterStrategy.Value;
if (!readAhead)
{
return reader.Read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal JsonConverter() { }
/// <returns>True if the type can be converted, false otherwise.</returns>
public abstract bool CanConvert(Type typeToConvert);

internal abstract ClassType ClassType { get; }
internal abstract ConverterStrategy ConverterStrategy { get; }

/// <summary>
/// Can direct Read or Write methods be called (for performance).
Expand Down Expand Up @@ -88,7 +88,7 @@ internal bool ShouldFlush(Utf8JsonWriter writer, ref WriteStack state)
/// </summary>
internal abstract void WriteWithQuotesAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, ref WriteStack state);

// Whether a type (ClassType.Object) is deserialized using a parameterized constructor.
// Whether a type (ConverterStrategy.Object) is deserialized using a parameterized constructor.
internal virtual bool ConstructorIsParameterized { get; }

internal ConstructorInfo? ConstructorInfo { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public abstract class JsonConverterFactory : JsonConverter
/// </summary>
protected JsonConverterFactory() { }

internal sealed override ClassType ClassType
internal sealed override ConverterStrategy ConverterStrategy
{
get
{
return ClassType.None;
return ConverterStrategy.None;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public partial class JsonConverter<T>
{
if (!state.IsContinuation)
{
if (!SingleValueReadWithReadAhead(ClassType, ref reader, ref state))
if (!SingleValueReadWithReadAhead(ConverterStrategy, ref reader, ref state))
{
if (state.SupportContinuation)
{
Expand All @@ -51,7 +51,7 @@ public partial class JsonConverter<T>
{
// For a continuation, read ahead here to avoid having to build and then tear
// down the call stack if there is more than one buffer fetch necessary.
if (!SingleValueReadWithReadAhead(ClassType.Value, ref reader, ref state))
if (!SingleValueReadWithReadAhead(ConverterStrategy.Value, ref reader, ref state))
{
state.BytesConsumed += reader.BytesConsumed;
return default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected internal JsonConverter()
// 2) A converter overroad HandleNull and returned false so HandleNullOnRead and HandleNullOnWrite
// will be their default values of false.

CanUseDirectReadOrWrite = !CanBePolymorphic && IsInternalConverter && ClassType == ClassType.Value;
CanUseDirectReadOrWrite = !CanBePolymorphic && IsInternalConverter && ConverterStrategy == ConverterStrategy.Value;
}

/// <summary>
Expand All @@ -53,7 +53,7 @@ public override bool CanConvert(Type typeToConvert)
return typeToConvert == typeof(T);
}

internal override ClassType ClassType => ClassType.Value;
internal override ConverterStrategy ConverterStrategy => ConverterStrategy.Value;

internal sealed override JsonPropertyInfo CreateJsonPropertyInfo()
{
Expand Down Expand Up @@ -145,7 +145,7 @@ internal virtual bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, J

internal bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out T? value)
{
if (ClassType == ClassType.Value)
if (ConverterStrategy == ConverterStrategy.Value)
{
// A value converter should never be within a continuation.
Debug.Assert(!state.IsContinuation);
Expand Down Expand Up @@ -342,7 +342,7 @@ internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions
{
if (value == null)
{
Debug.Assert(ClassType == ClassType.Value);
Debug.Assert(ConverterStrategy == ConverterStrategy.Value);
Debug.Assert(!state.IsContinuation);
Debug.Assert(HandleNullOnWrite);

Expand Down Expand Up @@ -388,7 +388,7 @@ internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions
}
}

if (ClassType == ClassType.Value)
if (ConverterStrategy == ConverterStrategy.Value)
{
Debug.Assert(!state.IsContinuation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static JsonPropertyInfo LookupProperty(
out bool useExtensionProperty,
bool createExtensionProperty = true)
{
Debug.Assert(state.Current.JsonTypeInfo.ClassType == ClassType.Object);
Debug.Assert(state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object);

useExtensionProperty = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Diagnostics;

namespace System.Text.Json.Serialization.Metadata
{
Expand Down Expand Up @@ -106,8 +107,9 @@ public static void InitializeObjectInfo<T>(
throw new ArgumentNullException(nameof(info));
}

if (info.ClassType != ClassType.Object)
if (info.PropertyInfoForTypeInfo != null)
{
// ConverterStrategy.Object is the only info type we won't have set PropertyInfoForTypeInfo for at this point.
throw new ArgumentException(SR.InitializeTypeInfoAsObjectInvalid, nameof(info));
}

Expand All @@ -122,6 +124,7 @@ public static void InitializeObjectInfo<T>(
}

((JsonTypeInfoInternal<T>)info).InitializeAsObject(options, createObjectFunc, propInitFunc, numberHandling);
Debug.Assert(info.PropertyInfoForTypeInfo!.ConverterStrategy == ConverterStrategy.Object);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class JsonPropertyInfo

private JsonTypeInfo? _runtimeTypeInfo;

internal ClassType ClassType;
internal ConverterStrategy ConverterStrategy;

internal abstract JsonConverter ConverterBase { get; set; }

Expand Down Expand Up @@ -117,7 +117,7 @@ internal void DetermineSerializationCapabilities(JsonIgnoreCondition? ignoreCond
{
Debug.Assert(MemberType == MemberTypes.Property || MemberType == MemberTypes.Field);

if ((ClassType & (ClassType.Enumerable | ClassType.Dictionary)) == 0)
if ((ConverterStrategy & (ConverterStrategy.Enumerable | ConverterStrategy.Dictionary)) == 0)
{
Debug.Assert(ignoreCondition != JsonIgnoreCondition.Always);

Expand Down Expand Up @@ -257,7 +257,7 @@ private bool NumberHandingIsApplicable()
}

if (!ConverterBase.IsInternalConverter ||
((ClassType.Enumerable | ClassType.Dictionary) & ClassType) == 0)
((ConverterStrategy.Enumerable | ConverterStrategy.Dictionary) & ConverterStrategy) == 0)
{
return false;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ internal virtual void Initialize(
Type parentClassType,
Type declaredPropertyType,
Type? runtimePropertyType,
ClassType runtimeClassType,
ConverterStrategy runtimeClassType,
MemberInfo? memberInfo,
JsonConverter converter,
JsonIgnoreCondition? ignoreCondition,
Expand All @@ -316,7 +316,7 @@ internal virtual void Initialize(
DeclaringType = parentClassType;
DeclaredPropertyType = declaredPropertyType;
RuntimePropertyType = runtimePropertyType;
ClassType = runtimeClassType;
ConverterStrategy = runtimeClassType;
MemberInfo = memberInfo;
ConverterBase = converter;
Options = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal override void Initialize(
Type parentClassType,
Type declaredPropertyType,
Type? runtimePropertyType,
ClassType runtimeClassType,
ConverterStrategy runtimeClassType,
MemberInfo? memberInfo,
JsonConverter converter,
JsonIgnoreCondition? ignoreCondition,
Expand Down Expand Up @@ -190,7 +190,7 @@ internal void InitializeForSourceGen(
_converterIsExternalAndPolymorphic = !converter.IsInternalConverter && DeclaredPropertyType != converter.TypeToConvert;
PropertyTypeCanBeNull = typeof(T).CanBeNull();
_propertyTypeEqualsTypeToConvert = converter.TypeToConvert == typeof(T);
ClassType = Converter!.ClassType;
ConverterStrategy = Converter!.ConverterStrategy;
RuntimePropertyType = DeclaredPropertyType;
DetermineIgnoreCondition(IgnoreCondition);
// TODO: this method needs to also take the number handling option for the declaring type.
Expand All @@ -212,7 +212,7 @@ internal static JsonPropertyInfo CreateForSourceGenTypeInfo(
JsonPropertyInfo<T> jsonPropertyInfo = new JsonPropertyInfo<T>();
jsonPropertyInfo.DeclaredPropertyType = declaredPropertyType;
jsonPropertyInfo.RuntimePropertyType = declaredPropertyType;
jsonPropertyInfo.ClassType = converter.ClassType;
jsonPropertyInfo.ConverterStrategy = converter.ConverterStrategy;
jsonPropertyInfo.RuntimeTypeInfo = runtimeTypeInfo;
jsonPropertyInfo.ConverterBase = converter;
jsonPropertyInfo.Options = options;
Expand Down Expand Up @@ -301,7 +301,7 @@ internal override bool GetMemberAndWriteJson(object obj, ref WriteStack state, U
if (Converter.HandleNullOnWrite)
{
// No object, collection, or re-entrancy converter handles null.
Debug.Assert(Converter.ClassType == ClassType.Value);
Debug.Assert(Converter.ConverterStrategy == ConverterStrategy.Value);

if (state.Current.PropertyState < StackFramePropertyState.Name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal static JsonPropertyInfo CreateProperty(
parentClassType,
declaredPropertyType,
runtimePropertyType,
runtimeClassType: converter.ClassType,
runtimeClassType: converter.ConverterStrategy,
memberInfo,
converter,
ignoreCondition,
Expand Down
Loading

0 comments on commit 3c06307

Please sign in to comment.