Skip to content

Conversation

stephentoub
Copy link
Member

Closes #108930

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces new JsonElement.Parse overloads for directly parsing JSON into a JsonElement, replaces existing JsonDocument.Parse(...).RootElement usage with the new API, updates tests to exercise and validate these overloads, and adds XML documentation for the new methods.

  • Added JsonElement.Parse overloads accepting string, ReadOnlySpan<char>, and ReadOnlySpan<byte> with optional JsonDocumentOptions
  • Refactored library and test code to use JsonElement.Parse in place of JsonDocument.Parse(...).RootElement
  • Updated XML docs and added tests covering valid, invalid, and option-respecting parse scenarios

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.

Show a summary per file
File Description
JsonElement.Parse.cs Added public Parse overloads and XML documentation
JsonDocument.Parse.cs Switched internal APIs to use ReadOnlySpan<char> instead of ReadOnlyMemory<char>
JsonNode.Parse.cs Changed calls from ParseValue to new Parse overloads
JsonSerializer.Write.Element.cs Updated serialization helpers to use JsonElement.Parse
Tests under System.Text.Json.Tests Refactored tests to use JsonElement.Parse, added new tests for valid/invalid parsing and options

@gregsdennis
Copy link
Contributor

gregsdennis commented Jun 6, 2025

@stephentoub could you describe a summary of the memory implications for these methods, please? If there's no JsonDocument, what owns the memory of the backing span? How is that managed?

I saw the ParseUnrented comment in the issue, and I figure that's related to memory, but I don't understand it.

@stephentoub
Copy link
Member Author

@stephentoub could you describe a summary of the memory implications for these methods, please? If there's no JsonDocument, what owns the memory of the backing span? How is that managed?

It's the same as if you did JsonSerializer.Deserialize<JsonElement>(json). The resulting JsonElement is just wrapping a byte[] it owns.

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@stephentoub stephentoub merged commit 678b5a6 into dotnet:main Jun 10, 2025
88 checks passed
@stephentoub stephentoub deleted the jsonelementparse branch June 10, 2025 14:19
@github-actions github-actions bot locked and limited conversation to collaborators Jul 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add JsonElement.Parse accelerator methods

4 participants