private readonly Type? _jsonValueType;
// Unsupported types
+ private readonly Type _delegateType;
private readonly Type _typeType;
private readonly Type _serializationInfoType;
private readonly Type _intPtrType;
_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));
}
}
else if (_knownUnsupportedTypes.Contains(type) ||
- ImplementsIAsyncEnumerableInterface(type))
+ ImplementsIAsyncEnumerableInterface(type) ||
+ _delegateType.IsAssignableFrom(type))
{
classType = ClassType.KnownUnsupportedType;
}
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
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();
+ }
}
}
[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
{
}
[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
{
}