-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Jsonpartner-impactThis issue impacts a partner who needs to be kept updatedThis issue impacts a partner who needs to be kept updated
Milestone
Description
Background and motivation
The JsonSerializer
class exposes serialization and deserialization overloads that accept JsonTypeInfo<T>
. These overloads are generally marked AOT/linker-safe and provide the only entrypoint for serializing using materialized metadata instances. We are however missing non-generic overloads for doing the same thing.
API Proposal
namespace System.Text.Json;
public partial static class JsonSerializer
{
public static object? Deserialize(string json, JsonTypeInfo jsonTypeInfo);
public static object? Deserialize(ReadOnlySpan<byte> utf8Json, JsonTypeInfo jsonTypeInfo);
public static object? Deserialize(ref Utf8JsonReader reader, JsonTypeInfo jsonTypeInfo);
public static object? Deserialize(Stream utf8Json, JsonTypeInfo jsonTypeInfo);
public static ValueTask<object?> DeserializeAsync(Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default);
public static object? Deserialize(JsonDocument document, JsonTypeInfo jsonTypeInfo);
public static object? Deserialize(JsonElement element, JsonTypeInfo jsonTypeInfo);
public static object? Deserialize(JsonNode node, JsonTypeInfo jsonTypeInfo);
public static string Serialize(object? value, JsonTypeInfo jsonTypeInfo);
public static byte[] SerializeToUtf8Bytes(object? value, JsonTypeInfo jsonTypeInfo);
public static void Serialize(Utf8JsonWriter writer, object? value, JsonTypeInfo jsonTypeInfo);
public static void Serialize(Stream utf8Json, object? value, JsonTypeInfo jsonTypeInfo);
public static Task SerializeAsync(Stream utf8Json, object? value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken = default);
public static JsonDocument SerializeToDocument(object? value, JsonTypeInfo jsonTypeInfo);
public static JsonElement SerializeToElement(object? value, JsonTypeInfo jsonTypeInfo);
public static JsonNode SerializeToNode(object? value, JsonTypeInfo jsonTypeInfo);
}
API Usage
The following example shows how we can perform non-generic serialization without invoking any of the RequiresUnreferencedCode APIs:
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(ContextA.Default, ContextB.Default)
};
JsonTypeInfo typeInfo = options.GetTypeInfo(typeof(SupportedPoco));
object? result = JsonSerializer.Deserialize("{}", typeInfo);
Console.WriteLine(result is SupportedPoco); // True
Alternative Designs
No response
Risks
No response
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Jsonpartner-impactThis issue impacts a partner who needs to be kept updatedThis issue impacts a partner who needs to be kept updated