Skip to content

Conversation

PranavSenthilnathan
Copy link
Member

Adds a new derived class that JsonValueConverter can use when the value is a primitive. Previously JsonElementOfValue was being used and more recently JsonValue. There are downsides with both. The JsonElement-backed class incurs overhead for the JsonDocument it needs to create and JsonValue does not allow common conversions in TryGet. The new class in this PR solves both by only storing necessary information and allowing conversions. It also preserves original escaping in WriteTo.

Fixes #116730

@PranavSenthilnathan PranavSenthilnathan added this to the 10.0.0 milestone Jun 18, 2025
@PranavSenthilnathan PranavSenthilnathan self-assigned this Jun 18, 2025
@Copilot Copilot AI review requested due to automatic review settings June 18, 2025 22:55
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 a new JsonValueOfJsonPrimitive class to optimize handling of JSON primitive values by storing raw UTF-8 bytes and escaping info, refactors reader and converter logic to use it, and adds comprehensive tests for primitive behaviors and conversions.

  • Add JsonValueOfJsonPrimitive to store only needed data for primitives and preserve original escaping in WriteTo.
  • Update JsonValueConverter, JsonDocument, and Utf8JsonReader to delegate GUID parsing to a new JsonReaderHelper.TryGetValue helper.
  • Extend tests in JsonValueTests.cs to cover deserialization, TryGetValue, WriteTo, deep-cloning, and conversion for all primitive types using a new WrappedT<T> wrapper.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonValueTests.cs Added tests for primitive JSON values (string, bool, number), WriteTo, cloning, deep-equals, and conversion; introduced WrappedT<T> helper.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Node/JsonValueConverter.cs Replaced token-type switch with instantiation of JsonValueOfJsonPrimitive for primitives.
src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs Replaced inline GUID parsing logic with JsonReaderHelper.TryGetValue call.
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs Added new overload TryGetValue(ReadOnlySpan<byte>, bool, out Guid) to centralize GUID parsing.
src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonValueOfJsonPrimitive.cs New internal sealed class implementing optimized storage and parsing for JSON primitives.
src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs Switched inline GUID parsing in TryGetValue to JsonReaderHelper.TryGetValue.
src/libraries/System.Text.Json/src/System.Text.Json.csproj Included JsonValueOfJsonPrimitive.cs in project file.

@PranavSenthilnathan
Copy link
Member Author

@eiriktsarpalis I can also take this PR one step further by adding separate derived classes for JSON string and numbers, and introducing the JsonNumber struct like we discussed offline.

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.

@PranavSenthilnathan
Copy link
Member Author

Keeping this fix more targeted by matching the logic in JsonValueOfElement.

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!

@PranavSenthilnathan PranavSenthilnathan enabled auto-merge (squash) July 22, 2025 07:38
@PranavSenthilnathan PranavSenthilnathan merged commit 4ea1713 into dotnet:main Jul 22, 2025
83 of 87 checks passed
@PranavSenthilnathan
Copy link
Member Author

/backport to release/10.0-preview7

Copy link
Contributor

Started backporting to release/10.0-preview7: https://github.com/dotnet/runtime/actions/runs/16439281496

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants