Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -903,7 +905,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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The "Exlude delegates" comment doesn't seem useful. If we keep it, there's also a typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I really just wanted some visual separation from the previous set of types. Will clean up in main/7.0+ in follow up PR.

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