Skip to content

Commit e0287b7

Browse files
authored
Make delegates unsupported by JsonSerializer (#63495)
1 parent e210a60 commit e0287b7

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ private sealed class Parser
9393
private readonly Type? _jsonValueType;
9494

9595
// Unsupported types
96+
private readonly Type _delegateType;
9697
private readonly Type _typeType;
9798
private readonly Type _serializationInfoType;
9899
private readonly Type _intPtrType;
@@ -221,6 +222,7 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
221222
_jsonValueType = _metadataLoadContext.Resolve(JsonValueFullName);
222223

223224
// Unsupported types.
225+
_delegateType = _metadataLoadContext.Resolve(SpecialType.System_Delegate);
224226
_typeType = _metadataLoadContext.Resolve(typeof(Type));
225227
_serializationInfoType = _metadataLoadContext.Resolve(typeof(Runtime.Serialization.SerializationInfo));
226228
_intPtrType = _metadataLoadContext.Resolve(typeof(IntPtr));
@@ -906,7 +908,8 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
906908
}
907909
}
908910
else if (_knownUnsupportedTypes.Contains(type) ||
909-
ImplementsIAsyncEnumerableInterface(type))
911+
ImplementsIAsyncEnumerableInterface(type) ||
912+
_delegateType.IsAssignableFrom(type))
910913
{
911914
classType = ClassType.KnownUnsupportedType;
912915
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UnsupportedTypeConverterFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public override bool CanConvert(Type type)
2323
type == typeof(SerializationInfo) ||
2424
type == typeof(IntPtr) ||
2525
type == typeof(UIntPtr) ||
26+
// Exlude delegates.
27+
typeof(Delegate).IsAssignableFrom(type) ||
2628
// DateOnly/TimeOnly support to be added in future releases;
2729
// guard against invalid object-based serializations for now.
2830
// cf. https://github.com/dotnet/runtime/issues/53539

src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,5 +2817,37 @@ public class TypeWith_IgnoredPropWith_BadConverter
28172817
public class BadConverter
28182818
{
28192819
}
2820+
2821+
[Fact]
2822+
public async Task TestClassWithIgnoredCallbacks()
2823+
{
2824+
Assert.Equal("{}", await JsonSerializerWrapperForString.SerializeWrapper(new ClassWithIgnoredCallbacks()));
2825+
var obj = await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithIgnoredCallbacks>(@"{""Func"":"""",""Action"":""""}");
2826+
Assert.False(obj.Func(""));
2827+
Assert.Null(obj.Action);
2828+
}
2829+
2830+
[Fact]
2831+
public async Task TestClassWithCallbacks()
2832+
{
2833+
await Assert.ThrowsAsync<NotSupportedException>(async () => await JsonSerializerWrapperForString.SerializeWrapper(new ClassWithCallbacks()));
2834+
await Assert.ThrowsAsync<NotSupportedException>(async () => await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithCallbacks>(@"{""Func"":{},""Action"":{}"));
2835+
}
2836+
2837+
public class ClassWithIgnoredCallbacks
2838+
{
2839+
[JsonIgnore]
2840+
public Func<string, bool> Func { get; set; } = (val) => false;
2841+
2842+
[JsonIgnore]
2843+
public Action<bool> Action { get; set; }
2844+
}
2845+
2846+
public class ClassWithCallbacks
2847+
{
2848+
public Func<string, bool> Func { get; set; }
2849+
2850+
public Action<bool> Action { get; set; } = (val) => Console.WriteLine();
2851+
}
28202852
}
28212853
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ public override async Task HonorJsonPropertyName_PrivateSetter()
268268
[JsonSerializable(typeof(TypeWith_IgnoredRefStringProp))]
269269
[JsonSerializable(typeof(TypeWith_PropWith_BadConverter))]
270270
[JsonSerializable(typeof(TypeWith_IgnoredPropWith_BadConverter))]
271+
[JsonSerializable(typeof(ClassWithIgnoredCallbacks))]
272+
[JsonSerializable(typeof(ClassWithCallbacks))]
271273
internal sealed partial class PropertyVisibilityTestsContext_Metadata : JsonSerializerContext
272274
{
273275
}
@@ -439,6 +441,8 @@ public override async Task JsonIgnoreCondition_WhenWritingNull_OnValueType_Fail_
439441
[JsonSerializable(typeof(TypeWith_IgnoredRefStringProp))]
440442
[JsonSerializable(typeof(TypeWith_PropWith_BadConverter))]
441443
[JsonSerializable(typeof(TypeWith_IgnoredPropWith_BadConverter))]
444+
[JsonSerializable(typeof(ClassWithIgnoredCallbacks))]
445+
[JsonSerializable(typeof(ClassWithCallbacks))]
442446
internal sealed partial class PropertyVisibilityTestsContext_Default : JsonSerializerContext
443447
{
444448
}

0 commit comments

Comments
 (0)