Skip to content

[API Proposal]: Add async parse overload for JsonNode #88248

@DoctorKrolic

Description

@DoctorKrolic

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions