New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BinaryData + JSON source gen produces compiler warnings #81702
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsIn #73448 we've added internal converter for BinaryData but that's not working with JSON source-gen. Reprousing System.Text.Json.Serialization;
[JsonSerializable(typeof(BinaryData))]
partial class MyContext : JsonSerializerContext { } Expected behavior:Works Actual
Code produced by source-gen (7.0): // <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
partial class MyContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData>? _BinaryData;
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData> BinaryData
{
get => _BinaryData ??= Create_BinaryData(Options, makeReadOnly: true);
}
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData> Create_BinaryData(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.BinaryData))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.BinaryData>(options, customConverter);
}
else
{
global::System.Text.Json.Serialization.JsonConverter converter = new global::System.BinaryDataConverter();
global::System.Type typeToConvert = typeof(global::System.BinaryData);
if (!converter.CanConvert(typeToConvert))
{
throw new global::System.InvalidOperationException(string.Format("The converter '{0}' is not compatible with the type '{1}'.", converter.GetType(), typeToConvert));
}
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.BinaryData> (options, converter);
}
if (makeReadOnly)
{
jsonTypeInfo.MakeReadOnly();
}
return jsonTypeInfo;
}
}
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsIn #73448 we've added internal converter for BinaryData but that's not working with JSON source-gen. Reprousing System.Text.Json.Serialization;
[JsonSerializable(typeof(BinaryData))]
partial class MyContext : JsonSerializerContext { } Expected behavior:Compiles and works when you serialize/deserialize Actual
Code produced by source-gen (7.0): // <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
partial class MyContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData>? _BinaryData;
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData> BinaryData
{
get => _BinaryData ??= Create_BinaryData(Options, makeReadOnly: true);
}
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData> Create_BinaryData(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.BinaryData))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.BinaryData>(options, customConverter);
}
else
{
global::System.Text.Json.Serialization.JsonConverter converter = new global::System.BinaryDataConverter();
global::System.Type typeToConvert = typeof(global::System.BinaryData);
if (!converter.CanConvert(typeToConvert))
{
throw new global::System.InvalidOperationException(string.Format("The converter '{0}' is not compatible with the type '{1}'.", converter.GetType(), typeToConvert));
}
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.BinaryData> (options, converter);
}
if (makeReadOnly)
{
jsonTypeInfo.MakeReadOnly();
}
return jsonTypeInfo;
}
}
|
In order to have the type supported in source gen the custom converter needs to be made public. |
@eiriktsarpalis with #87136 do we stop generating the converter logic? |
That's right. It will no longer fail to compile but the custom converter won't be picked up. |
#87140 tracks adding a SG diagnostic for this particular failure. |
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsIn #73448 we've added internal converter for BinaryData but that's not working with JSON source-gen. Reprousing System.Text.Json.Serialization;
[JsonSerializable(typeof(BinaryData))]
partial class MyContext : JsonSerializerContext { } Expected behavior:Compiles and works when you serialize/deserialize Actual
Code produced by source-gen (7.0): // <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
partial class MyContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData>? _BinaryData;
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData> BinaryData
{
get => _BinaryData ??= Create_BinaryData(Options, makeReadOnly: true);
}
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData> Create_BinaryData(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.BinaryData>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.BinaryData))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.BinaryData>(options, customConverter);
}
else
{
global::System.Text.Json.Serialization.JsonConverter converter = new global::System.BinaryDataConverter();
global::System.Type typeToConvert = typeof(global::System.BinaryData);
if (!converter.CanConvert(typeToConvert))
{
throw new global::System.InvalidOperationException(string.Format("The converter '{0}' is not compatible with the type '{1}'.", converter.GetType(), typeToConvert));
}
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.BinaryData> (options, converter);
}
if (makeReadOnly)
{
jsonTypeInfo.MakeReadOnly();
}
return jsonTypeInfo;
}
}
|
namespace System.Text.Json.Serialization;
public sealed class BinaryDataJsonConverter : JsonConverter<BinaryData>
{
// Overrides omitted
} |
EDIT by @eiriktsarpalis to reflect more recent error modes
In #73448 we've added internal converter for BinaryData but that's not working with JSON source-gen.
Repro
Expected behavior:
Compiles and works when you serialize/deserialize
Actual
API Proposal
We should make the constructor of the already included custom converter public:
The text was updated successfully, but these errors were encountered: