-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Text.Json
Milestone
Description
Background and motivation
JsonSerializer requires async execution for some types, like IAsyncEnumerable. I would be nice to have a method SerializeAsync, which returns serialized string instead of writing to stream.
API Proposal
Add the following methods:
public static Task<string> SerializeAsync<TValue>(TValue value, JsonSerializerOptions? options = null);
public static Task<string> SerializeAsync<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
public static Task<string> SerializeAsync(object? value, Type inputType, JsonSerializerContext context);
public static Task<string> SerializeAsync(object? value, Type inputType, JsonSerializerOptions? options = null);
public static Task<string> SerializeAsync(object? value, JsonTypeInfo jsonTypeInfo);
public static Task<byte[]> SerializeToUtf8BytesAsync<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
public static Task<byte[]> SerializeToUtf8BytesAsync<TValue>(TValue value, JsonSerializerOptions? options = null);
public static Task<byte[]> SerializeToUtf8BytesAsync(object? value, Type inputType, JsonSerializerContext context);
public static Task<byte[]> SerializeToUtf8BytesAsync(object? value, Type inputType, JsonSerializerOptions? options = null);
public static Task<byte[]> SerializeToUtf8BytesAsync(object? value, JsonTypeInfo jsonTypeInfo);
public static Task<JsonNode?> SerializeToNodeAsync<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
public static Task<JsonNode?> SerializeToNodeAsync<TValue>(TValue value, JsonSerializerOptions? options = null);
public static Task<JsonNode?> SerializeToNodeAsync(object? value, Type inputType, JsonSerializerContext context);
public static Task<JsonNode?> SerializeToNodeAsync(object? value, Type inputType, JsonSerializerOptions? options = null);
public static Task<JsonNode?> SerializeToNodeAsync(object? value, JsonTypeInfo jsonTypeInfo);
public static Task<JsonElement> SerializeToElementAsync<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
public static Task<JsonElement> SerializeToElementAsync<TValue>(TValue value, JsonSerializerOptions? options = null);
public static Task<JsonElement> SerializeToElementAsync(object? value, Type inputType, JsonSerializerContext context);
public static Task<JsonElement> SerializeToElementAsync(object? value, Type inputType, JsonSerializerOptions? options = null);
public static Task<JsonElement> SerializeToElementAsync(object? value, JsonTypeInfo jsonTypeInfo);
public static Task<JsonDocument> SerializeToDocumentAsync<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo);
public static Task<JsonDocument> SerializeToDocumentAsync<TValue>(TValue value, JsonSerializerOptions? options = null);
public static Task<JsonDocument> SerializeToDocumentAsync(object? value, Type inputType, JsonSerializerContext context);
public static Task<JsonDocument> SerializeToDocumentAsync(object? value, Type inputType, JsonSerializerOptions? options = null);
public static Task<JsonDocument> SerializeToDocumentAsync(object? value, JsonTypeInfo jsonTypeInfo);API Usage
IAsyncEnumerable<MyType> asyncEnumerable = GetAsyncEnumerable();
var json = await JsonSerializer.SerializeAsync(asyncEnumerable);Alternative Designs
No response
Risks
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Text.Json