Skip to content

Commit

Permalink
Make delegates unsupported by JsonSerializer (dotnet#63495)
Browse files Browse the repository at this point in the history
  • Loading branch information
layomia authored Jan 7, 2022
1 parent e210a60 commit e0287b7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private sealed class Parser
private readonly Type? _jsonValueType;

// Unsupported types
private readonly Type _delegateType;
private readonly Type _typeType;
private readonly Type _serializationInfoType;
private readonly Type _intPtrType;
Expand Down Expand Up @@ -221,6 +222,7 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
_jsonValueType = _metadataLoadContext.Resolve(JsonValueFullName);

// Unsupported types.
_delegateType = _metadataLoadContext.Resolve(SpecialType.System_Delegate);
_typeType = _metadataLoadContext.Resolve(typeof(Type));
_serializationInfoType = _metadataLoadContext.Resolve(typeof(Runtime.Serialization.SerializationInfo));
_intPtrType = _metadataLoadContext.Resolve(typeof(IntPtr));
Expand Down Expand Up @@ -906,7 +908,8 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
}
}
else if (_knownUnsupportedTypes.Contains(type) ||
ImplementsIAsyncEnumerableInterface(type))
ImplementsIAsyncEnumerableInterface(type) ||
_delegateType.IsAssignableFrom(type))
{
classType = ClassType.KnownUnsupportedType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public override bool CanConvert(Type type)
type == typeof(SerializationInfo) ||
type == typeof(IntPtr) ||
type == typeof(UIntPtr) ||
// Exlude delegates.
typeof(Delegate).IsAssignableFrom(type) ||
// DateOnly/TimeOnly support to be added in future releases;
// guard against invalid object-based serializations for now.
// cf. https://github.com/dotnet/runtime/issues/53539
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2817,5 +2817,37 @@ public class TypeWith_IgnoredPropWith_BadConverter
public class BadConverter
{
}

[Fact]
public async Task TestClassWithIgnoredCallbacks()
{
Assert.Equal("{}", await JsonSerializerWrapperForString.SerializeWrapper(new ClassWithIgnoredCallbacks()));
var obj = await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithIgnoredCallbacks>(@"{""Func"":"""",""Action"":""""}");
Assert.False(obj.Func(""));
Assert.Null(obj.Action);
}

[Fact]
public async Task TestClassWithCallbacks()
{
await Assert.ThrowsAsync<NotSupportedException>(async () => await JsonSerializerWrapperForString.SerializeWrapper(new ClassWithCallbacks()));
await Assert.ThrowsAsync<NotSupportedException>(async () => await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithCallbacks>(@"{""Func"":{},""Action"":{}"));
}

public class ClassWithIgnoredCallbacks
{
[JsonIgnore]
public Func<string, bool> Func { get; set; } = (val) => false;

[JsonIgnore]
public Action<bool> Action { get; set; }
}

public class ClassWithCallbacks
{
public Func<string, bool> Func { get; set; }

public Action<bool> Action { get; set; } = (val) => Console.WriteLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ public override async Task HonorJsonPropertyName_PrivateSetter()
[JsonSerializable(typeof(TypeWith_IgnoredRefStringProp))]
[JsonSerializable(typeof(TypeWith_PropWith_BadConverter))]
[JsonSerializable(typeof(TypeWith_IgnoredPropWith_BadConverter))]
[JsonSerializable(typeof(ClassWithIgnoredCallbacks))]
[JsonSerializable(typeof(ClassWithCallbacks))]
internal sealed partial class PropertyVisibilityTestsContext_Metadata : JsonSerializerContext
{
}
Expand Down Expand Up @@ -439,6 +441,8 @@ public override async Task JsonIgnoreCondition_WhenWritingNull_OnValueType_Fail_
[JsonSerializable(typeof(TypeWith_IgnoredRefStringProp))]
[JsonSerializable(typeof(TypeWith_PropWith_BadConverter))]
[JsonSerializable(typeof(TypeWith_IgnoredPropWith_BadConverter))]
[JsonSerializable(typeof(ClassWithIgnoredCallbacks))]
[JsonSerializable(typeof(ClassWithCallbacks))]
internal sealed partial class PropertyVisibilityTestsContext_Default : JsonSerializerContext
{
}
Expand Down

0 comments on commit e0287b7

Please sign in to comment.