-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Replace Unsafe.Unbox with StrongBox for JSON source-generated struct accessors #133997
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
Open
jkoritzinsky
wants to merge
3
commits into
main
Choose a base branch
from
dev/jkoritzinsky/json-no-unsafe-unbox
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d609dfd
Replace Unsafe.Unbox with StrongBox for JSON source-generated struct …
jkoritzinsky a46a31c
Address PR feedback on StrongBox struct deserialization and fix CI co…
jkoritzinsky 8a9c9c0
Preserve legacy Get/Set contract for StrongBox-wrapped struct instances
jkoritzinsky 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur, this is a breaking change unfortunately. Users can access, modify, or wrap the delegate directly via the
JsonPropertyInfo.Setproperty and anybody doing so today is relying on the current implicit contract.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 8a9c9c0 without changing
JsonPropertyInfo.Get/.Set's existing contract or adding new public API.JsonPropertyInfo<T>now lazily detects, per property, whether the assignedGet/Setdelegate understands aStrongBox<TDeclaringType>-wrappedobj: it tries the delegate directly first, and onInvalidCastExceptionfalls back to a legacy unbox/invoke/rebox path (using the pre-existing publicSystem.Runtime.CompilerServices.IStrongBoxinterface, whichStrongBox<T>implements). The result is cached per property so the fallback only triggers once. This transparently covers:StrongBox-based struct accessors existed, andobjis a boxedTDeclaringType" contract (the exact scenario you flagged) - including one captured/wrapped fromJsonPropertyInfo.Get/.Setand reassigned.Since the public untyped
Get/Setproperties must keep returning the exact delegate instance a caller assigned (there's an existing test enforcing that), the compatibility handling lives behind the internal typed accessor instead, and the two internal call sites that previously invoked the untypedGet/Setdirectly (TryGetPrePopulatedValue, extension-data population) now go through newGetValueAsObject/SetValueAsObjectmethods so they get the same handling.No emitter changes were needed - new-gen's
StrongBox-aware delegates always succeed on the fast path.Note
This reply was generated with AI assistance.
Auto-replied by the GitHub Copilot app
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still breaks callers of the
JsonPropertyInfo.Setdelegate populated by the source generator. For example, this console app uses a contract modifier to trim string properties after deserialization. It invokes the generatedGet/Setdelegates without replacing them:Before the change, this prints:
After rebuilding with the generator and library from
8a9c9c0, it throws atproperty.Set!(obj, value.Trim()):I don't think this would be possible to fix without keeping our dependency on
Unsafe.Unbox.