-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Support polymorphic value-type converters and add validation on values returned by custom converters #40914
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
Support polymorphic value-type converters and add validation on values returned by custom converters #40914
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6d70fff
IL emit Box/Unbox in member access
devsko e77e7cc
Test case for object converter
devsko c35942c
Test case for class with primitive members and object converter
devsko 9cd5f5e
Address feedback
devsko 906b41c
Fixed issues with JsonConverter<object>and nullable value-types
devsko 1b795cf
Address feedback
devsko f635c21
Fix test for .NETStandard version
devsko a27f823
Fix #41146
devsko fd5d793
Accidentally commited the local test hack
devsko 71a9c8f
Throw JsonException when a deserialized value cannot be assigned to t…
devsko a264a6c
Merge branch 'master' into box-member-access
devsko 1f36572
Fix merge
devsko 177d9bd
Merge branch 'box-member-access' of https://github.com/devsko/runtime…
devsko e66be2e
Fix merge again
devsko 36f7ec6
Undo Fix #41146
devsko baa58c7
Consolidate nullable annotations
devsko 9151c96
Addressed feedback
devsko a8b2046
Addressed feedback
devsko 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
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
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
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
42 changes: 42 additions & 0 deletions
42
src/libraries/System.Text.Json/src/System/Text/Json/TypeExtensions.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,42 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace System.Text.Json | ||
| { | ||
| internal static class TypeExtensions | ||
| { | ||
| /// <summary> | ||
| /// Returns <see langword="true" /> when the given type is of type <see cref="Nullable{T}"/>. | ||
| /// </summary> | ||
| public static bool IsNullableValueType(this Type type) | ||
| { | ||
| return Nullable.GetUnderlyingType(type) != null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <see langword="true" /> when the given type is either a reference type or of type <see cref="Nullable{T}"/>. | ||
| /// </summary> | ||
| public static bool IsNullableType(this Type type) | ||
| { | ||
| return !type.IsValueType || IsNullableValueType(type); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <see langword="true" /> when the given type is assignable from <paramref name="from"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Other than <see cref="Type.IsAssignableFrom(Type)"/> also returns <see langword="true" /> when <paramref name="type"/> is of type <see cref="Nullable{T}"/> where <see langword="T" /> : <see langword="IInterface" /> and <paramref name="from"/> is of type <see langword="IInterface" />. | ||
| /// </remarks> | ||
| public static bool IsAssignableFromInternal(this Type type, Type from) | ||
| { | ||
| if (IsNullableValueType(from) && type.IsInterface) | ||
| { | ||
| return type.IsAssignableFrom(from.GetGenericArguments()[0]); | ||
| } | ||
|
|
||
| return type.IsAssignableFrom(from); | ||
| } | ||
| } | ||
| } |
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,20 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| namespace System.Text.Json.Tests | ||
| { | ||
| public static class AssertHelper | ||
| { | ||
| public static void ValidateJson(IEnumerable<string> expectedProperties, string json) | ||
| { | ||
| Assert.StartsWith("{", json); | ||
| Assert.EndsWith("}", json); | ||
| foreach (string expectedProperty in expectedProperties) | ||
| Assert.Contains(expectedProperty, json); | ||
| } | ||
|
|
||
| } | ||
| } |
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.