-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add documentation for CA2026: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement #49640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a8fed07
Initial plan
Copilot 91cf875
Add documentation for CA2026 analyzer rule
Copilot 8bae41a
Apply style guidelines to CA2026 documentation
Copilot 215b61f
human edits
gewarren 6e6ee3a
add to toc
gewarren 14d2c12
Move CA2026 code examples to external snippet files
Copilot 1373c56
Apply suggestions from code review
gewarren 63c0223
use .net 10
gewarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| title: "CA2026: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement" | ||
| description: "Learn about code analysis rule CA2026 - Prefer JsonElement.Parse over JsonDocument.Parse().RootElement" | ||
| ms.date: 11/05/2025 | ||
| f1_keywords: | ||
| - CA2026 | ||
| - PreferJsonElementParseOverJsonDocumentParse | ||
| helpviewer_keywords: | ||
| - CA2026 | ||
| ai-usage: ai-assisted | ||
| dev_langs: | ||
| - CSharp | ||
| - VB | ||
| --- | ||
|
|
||
| # CA2026: Prefer JsonElement.Parse over JsonDocument.Parse().RootElement | ||
|
|
||
| | Property | Value | | ||
| |-------------------------------------|--------------------------------------------------------------------------| | ||
| | **Rule ID** | CA2026 | | ||
| | **Title** | Prefer `JsonElement.Parse` over `JsonDocument.Parse().RootElement` | | ||
| | **Category** | [Reliability](reliability-warnings.md) | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
| | **Enabled by default in .NET 10** | As suggestion | | ||
|
|
||
| ## Cause | ||
|
|
||
| Code uses `JsonDocument.Parse().RootElement` to parse JSON into a <xref:System.Text.Json.JsonElement>. | ||
|
|
||
| ## Rule description | ||
|
|
||
| <xref:System.Text.Json.JsonElement.Parse*?displayProperty=nameWithType> is more efficient than calling `JsonDocument.Parse().RootElement`. The <xref:System.Text.Json.JsonDocument> type implements <xref:System.IDisposable> and rents arrays from the <xref:System.Buffers.ArrayPool%601>, which can lead to memory leaks or increased GC pressure if you don't properly dispose the document. The `JsonElement.Parse` method, introduced in .NET 10, parses JSON directly into a <xref:System.Text.Json.JsonElement> without these concerns, making it more efficient and less error-prone. | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| Replace calls to `JsonDocument.Parse().RootElement` with `JsonElement.Parse()`. | ||
|
|
||
| ## Examples | ||
|
|
||
| The following code snippet shows a violation of CA2026: | ||
|
|
||
| :::code language="csharp" source="snippets/csharp/all-rules/ca2026.cs" id="Violation"::: | ||
| :::code language="vb" source="snippets/vb/all-rules/ca2026.vb" id="Violation"::: | ||
|
|
||
| The following code snippet fixes the violation: | ||
|
|
||
| :::code language="csharp" source="snippets/csharp/all-rules/ca2026.cs" id="Fixed"::: | ||
| :::code language="vb" source="snippets/vb/all-rules/ca2026.vb" id="Fixed"::: | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| Don't suppress warnings from this rule. If you're targeting .NET 10 or later, you should use `JsonElement.Parse()` for better performance and resource management. | ||
|
|
||
| ## Suppress a warning | ||
|
|
||
| If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable CA2026 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore CA2026 | ||
| ``` | ||
|
|
||
| To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
|
||
| ```ini | ||
| [*.{cs,vb}] | ||
| dotnet_diagnostic.CA2026.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). | ||
|
|
||
| ## See also | ||
|
|
||
| - [Reliability rules](reliability-warnings.md) | ||
| - <xref:System.Text.Json.JsonElement.Parse%2A?displayProperty=nameWithType> | ||
| - <xref:System.Text.Json.JsonDocument.Parse%2A?displayProperty=nameWithType> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2026.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System.Text.Json; | ||
|
|
||
| namespace ca2026 | ||
| { | ||
| public static class Examples | ||
| { | ||
| //<Violation> | ||
| public static void ProcessJsonViolation(string json) | ||
| { | ||
| JsonElement element = JsonDocument.Parse(json).RootElement; | ||
| //... | ||
| } | ||
| //</Violation> | ||
|
|
||
| //<Fixed> | ||
| public static void ProcessJsonFixed(string json) | ||
| { | ||
| JsonElement element = JsonElement.Parse(json); | ||
| //... | ||
| } | ||
| //</Fixed> | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
docs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca2026.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| Imports System.Text.Json | ||
|
|
||
| Namespace ca2026 | ||
|
|
||
| Public Module Examples | ||
|
|
||
| '<Violation> | ||
| Public Sub ProcessJsonViolation(json As String) | ||
| Dim element As JsonElement = JsonDocument.Parse(json).RootElement | ||
| '... | ||
| End Sub | ||
| '</Violation> | ||
|
|
||
| '<Fixed> | ||
| Public Sub ProcessJsonFixed(json As String) | ||
| Dim element As JsonElement = JsonElement.Parse(json) | ||
| '... | ||
| End Sub | ||
| '</Fixed> | ||
|
|
||
| End Module | ||
|
|
||
| End Namespace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.