runtime: Introduction of JsonTypeInfo in one of the Serialize overloads breaks the code in .net 6

I have EF Core conversion lambdas as follows:

entity.Property(e => e.Value).HasConversion(v => JsonSerializer.Serialize(v,null), v => JsonSerializer.Deserialize(v, null)); where ‘v’ is a HashSet<string> . It worked in .net5

After moving to .net 6, I got this compilation error:

“The call is ambiguous between the following methods or properties: ‘JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions?)’ and ‘JsonSerializer.Serialize<TValue>(TValue, JsonTypeInfo<TValue>)’”

This is because a new overloaded Serialize was introduced as a performance update to JsonSerializer (via https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/ )?

However, I cannot keep null as a named parameter or omit the null as an optional parameter because it is not allowed in lambads ( CS0854, there is no documentation page for this error though: https://docs.microsoft.com/en-us/dotnet/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error?f1url=%3FappId%3Droslyn%26k%3Dk(CS0854))

Consequently the code needs to be changed to:

entity.Property(e => e.Value).IsRequired().HasConversion(v => JsonSerializer.Serialize(v, new JsonSerializerOptions()), v => JsonSerializer.Deserialize<HashSet<string>>(v, new JsonSerializerOptions()));

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (15 by maintainers)

Most upvoted comments

@ericstj It’s worth noting that passing an expression tree to EF that uses this is very common. It’s even in our docs as an example. Many people will experience this break.

Thanks all!

It doesn’t hurt to have the breaking change notice. It can help folks who search the error string see it’s a known issue. I reopened the doc issue. Will close this since it is addressed.

Overloads can always cause this type of source break and we don’t document them as breaking changes. It’s similar to adding a new type. It always has the potential for a source breaking name collision. This change was not binary breaking.

@mayur-ekbote have you tried disambiguating by casting the null value, e.g. specifying (JsonSerializerOptions)null?