diff --git a/src/libraries/System.Text.Json/ref/System.Text.Json.cs b/src/libraries/System.Text.Json/ref/System.Text.Json.cs index 86e3c7011e176..019744c138976 100644 --- a/src/libraries/System.Text.Json/ref/System.Text.Json.cs +++ b/src/libraries/System.Text.Json/ref/System.Text.Json.cs @@ -186,144 +186,144 @@ public partial struct JsonReaderState } public static partial class JsonSerializer { - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize(System.IO.Stream utf8Json, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize(System.IO.Stream utf8Json, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize(System.ReadOnlySpan utf8Json, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize(System.ReadOnlySpan utf8Json, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] System.ReadOnlySpan json, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] System.ReadOnlySpan json, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize(this System.Text.Json.JsonDocument document, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize(this System.Text.Json.JsonDocument document, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize(this System.Text.Json.JsonElement element, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize(this System.Text.Json.JsonElement element, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize(this System.Text.Json.Nodes.JsonNode? node, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize(this System.Text.Json.Nodes.JsonNode? node, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static object? Deserialize(ref System.Text.Json.Utf8JsonReader reader, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static object? Deserialize(ref System.Text.Json.Utf8JsonReader reader, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Threading.Tasks.ValueTask DeserializeAsync(System.IO.Stream utf8Json, System.Type returnType, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static System.Threading.Tasks.ValueTask DeserializeAsync(System.IO.Stream utf8Json, System.Type returnType, System.Text.Json.Serialization.JsonSerializerContext context, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Collections.Generic.IAsyncEnumerable DeserializeAsyncEnumerable(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static System.Collections.Generic.IAsyncEnumerable DeserializeAsyncEnumerable(System.IO.Stream utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Threading.Tasks.ValueTask DeserializeAsync(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static System.Threading.Tasks.ValueTask DeserializeAsync(System.IO.Stream utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize(System.IO.Stream utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize(System.ReadOnlySpan utf8Json, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize(System.ReadOnlySpan utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] System.ReadOnlySpan json, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] System.ReadOnlySpan json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize(this System.Text.Json.JsonDocument document, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize(this System.Text.Json.JsonDocument document, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize(this System.Text.Json.JsonElement element, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize(this System.Text.Json.JsonElement element, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize(this System.Text.Json.Nodes.JsonNode? node, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize(this System.Text.Json.Nodes.JsonNode? node, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static TValue? Deserialize(ref System.Text.Json.Utf8JsonReader reader, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static TValue? Deserialize(ref System.Text.Json.Utf8JsonReader reader, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static void Serialize(System.IO.Stream utf8Json, object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null) { } public static void Serialize(System.IO.Stream utf8Json, object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context) { } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static string Serialize(object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static string Serialize(object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null) { } public static void Serialize(System.Text.Json.Utf8JsonWriter writer, object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context) { } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Threading.Tasks.Task SerializeAsync(System.IO.Stream utf8Json, object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static System.Threading.Tasks.Task SerializeAsync(System.IO.Stream utf8Json, object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Threading.Tasks.Task SerializeAsync(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static System.Threading.Tasks.Task SerializeAsync(System.IO.Stream utf8Json, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Text.Json.JsonDocument SerializeToDocument(object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static System.Text.Json.JsonDocument SerializeToDocument(object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Text.Json.JsonDocument SerializeToDocument(TValue value, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static System.Text.Json.JsonDocument SerializeToDocument(TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Text.Json.JsonElement SerializeToElement(object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static System.Text.Json.JsonElement SerializeToElement(object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Text.Json.JsonElement SerializeToElement(TValue value, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static System.Text.Json.JsonElement SerializeToElement(TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Text.Json.Nodes.JsonNode? SerializeToNode(object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static System.Text.Json.Nodes.JsonNode? SerializeToNode(object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static System.Text.Json.Nodes.JsonNode? SerializeToNode(TValue value, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static System.Text.Json.Nodes.JsonNode? SerializeToNode(TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static byte[] SerializeToUtf8Bytes(object? value, System.Type inputType, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static byte[] SerializeToUtf8Bytes(object? value, System.Type inputType, System.Text.Json.Serialization.JsonSerializerContext context) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static byte[] SerializeToUtf8Bytes(TValue value, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static byte[] SerializeToUtf8Bytes(TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static void Serialize(System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = null) { } public static void Serialize(System.IO.Stream utf8Json, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static void Serialize(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.JsonSerializerOptions? options = null) { } public static void Serialize(System.Text.Json.Utf8JsonWriter writer, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public static string Serialize(TValue value, System.Text.Json.JsonSerializerOptions? options = null) { throw null; } public static string Serialize(TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo) { throw null; } } @@ -344,8 +344,8 @@ public JsonSerializerOptions(System.Text.Json.JsonSerializerOptions options) { } public System.Text.Json.Serialization.JsonIgnoreCondition DefaultIgnoreCondition { get { throw null; } set { } } public System.Text.Json.JsonNamingPolicy? DictionaryKeyPolicy { get { throw null; } set { } } public System.Text.Encodings.Web.JavaScriptEncoder? Encoder { get { throw null; } set { } } - [System.ObsoleteAttribute("JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull.", DiagnosticId = "SYSLIB0020", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + [System.ObsoleteAttribute("JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull.", DiagnosticId="SYSLIB0020", UrlFormat="https://aka.ms/dotnet-warnings/{0}")] public bool IgnoreNullValues { get { throw null; } set { } } public bool IgnoreReadOnlyFields { get { throw null; } set { } } public bool IgnoreReadOnlyProperties { get { throw null; } set { } } @@ -356,19 +356,12 @@ public JsonSerializerOptions(System.Text.Json.JsonSerializerOptions options) { } public System.Text.Json.JsonNamingPolicy? PropertyNamingPolicy { get { throw null; } set { } } public System.Text.Json.JsonCommentHandling ReadCommentHandling { get { throw null; } set { } } public System.Text.Json.Serialization.ReferenceHandler? ReferenceHandler { get { throw null; } set { } } - public System.Collections.Generic.IList PolymorphicTypeConfigurations { get { throw null; } } - public System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver TypeInfoResolver - { - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] - [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] - get { throw null; } - set { } - } + public System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver TypeInfoResolver { [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications."), System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] get { throw null; } set { } } public System.Text.Json.Serialization.JsonUnknownTypeHandling UnknownTypeHandling { get { throw null; } set { } } public bool WriteIndented { get { throw null; } set { } } public void AddContext() where TContext : System.Text.Json.Serialization.JsonSerializerContext, new() { } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Getting a converter for a type may require reflection which depends on unreferenced code.")] - [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Getting a converter for a type may require reflection which depends on runtime code generation.")] + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Getting a converter for a type may require reflection which depends on runtime code generation.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Getting a converter for a type may require reflection which depends on unreferenced code.")] public System.Text.Json.Serialization.JsonConverter GetConverter(System.Type typeToConvert) { throw null; } } public enum JsonTokenType : byte @@ -425,8 +418,8 @@ public ref partial struct Utf8JsonReader public readonly bool ValueIsEscaped { get { throw null; } } public readonly System.Buffers.ReadOnlySequence ValueSequence { get { throw null; } } public readonly System.ReadOnlySpan ValueSpan { get { throw null; } } - public readonly int CopyString(Span utf8Destination) { throw null; } - public readonly int CopyString(Span destination) { throw null; } + public readonly int CopyString(System.Span utf8Destination) { throw null; } + public readonly int CopyString(System.Span destination) { throw null; } public bool GetBoolean() { throw null; } public byte GetByte() { throw null; } public byte[] GetBytesFromBase64() { throw null; } @@ -559,9 +552,9 @@ public void WritePropertyName(System.ReadOnlySpan utf8PropertyName) { } public void WritePropertyName(System.ReadOnlySpan propertyName) { } public void WritePropertyName(string propertyName) { } public void WritePropertyName(System.Text.Json.JsonEncodedText propertyName) { } - public void WriteRawValue([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, bool skipInputValidation = false) { } public void WriteRawValue(System.ReadOnlySpan utf8Json, bool skipInputValidation = false) { } public void WriteRawValue([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] System.ReadOnlySpan json, bool skipInputValidation = false) { } + public void WriteRawValue([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, bool skipInputValidation = false) { } public void WriteStartArray() { } public void WriteStartArray(System.ReadOnlySpan utf8PropertyName) { } public void WriteStartArray(System.ReadOnlySpan propertyName) { } @@ -619,8 +612,8 @@ public JsonArray(params System.Text.Json.Nodes.JsonNode?[] items) { } public int Count { get { throw null; } } bool System.Collections.Generic.ICollection.IsReadOnly { get { throw null; } } public void Add(System.Text.Json.Nodes.JsonNode? item) { } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Creating JsonValue instances with non-primitive types requires generating code at runtime.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.")] public void Add(T? value) { } public void Clear() { } public bool Contains(System.Text.Json.Nodes.JsonNode? item) { throw null; } @@ -647,88 +640,88 @@ internal JsonNode() { } public System.Text.Json.Nodes.JsonValue AsValue() { throw null; } public string GetPath() { throw null; } public virtual T GetValue() { throw null; } - public static explicit operator bool(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator byte(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator char(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator System.DateTime(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator System.DateTimeOffset(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator decimal(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator double(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator System.Guid(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator short(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator int(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator long(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator bool?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator byte?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator char?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator System.DateTimeOffset?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator System.DateTime?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator decimal?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator double?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator System.Guid?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator short?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator int?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator long?(System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator bool (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator byte (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator char (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator System.DateTime (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator System.DateTimeOffset (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator decimal (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator double (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator System.Guid (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator short (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator int (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator long (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator bool? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator byte? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator char? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator System.DateTimeOffset? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator System.DateTime? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator decimal? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator double? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator System.Guid? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator short? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator int? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator long? (System.Text.Json.Nodes.JsonNode? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator sbyte?(System.Text.Json.Nodes.JsonNode? value) { throw null; } - public static explicit operator float?(System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator sbyte? (System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator float? (System.Text.Json.Nodes.JsonNode? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator ushort?(System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator ushort? (System.Text.Json.Nodes.JsonNode? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator uint?(System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator uint? (System.Text.Json.Nodes.JsonNode? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator ulong?(System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator ulong? (System.Text.Json.Nodes.JsonNode? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator sbyte(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator float(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static explicit operator string?(System.Text.Json.Nodes.JsonNode? value) { throw null; } + public static explicit operator sbyte (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator float (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator string? (System.Text.Json.Nodes.JsonNode? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator ushort(System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator ushort (System.Text.Json.Nodes.JsonNode value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator uint(System.Text.Json.Nodes.JsonNode value) { throw null; } + public static explicit operator uint (System.Text.Json.Nodes.JsonNode value) { throw null; } [System.CLSCompliantAttribute(false)] - public static explicit operator ulong(System.Text.Json.Nodes.JsonNode value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(bool value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(byte value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(char value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(System.DateTime value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(System.DateTimeOffset value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(decimal value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(double value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(System.Guid value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(short value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(int value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(long value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(bool? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(byte? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(char? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(System.DateTimeOffset? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(System.DateTime? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(decimal? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(double? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(System.Guid? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(short? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(int? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(long? value) { throw null; } + public static explicit operator ulong (System.Text.Json.Nodes.JsonNode value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (bool value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (byte value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (char value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (System.DateTime value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (System.DateTimeOffset value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (decimal value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (double value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (System.Guid value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (short value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (int value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (long value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (bool? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (byte? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (char? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (System.DateTimeOffset? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (System.DateTime? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (decimal? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (double? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (System.Guid? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (short? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (int? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (long? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode?(sbyte? value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(float? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (sbyte? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (float? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode?(ushort? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (ushort? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode?(uint? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (uint? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode?(ulong? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (ulong? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode(sbyte value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode(float value) { throw null; } - public static implicit operator System.Text.Json.Nodes.JsonNode?(string? value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (sbyte value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (float value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode? (string? value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode(ushort value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (ushort value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode(uint value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (uint value) { throw null; } [System.CLSCompliantAttribute(false)] - public static implicit operator System.Text.Json.Nodes.JsonNode(ulong value) { throw null; } + public static implicit operator System.Text.Json.Nodes.JsonNode (ulong value) { throw null; } public static System.Text.Json.Nodes.JsonNode? Parse(System.IO.Stream utf8Json, System.Text.Json.Nodes.JsonNodeOptions? nodeOptions = default(System.Text.Json.Nodes.JsonNodeOptions?), System.Text.Json.JsonDocumentOptions documentOptions = default(System.Text.Json.JsonDocumentOptions)) { throw null; } public static System.Text.Json.Nodes.JsonNode? Parse(System.ReadOnlySpan utf8Json, System.Text.Json.Nodes.JsonNodeOptions? nodeOptions = default(System.Text.Json.Nodes.JsonNodeOptions?), System.Text.Json.JsonDocumentOptions documentOptions = default(System.Text.Json.JsonDocumentOptions)) { throw null; } public static System.Text.Json.Nodes.JsonNode? Parse([System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Json")] string json, System.Text.Json.Nodes.JsonNodeOptions? nodeOptions = default(System.Text.Json.Nodes.JsonNodeOptions?), System.Text.Json.JsonDocumentOptions documentOptions = default(System.Text.Json.JsonDocumentOptions)) { throw null; } @@ -767,7 +760,7 @@ public override void WriteTo(System.Text.Json.Utf8JsonWriter writer, System.Text } public abstract partial class JsonValue : System.Text.Json.Nodes.JsonNode { - private protected JsonValue(System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } + internal JsonValue() { } public static System.Text.Json.Nodes.JsonValue Create(bool value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } public static System.Text.Json.Nodes.JsonValue Create(byte value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } public static System.Text.Json.Nodes.JsonValue Create(char value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } @@ -811,10 +804,10 @@ public abstract partial class JsonValue : System.Text.Json.Nodes.JsonNode public static System.Text.Json.Nodes.JsonValue Create(uint value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } [System.CLSCompliantAttribute(false)] public static System.Text.Json.Nodes.JsonValue Create(ulong value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } - public static System.Text.Json.Nodes.JsonValue? Create(T? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed. Use the overload that takes a JsonTypeInfo, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Creating JsonValue instances with non-primitive types requires generating code at runtime.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed. Use the overload that takes a JsonTypeInfo, or make sure all of the required types are preserved.")] public static System.Text.Json.Nodes.JsonValue? Create(T? value, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } + public static System.Text.Json.Nodes.JsonValue? Create(T? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; } public abstract bool TryGetValue([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out T? value); } } @@ -840,7 +833,7 @@ public abstract partial class JsonAttribute : System.Attribute { protected JsonAttribute() { } } - [System.AttributeUsageAttribute(System.AttributeTargets.Constructor, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Constructor, AllowMultiple=false)] public sealed partial class JsonConstructorAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonConstructorAttribute() { } @@ -850,7 +843,7 @@ public abstract partial class JsonConverter internal JsonConverter() { } public abstract bool CanConvert(System.Type typeToConvert); } - [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)] public partial class JsonConverterAttribute : System.Text.Json.Serialization.JsonAttribute { protected JsonConverterAttribute() { } @@ -879,7 +872,7 @@ public abstract void Write( System.Text.Json.JsonSerializerOptions options); public virtual void WriteAsPropertyName(System.Text.Json.Utf8JsonWriter writer, T value, System.Text.Json.JsonSerializerOptions options) { } } - [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = true, Inherited = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple=true, Inherited=false)] public partial class JsonDerivedTypeAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonDerivedTypeAttribute(System.Type derivedType) { } @@ -888,12 +881,12 @@ public JsonDerivedTypeAttribute(System.Type derivedType, string typeDiscriminato public System.Type DerivedType { get { throw null; } } public object? TypeDiscriminator { get { throw null; } } } - [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)] public sealed partial class JsonExtensionDataAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonExtensionDataAttribute() { } } - [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)] public sealed partial class JsonIgnoreAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonIgnoreAttribute() { } @@ -906,7 +899,7 @@ public enum JsonIgnoreCondition WhenWritingDefault = 2, WhenWritingNull = 3, } - [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)] public sealed partial class JsonIncludeAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonIncludeAttribute() { } @@ -924,38 +917,38 @@ public enum JsonNumberHandling WriteAsString = 2, AllowNamedFloatingPointLiterals = 4, } - [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Field | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Field | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)] public sealed partial class JsonNumberHandlingAttribute : System.Text.Json.Serialization.JsonAttribute { 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)] + [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple=false, Inherited=false)] public sealed partial class JsonPolymorphicAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonPolymorphicAttribute() { } - public string? TypeDiscriminatorPropertyName { get { throw null; } set { } } public bool IgnoreUnrecognizedTypeDiscriminators { get { throw null; } set { } } + public string? TypeDiscriminatorPropertyName { get { throw null; } set { } } public System.Text.Json.Serialization.JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get { throw null; } set { } } } - [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)] public sealed partial class JsonPropertyNameAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonPropertyNameAttribute(string name) { } public string Name { get { throw null; } } } - [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)] public sealed partial class JsonPropertyOrderAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonPropertyOrderAttribute(int order) { } public int Order { get { throw null; } } } - [System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple = true)] + [System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=true)] public sealed partial class JsonSerializableAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonSerializableAttribute(System.Type type) { } - public string? TypeInfoPropertyName { get { throw null; } set { } } public System.Text.Json.Serialization.JsonSourceGenerationMode GenerationMode { get { throw null; } set { } } + public string? TypeInfoPropertyName { get { throw null; } set { } } } public abstract partial class JsonSerializerContext : System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver { @@ -963,28 +956,28 @@ protected JsonSerializerContext(System.Text.Json.JsonSerializerOptions? options) protected abstract System.Text.Json.JsonSerializerOptions? GeneratedSerializerOptions { get; } public System.Text.Json.JsonSerializerOptions Options { get { throw null; } } public abstract System.Text.Json.Serialization.Metadata.JsonTypeInfo? GetTypeInfo(System.Type type); - System.Text.Json.Serialization.Metadata.JsonTypeInfo System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options) { throw null; } + System.Text.Json.Serialization.Metadata.JsonTypeInfo System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.GetTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) { throw null; } + } + [System.FlagsAttribute] + public enum JsonSourceGenerationMode + { + Default = 0, + Metadata = 1, + Serialization = 2, } - [System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple = false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=false)] public sealed partial class JsonSourceGenerationOptionsAttribute : System.Text.Json.Serialization.JsonAttribute { public JsonSourceGenerationOptionsAttribute() { } public System.Text.Json.Serialization.JsonIgnoreCondition DefaultIgnoreCondition { get { throw null; } set { } } + public System.Text.Json.Serialization.JsonSourceGenerationMode GenerationMode { get { throw null; } set { } } public bool IgnoreReadOnlyFields { get { throw null; } set { } } public bool IgnoreReadOnlyProperties { get { throw null; } set { } } public bool IncludeFields { get { throw null; } set { } } public System.Text.Json.Serialization.JsonKnownNamingPolicy PropertyNamingPolicy { get { throw null; } set { } } public bool WriteIndented { get { throw null; } set { } } - public System.Text.Json.Serialization.JsonSourceGenerationMode GenerationMode { get { throw null; } set { } } - } - [System.FlagsAttribute] - public enum JsonSourceGenerationMode - { - Default = 0, - Metadata = 1, - Serialization = 2, } - [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] public partial class JsonStringEnumConverter : System.Text.Json.Serialization.JsonConverterFactory { public JsonStringEnumConverter() { } @@ -996,40 +989,13 @@ public enum JsonUnknownDerivedTypeHandling { FailSerialization = 0, FallBackToBaseType = 1, - FallBackToNearestAncestor = 2 + FallBackToNearestAncestor = 2, } public enum JsonUnknownTypeHandling { JsonElement = 0, JsonNode = 1, } - public partial class JsonPolymorphicTypeConfiguration : System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>, System.Collections.Generic.IEnumerable<(System.Type DerivedType, object? TypeDiscriminator)>, System.Collections.IEnumerable - { - public JsonPolymorphicTypeConfiguration(System.Type baseType) { } - public System.Type BaseType { get { throw null; } } - public string? TypeDiscriminatorPropertyName { get { throw null; } set { } } - public bool IgnoreUnrecognizedTypeDiscriminators { get { throw null; } set { } } - int System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>.Count { get { throw null; } } - public System.Text.Json.Serialization.JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get { throw null; } set { } } - bool System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>.IsReadOnly { get { throw null; } } - void System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>.Add((System.Type DerivedType, object? TypeDiscriminator) item) { } - void System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>.Clear() { } - bool System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>.Contains((System.Type DerivedType, object? TypeDiscriminator) item) { throw null; } - void System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>.CopyTo((System.Type DerivedType, object? TypeDiscriminator)[] array, int arrayIndex) { } - bool System.Collections.Generic.ICollection<(System.Type DerivedType, object? TypeDiscriminator)>.Remove((System.Type DerivedType, object? TypeDiscriminator) item) { throw null; } - System.Collections.Generic.IEnumerator<(System.Type DerivedType, object? TypeDiscriminator)> System.Collections.Generic.IEnumerable<(System.Type DerivedType, object? TypeDiscriminator)>.GetEnumerator() { throw null; } - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } - public System.Text.Json.Serialization.JsonPolymorphicTypeConfiguration WithDerivedType(System.Type derivedType) { throw null; } - public System.Text.Json.Serialization.JsonPolymorphicTypeConfiguration WithDerivedType(System.Type derivedType, int typeDiscriminator) { throw null; } - public System.Text.Json.Serialization.JsonPolymorphicTypeConfiguration WithDerivedType(System.Type derivedType, string typeDiscriminator) { throw null; } - } - public partial class JsonPolymorphicTypeConfiguration : System.Text.Json.Serialization.JsonPolymorphicTypeConfiguration where TBaseType : class - { - public JsonPolymorphicTypeConfiguration() : base(default(System.Type)) { } - public System.Text.Json.Serialization.JsonPolymorphicTypeConfiguration WithDerivedType() where TDerivedType : TBaseType { throw null; } - public System.Text.Json.Serialization.JsonPolymorphicTypeConfiguration WithDerivedType(int typeDiscriminator) where TDerivedType : TBaseType { throw null; } - public System.Text.Json.Serialization.JsonPolymorphicTypeConfiguration WithDerivedType(string typeDiscriminator) where TDerivedType : TBaseType { throw null; } - } public abstract partial class ReferenceHandler { protected ReferenceHandler() { } @@ -1052,17 +1018,15 @@ protected ReferenceResolver() { } } namespace System.Text.Json.Serialization.Metadata { - public class DefaultJsonTypeInfoResolver : System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver + public partial class DefaultJsonTypeInfoResolver : System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver { - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] public DefaultJsonTypeInfoResolver() { } - + public System.Collections.Generic.IList> Modifiers { get { throw null; } } public virtual System.Text.Json.Serialization.Metadata.JsonTypeInfo GetTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) { throw null; } - - public System.Collections.Generic.IList> Modifiers { get; } } - public interface IJsonTypeInfoResolver + public partial interface IJsonTypeInfoResolver { System.Text.Json.Serialization.Metadata.JsonTypeInfo? GetTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options); } @@ -1070,12 +1034,22 @@ public interface IJsonTypeInfoResolver public sealed partial class JsonCollectionInfoValues { public JsonCollectionInfoValues() { } - public System.Func? ObjectCreator { get { throw null; } init { } } public System.Text.Json.Serialization.Metadata.JsonTypeInfo ElementInfo { get { throw null; } init { } } public System.Text.Json.Serialization.Metadata.JsonTypeInfo? KeyInfo { get { throw null; } init { } } public System.Text.Json.Serialization.JsonNumberHandling NumberHandling { get { throw null; } init { } } + public System.Func? ObjectCreator { get { throw null; } init { } } public System.Action? SerializeHandler { get { throw null; } init { } } } + public readonly partial struct JsonDerivedType + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public JsonDerivedType(System.Type derivedType) { throw null; } + public JsonDerivedType(System.Type derivedType, int typeDiscriminator) { throw null; } + public JsonDerivedType(System.Type derivedType, string typeDiscriminator) { throw null; } + public System.Type DerivedType { get { throw null; } } + public object? TypeDiscriminator { get { throw null; } } + } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public static partial class JsonMetadataServices { @@ -1092,12 +1066,12 @@ public static partial class JsonMetadataServices public static System.Text.Json.Serialization.JsonConverter Int32Converter { get { throw null; } } public static System.Text.Json.Serialization.JsonConverter Int64Converter { get { throw null; } } public static System.Text.Json.Serialization.JsonConverter JsonArrayConverter { get { throw null; } } + public static System.Text.Json.Serialization.JsonConverter JsonDocumentConverter { get { throw null; } } public static System.Text.Json.Serialization.JsonConverter JsonElementConverter { get { throw null; } } public static System.Text.Json.Serialization.JsonConverter JsonNodeConverter { get { throw null; } } public static System.Text.Json.Serialization.JsonConverter JsonObjectConverter { get { throw null; } } public static System.Text.Json.Serialization.JsonConverter JsonValueConverter { get { throw null; } } - public static System.Text.Json.Serialization.JsonConverter JsonDocumentConverter { get { throw null; } } - public static System.Text.Json.Serialization.JsonConverter ObjectConverter { get { throw null; } } + public static System.Text.Json.Serialization.JsonConverter ObjectConverter { get { throw null; } } [System.CLSCompliantAttribute(false)] public static System.Text.Json.Serialization.JsonConverter SByteConverter { get { throw null; } } public static System.Text.Json.Serialization.JsonConverter SingleConverter { get { throw null; } } @@ -1135,10 +1109,10 @@ public static partial class JsonMetadataServices public static System.Text.Json.Serialization.Metadata.JsonTypeInfo CreateStackInfo(System.Text.Json.JsonSerializerOptions options, System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues collectionInfo, System.Action addFunc) where TCollection : System.Collections.IEnumerable { throw null; } public static System.Text.Json.Serialization.Metadata.JsonTypeInfo CreateStackInfo(System.Text.Json.JsonSerializerOptions options, System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues collectionInfo) where TCollection : System.Collections.Generic.Stack { throw null; } public static System.Text.Json.Serialization.Metadata.JsonTypeInfo CreateValueInfo(System.Text.Json.JsonSerializerOptions options, System.Text.Json.Serialization.JsonConverter converter) { throw null; } - public static System.Text.Json.Serialization.JsonConverter GetUnsupportedTypeConverter() { throw null; } public static System.Text.Json.Serialization.JsonConverter GetEnumConverter(System.Text.Json.JsonSerializerOptions options) where T : struct { throw null; } - public static System.Text.Json.Serialization.JsonConverter GetNullableConverter(System.Text.Json.Serialization.Metadata.JsonTypeInfo underlyingTypeInfo) where T : struct { throw null; } public static System.Text.Json.Serialization.JsonConverter GetNullableConverter(System.Text.Json.JsonSerializerOptions options) where T : struct { throw null; } + public static System.Text.Json.Serialization.JsonConverter GetNullableConverter(System.Text.Json.Serialization.Metadata.JsonTypeInfo underlyingTypeInfo) where T : struct { throw null; } + public static System.Text.Json.Serialization.JsonConverter GetUnsupportedTypeConverter() { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public sealed partial class JsonObjectInfoValues @@ -1161,6 +1135,15 @@ public JsonParameterInfoValues() { } public System.Type ParameterType { get { throw null; } init { } } public int Position { get { throw null; } init { } } } + public partial class JsonPolymorphismOptions + { + public JsonPolymorphismOptions() { } + public System.Collections.Generic.IList DerivedTypes { get { throw null; } } + public bool IgnoreUnrecognizedTypeDiscriminators { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.AllowNullAttribute] + public string TypeDiscriminatorPropertyName { get { throw null; } set { } } + public System.Text.Json.Serialization.JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get { throw null; } set { } } + } public abstract partial class JsonPropertyInfo { internal JsonPropertyInfo() { } @@ -1168,8 +1151,8 @@ internal JsonPropertyInfo() { } public System.Func? Get { get { throw null; } set { } } public string Name { get { throw null; } set { } } public System.Text.Json.Serialization.JsonNumberHandling? NumberHandling { get { throw null; } set { } } - public System.Type PropertyType { get { throw null; } } public System.Text.Json.JsonSerializerOptions Options { get { throw null; } } + public System.Type PropertyType { get { throw null; } } public System.Action? Set { get { throw null; } set { } } public System.Func? ShouldSerialize { get { throw null; } set { } } } @@ -1180,8 +1163,8 @@ public JsonPropertyInfoValues() { } public System.Text.Json.Serialization.JsonConverter? Converter { get { throw null; } init { } } public System.Type DeclaringType { get { throw null; } init { } } public System.Func? Getter { get { throw null; } init { } } - public System.Text.Json.Serialization.JsonIgnoreCondition? IgnoreCondition { get { throw null; } init { } } public bool HasJsonInclude { get { throw null; } init { } } + public System.Text.Json.Serialization.JsonIgnoreCondition? IgnoreCondition { get { throw null; } init { } } public bool IsExtensionData { get { throw null; } init { } } public bool IsProperty { get { throw null; } init { } } public bool IsPublic { get { throw null; } init { } } @@ -1192,27 +1175,35 @@ public JsonPropertyInfoValues() { } public System.Text.Json.Serialization.Metadata.JsonTypeInfo PropertyTypeInfo { get { throw null; } init { } } public System.Action? Setter { get { throw null; } init { } } } - public static class JsonTypeInfoResolver - { - public static System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver Combine(params System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver[] resolvers) { throw null; } - } public abstract partial class JsonTypeInfo { internal JsonTypeInfo() { } - public System.Text.Json.JsonSerializerOptions Options { get { throw null; } } - public System.Collections.Generic.IList Properties { get { throw null; } } - public System.Type Type { get { throw null; } } public System.Text.Json.Serialization.JsonConverter Converter { get { throw null; } } public System.Func? CreateObject { get { throw null; } set { } } public System.Text.Json.Serialization.Metadata.JsonTypeInfoKind Kind { get { throw null; } } public System.Text.Json.Serialization.JsonNumberHandling? NumberHandling { get { throw null; } set { } } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] - [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] - public static System.Text.Json.Serialization.Metadata.JsonTypeInfo CreateJsonTypeInfo(System.Text.Json.JsonSerializerOptions options) { throw null; } - [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] - [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] + public System.Text.Json.JsonSerializerOptions Options { get { throw null; } } + public System.Text.Json.Serialization.Metadata.JsonPolymorphismOptions? PolymorphismOptions { get { throw null; } set { } } + public System.Collections.Generic.IList Properties { get { throw null; } } + public System.Type Type { get { throw null; } } + public System.Text.Json.Serialization.Metadata.JsonPropertyInfo CreateJsonPropertyInfo(System.Type propertyType, string name) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] public static System.Text.Json.Serialization.Metadata.JsonTypeInfo CreateJsonTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) { throw null; } - public JsonPropertyInfo CreateJsonPropertyInfo(Type propertyType, string name) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use generic overload or System.Text.Json source generation for native AOT applications.")] + public static System.Text.Json.Serialization.Metadata.JsonTypeInfo CreateJsonTypeInfo(System.Text.Json.JsonSerializerOptions options) { throw null; } + } + public enum JsonTypeInfoKind + { + None = 0, + Object = 1, + Enumerable = 2, + Dictionary = 3, + } + public static partial class JsonTypeInfoResolver + { + public static System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver Combine(params System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver[] resolvers) { throw null; } } public abstract partial class JsonTypeInfo : System.Text.Json.Serialization.Metadata.JsonTypeInfo { @@ -1221,11 +1212,4 @@ internal JsonTypeInfo() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public System.Action? SerializeHandler { get { throw null; } } } - public enum JsonTypeInfoKind - { - None = 0, - Object = 1, - Enumerable = 2, - Dictionary = 3 - } } diff --git a/src/libraries/System.Text.Json/src/Resources/Strings.resx b/src/libraries/System.Text.Json/src/Resources/Strings.resx index c5b2e82a14bfc..c951a42f10b11 100644 --- a/src/libraries/System.Text.Json/src/Resources/Strings.resx +++ b/src/libraries/System.Text.Json/src/Resources/Strings.resx @@ -665,4 +665,7 @@ JsonTypeInfo metadata references a JsonSerializerOptions instance that doesn't specify a TypeInfoResolver. + + Parameter already associated with a different JsonTypeInfo instance. + diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index d76756fb60540..ba9de5afc0979 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -1,4 +1,4 @@ - + $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) true @@ -123,12 +123,13 @@ The System.Text.Json library is built-in as part of the shared framework in .NET - + + + - @@ -237,10 +238,8 @@ The System.Text.Json library is built-in as part of the shared framework in .NET - - diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ConfigurationList.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ConfigurationList.cs index 5199a93bba29c..fa283853ebe34 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ConfigurationList.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ConfigurationList.cs @@ -22,7 +22,6 @@ public ConfigurationList(IList? source = null) protected abstract bool IsLockedInstance { get; } protected abstract void VerifyMutable(); - protected virtual void OnItemAdded(TItem item) { } public TItem this[int index] { @@ -39,7 +38,6 @@ public TItem this[int index] VerifyMutable(); _list[index] = value; - OnItemAdded(value); } } @@ -56,7 +54,6 @@ public void Add(TItem item) VerifyMutable(); _list.Add(item); - OnItemAdded(item); } public void Clear() @@ -94,7 +91,6 @@ public void Insert(int index, TItem item) VerifyMutable(); _list.Insert(index, item); - OnItemAdded(item); } public bool Remove(TItem item) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPolymorphicTypeConfiguration.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPolymorphicTypeConfiguration.cs deleted file mode 100644 index caa6e573ec0e8..0000000000000 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPolymorphicTypeConfiguration.cs +++ /dev/null @@ -1,246 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.Text.Json.Serialization.Metadata; - -namespace System.Text.Json.Serialization -{ - /// - /// Defines polymorphic configuration for a specified base type. - /// - public class JsonPolymorphicTypeConfiguration : IJsonPolymorphicTypeConfiguration, ICollection<(Type DerivedType, object? TypeDiscriminator)> - { - private readonly List<(Type DerivedType, object? TypeDiscriminator)> _derivedTypes = new(); - private string? _customTypeDiscriminatorPropertyName; - private JsonUnknownDerivedTypeHandling _unknownDerivedTypeHandling; - private bool _ignoreUnrecognizedTypeDiscriminators; - - /// - /// Creates a new polymorphic configuration instance for a given base type. - /// - /// The base type for which to configure polymorphic serialization. - public JsonPolymorphicTypeConfiguration(Type baseType) - { - if (baseType is null) - { - throw new ArgumentNullException(nameof(baseType)); - } - - if (!PolymorphicTypeResolver.IsSupportedPolymorphicBaseType(baseType)) - { - throw new ArgumentException(SR.Format(SR.Polymorphism_TypeDoesNotSupportPolymorphism, baseType), nameof(baseType)); - } - - BaseType = baseType; - } - - /// - /// Gets the base type for which polymorphic serialization is being configured. - /// - public Type BaseType { get; } - - /// - /// Gets or sets the behavior when serializing an undeclared derived runtime type. - /// - public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling - { - get => _unknownDerivedTypeHandling; - set - { - VerifyMutable(); - _unknownDerivedTypeHandling = value; - } - } - - /// - /// When set to , instructs the serializer to ignore any - /// unrecognized type discriminator id's and reverts to the contract of the base type. - /// Otherwise, it will fail the deserialization. - /// - public bool IgnoreUnrecognizedTypeDiscriminators - { - get => _ignoreUnrecognizedTypeDiscriminators; - set - { - VerifyMutable(); - _ignoreUnrecognizedTypeDiscriminators = value; - } - } - - /// - /// Gets or sets a custom type discriminator property name for the polymorhic type. - /// Uses the default '$type' property name if left unset. - /// - public string? TypeDiscriminatorPropertyName - { - get => _customTypeDiscriminatorPropertyName; - set - { - VerifyMutable(); - _customTypeDiscriminatorPropertyName = value; - } - } - - /// - /// Opts in polymorphic serialization for the specified derived type. - /// - /// The derived type for which to enable polymorphism. - /// The same instance after it has been updated. - public JsonPolymorphicTypeConfiguration WithDerivedType(Type derivedType) - => WithDerivedTypeCore(derivedType, null); - - /// - /// Opts in polymorphic serialization for the specified derived type. - /// - /// The derived type for which to enable polymorphism. - /// The type discriminator id to use for the specified derived type. - /// The same instance after it has been updated. - public JsonPolymorphicTypeConfiguration WithDerivedType(Type derivedType, string typeDiscriminator) => - WithDerivedTypeCore(derivedType, typeDiscriminator); - - /// - /// Opts in polymorphic serialization for the specified derived type. - /// - /// The derived type for which to enable polymorphism. - /// The type discriminator id to use for the specified derived type. - /// The same instance after it has been updated. - public JsonPolymorphicTypeConfiguration WithDerivedType(Type derivedType, int typeDiscriminator) => - WithDerivedTypeCore(derivedType, typeDiscriminator); - - private JsonPolymorphicTypeConfiguration WithDerivedTypeCore(Type derivedType, object? typeDiscriminator) - { - Debug.Assert(typeDiscriminator is null or string or int); - - VerifyMutable(); - - if (derivedType is null) - { - throw new ArgumentNullException(nameof(derivedType)); - } - - if (!PolymorphicTypeResolver.IsSupportedDerivedType(BaseType, derivedType)) - { - throw new ArgumentException(SR.Format(SR.Polymorphism_DerivedTypeIsNotSupported, derivedType, BaseType), nameof(derivedType)); - } - - // Perform a linear traversal to determine any duplicate derived types or discriminator Id's - // The assumption is that each type maintains a small number of subtypes so this is preferable - // to maintaing hashtables to existing entries. - foreach ((Type DerivedType, object? TypeDiscriminator) entry in _derivedTypes) - { - if (entry.DerivedType == derivedType) - { - throw new ArgumentException(SR.Format(SR.Polymorphism_DerivedTypeIsAlreadySpecified, BaseType, derivedType), nameof(derivedType)); - } - - if (typeDiscriminator != null && typeDiscriminator.Equals(entry.TypeDiscriminator)) - { - throw new ArgumentException(SR.Format(SR.Polymorphism_TypeDicriminatorIdIsAlreadySpecified, BaseType, typeDiscriminator), nameof(typeDiscriminator)); - } - } - - // Validation complete; update the configuration state. - _derivedTypes.Add((derivedType, typeDiscriminator)); - return this; - } - - IEnumerable<(Type DerivedType, object? TypeDiscriminator)> IJsonPolymorphicTypeConfiguration.GetSupportedDerivedTypes() - { - foreach ((Type, string?) entry in _derivedTypes) - { - yield return entry; - } - } - - internal bool IsAssignedToOptionsInstance { get; set; } - - private void VerifyMutable() - { - if (IsAssignedToOptionsInstance) - { - ThrowHelper.ThrowInvalidOperationException_SerializerOptionsImmutable(context: null); - } - } - - bool ICollection<(Type DerivedType, object? TypeDiscriminator)>.Contains((Type DerivedType, object? TypeDiscriminator) item) => _derivedTypes.Contains(item); - void ICollection<(Type DerivedType, object? TypeDiscriminator)>.CopyTo((Type DerivedType, object? TypeDiscriminator)[] array, int arrayIndex) => _derivedTypes.CopyTo(array, arrayIndex); - bool ICollection<(Type DerivedType, object? TypeDiscriminator)>.Remove((Type DerivedType, object? TypeDiscriminator) item) - { - VerifyMutable(); - return _derivedTypes.Remove(item); - } - - bool ICollection<(Type DerivedType, object? TypeDiscriminator)>.IsReadOnly => IsAssignedToOptionsInstance; - int ICollection<(Type DerivedType, object? TypeDiscriminator)>.Count => _derivedTypes.Count; - void ICollection<(Type DerivedType, object? TypeDiscriminator)>.Add((Type DerivedType, object? TypeDiscriminator) item) - { - if (item.TypeDiscriminator is not (null or string or int)) - { - throw new ArgumentException(nameof(item)); - } - - WithDerivedTypeCore(item.DerivedType, item.TypeDiscriminator); - } - - void ICollection<(Type DerivedType, object? TypeDiscriminator)>.Clear() - { - VerifyMutable(); - _derivedTypes.Clear(); - } - - IEnumerator<(Type DerivedType, object? TypeDiscriminator)> IEnumerable<(Type DerivedType, object? TypeDiscriminator)>.GetEnumerator() => _derivedTypes.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => _derivedTypes.GetEnumerator(); - } - - /// - /// Defines polymorphic type configuration for a given type. - /// - /// The type for which polymorphic configuration is provided. - public class JsonPolymorphicTypeConfiguration : JsonPolymorphicTypeConfiguration where TBaseType : class - { - /// - /// Creates a new polymorphic configuration instance for a given base type. - /// - public JsonPolymorphicTypeConfiguration() : base(typeof(TBaseType)) - { - } - - /// - /// Associates specified derived type with supplied string identifier. - /// - /// The derived type with which to associate a type identifier. - /// The same instance after it has been updated. - public JsonPolymorphicTypeConfiguration WithDerivedType() where TDerivedType : TBaseType - { - WithDerivedType(typeof(TDerivedType)); - return this; - } - - /// - /// Associates specified derived type with supplied string identifier. - /// - /// The derived type with which to associate a type identifier. - /// The type identifier to use for the specified derived type. - /// The same instance after it has been updated. - public JsonPolymorphicTypeConfiguration WithDerivedType(string typeDiscriminator) where TDerivedType : TBaseType - { - WithDerivedType(typeof(TDerivedType), typeDiscriminator); - return this; - } - - /// - /// Associates specified derived type with supplied string identifier. - /// - /// The derived type with which to associate a type identifier. - /// The type identifier to use for the specified derived type. - /// The same instance after it has been updated. - public JsonPolymorphicTypeConfiguration WithDerivedType(int typeDiscriminator) where TDerivedType : TBaseType - { - WithDerivedType(typeof(TDerivedType), typeDiscriminator); - return this; - } - } -} diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs index 05f5707890e0c..029c2b43fc38c 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs @@ -11,10 +11,15 @@ namespace System.Text.Json { public static partial class JsonSerializer { - internal static readonly byte[] s_idPropertyName = "$id"u8.ToArray(); - internal static readonly byte[] s_refPropertyName = "$ref"u8.ToArray(); - internal static readonly byte[] s_typePropertyName = "$type"u8.ToArray(); - internal static readonly byte[] s_valuesPropertyName = "$values"u8.ToArray(); + internal const string IdPropertyName = "$id"; + internal const string RefPropertyName = "$ref"; + internal const string TypePropertyName = "$type"; + internal const string ValuesPropertyName = "$values"; + + internal static readonly byte[] s_idPropertyName = Encoding.UTF8.GetBytes(IdPropertyName); + internal static readonly byte[] s_refPropertyName = Encoding.UTF8.GetBytes(RefPropertyName); + internal static readonly byte[] s_typePropertyName = Encoding.UTF8.GetBytes(TypePropertyName); + internal static readonly byte[] s_valuesPropertyName = Encoding.UTF8.GetBytes(ValuesPropertyName); internal static bool TryReadMetadata(JsonConverter converter, JsonTypeInfo jsonTypeInfo, ref Utf8JsonReader reader, ref ReadStack state) { @@ -97,7 +102,7 @@ internal static bool TryReadMetadata(JsonConverter converter, JsonTypeInfo jsonT break; case MetadataPropertyName.Type: - state.Current.JsonPropertyName = jsonTypeInfo.PolymorphicTypeResolver?.CustomTypeDiscriminatorPropertyNameUtf8 ?? s_typePropertyName; + state.Current.JsonPropertyName = jsonTypeInfo.PolymorphicTypeResolver?.TypeDiscriminatorPropertyNameUtf8 ?? s_typePropertyName; if (jsonTypeInfo.PolymorphicTypeResolver is null) { @@ -219,7 +224,7 @@ internal static bool IsMetadataPropertyName(ReadOnlySpan propertyName, Pol { return (propertyName.Length > 0 && propertyName[0] == '$') || - (resolver?.CustomTypeDiscriminatorPropertyNameUtf8?.AsSpan().SequenceEqual(propertyName) == true); + (resolver?.TypeDiscriminatorPropertyNameUtf8?.AsSpan().SequenceEqual(propertyName) == true); } internal static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan propertyName, PolymorphicTypeResolver? resolver) @@ -245,7 +250,7 @@ internal static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan } break; - case 5 when resolver?.CustomTypeDiscriminatorPropertyNameUtf8 is null: + case 5 when resolver?.TypeDiscriminatorPropertyNameUtf8 is null: if (propertyName[1] == 't' && propertyName[2] == 'y' && propertyName[3] == 'p' && @@ -269,7 +274,7 @@ internal static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan } } - if (resolver?.CustomTypeDiscriminatorPropertyNameUtf8 is byte[] customTypeDiscriminator && + if (resolver?.TypeDiscriminatorPropertyNameUtf8 is byte[] customTypeDiscriminator && propertyName.SequenceEqual(customTypeDiscriminator)) { return MetadataPropertyName.Type; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleMetadata.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleMetadata.cs index 18b6c1775edba..65f3d4938364b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleMetadata.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.HandleMetadata.cs @@ -9,10 +9,10 @@ namespace System.Text.Json public static partial class JsonSerializer { // Pre-encoded metadata properties. - internal static readonly JsonEncodedText s_metadataId = JsonEncodedText.Encode("$id", encoder: null); - internal static readonly JsonEncodedText s_metadataRef = JsonEncodedText.Encode("$ref", encoder: null); - internal static readonly JsonEncodedText s_metadataType = JsonEncodedText.Encode("$type", encoder: null); - internal static readonly JsonEncodedText s_metadataValues = JsonEncodedText.Encode("$values", encoder: null); + internal static readonly JsonEncodedText s_metadataId = JsonEncodedText.Encode(IdPropertyName, encoder: null); + internal static readonly JsonEncodedText s_metadataRef = JsonEncodedText.Encode(RefPropertyName, encoder: null); + internal static readonly JsonEncodedText s_metadataType = JsonEncodedText.Encode(TypePropertyName, encoder: null); + internal static readonly JsonEncodedText s_metadataValues = JsonEncodedText.Encode(ValuesPropertyName, encoder: null); internal static MetadataPropertyName WriteMetadataForObject( JsonConverter jsonConverter, diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs index a1f67039f0b80..e06683aebf243 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs @@ -313,8 +313,7 @@ public bool Equals(JsonSerializerOptions? left, JsonSerializerOptions? right) left._propertyNameCaseInsensitive == right._propertyNameCaseInsensitive && left._writeIndented == right._writeIndented && NormalizeResolver(left._typeInfoResolver) == NormalizeResolver(right._typeInfoResolver) && - CompareLists(left._converters, right._converters) && - CompareLists(left._polymorphicTypeConfigurations, right._polymorphicTypeConfigurations); + CompareLists(left._converters, right._converters); static bool CompareLists(ConfigurationList left, ConfigurationList right) { @@ -359,7 +358,6 @@ public int GetHashCode(JsonSerializerOptions options) hc.Add(options._writeIndented); hc.Add(NormalizeResolver(options._typeInfoResolver)); GetHashCode(ref hc, options._converters); - GetHashCode(ref hc, options._polymorphicTypeConfigurations); static void GetHashCode(ref HashCode hc, ConfigurationList list) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs index e92ebb0408288..ff47237a29267 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs @@ -26,14 +26,6 @@ public sealed partial class JsonSerializerOptions /// public IList Converters => _converters; - /// - /// The list of custom polymorphic type configurations. - /// - /// - /// Once serialization or deserialization occurs, the list cannot be modified. - /// - public IList PolymorphicTypeConfigurations => _polymorphicTypeConfigurations; - // This may return factory converter internal JsonConverter? GetCustomConverterFromMember(Type? parentClassType, Type typeToConvert, MemberInfo? memberInfo) { 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 a4403c8829b76..1a40930ea450c 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 @@ -43,7 +43,6 @@ public sealed partial class JsonSerializerOptions private ReferenceHandler? _referenceHandler; private JavaScriptEncoder? _encoder; private ConfigurationList _converters; - private ConfigurationList _polymorphicTypeConfigurations; private JsonIgnoreCondition _defaultIgnoreCondition; private JsonNumberHandling _numberHandling; private JsonUnknownTypeHandling _unknownTypeHandling; @@ -66,7 +65,6 @@ public sealed partial class JsonSerializerOptions public JsonSerializerOptions() { _converters = new ConverterList(this); - _polymorphicTypeConfigurations = new PolymorphicConfigurationList(this); TrackOptionsInstance(this); } @@ -90,7 +88,6 @@ public JsonSerializerOptions(JsonSerializerOptions options) _readCommentHandling = options._readCommentHandling; _referenceHandler = options._referenceHandler; _converters = new ConverterList(this, options._converters); - _polymorphicTypeConfigurations = new PolymorphicConfigurationList(this, options._polymorphicTypeConfigurations); _encoder = options._encoder; _defaultIgnoreCondition = options._defaultIgnoreCondition; _numberHandling = options._numberHandling; @@ -749,21 +746,6 @@ public ConverterList(JsonSerializerOptions options, IList? source protected override void VerifyMutable() => _options.VerifyMutable(); } - private sealed class PolymorphicConfigurationList : ConfigurationList - { - private readonly JsonSerializerOptions _options; - - public PolymorphicConfigurationList(JsonSerializerOptions options, IList? source = null) - : base(source) - { - _options = options; - } - - protected override bool IsLockedInstance => _options._isLockedInstance; - protected override void VerifyMutable() => _options.VerifyMutable(); - protected override void OnItemAdded(JsonPolymorphicTypeConfiguration config) => config.IsAssignedToOptionsInstance = true; - } - private static JsonSerializerOptions CreateDefaultImmutableInstance() { var options = new JsonSerializerOptions { _isLockedInstance = true }; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/AttributePolymorphicTypeConfiguration.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/AttributePolymorphicTypeConfiguration.cs deleted file mode 100644 index fbbc70b432819..0000000000000 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/AttributePolymorphicTypeConfiguration.cs +++ /dev/null @@ -1,61 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; -using System.Reflection; - -namespace System.Text.Json.Serialization.Metadata -{ - /// - /// Maps attribute-based polymorphism configuration to IJsonPolymorphicTypeConfiguration - /// - internal sealed class AttributePolymorphicTypeConfiguration : IJsonPolymorphicTypeConfiguration - { -#pragma warning disable CA2252 // This API requires opting into preview features - private readonly JsonPolymorphicAttribute? _polymorphicTypeAttribute; - private readonly IEnumerable _derivedTypeAttributes; - - private AttributePolymorphicTypeConfiguration(Type baseType, JsonPolymorphicAttribute? polymorphicTypeAttribute, IEnumerable derivedTypeAttributes) - { - BaseType = baseType; - _polymorphicTypeAttribute = polymorphicTypeAttribute; - _derivedTypeAttributes = derivedTypeAttributes; - } - - public static AttributePolymorphicTypeConfiguration? Create(Type baseType) - { - JsonPolymorphicAttribute? polymorphicTypeAttribute = baseType.GetCustomAttribute(inherit: false); - IEnumerable derivedTypeAttributes = baseType.GetCustomAttributes(inherit: false); - - if (polymorphicTypeAttribute is null && IsEmpty(derivedTypeAttributes)) - { - return null; - } - - return new AttributePolymorphicTypeConfiguration(baseType, polymorphicTypeAttribute, derivedTypeAttributes); - - static bool IsEmpty(IEnumerable source) - { - using IEnumerator enumerator = source.GetEnumerator(); - return !enumerator.MoveNext(); - } - } - - public Type BaseType { get; } - - public string? TypeDiscriminatorPropertyName => _polymorphicTypeAttribute?.TypeDiscriminatorPropertyName; - - public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling => _polymorphicTypeAttribute?.UnknownDerivedTypeHandling ?? default; - - public bool IgnoreUnrecognizedTypeDiscriminators => _polymorphicTypeAttribute?.IgnoreUnrecognizedTypeDiscriminators ?? false; - - public IEnumerable<(Type DerivedType, object? TypeDiscriminator)> GetSupportedDerivedTypes() - { - foreach (JsonDerivedTypeAttribute attribute in _derivedTypeAttributes) - { - yield return (attribute.DerivedType, attribute.TypeDiscriminator); - } - } -#pragma warning restore CA2252 // This API requires opting into preview features - } -} diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/IJsonPolymorphicTypeConfiguration.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/IJsonPolymorphicTypeConfiguration.cs deleted file mode 100644 index 3fbf234a99423..0000000000000 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/IJsonPolymorphicTypeConfiguration.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; - -namespace System.Text.Json.Serialization.Metadata -{ - internal interface IJsonPolymorphicTypeConfiguration - { -#pragma warning disable CA2252 // This API requires opting into preview features - Type BaseType { get; } - string? TypeDiscriminatorPropertyName { get; } - JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get; } - bool IgnoreUnrecognizedTypeDiscriminators { get; } - IEnumerable<(Type DerivedType, object? TypeDiscriminator)> GetSupportedDerivedTypes(); -#pragma warning restore CA2252 // This API requires opting into preview features - } -} diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonDerivedType.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonDerivedType.cs new file mode 100644 index 0000000000000..1d49c49d29617 --- /dev/null +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonDerivedType.cs @@ -0,0 +1,68 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics; + +namespace System.Text.Json.Serialization.Metadata +{ + /// + /// Represents a supported derived type defined in the metadata of a polymorphic type. + /// + public readonly struct JsonDerivedType + { + /// + /// Specifies a supported derived type without a type discriminator. + /// + /// The derived type to be supported by the polymorphic type metadata. + public JsonDerivedType(Type derivedType) + { + DerivedType = derivedType; + TypeDiscriminator = null; + } + + /// + /// Specifies a supported derived type with an integer type discriminator. + /// + /// The derived type to be supported by the polymorphic type metadata. + /// The type discriminator to be associated with the derived type. + public JsonDerivedType(Type derivedType, int typeDiscriminator) + { + DerivedType = derivedType; + TypeDiscriminator = typeDiscriminator; + } + + /// + /// Specifies a supported derived type with a string type discriminator. + /// + /// The derived type to be supported by the polymorphic type metadata. + /// The type discriminator to be associated with the derived type. + public JsonDerivedType(Type derivedType, string typeDiscriminator) + { + DerivedType = derivedType; + TypeDiscriminator = typeDiscriminator; + } + + internal JsonDerivedType(Type derivedType, object? typeDiscriminator) + { + Debug.Assert(typeDiscriminator is null or int or string); + DerivedType = derivedType; + TypeDiscriminator = typeDiscriminator; + } + + /// + /// A derived type that should be supported in polymorphic serialization of the declared base type. + /// + public Type DerivedType { get; } + + /// + /// The type discriminator identifier to be used for the serialization of the subtype. + /// + public object? TypeDiscriminator { get; } + + internal void Deconstruct(out Type derivedType, out object? typeDiscriminator) + { + derivedType = DerivedType; + typeDiscriminator = TypeDiscriminator; + } + } +} diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPolymorphismOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPolymorphismOptions.cs new file mode 100644 index 0000000000000..f550e910b2708 --- /dev/null +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPolymorphismOptions.cs @@ -0,0 +1,107 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; + +namespace System.Text.Json.Serialization.Metadata +{ + /// + /// Defines polymorphic configuration for a specified base type. + /// + public class JsonPolymorphismOptions + { + private DerivedTypeList? _derivedTypes; + private bool _ignoreUnrecognizedTypeDiscriminators; + private JsonUnknownDerivedTypeHandling _unknownDerivedTypeHandling; + private string? _typeDiscriminatorPropertyName; + + /// + /// Gets the list of derived types supported in the current polymorphic type configuration. + /// + public IList DerivedTypes => _derivedTypes ??= new(this); + + /// + /// When set to , instructs the serializer to ignore any + /// unrecognized type discriminator id's and reverts to the contract of the base type. + /// Otherwise, it will fail the deserialization. + /// + public bool IgnoreUnrecognizedTypeDiscriminators + { + get => _ignoreUnrecognizedTypeDiscriminators; + set + { + VerifyMutable(); + _ignoreUnrecognizedTypeDiscriminators = value; + } + } + + /// + /// Gets or sets the behavior when serializing an undeclared derived runtime type. + /// + public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling + { + get => _unknownDerivedTypeHandling; + set + { + VerifyMutable(); + _unknownDerivedTypeHandling = value; + } + } + + /// + /// Gets or sets a custom type discriminator property name for the polymorhic type. + /// Uses the default '$type' property name if left unset. + /// + [AllowNull] + public string TypeDiscriminatorPropertyName + { + get => _typeDiscriminatorPropertyName ?? JsonSerializer.TypePropertyName; + set + { + VerifyMutable(); + _typeDiscriminatorPropertyName = value; + } + } + + private void VerifyMutable() => DeclaringTypeInfo?.VerifyMutable(); + + internal JsonTypeInfo? DeclaringTypeInfo { get; set; } + + private sealed class DerivedTypeList : ConfigurationList + { + private readonly JsonPolymorphismOptions _parent; + + public DerivedTypeList(JsonPolymorphismOptions parent) + { + _parent = parent; + } + + protected override bool IsLockedInstance => _parent.DeclaringTypeInfo?.IsConfigured == true; + protected override void VerifyMutable() => _parent.DeclaringTypeInfo?.VerifyMutable(); + } + + internal static JsonPolymorphismOptions? CreateFromAttributeDeclarations(Type baseType) + { + JsonPolymorphismOptions? options = null; + + if (baseType.GetCustomAttribute(inherit: false) is JsonPolymorphicAttribute polymorphicAttribute) + { + options = new() + { + IgnoreUnrecognizedTypeDiscriminators = polymorphicAttribute.IgnoreUnrecognizedTypeDiscriminators, + UnknownDerivedTypeHandling = polymorphicAttribute.UnknownDerivedTypeHandling, + TypeDiscriminatorPropertyName = polymorphicAttribute.TypeDiscriminatorPropertyName, + }; + } + + foreach (JsonDerivedTypeAttribute attr in baseType.GetCustomAttributes(inherit: false)) + { + (options ??= new()).DerivedTypes.Add(new JsonDerivedType(attr.DerivedType, attr.TypeDiscriminator)); + } + + return options; + } + } +} 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 055c94c8334b9..d049bdf8dc9b1 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 @@ -67,6 +67,32 @@ public IList Properties } } + /// + /// Gets or sets a configuration object specifying polymorphism metadata. + /// + public JsonPolymorphismOptions? PolymorphismOptions + { + get => _polymorphismOptions; + set + { + VerifyMutable(); + + if (value != null) + { + if (value.DeclaringTypeInfo != null && value.DeclaringTypeInfo != this) + { + ThrowHelper.ThrowArgumentException_JsonPolymorphismOptionsAssociatedWithDifferentJsonTypeInfo(nameof(value)); + } + + value.DeclaringTypeInfo = this; + } + + _polymorphismOptions = value; + } + } + + private JsonPolymorphismOptions? _polymorphismOptions; + internal object? CreateObjectWithArgs { get; set; } // Add method delegate for non-generic Stack and Queue; and types that derive from them. @@ -231,7 +257,7 @@ public JsonNumberHandling? NumberHandling get => _numberHandling; set { - CheckMutable(); + VerifyMutable(); _numberHandling = value; } } @@ -244,7 +270,6 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions Options = options; PropertyInfoForTypeInfo = CreatePropertyInfoForTypeInfo(Type, converter, Options, this); ElementType = converter.ElementType; - ConfigurePolymorphism(converter, options); switch (PropertyInfoForTypeInfo.ConverterStrategy) { @@ -270,7 +295,7 @@ internal JsonTypeInfo(Type type, JsonConverter converter, JsonSerializerOptions Kind = GetTypeInfoKind(type, PropertyInfoForTypeInfo.ConverterStrategy); } - private protected void CheckMutable() + internal void VerifyMutable() { if (_isConfigured) { @@ -281,6 +306,8 @@ private protected void CheckMutable() private protected volatile bool _isConfigured; private readonly object _configureLock = new object(); + internal bool IsConfigured => _isConfigured; + internal void EnsureConfigured() { if (_isConfigured) @@ -358,6 +385,11 @@ internal virtual void Configure() InitializeConstructorParameters(GetParameterInfoValues(), sourceGenMode: Options.SerializerContext != null); } } + + if (PolymorphismOptions != null) + { + PolymorphicTypeResolver = new PolymorphicTypeResolver(this); + } } #if DEBUG @@ -636,33 +668,6 @@ private static bool IsByRefLike(Type type) #endif } - internal void ConfigurePolymorphism(JsonConverter converter, JsonSerializerOptions options) - { -#pragma warning disable CA2252 // This API requires opting into preview features - Debug.Assert(Type != null); - - IJsonPolymorphicTypeConfiguration? configuration = null; - - // 1. Look up configuration from JsonSerializerOptions - foreach (JsonPolymorphicTypeConfiguration config in options.PolymorphicTypeConfigurations) - { - if (config.BaseType == Type) - { - configuration = config; - } - } - - // 2. Look up configuration from attributes - configuration ??= AttributePolymorphicTypeConfiguration.Create(Type); - - // Construct the resolver from configuration. - if (configuration != null) - { - PolymorphicTypeResolver = new PolymorphicTypeResolver(converter, configuration, options); - } -#pragma warning restore CA2252 // This API requires opting into preview features - } - internal bool IsValidDataExtensionProperty(JsonPropertyInfo jsonPropertyInfo) { Type memberType = jsonPropertyInfo.PropertyType; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.cs index 0a92289aac198..9c1fe6e8a583d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.cs @@ -32,7 +32,7 @@ private protected override void SetCreateObject(Delegate? createObject) { Debug.Assert(createObject is null or Func or Func); - CheckMutable(); + VerifyMutable(); if (Kind == JsonTypeInfoKind.None) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/PolymorphicTypeResolver.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/PolymorphicTypeResolver.cs index 07203e914556c..6f50d0298b1f7 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/PolymorphicTypeResolver.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/PolymorphicTypeResolver.cs @@ -15,17 +15,18 @@ namespace System.Text.Json.Serialization.Metadata /// internal sealed class PolymorphicTypeResolver { -#pragma warning disable CA2252 // This API requires opting into preview features - private readonly JsonSerializerOptions _options; + private readonly JsonTypeInfo _declaringTypeInfo; private readonly ConcurrentDictionary _typeToDiscriminatorId = new(); private readonly Dictionary? _discriminatorIdtoType; - public PolymorphicTypeResolver(JsonConverter baseConverter, IJsonPolymorphicTypeConfiguration configuration, JsonSerializerOptions options) + public PolymorphicTypeResolver(JsonTypeInfo jsonTypeInfo) { - _options = options; - BaseType = configuration.BaseType; - UnknownDerivedTypeHandling = configuration.UnknownDerivedTypeHandling; - IgnoreUnrecognizedTypeDiscriminators = configuration.IgnoreUnrecognizedTypeDiscriminators; + Debug.Assert(jsonTypeInfo.PolymorphismOptions != null); + + JsonPolymorphismOptions polymorphismOptions = jsonTypeInfo.PolymorphismOptions; + UnknownDerivedTypeHandling = polymorphismOptions.UnknownDerivedTypeHandling; + IgnoreUnrecognizedTypeDiscriminators = polymorphismOptions.IgnoreUnrecognizedTypeDiscriminators; + _declaringTypeInfo = jsonTypeInfo; if (!IsSupportedPolymorphicBaseType(BaseType)) { @@ -33,16 +34,16 @@ public PolymorphicTypeResolver(JsonConverter baseConverter, IJsonPolymorphicType } bool containsDerivedTypes = false; - foreach ((Type derivedType, object? typeDiscriminator) in configuration.GetSupportedDerivedTypes()) + foreach ((Type derivedType, object? typeDiscriminator) in polymorphismOptions.DerivedTypes) { + Debug.Assert(typeDiscriminator is null or int or string); + if (!IsSupportedDerivedType(BaseType, derivedType) || (derivedType.IsAbstract && UnknownDerivedTypeHandling != JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor)) { ThrowHelper.ThrowInvalidOperationException_DerivedTypeNotSupported(BaseType, derivedType); } - Debug.Assert(typeDiscriminator is null or int or string); - var derivedJsonTypeInfo = new DerivedJsonTypeInfo(derivedType, typeDiscriminator); if (!_typeToDiscriminatorId.TryAdd(derivedType, derivedJsonTypeInfo)) @@ -70,34 +71,35 @@ public PolymorphicTypeResolver(JsonConverter baseConverter, IJsonPolymorphicType if (UsesTypeDiscriminators) { - if (!baseConverter.CanHaveMetadata) + if (!jsonTypeInfo.Converter.CanHaveMetadata) { ThrowHelper.ThrowNotSupportedException_BaseConverterDoesNotSupportMetadata(BaseType); } - if (configuration.TypeDiscriminatorPropertyName is string customPropertyName) - { - JsonEncodedText jsonEncodedName = JsonEncodedText.Encode(customPropertyName, options.Encoder); + string propertyName = jsonTypeInfo.PolymorphismOptions.TypeDiscriminatorPropertyName; - // Check if the property name conflicts with other metadata property names - if ((JsonSerializer.GetMetadataPropertyName(jsonEncodedName.EncodedUtf8Bytes, resolver: null) & ~MetadataPropertyName.Type) != 0) - { - ThrowHelper.ThrowInvalidOperationException_InvalidCustomTypeDiscriminatorPropertyName(); - } + JsonEncodedText jsonEncodedName = propertyName == JsonSerializer.TypePropertyName + ? JsonSerializer.s_metadataType + : JsonEncodedText.Encode(propertyName, jsonTypeInfo.Options.Encoder); - TypeDiscriminatorPropertyName = customPropertyName; - CustomTypeDiscriminatorPropertyNameUtf8 = jsonEncodedName.EncodedUtf8Bytes.ToArray(); - CustomTypeDiscriminatorPropertyNameJsonEncoded = jsonEncodedName; + // Check if the property name conflicts with other metadata property names + if ((JsonSerializer.GetMetadataPropertyName(jsonEncodedName.EncodedUtf8Bytes, resolver: null) & ~MetadataPropertyName.Type) != 0) + { + ThrowHelper.ThrowInvalidOperationException_InvalidCustomTypeDiscriminatorPropertyName(); } + + TypeDiscriminatorPropertyName = propertyName; + TypeDiscriminatorPropertyNameUtf8 = jsonEncodedName.EncodedUtf8Bytes.ToArray(); + CustomTypeDiscriminatorPropertyNameJsonEncoded = jsonEncodedName; } } - public Type BaseType { get; } + public Type BaseType => _declaringTypeInfo.Type; public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get; } public bool UsesTypeDiscriminators { get; } public bool IgnoreUnrecognizedTypeDiscriminators { get; } public string? TypeDiscriminatorPropertyName { get; } - public byte[]? CustomTypeDiscriminatorPropertyNameUtf8 { get; } + public byte[]? TypeDiscriminatorPropertyNameUtf8 { get; } public JsonEncodedText? CustomTypeDiscriminatorPropertyNameJsonEncoded { get; } public bool TryGetDerivedJsonTypeInfo(Type runtimeType, [NotNullWhen(true)] out JsonTypeInfo? jsonTypeInfo, out object? typeDiscriminator) @@ -138,7 +140,7 @@ public bool TryGetDerivedJsonTypeInfo(Type runtimeType, [NotNullWhen(true)] out } else { - jsonTypeInfo = result.GetJsonTypeInfo(_options); + jsonTypeInfo = result.GetJsonTypeInfo(_declaringTypeInfo.Options); typeDiscriminator = result.TypeDiscriminator; return true; } @@ -153,7 +155,7 @@ public bool TryGetDerivedJsonTypeInfo(object typeDiscriminator, [NotNullWhen(tru if (_discriminatorIdtoType.TryGetValue(typeDiscriminator, out DerivedJsonTypeInfo? result)) { Debug.Assert(typeDiscriminator.Equals(result.TypeDiscriminator)); - jsonTypeInfo = result.GetJsonTypeInfo(_options); + jsonTypeInfo = result.GetJsonTypeInfo(_declaringTypeInfo.Options); return true; } @@ -249,6 +251,5 @@ public DerivedJsonTypeInfo(Type type, object? typeDiscriminator) public JsonTypeInfo GetJsonTypeInfo(JsonSerializerOptions options) => _jsonTypeInfo ??= options.GetOrAddJsonTypeInfo(DerivedType); } -#pragma warning restore CA2252 // This API requires opting into preview features } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionJsonTypeInfoOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionJsonTypeInfoOfT.cs index a5ff1fdcb795f..9da7736b398da 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionJsonTypeInfoOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionJsonTypeInfoOfT.cs @@ -34,6 +34,7 @@ internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions o : base(converter, options) { NumberHandling = GetNumberHandlingForType(Type); + PolymorphismOptions = JsonPolymorphismOptions.CreateFromAttributeDeclarations(Type); if (PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object) { 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 85cfa258d8703..80c1ace7b2ae7 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 @@ -20,6 +20,7 @@ internal sealed class SourceGenJsonTypeInfo : JsonTypeInfo public SourceGenJsonTypeInfo(JsonConverter converter, JsonSerializerOptions options) : base(converter, options) { + PolymorphismOptions = JsonPolymorphismOptions.CreateFromAttributeDeclarations(Type); } /// @@ -42,6 +43,7 @@ public SourceGenJsonTypeInfo(JsonSerializerOptions options, JsonObjectInfoValues SerializeHandler = objectInfo.SerializeHandler; NumberHandling = objectInfo.NumberHandling; + PolymorphismOptions = JsonPolymorphismOptions.CreateFromAttributeDeclarations(Type); } /// @@ -68,6 +70,7 @@ public SourceGenJsonTypeInfo( CreateObjectWithArgs = createObjectWithArgs; AddMethodDelegate = addFunc; CreateObject = collectionInfo.ObjectCreator; + PolymorphismOptions = JsonPolymorphismOptions.CreateFromAttributeDeclarations(Type); } private static JsonConverter GetConverter(JsonObjectInfoValues objectInfo) 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 0f08484bdc69a..b1e08677e739b 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 @@ -755,5 +755,11 @@ public static void ThrowJsonException_UnrecognizedTypeDiscriminator(object typeD { ThrowJsonException(SR.Format(SR.Polymorphism_UnrecognizedTypeDiscriminator, typeDiscriminator)); } + + [DoesNotReturn] + public static void ThrowArgumentException_JsonPolymorphismOptionsAssociatedWithDifferentJsonTypeInfo(string parameterName) + { + throw new ArgumentException(SR.JsonPolymorphismOptionsAssociatedWithDifferentJsonTypeInfo, paramName: parameterName); + } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonPolymorphicTypeConfigurationTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonPolymorphicTypeConfigurationTests.cs deleted file mode 100644 index ea1e75577f992..0000000000000 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonPolymorphicTypeConfigurationTests.cs +++ /dev/null @@ -1,189 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections; -using System.Collections.Generic; -using System.Reflection; -using System.Text.Json.Serialization; -using Xunit; - -namespace System.Text.Json.Tests.Serialization -{ - public static class JsonPolymorphicTypeConfigurationTests - { - [Theory] - [InlineData(typeof(IEnumerable))] - [InlineData(typeof(Interface))] - [InlineData(typeof(Class))] - [InlineData(typeof(GenericClass))] - public static void SupportedBaseTypeArgument_ShouldSucceed(Type baseType) - { - var configuration = new JsonPolymorphicTypeConfiguration(baseType); - Assert.Equal(baseType, configuration.BaseType); - Assert.Empty(configuration); - } - - [Theory] - [InlineData(typeof(IEnumerable), typeof(IEnumerable))] - [InlineData(typeof(IList), typeof(string[]))] - [InlineData(typeof(MemberInfo), typeof(Type))] - [InlineData(typeof(Interface), typeof(Class))] - [InlineData(typeof(Interface), typeof(Struct))] - [InlineData(typeof(Class), typeof(GenericClass))] - public static void SupportedDerivedTypeArgument_ShouldSucceed(Type baseType, Type derivedType) - { - var configuration = new JsonPolymorphicTypeConfiguration(baseType).WithDerivedType(derivedType); - Assert.Equal(new (Type, object?)[] { (derivedType, null) }, configuration); - - configuration = new JsonPolymorphicTypeConfiguration(baseType).WithDerivedType(derivedType, "typeDiscriminator"); - Assert.Equal(new (Type, object?)[] { (derivedType, "typeDiscriminator") }, configuration); - - configuration = new JsonPolymorphicTypeConfiguration(baseType).WithDerivedType(derivedType, 42); - Assert.Equal(new (Type, object?)[] { (derivedType, 42) }, configuration); - } - - [Fact] - public static void SupportsDeclaringBaseTypeAsDerivedType() - { - var configuration = - new JsonPolymorphicTypeConfiguration(typeof(Class)) - .WithDerivedType(typeof(Class)); - - Assert.Equal(new (Type, object?)[] { (typeof(Class), null) }, configuration); - - configuration = - new JsonPolymorphicTypeConfiguration(typeof(Class)) - .WithDerivedType(typeof(Class), "typeDiscriminator"); - - Assert.Equal(new (Type, object?)[] { (typeof(Class), "typeDiscriminator") }, configuration); - - configuration = - new JsonPolymorphicTypeConfiguration(typeof(Class)) - .WithDerivedType(typeof(Class), 42); - - Assert.Equal(new (Type, object?)[] { (typeof(Class), 42) }, configuration); - } - - [Fact] - public static void SupportsMixingAndMatchingTypeDiscriminators() - { - var configuration = - new JsonPolymorphicTypeConfiguration(typeof(Class)) - .WithDerivedType(typeof(GenericClass)) - .WithDerivedType(typeof(GenericClass), 42) - .WithDerivedType(typeof(GenericClass), "typeDiscriminator"); - - Assert.Equal( - new (Type, object?)[] - { - (typeof(GenericClass), null), - (typeof(GenericClass), 42), - (typeof(GenericClass), "typeDiscriminator"), - }, - configuration); - } - - [Theory] - [InlineData(typeof(int))] - [InlineData(typeof(int*))] - [InlineData(typeof(string))] - [InlineData(typeof(object))] - [InlineData(typeof(Guid))] - [InlineData(typeof(Struct))] - [InlineData(typeof(ReadOnlySpan))] - [InlineData(typeof(SealedClass))] - [InlineData(typeof(GenericClass<>))] - public static void InvalidBaseTypeArgument_ThrowsArgumentException(Type baseType) - { - Assert.Throws(() => new JsonPolymorphicTypeConfiguration(baseType)); - } - - [Fact] - public static void NullBaseTypeArgument_ThrowsArgumentNullException() - { - Assert.Throws(() => new JsonPolymorphicTypeConfiguration(null)); - } - - [Theory] - [InlineData(typeof(Interface), typeof(object))] - [InlineData(typeof(Class), typeof(Interface))] - [InlineData(typeof(Class), typeof(GenericClass<>))] - public static void InvalidDerivedTypeArgument_ThrowsArgumentException(Type baseType, Type derivedType) - { - var configuration = new JsonPolymorphicTypeConfiguration(baseType); - - Assert.Throws(() => configuration.WithDerivedType(derivedType)); - Assert.Empty(configuration); - - Assert.Throws(() => configuration.WithDerivedType(derivedType, "typeDiscriminator")); - Assert.Empty(configuration); - } - - [Fact] - public static void NullDerivedTypeArgument_ThrowsArgumentNullException() - { - var configuration = new JsonPolymorphicTypeConfiguration(typeof(Class)); - Assert.Throws(() => configuration.WithDerivedType(derivedType: null)); - Assert.Empty(configuration); - } - - [Fact] - public static void DuplicateDerivedType_ThrowsArgumentException() - { - var configuration = new JsonPolymorphicTypeConfiguration(typeof(Class)).WithDerivedType(typeof(GenericClass)); - Assert.Throws(() => configuration.WithDerivedType(typeof(GenericClass))); - Assert.Equal(new (Type, object?)[] { (typeof(GenericClass), null) }, configuration); - } - - [Fact] - public static void DuplicateTypeDiscriminator_String_ThrowsArgumentException() - { - var configuration = - new JsonPolymorphicTypeConfiguration(typeof(Class)) - .WithDerivedType(typeof(GenericClass), "discriminator1") - .WithDerivedType(typeof(GenericClass), "discriminator2"); - - Assert.Equal(new (Type, object?)[] { (typeof(GenericClass), "discriminator1"), (typeof(GenericClass), "discriminator2") }, configuration); - - Assert.Throws(() => configuration.WithDerivedType(typeof(GenericClass), "discriminator2")); - - Assert.Equal(new (Type, object?)[] { (typeof(GenericClass), "discriminator1"), (typeof(GenericClass), "discriminator2") }, configuration); - } - - [Fact] - public static void DuplicateTypeDiscriminator_Int_ThrowsArgumentException() - { - var configuration = - new JsonPolymorphicTypeConfiguration(typeof(Class)) - .WithDerivedType(typeof(GenericClass), 0) - .WithDerivedType(typeof(GenericClass), 1); - - Assert.Equal(new (Type, object?)[] { (typeof(GenericClass), 0), (typeof(GenericClass), 1) }, configuration); - - Assert.Throws(() => configuration.WithDerivedType(typeof(GenericClass), 1)); - - Assert.Equal(new (Type, object?)[] { (typeof(GenericClass), 0), (typeof(GenericClass), 1) }, configuration); - } - - [Fact] - public static void ModifyingAfterAssignmentToOptions_ShouldThrowInvalidOperationException() - { - var config = new JsonPolymorphicTypeConfiguration(typeof(Class)) - .WithDerivedType(typeof(GenericClass), "derived"); - - _ = new JsonSerializerOptions { PolymorphicTypeConfigurations = { config } }; - - Assert.Throws(() => config.WithDerivedType(typeof(GenericClass), "derived2")); - Assert.Throws(() => config.WithDerivedType(typeof(GenericClass), 42)); - Assert.Throws(() => config.TypeDiscriminatorPropertyName = "_case"); - Assert.Throws(() => config.UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType); - Assert.Throws(() => config.IgnoreUnrecognizedTypeDiscriminators = true); - } - - private interface Interface { } - private class Class : Interface { } - private struct Struct : Interface { } - private sealed class SealedClass : Interface { } - private class GenericClass : Class { } - } -} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonPolymorphismOptionsTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonPolymorphismOptionsTests.cs new file mode 100644 index 0000000000000..b16720a6c820c --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonPolymorphismOptionsTests.cs @@ -0,0 +1,136 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; +using Xunit; +using static System.Text.Json.Serialization.Tests.PolymorphicTests; + +namespace System.Text.Json.Tests.Serialization +{ + public static class JsonPolymorphismOptionsTests + { + [Fact] + public static void JsonPolymorphismOptions_DefaultInstance() + { + var options = new JsonPolymorphismOptions(); + + Assert.False(options.IgnoreUnrecognizedTypeDiscriminators); + Assert.Equal(JsonUnknownDerivedTypeHandling.FailSerialization, options.UnknownDerivedTypeHandling); + Assert.Equal("$type", options.TypeDiscriminatorPropertyName); + Assert.Empty(options.DerivedTypes); + } + + [Theory] + [MemberData(nameof(GetDerivedTypes))] + public static void JsonPolymorphismOptions_AddDerivedTypes(JsonDerivedType[] derivedTypes) + { + var options = new JsonPolymorphismOptions(); + foreach (JsonDerivedType derivedType in derivedTypes) + { + options.DerivedTypes.Add(derivedType); + } + + Assert.Equal(derivedTypes, options.DerivedTypes); + } + + public static IEnumerable GetDerivedTypes() + { + yield return WrapArgs(default(JsonDerivedType)); + yield return WrapArgs(new JsonDerivedType(typeof(int))); + yield return WrapArgs(new JsonDerivedType(typeof(void), "void")); + yield return WrapArgs(new JsonDerivedType(typeof(object), 42)); + yield return WrapArgs(new JsonDerivedType(typeof(string))); + yield return WrapArgs( + new JsonDerivedType(typeof(JsonSerializerOptions)), + new JsonDerivedType(typeof(int), 42), + new JsonDerivedType(typeof(void), "void")); + + static object[] WrapArgs(params JsonDerivedType[] derivedTypes) => new object[] { derivedTypes }; + } + + [Fact] + public static void JsonPolymorphismOptions_AssigningOptionsToSecondJsonTypeInfo_ThrowsInvalidOperationException() + { + var options = new JsonPolymorphismOptions(); + + JsonTypeInfo jti1 = JsonTypeInfo.CreateJsonTypeInfo(typeof(PolymorphicClass), new()); + jti1.PolymorphismOptions = options; + + JsonTypeInfo jti2 = JsonTypeInfo.CreateJsonTypeInfo(typeof(PolymorphicClass), new()); + Assert.Throws(() => jti2.PolymorphismOptions = options); + } + + [Fact] + public static void JsonPolymorphismOptions_CreateBlankJsonTypeInfo_ContainsNoPolymorphismMetadata() + { + JsonSerializerOptions options = JsonSerializerOptions.Default; + + // Sanity check: type returns polymorphism options using the default resolver + JsonTypeInfo jti = options.TypeInfoResolver.GetTypeInfo(typeof(PolymorphicClass), options); + Assert.NotNull(jti.PolymorphismOptions); + + // Blank instance should not contain polymorphism options + jti = JsonTypeInfo.CreateJsonTypeInfo(typeof(PolymorphicClass), options); + Assert.Null(jti.PolymorphismOptions); + } + + [Theory] + [InlineData(typeof(int))] + [InlineData(typeof(string))] + [InlineData(typeof(object))] + [InlineData(typeof(DateTime))] + [InlineData(typeof(IEnumerable))] + [InlineData(typeof(PolymorphicClass))] + [InlineData(typeof(PolymorphicClass.DerivedClass1_NoTypeDiscriminator))] + [InlineData(typeof(PolymorphicClass.DerivedClass1_TypeDiscriminator))] + [InlineData(typeof(PolymorphicClassWithConstructor))] + [InlineData(typeof(PolymorphicList))] + [InlineData(typeof(PolymorphicDictionary))] + [InlineData(typeof(PolymorphicClassWithCustomTypeDiscriminator))] + [InlineData(typeof(PolymorphicClassWithoutDerivedTypeAttribute))] + [InlineData(typeof(PolymorphicClass_InvalidCustomTypeDiscriminatorPropertyName))] + [InlineData(typeof(PolymorphicClassWithNullDerivedTypeAttribute))] + [InlineData(typeof(PolymorphicClassWithStructDerivedTypeAttribute))] + [InlineData(typeof(PolymorphicClassWithObjectDerivedTypeAttribute))] + [InlineData(typeof(PolymorphicClassWithNonAssignableDerivedTypeAttribute))] + [InlineData(typeof(PolymorphicAbstractClassWithAbstractClassDerivedType))] + [InlineData(typeof(PolymorphicClassWithDuplicateDerivedTypeRegistrations))] + [InlineData(typeof(PolymorphicClasWithDuplicateTypeDiscriminators))] + [InlineData(typeof(PolymorphicGenericClass))] + [InlineData(typeof(PolymorphicDerivedGenericClass.DerivedClass))] + [InlineData(typeof(PolymorphicClass_CustomConverter_TypeDiscriminator))] + [InlineData(typeof(PolymorphicClass_CustomConverter_NoTypeDiscriminator))] + public static void DefaultResolver_ReportsCorrectPolymorphismMetadata(Type polymorphicType) + { + JsonPolymorphicAttribute? polymorphicAttribute = polymorphicType.GetCustomAttribute(inherit: false); + JsonDerivedTypeAttribute[] derivedTypeAttributes = polymorphicType.GetCustomAttributes(inherit: false).ToArray(); + + JsonSerializer.Serialize(42); // Ensure default converters have been rooted + var options = JsonSerializerOptions.Default; + JsonTypeInfo jsonTypeInfo = options.TypeInfoResolver.GetTypeInfo(polymorphicType, options); + + Assert.Equal(polymorphicType, jsonTypeInfo.Type); + + JsonPolymorphismOptions? polyOptions = jsonTypeInfo.PolymorphismOptions; + if (polymorphicAttribute == null && derivedTypeAttributes.Length == 0) + { + Assert.Null(polyOptions); + } + else + { + Assert.NotNull(polyOptions); + + Assert.Equal(polymorphicAttribute?.IgnoreUnrecognizedTypeDiscriminators ?? false, polyOptions.IgnoreUnrecognizedTypeDiscriminators); + Assert.Equal(polymorphicAttribute?.UnknownDerivedTypeHandling ?? default, polyOptions.UnknownDerivedTypeHandling); + Assert.Equal(polymorphicAttribute?.TypeDiscriminatorPropertyName ?? "$type", polyOptions.TypeDiscriminatorPropertyName); + Assert.Equal( + expected: derivedTypeAttributes.Select(attr => (attr.DerivedType, attr.TypeDiscriminator)), + actual: polyOptions.DerivedTypes.Select(attr => (attr.DerivedType, attr.TypeDiscriminator))); + } + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.JsonTypeInfo.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.JsonTypeInfo.cs index d46978ee314fb..03b63c364c951 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.JsonTypeInfo.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.JsonTypeInfo.cs @@ -385,12 +385,26 @@ private static void TestTypeInfoImmutability(JsonTypeInfo typeInfo) Assert.True(typeInfo.Converter.CanConvert(typeof(T))); JsonPropertyInfo prop = typeInfo.CreateJsonPropertyInfo(typeof(string), "foo"); + Assert.True(typeInfo.Properties.IsReadOnly); Assert.Throws(() => untyped.CreateObject = untyped.CreateObject); Assert.Throws(() => typeInfo.CreateObject = typeInfo.CreateObject); Assert.Throws(() => typeInfo.NumberHandling = typeInfo.NumberHandling); Assert.Throws(() => typeInfo.Properties.Clear()); Assert.Throws(() => typeInfo.Properties.Add(prop)); Assert.Throws(() => typeInfo.Properties.Insert(0, prop)); + Assert.Throws(() => typeInfo.PolymorphismOptions = null); + Assert.Throws(() => typeInfo.PolymorphismOptions = new()); + + if (typeInfo.PolymorphismOptions is JsonPolymorphismOptions jpo) + { + Assert.True(jpo.DerivedTypes.IsReadOnly); + Assert.Throws(() => jpo.IgnoreUnrecognizedTypeDiscriminators = true); + Assert.Throws(() => jpo.TypeDiscriminatorPropertyName = "__case"); + Assert.Throws(() => jpo.UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor); + Assert.Throws(() => jpo.DerivedTypes.Clear()); + Assert.Throws(() => jpo.DerivedTypes.Add(default)); + Assert.Throws(() => jpo.DerivedTypes.Insert(0, default)); + } foreach (var property in typeInfo.Properties) { @@ -639,6 +653,7 @@ public static IEnumerable GetTypeInfoTestData() yield return new object[] { 13 }; yield return new object[] { new SomeClass { IntProp = 17 } }; yield return new object[] { new SomeRecursiveClass() }; + yield return new object[] { new SomePolymorphicClass() }; } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.cs index a1b1b3d2cc038..5c5872994a19d 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.cs @@ -221,5 +221,13 @@ private class SomeRecursiveClass public int IntProp { get; set; } public SomeRecursiveClass RecursiveProperty { get; set; } } + + [JsonDerivedType(typeof(DerivedClass))] + private class SomePolymorphicClass + { + public class DerivedClass : SomePolymorphicClass + { + } + } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs index 951441d8b482c..56c1ccdbef95b 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs @@ -678,13 +678,6 @@ private static JsonSerializerOptions GetFullyPopulatedOptionsInstance() options.Converters.Add(new JsonStringEnumConverter()); options.Converters.Add(new ConverterForInt32()); } - else if (propertyType == typeof(IList)) - { - options.PolymorphicTypeConfigurations.Add( - new JsonPolymorphicTypeConfiguration() - .WithDerivedType("point_with_array") - .WithDerivedType("point_with_dictionary")); - } else if (propertyType == typeof(JavaScriptEncoder)) { options.Encoder = JavaScriptEncoder.Default; @@ -745,17 +738,6 @@ private static void VerifyOptionsEqual(JsonSerializerOptions options, JsonSerial Assert.Same(list1[i], list2[i]); } } - else if (propertyType == typeof(IList)) - { - var list1 = (IList)property.GetValue(options); - var list2 = (IList)property.GetValue(newOptions); - - Assert.Equal(list1.Count, list2.Count); - for (int i = 0; i < list1.Count; i++) - { - Assert.Same(list1[i], list2[i]); - } - } else if (propertyType.IsValueType) { if (property.Name == "ReadCommentHandling") diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PolymorphicTests.CustomTypeHierarchies.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PolymorphicTests.CustomTypeHierarchies.cs index dc30a27889126..834b7f2092b70 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PolymorphicTests.CustomTypeHierarchies.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PolymorphicTests.CustomTypeHierarchies.cs @@ -5,7 +5,9 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Reflection; using System.Text.Encodings.Web; +using System.Text.Json.Serialization.Metadata; using System.Threading.Tasks; using Xunit; @@ -235,17 +237,42 @@ public async Task PolymorphicClass_ConfigWithAbstractClass_ShouldThrowNotSupport { var options = new JsonSerializerOptions { - PolymorphicTypeConfigurations = + TypeInfoResolver = new CustomPolymorphismResolver { - new JsonPolymorphicTypeConfiguration { UnknownDerivedTypeHandling = jsonUnknownDerivedTypeHandling } - .WithDerivedType() + UnknownDerivedTypeHandling = jsonUnknownDerivedTypeHandling, } + .WithDerivedType() }; PolymorphicClass value = new PolymorphicClass.DerivedAbstractClass.DerivedClass(); await Assert.ThrowsAsync(() => Serializer.SerializeWrapper(value)); } + [Fact] + public async Task PolymorphicClass_ClearPolymorphismOptions_DoesNotUsePolymorphism() + { + var options = new JsonSerializerOptions + { + TypeInfoResolver = new DefaultJsonTypeInfoResolver() + { + Modifiers = + { + static jsonTypeInfo => + { + if (jsonTypeInfo.Type == typeof(PolymorphicClass)) + { + jsonTypeInfo.PolymorphismOptions = null; + } + } + } + } + }; + + PolymorphicClass value = new PolymorphicClass.DerivedAbstractClass.DerivedClass { Number = 42, Boolean = true }; + string json = await Serializer.SerializeWrapper(value, options); + JsonTestHelper.AssertJsonEqual("""{"Number":42}""", json); + } + [JsonDerivedType(typeof(DerivedClass1_NoTypeDiscriminator))] [JsonDerivedType(typeof(DerivedClass1_NoTypeDiscriminator.DerivedClass))] [JsonDerivedType(typeof(DerivedClass1_TypeDiscriminator), "derivedClass1")] @@ -594,23 +621,20 @@ public static IEnumerable GetSerializeTestData() public static JsonSerializerOptions CustomConfigWithBaseTypeFallback { get; } = new JsonSerializerOptions { - PolymorphicTypeConfigurations = + TypeInfoResolver = new CustomPolymorphismResolver { - new JsonPolymorphicTypeConfiguration - { - TypeDiscriminatorPropertyName = "_case", - UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType - } - .WithDerivedType() - .WithDerivedType("derivedClass1") - .WithDerivedType("derivedClassOfDerivedClass1") - .WithDerivedType("derivedClass2") - .WithDerivedType("derivedCollection") - .WithDerivedType() - .WithDerivedType("derivedDictionaryOfDerivedDictionary") - .WithDerivedType("derivedClassOfDerivedClassWithCtor") - .WithDerivedType("derivedClassWithCustomConverter") + TypeDiscriminatorPropertyName = "_case", + UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType } + .WithDerivedType() + .WithDerivedType("derivedClass1") + .WithDerivedType("derivedClassOfDerivedClass1") + .WithDerivedType("derivedClass2") + .WithDerivedType("derivedCollection") + .WithDerivedType() + .WithDerivedType("derivedDictionaryOfDerivedDictionary") + .WithDerivedType("derivedClassOfDerivedClassWithCtor") + .WithDerivedType("derivedClassWithCustomConverter") }; public static IEnumerable GetSerializeTestData_CustomConfigWithBaseTypeFallback() @@ -718,24 +742,21 @@ public static IEnumerable GetSerializeTestData_CustomConfigWithBaseTyp public static JsonSerializerOptions CustomConfigWithNearestAncestorFallback { get; } = new JsonSerializerOptions { - PolymorphicTypeConfigurations = + TypeInfoResolver = new CustomPolymorphismResolver { - new JsonPolymorphicTypeConfiguration - { - TypeDiscriminatorPropertyName = "_case", - UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor - } - .WithDerivedType() - .WithDerivedType("derivedClass1") - .WithDerivedType("derivedClassOfDerivedClass1") - .WithDerivedType("derivedClass2") - .WithDerivedType("derivedAbstractClass") - .WithDerivedType("derivedCollection") - .WithDerivedType() - .WithDerivedType("derivedDictionaryOfDerivedDictionary") - .WithDerivedType("derivedClassOfDerivedClassWithCtor") - .WithDerivedType("derivedClassWithCustomConverter") + TypeDiscriminatorPropertyName = "_case", + UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor } + .WithDerivedType() + .WithDerivedType("derivedClass1") + .WithDerivedType("derivedClassOfDerivedClass1") + .WithDerivedType("derivedClass2") + .WithDerivedType("derivedAbstractClass") + .WithDerivedType("derivedCollection") + .WithDerivedType() + .WithDerivedType("derivedDictionaryOfDerivedDictionary") + .WithDerivedType("derivedClassOfDerivedClassWithCtor") + .WithDerivedType("derivedClassWithCustomConverter") }; public static IEnumerable GetSerializeTestData_CustomConfigWithNearestAncestorFallback() @@ -1215,9 +1236,9 @@ await TestMultiContextDeserialization( [Theory] [MemberData(nameof(Get_PolymorphicInterface_DiamondInducingConfigurations_ShouldThrowNotSupportedException))] - public async Task PolymorphicInterface_DiamondInducingConfigurations_ShouldThrowNotSupportedException(PolymorphicInterface value, JsonPolymorphicTypeConfiguration configuration) + public async Task PolymorphicInterface_DiamondInducingConfigurations_ShouldThrowNotSupportedException(PolymorphicInterface value, CustomPolymorphismResolver configuration) { - var options = new JsonSerializerOptions { PolymorphicTypeConfigurations = { configuration } }; + var options = new JsonSerializerOptions { TypeInfoResolver = configuration }; await Assert.ThrowsAsync(() => Serializer.SerializeWrapper(value, options)); } @@ -1332,17 +1353,14 @@ public static IEnumerable GetSerializeTestData() public static JsonSerializerOptions CustomConfigWithNearestAncestorFallback { get; } = new JsonSerializerOptions { - PolymorphicTypeConfigurations = + TypeInfoResolver = new CustomPolymorphismResolver { - new JsonPolymorphicTypeConfiguration - { - UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor - } - .WithDerivedType("derivedClass") - .WithDerivedType() - .WithDerivedType() - .WithDerivedType() + UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor } + .WithDerivedType("derivedClass") + .WithDerivedType() + .WithDerivedType() + .WithDerivedType() }; public static IEnumerable GetSerializeTestData_CustomConfigWithNearestAncestorFallback() @@ -1387,17 +1405,17 @@ public static IEnumerable GetSerializeTestData_CustomConfigWithNearest } - public static IEnumerable<(PolymorphicInterface diamondValue, JsonPolymorphicTypeConfiguration configuration)> GetDiamondInducingConfigurations() + public static IEnumerable<(PolymorphicInterface diamondValue, CustomPolymorphismResolver configuration)> GetDiamondInducingConfigurations() { yield return ( new DiamondKind1(), - new JsonPolymorphicTypeConfiguration { UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor } + new CustomPolymorphismResolver { UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor } .WithDerivedType() .WithDerivedType()); yield return ( new DiamondKind2(), - new JsonPolymorphicTypeConfiguration { UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor } + new CustomPolymorphismResolver { UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor } .WithDerivedType() .WithDerivedType()); } @@ -1557,16 +1575,13 @@ public async Task PolymorphicCollectionInterface_Deserialization() private readonly static JsonSerializerOptions s_optionsWithPolymorphicCollectionInterface = new JsonSerializerOptions { - PolymorphicTypeConfigurations = - { - new JsonPolymorphicTypeConfiguration> - { - UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor - } - .WithDerivedType>("list") - .WithDerivedType>("queue") - .WithDerivedType>("set") - } + TypeInfoResolver = new CustomPolymorphismResolver> + { + UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor + } + .WithDerivedType>("list") + .WithDerivedType>("queue") + .WithDerivedType>("set") }; #endregion @@ -1716,16 +1731,13 @@ public async Task PolymorphicDictionaryInterface_Deserialization() private readonly static JsonSerializerOptions s_optionsWithPolymorphicDictionaryInterface = new JsonSerializerOptions { - PolymorphicTypeConfigurations = - { - new JsonPolymorphicTypeConfiguration>> - { - UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor - } - .WithDerivedType>("dictionary") - .WithDerivedType>("sortedDictionary") - .WithDerivedType>("readOnlyDictionary") - } + TypeInfoResolver = new CustomPolymorphismResolver>> + { + UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor + } + .WithDerivedType>("dictionary") + .WithDerivedType>("sortedDictionary") + .WithDerivedType>("readOnlyDictionary") }; #endregion @@ -2161,15 +2173,12 @@ public class DerivedClass : PolymorphicInterfaceWithInterfaceDerivedType public static JsonSerializerOptions PolymorphicInterfaceWithInterfaceDerivedType_OptionsWithFallbackToNearestAncestor { get; } = new JsonSerializerOptions { - PolymorphicTypeConfigurations = + TypeInfoResolver = new CustomPolymorphismResolver() { - new JsonPolymorphicTypeConfiguration() - { - UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor - } - .WithDerivedType("derivedInterface") - .WithDerivedType("derivedClass") + UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor } + .WithDerivedType("derivedInterface") + .WithDerivedType("derivedClass") }; [Fact] @@ -2388,14 +2397,11 @@ public class DerivedClass : PolymorphicClass_InvalidCustomTypeDiscriminatorPrope return new JsonSerializerOptions { - PolymorphicTypeConfigurations = + TypeInfoResolver = new CustomPolymorphismResolver { - new JsonPolymorphicTypeConfiguration - { - TypeDiscriminatorPropertyName = customPropertyName - } - .WithDerivedType("derivedClass") + TypeDiscriminatorPropertyName = customPropertyName } + .WithDerivedType("derivedClass") }; } } @@ -2444,6 +2450,74 @@ public bool Equals(TBaseType? left, TBaseType? right) public int GetHashCode(TBaseType _) => throw new NotImplementedException(); } + + public class CustomPolymorphismResolver : DefaultJsonTypeInfoResolver + { + private List _jsonDerivedTypes = new(); + + public CustomPolymorphismResolver(Type baseType) + { + BaseType = baseType; + } + + public Type BaseType { get; } + public bool IgnoreUnrecognizedTypeDiscriminators { get; set; } + public JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get; set; } + public string? TypeDiscriminatorPropertyName { get; set; } + + public CustomPolymorphismResolver WithDerivedType(JsonDerivedType jsonDerivedType) + { + _jsonDerivedTypes.Add(jsonDerivedType); + return this; + } + + public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) + { + JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options); + if (jsonTypeInfo.Type == BaseType) + { + jsonTypeInfo.PolymorphismOptions = new() + { + IgnoreUnrecognizedTypeDiscriminators = IgnoreUnrecognizedTypeDiscriminators, + UnknownDerivedTypeHandling = UnknownDerivedTypeHandling, + TypeDiscriminatorPropertyName = TypeDiscriminatorPropertyName, + }; + + foreach (JsonDerivedType derivedType in _jsonDerivedTypes) + { + jsonTypeInfo.PolymorphismOptions.DerivedTypes.Add(derivedType); + } + } + + return jsonTypeInfo; + } + } + + public class CustomPolymorphismResolver : CustomPolymorphismResolver + where TBaseType : class + { + public CustomPolymorphismResolver() : base(typeof(TBaseType)) + { + } + + public CustomPolymorphismResolver WithDerivedType() where TDerivedType : TBaseType + { + WithDerivedType(new JsonDerivedType(typeof(TDerivedType))); + return this; + } + + public CustomPolymorphismResolver WithDerivedType(int typeDiscriminatorId) where TDerivedType : TBaseType + { + WithDerivedType(new JsonDerivedType(typeof(TDerivedType), typeDiscriminatorId)); + return this; + } + + public CustomPolymorphismResolver WithDerivedType(string typeDiscriminatorId) where TDerivedType : TBaseType + { + WithDerivedType(new JsonDerivedType(typeof(TDerivedType), typeDiscriminatorId)); + return this; + } + } #endregion } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj index 0f6efb07c8520..7e05b84a1deeb 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj @@ -168,7 +168,7 @@ - +