-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Json
Milestone
Description
Background and motivation
Both JsonSerializer and JsonDocument have async parse method overloads when deserializing from Stream. However, JsonNode and JsonElement do not. This is especially odd since JsonNode.Parse under the hood calls JsonElement.ParseElement which under the hood uses JsonDocument which already has necessary asyn overloads.
I won't write a paragraph about why async is preferred when using IO operations.
API Proposal
Basically copy of existing Stream-related signature, but with Async suffix in name, warpped return type into Task and additional optional CancellationToken parameter:
namespace System.Text.Json;
public class JsonNode
{
public static Task<JsonNode?> ParseAsync(
Stream utf8Json,
JsonNodeOptions? nodeOptions = null,
JsonDocumentOptions documentOptions = default,
CancellationToken cancellationToken = default);
}API Usage
Nothing revolutional really...
using System.IO;
using System.Text.Json.Nodes;
using var stream = File.OpenRead("myFile.json");
var node = await JsonNode.ParseAsync(stream);
...Alternative Designs
No response
Risks
No response
Reactions are currently unavailable
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.Json