Skip to content

Enable NativeAOT-compatible usage of S.T.Json POCO serialization #5355

Description

@emrys90

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

Activity

  1. changed the title [-]How to use builder.UseSystemTextJson()?[/-] [+]Enable NativeAOT-compatible usage of S.T.Json POCO serialization[/+] on Oct 28, 2023
  2. added this to the 9.0.0 milestone on Oct 28, 2023
  3. roji commented on Oct 28, 2023

    @roji
    Member

    @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

  4. emrys90 commented on Oct 28, 2023

    @emrys90
    Author

    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?

  5. emrys90 commented on Oct 28, 2023

    @emrys90
    Author

    I realized I can read/write it as a string instead, and then serialize/deserialize it from there manually.

  6. roji commented on Oct 28, 2023

    @roji
    Member

    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.

  7. roji commented on Oct 29, 2023

    @roji
    Member

    Reopening to track unlocking NativeAOT-compatible POCO JSON serialization in the future.

  8. reopened this on Oct 29, 2023
  9. NinoFloris commented on Oct 29, 2023

    @NinoFloris
    Member

    @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

    public TDataSource EnableJsonMappings(JsonTypeInfo[] json, JsonTypeInfo[] jsonb); // Each JsonTypeInfo defines its Options and Type
    public TDataSource EnableJsonMappings(Type[] json, Type[] jsonb, JsonSerializerContext jsonSerializerContext);
    public TDataSource EnableJsonMappings(Type[] json, Type[] jsonb, JsonSerializerOptions jsonSerializerOptions);
  10. roji commented on Oct 29, 2023

    @roji
    Member

    Sounds good! Would the first overload also accept a JsonSerializerOptions, to tweak various non-AOT-related options?

    Should we do this for 8.0?

  11. NinoFloris commented on Oct 29, 2023

    @NinoFloris
    Member

    The first can't, JsonTypeInfo is already the 'closed-over' resolved info, it contains a reference to its owning options.

  12. NinoFloris commented on Oct 29, 2023

    @NinoFloris
    Member

    Should we do this for 8.0?

    Are you volunteering? ;)

  13. roji commented on Oct 29, 2023

    @roji
    Member

    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...

  14. modified the milestones: 9.0.0, 10.0.0 on Oct 21, 2024
  15. added theissue type on Jun 14, 2025
  16. modified the milestones: 10.0.0, 11.0.0 on Nov 2, 2025
  17. modified the milestones: 11.0.0, 12.0.0 on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions