-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Avoid closures in JsonPropertyInfoOfT/JsonTypeInfoOfT
#117498
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
Closed
Closed
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -46,12 +46,12 @@ private protected override void SetGetter(Delegate? getter) | |
| else if (getter is Func<object, T> typedGetter) | ||
| { | ||
| _typedGet = typedGetter; | ||
| _untypedGet = getter is Func<object, object?> untypedGet ? untypedGet : obj => typedGetter(obj); | ||
| _untypedGet = getter is Func<object, object?> untypedGet ? untypedGet : obj => _typedGet(obj); | ||
| } | ||
| else | ||
| { | ||
| Func<object, object?> untypedGet = (Func<object, object?>)getter; | ||
| _typedGet = (obj => (T)untypedGet(obj)!); | ||
| _typedGet = (obj => (T)_untypedGet!(obj)!); | ||
| _untypedGet = untypedGet; | ||
| } | ||
| } | ||
|
|
@@ -69,12 +69,12 @@ private protected override void SetSetter(Delegate? setter) | |
| else if (setter is Action<object, T> typedSetter) | ||
| { | ||
| _typedSet = typedSetter; | ||
| _untypedSet = setter is Action<object, object?> untypedSet ? untypedSet : (obj, value) => typedSetter(obj, (T)value!); | ||
| _untypedSet = setter is Action<object, object?> untypedSet ? untypedSet : (obj, value) => _typedSet(obj, (T)value!); | ||
| } | ||
| else | ||
| { | ||
| Action<object, object?> untypedSet = (Action<object, object?>)setter; | ||
| _typedSet = ((obj, value) => untypedSet(obj, value)); | ||
| _typedSet = ((obj, value) => _untypedSet!(obj, value)); | ||
| _untypedSet = untypedSet; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
| } | ||
| } | ||
|
|
@@ -100,12 +100,12 @@ private protected override void SetShouldSerialize(Delegate? predicate) | |
| else if (predicate is Func<object, T?, bool> typedPredicate) | ||
| { | ||
| _shouldSerializeTyped = typedPredicate; | ||
| _shouldSerialize = typedPredicate is Func<object, object?, bool> untypedPredicate ? untypedPredicate : (obj, value) => typedPredicate(obj, (T?)value); | ||
| _shouldSerialize = typedPredicate is Func<object, object?, bool> untypedPredicate ? untypedPredicate : (obj, value) => _shouldSerializeTyped(obj, (T?)value); | ||
| } | ||
| else | ||
| { | ||
| Func<object, object?, bool> untypedPredicate = (Func<object, object?, bool>)predicate; | ||
| _shouldSerializeTyped = (obj, value) => untypedPredicate(obj, value); | ||
| _shouldSerializeTyped = (obj, value) => _shouldSerialize!(obj, value); | ||
| _shouldSerialize = untypedPredicate; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
| } | ||
| } | ||
|
|
||
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.
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.
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.
Nit: it doesn't actually matter, because the delegate won't be invoked yet, but could we switch these two lines so that _untypedGet is set before it's referenced? I find it a bit easier to reason about. (You might also be able to remove the
!, I'm not sure)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.
Hypothetically, if I called the getter to store a copy of the existing delegate, but then set a new delegate via the setter, my copy would start pointing to the new behaviour. I think that makes a subtle difference that could regress code that is heavy on contract customization.
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 is in reference to the PR, not to my nit, yes?
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.
Yep
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.
Does this mean I should close this?
I don't mind either way. This is a drop in the bucket. I'm looking at a relatively big app (40 MB) where 15% of the app is System.Text.Json because of how many generics S.T.Json will create for every type used with Json. This looked like something we could do something about, the rest I don't know how to fix.
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.
Yeah I think so. It doesn't change things much either way so I would err towards preserving the current behaviour.