-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Description
The JSON type info generator triggers an exception when trying to serialize a object with a field that is marked as JsonIgnore but has no callable type constructor available.
System.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.'The reason is that the only constructor of class Secret (see code below) has a ref ROS field. And to date, Reflection.Emit cannot invoke such methods. That is all reasonable, but the serializer was explicitly instructed to ignore that field. The documentation is rather strict on stating that such fields are always ignored if attributed that way.
The deserializer should also never set this field, therefore never require to actually create any object and invoke any constructor.
My understanding is that a field/property marked as JsonIgnore is never touched or set by the serializer/deserializer. And when an object containing such a field is created, the field is never modified either.
Reproduction Steps
using System.Text.Json;
using System.Text.Json.Serialization;
var Obj = new Container();
var ObjText = JsonSerializer.Serialize(Obj, new JsonSerializerOptions { IncludeFields = true });
class Container
{
public ulong A;
public ulong B;
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public Secret? C;
}
class Secret
{
public ulong Code;
public Secret(in ReadOnlySpan<byte> payload)
{
}
}Expected behavior
no exception
Actual behavior
System.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.'Bonus: it would be nice if json could aggregate that exception and rethrow it upstream with the problematic field name added.
> [Exception] System.Private.CoreLib.dll!System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(System.RuntimeType type) Line 871 C#
[Exception] System.Private.CoreLib.dll!System.RuntimeType.SanityCheckGenericArguments(System.RuntimeType[] genericArguments, System.RuntimeType[] genericParameters) Line 879 C#
[Exception] System.Private.CoreLib.dll!System.RuntimeType.MakeGenericType(System.Type[] instantiation) Line 3513 C#
[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Converters.ObjectConverterFactory.CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) Line 84 C#
[Exception] System.Text.Json.dll!System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) Line 50 C#
[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetConverterForType(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options, bool resolveJsonConverterAttribute) Line 156 C#
[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.CreateJsonTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) Line 89 C#
[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) Line 71 C#
[Exception] System.Text.Json.dll!System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(System.Type type) Line 651 C#
...
Regression?
not to my knowledge
Known Workarounds
create a dummy, second parameterless public constructor, that triggers an exception when ever being called
Configuration
7.0.0-rc.1.22427.1
Other information
No response