-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Json source generator should support JsonConverter on non-nullable and nullable types #66547
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionI have a custom JsonConverter like I have to create 2 converters Reproduction Steps
using System.Text.Json;
using System.Text.Json.Serialization;
Console.WriteLine(JsonSerializer.Serialize(new Sample()));
class Sample
{
[JsonConverter(typeof(DateTimeOffsetToTimestampJsonConverter))]
public DateTimeOffset Start { get; set; }
[JsonConverter(typeof(DateTimeOffsetToTimestampJsonConverter))]
public DateTimeOffset? End { get; set; } // Without this property, this is fine
}
class DateTimeOffsetToTimestampJsonConverter : JsonConverter<DateTimeOffset>
{
internal const long TicksPerMicroseconds = 10;
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetInt64();
return new DateTimeOffset(value * TicksPerMicroseconds, TimeSpan.Zero);
}
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Ticks / TicksPerMicroseconds);
}
}
[JsonSourceGenerationOptions]
[JsonSerializable(typeof(Sample))]
internal sealed partial class SourceGenerationContext : JsonSerializerContext
{
} Expected behaviorI can use the json source generator without compilation errors. Actual behavior
Regression?No response Known WorkaroundsUse 2 different converters, one for non-nullable values and one for nullable values. Configuration
Other informationNo response
|
Interesting note, you can override the default options (not sure how safe this is but it works, would be nice to have an extensible method for this instead, but I digress) that will let your source generator work for the majority of scenarios. [JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(MyObject))]
public partial class MyJsonSerializerContext : JsonSerializerContext
{
static MyJsonSerializerContext()
{
s_defaultOptions.Converters.Add(new JsonStringEnumConverter());
}
} |
Hi @layomia
|
If anything was fixed, it was probably done accidentally. Before closing we should at least add testing validating that the scenario works. |
@pedrobsaila I don't believe that the issue has been addressed. Even though the changes in #86526 mean that the code now compiles, the example will still fail at run time with a cast exception. |
Description
I have a custom JsonConverter like
CustomConverter : JsonConverter<DateTime>
. Without the source generator I can use the custom converter forDateTime
andDateTime?
properties. Using the source generator, the generated code doesn't compile for nullable properties.I have to create 2 converters
CustomConverter : JsonConverter<DateTime>
andNullableCustomConverter : JsonConverter<DateTime?>
.Reproduction Steps
Expected behavior
I can use the json source generator without compilation errors.
Actual behavior
Regression?
No response
Known Workarounds
Use 2 different converters, one for non-nullable values and one for nullable values.
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: