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
Enable NativeAOT-compatible usage of S.T.Json POCO serialization #5355
I'm attempting to setup source generation for System.Text.Json to be compatible with NativeAOT, so looking to figure out how to set the default json settings for npgsql. From what I can find in previous merges, UseSystemTextJson is something that should be available now? I am not seeing it as a valid method, nor seeing any nuget packets that sound like it.
Npgsql version: 8.0.0-rc.2
Reacted by dnetguru, Mike, Noah Stolk, Leonid Smirnov, boukenka, Jakov Glavina, Glen, Michael Hofer, Arman Ossi Loko, Andrey Kuleshov and 103 more
@emrys90 the method you're looking for is EnableDynamicJsonMappings(); this is documented in the 8.0 release notes.
However, this does not allow you to use arbitrary POCO serialization with S.T.Json and NativeAOT - the method is annotated with [RequiresDynamicCode]; this annotation was the main reason for separating dynamic JSON functionality out to its own opt-in (previously this was built-in without an opt-in).
S.T.Json indeed supports NativeAOT, but for 8.0 we won't yet be supporting integrating with that in Npgsql. The only S.T.JSON serialization that's supported with NativeAOT is non-POCO serialization (e.g. JsonElement).
I see that EnableDynamicJsonMappings has a parameter for JsonSerializerOptions. Given that source generation for System.Text.Json can be used through JsonSerializerOptions, would it not work by just passing that in once configured?
I see that EnableDynamicJsonMappings has a parameter for JsonSerializerOptions. Given that source generation for System.Text.Json can be used through JsonSerializerOptions, would it not work by just passing that in once configured?
It could - I don't know enough yet about how that works (and interacts with the source generator). This is definitely something we should look into e.g. for 9.0.
I realized I can read/write it as a string instead, and then serialize/deserialize it from there manually.
Yep - that's always an option, and doesn't rely on Npgsql doing any JSON processing at all.
@roji technically we can already support it via our EnableDynamicMappings api but it'd generate warnings due to our (null coalescing) references to JsonSerializerOptions.Default and the DefaultJsonTypeInfoResolver.
To make things warning free we should add EnableJsonMappings which would be without RUC/RDC attributes.
Default overloads of JsonSerializer.Deserialize/Serialize state:
JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
We could then add overloads like
publicTDataSourceEnableJsonMappings(JsonTypeInfo[]json,JsonTypeInfo[]jsonb);// Each JsonTypeInfo defines its Options and TypepublicTDataSourceEnableJsonMappings(Type[]json,Type[]jsonb,JsonSerializerContextjsonSerializerContext);publicTDataSourceEnableJsonMappings(Type[]json,Type[]jsonb,JsonSerializerOptionsjsonSerializerOptions);
Activity
[-]How to use builder.UseSystemTextJson()?[/-][+]Enable NativeAOT-compatible usage of S.T.Json POCO serialization[/+]roji commentedon Oct 28, 2023
@emrys90 the method you're looking for is EnableDynamicJsonMappings(); this is documented in the 8.0 release notes.
However, this does not allow you to use arbitrary POCO serialization with S.T.Json and NativeAOT - the method is annotated with [RequiresDynamicCode]; this annotation was the main reason for separating dynamic JSON functionality out to its own opt-in (previously this was built-in without an opt-in).
S.T.Json indeed supports NativeAOT, but for 8.0 we won't yet be supporting integrating with that in Npgsql. The only S.T.JSON serialization that's supported with NativeAOT is non-POCO serialization (e.g. JsonElement).
/cc @NinoFloris
emrys90 commentedon Oct 28, 2023
I see that EnableDynamicJsonMappings has a parameter for JsonSerializerOptions. Given that source generation for System.Text.Json can be used through JsonSerializerOptions, would it not work by just passing that in once configured?
emrys90 commentedon Oct 28, 2023
I realized I can read/write it as a string instead, and then serialize/deserialize it from there manually.
roji commentedon Oct 28, 2023
It could - I don't know enough yet about how that works (and interacts with the source generator). This is definitely something we should look into e.g. for 9.0.
Yep - that's always an option, and doesn't rely on Npgsql doing any JSON processing at all.
roji commentedon Oct 29, 2023
Reopening to track unlocking NativeAOT-compatible POCO JSON serialization in the future.
NinoFloris commentedon Oct 29, 2023
@roji technically we can already support it via our EnableDynamicMappings api but it'd generate warnings due to our (null coalescing) references to JsonSerializerOptions.Default and the DefaultJsonTypeInfoResolver.
To make things warning free we should add
EnableJsonMappingswhich would be without RUC/RDC attributes.Default overloads of JsonSerializer.Deserialize/Serialize state:
We could then add overloads like
roji commentedon Oct 29, 2023
Sounds good! Would the first overload also accept a JsonSerializerOptions, to tweak various non-AOT-related options?
Should we do this for 8.0?
NinoFloris commentedon Oct 29, 2023
The first can't, JsonTypeInfo is already the 'closed-over' resolved info, it contains a reference to its owning options.
NinoFloris commentedon Oct 29, 2023
Are you volunteering? ;)
roji commentedon Oct 29, 2023
Can't commit given all that's going on :)
We can leave it open for now and see if one of us gets around to it...