Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public JsonSerializerOptions(System.Text.Json.JsonSerializerOptions options) { }
public bool IncludeFields { get { throw null; } set { } }
public int MaxDepth { get { throw null; } set { } }
public System.Text.Json.Serialization.JsonNumberHandling NumberHandling { get { throw null; } set { } }
public System.Func<Type, bool> SupportedPolymorphicTypes { get { throw null; } set { } }
public bool PropertyNameCaseInsensitive { get { throw null; } set { } }
public System.Text.Json.JsonNamingPolicy? PropertyNamingPolicy { get { throw null; } set { } }
public System.Text.Json.JsonCommentHandling ReadCommentHandling { get { throw null; } set { } }
Expand Down Expand Up @@ -796,6 +797,11 @@ public sealed partial class JsonNumberHandlingAttribute : System.Text.Json.Seria
public JsonNumberHandlingAttribute(System.Text.Json.Serialization.JsonNumberHandling handling) { }
public System.Text.Json.Serialization.JsonNumberHandling Handling { get { throw null; } }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
public sealed partial class JsonPolymorphicTypeAttribute : System.Text.Json.Serialization.JsonAttribute
{
public JsonPolymorphicTypeAttribute() { }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public sealed partial class JsonPropertyNameAttribute : System.Text.Json.Serialization.JsonAttribute
{
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="System\Text\Json\Serialization\Attributes\JsonIgnoreAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonIncludeAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonNumberHandlingAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonPolymorphicTypeAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonPropertyNameAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonSerializableAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonMetadataServicesConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Text.Json.Serialization
{
/// <summary>
/// When placed on a type declaration, indicates that instances of that type
/// should be serialized using the schema based on their derived runtime types.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
public sealed class JsonPolymorphicTypeAttribute : JsonAttribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value,
int index = state.Current.EnumeratorIndex;

JsonConverter<TElement> elementConverter = GetElementConverter(ref state);
if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
if (GetElementTypeInfo(ref state).CanUseDirectWrite && state.Current.NumberHandling == null)
{
// Fast path that avoids validation and extra indirection.
for (; index < array.Length; index++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal sealed override bool OnTryRead(
CreateCollection(ref reader, ref state);

_valueConverter ??= GetConverter<TValue>(elementTypeInfo);
if (_valueConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
if (elementTypeInfo.CanUseDirectRead && state.Current.NumberHandling == null)
{
// Process all elements.
while (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected internal override bool OnWriteResume(
_keyConverter ??= GetConverter<TKey>(typeInfo.KeyTypeInfo!);
_valueConverter ??= GetConverter<TValue>(typeInfo.ElementTypeInfo!);

if (!state.SupportContinuation && _valueConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
if (!state.SupportContinuation && typeInfo.ElementTypeInfo!.CanUseDirectWrite && state.Current.NumberHandling == null)
{
// Fast path that avoids validation and extra indirection.
do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ protected static JsonConverter<TElement> GetElementConverter(ref WriteStack stat
return converter;
}

protected static JsonTypeInfo GetElementTypeInfo(ref WriteStack state)
{
JsonTypeInfo typeInfo = state.Current.DeclaredJsonPropertyInfo!.RuntimeTypeInfo;
Debug.Assert(typeInfo != null); // It should not be possible to have a null JsonTypeInfo

return typeInfo;
}

internal override bool OnTryRead(
ref Utf8JsonReader reader,
Type typeToConvert,
Expand All @@ -54,7 +62,7 @@ internal override bool OnTryRead(
CreateCollection(ref reader, ref state, options);

JsonConverter<TElement> elementConverter = GetElementConverter(elementTypeInfo);
if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
if (elementTypeInfo.CanUseDirectRead && state.Current.NumberHandling == null)
{
// Fast path that avoids validation and extra indirection.
while (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value,
int index = state.Current.EnumeratorIndex;
JsonConverter<object?> elementConverter = GetElementConverter(ref state);

if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
if (GetElementTypeInfo(ref state).CanUseDirectWrite && state.Current.NumberHandling == null)
{
// Fast path that avoids validation and extra indirection.
for (; index < list.Count; index++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value,
int index = state.Current.EnumeratorIndex;
JsonConverter<TElement> elementConverter = GetElementConverter(ref state);

if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
if (GetElementTypeInfo(ref state).CanUseDirectWrite && state.Current.NumberHandling == null)
{
// Fast path that avoids validation and extra indirection.
for (; index < list.Count; index++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ internal sealed override bool OnTryWrite(

if (!jsonPropertyInfo.GetMemberAndWriteJson(objectValue!, ref state, writer))
{
Debug.Assert(jsonPropertyInfo.ConverterBase.ConverterStrategy != ConverterStrategy.Value);
Debug.Assert(jsonPropertyInfo.ConverterBase.ConverterStrategy != ConverterStrategy.Value ||
jsonPropertyInfo.ConverterBase.TypeToConvert == JsonTypeInfo.ObjectType);

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public ObjectConverter()

public override void Write(Utf8JsonWriter writer, object? value, JsonSerializerOptions options)
{
throw new InvalidOperationException();
Debug.Assert(value?.GetType() == typeof(object));

writer.WriteStartObject();
writer.WriteEndObject();
}

internal override object ReadWithQuotes(ref Utf8JsonReader reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@ internal JsonConverter() { }

internal abstract ConverterStrategy ConverterStrategy { get; }

/// <summary>
/// Can direct Read or Write methods be called (for performance).
/// </summary>
internal bool CanUseDirectReadOrWrite { get; set; }

/// <summary>
/// Can the converter have $id metadata.
/// </summary>
internal virtual bool CanHaveIdMetadata => true;

internal bool CanBePolymorphic { get; set; }

/// <summary>
/// Used to support JsonObject as an extension property in a loosely-typed, trimmable manner.
/// </summary>
Expand Down
Loading