You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Relational: Allow a custom serializer for JSON columns #28043
@SaifAqqad@LuohuaRain We made significant changes to the JSON infrastructure in 8, which means some additional design is now needed to expose customization of what is now already a custom serialization mechanism.
@wjrogers the Npgsql provider's support for JSON which this affects is very different from the EF owned entity support for JSON (ToJson()) that's discussed here. For 8.0 I'm working on bringing the latter into Npgsql as well.
Definitely need this - I was hoping to use built-in JSON serialization, but it's not handling badly formed data well at all (the exceptions don't even tell me where the error is!).
But none of the JsonConvertor (or other Json-) attributes I'm adding seem to be getting acknowledged at all.
That way it honors all the attributes that normally work to control JSON serialisation (though it doesn't allow using DI-managed JsonSerializerOptions).
Reacted by Systems Projects I+D, Dobóczy Ákos, Dazfl, Yury Romanov, 滨灯诗夜, johnbaro, Henrik Hoyer, enchart and Roger Far
That way it honors all the attributes that normally work to control JSON serialisation (though it doesn't allow using DI-managed JsonSerializerOptions).
That way it honors all the attributes that normally work to control JSON serialisation (though it doesn't allow using DI-managed JsonSerializerOptions).
This works, but as long as we don't expect a correct LINQ->SQL translation(mapping of columns inside POCO to json functions vars) with the modified Case (snake_case for ex).
The same thing was said here: npgsql/efcore.pg#1107 (comment)
Any workarounds (except using JsonPropertyNameAttribute)?
I need a way to ignore properties with null values in serialization. This would allow me to use completely different properties in different environments without unnecessary overhead in the database. Serialization options would be very important for this. LINQ->SQL translation is essential.
Reacted by Cubody, Saif Alaqqad, Roger Far, Matt, vfbib79, Mark Murphy, George Drak, Pål K Halle, Pascal Martin, lonix1 and 3 more
To expand on the previous comment... We don't want to persist nulls, because it wastes space, e.g. "{ ""Foo"": null, ""Bar"": null, ""Baz"": null, ""Qux"": null, ..., ""Something"": ""Hello"" }". That's a simple example, but data serialised as JSON is often of the "bag of stuff" variety which may have many properties.
And it does not honour [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)], so there's no way to prevent that behaviour without reconfiguring the entire context.
Our workaround is to avoid ToJson(), and use the old way instead:
varoptions=newJsonSerializerOptions{DefaultIgnoreCondition=JsonIgnoreCondition.WhenWritingNull,// <-----};builder.Property(x =>x.Cat).HasColumnType("jsonb").HasConversion(newValueConverter<Animal,string>(
x =>JsonSerializer.Serialize(x,options),
x =>JsonSerializer.Deserialize<Animal>(x,options)),newValueComparer<Animal>(...));
Other than the extra complexity, another downside is we can't query those properties.
Activity
[-]Allow a custom serializer for JSON columns[/-][+]Relational: Allow a custom serializer for JSON columns[/+]SaifAqqad commentedon Sep 19, 2023
Is this still planned for release with .NET 8?
LuohuaRain commentedon Sep 22, 2023
I'd like to know as well.
ajcvickers commentedon Oct 3, 2023
@SaifAqqad @LuohuaRain We made significant changes to the JSON infrastructure in 8, which means some additional design is now needed to expose customization of what is now already a custom serialization mechanism.
9 remaining items
shahrzadabedi commentedon Oct 25, 2023
Thanks Arthur
shahrzadabedi commentedon Oct 25, 2023
Thank you for the clarification, Shay.
wizofaus commentedon Jan 23, 2024
Definitely need this - I was hoping to use built-in JSON serialization, but it's not handling badly formed data well at all (the exceptions don't even tell me where the error is!).
But none of the JsonConvertor (or other Json-) attributes I'm adding seem to be getting acknowledged at all.
romanov commentedon Jan 26, 2024
Is there any temp solution for now? Like global setting, or EF uses built-in serialization?
wizofaus commentedon Jan 27, 2024
I got it working the way I needed using
which you can use in the OnModelCreating as needed, e.g.:
modelBuilder.Entity<MyRowType>().Property(expr=> expr.JsonField).HasJsonConversion();That way it honors all the attributes that normally work to control JSON serialisation (though it doesn't allow using DI-managed JsonSerializerOptions).
dev-mfm commentedon Jul 23, 2024
I tried your solution but it didn't work.
wizofaus commentedon Jul 27, 2024
Didn't work in what sense? Don't have access to the codebase I was working on where I did that now but it was definitely doing the job for me.
JsonSerializerOptions? #27991 as a duplicate of this issueCubody commentedon Jun 19, 2025
This works, but as long as we don't expect a correct LINQ->SQL translation(mapping of columns inside POCO to json functions vars) with the modified Case (snake_case for ex).
The same thing was said here:
npgsql/efcore.pg#1107 (comment)
Any workarounds (except using JsonPropertyNameAttribute)?
DarkGraySun commentedon Jul 7, 2025
I need a way to ignore properties with null values in serialization. This would allow me to use completely different properties in different environments without unnecessary overhead in the database. Serialization options would be very important for this. LINQ->SQL translation is essential.
lonix1 commentedon Jan 19, 2026
To expand on the previous comment... We don't want to persist nulls, because it wastes space, e.g.
"{ ""Foo"": null, ""Bar"": null, ""Baz"": null, ""Qux"": null, ..., ""Something"": ""Hello"" }". That's a simple example, but data serialised as JSON is often of the "bag of stuff" variety which may have many properties.And it does not honour
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)], so there's no way to prevent that behaviour without reconfiguring the entire context.Our workaround is to avoid
ToJson(), and use the old way instead:Other than the extra complexity, another downside is we can't query those properties.