Skip to content

Allow mapping CLR types to JSON at the type mapper level #2568

Closed as not planned
Closed as not planned

Description

@roji

Our current JSON support - both the older JSON.NET plugin and the newer System.Text.Json support introduced in #2306 - require you to manually specify NpgsqlDbType.{Json,Jsonb} on every parameter. In other words, it is currently not possible to map a user POCO type to json/jsonb at the global or connector level (the JSON.NET plugin exposes an API for this, but it does not actually work for over a single type).

This is because our type mapping currently doesn't support a situation where more than one CLR type is mapped to the same PostgreSQL type.

Note: it will not be possible to have the user POCO returned by default when reading (i.e. without explicitly specifying it in GetFieldValue), since there's no way for us to distinguish between incoming JSON documents coming back from the database.

Another reason we need to allow mapping at the type mapper level, is in order to allow the user to specify JSON serialization options.

This is very related (and could be a dup of) #2439.

Activity

  1. added this to the 5.0 milestone on Aug 14, 2019
  2. self-assigned this
    on Aug 14, 2019
  3. modified the milestones: 5.0, Backlog on Sep 17, 2020
  4. vonzshik commented on Jan 8, 2023

    @vonzshik
    Contributor

    I think we can close this issue since we don't need to specify NpgsqlDbType.{Json,Jsonb} anymore?

  5. maximiliancsuk commented on Jan 9, 2023

    @maximiliancsuk

    I think we can close this issue since we don't need to specify NpgsqlDbType.{Json,Jsonb} anymore?

    I don't think so. IMHO this issue is about being able to directly map JSON columns to CLR types, without any intermediate structure such as JsonDocument. And for that, the type mapper needs to be able to transform any JSON result column into a user-supplied CLR type, potentially including JSON serialization options. That's what I thought at least and why I subscribed to this issue... I could be wrong though.

  6. roji commented on Jan 9, 2023

    @roji
    MemberAuthor

    I think we can close this issue since we don't need to specify NpgsqlDbType.{Json,Jsonb} anymore?

    @vonzshik this issue was about writing arbitrary user POCOs without NpgsqlDbType, not JsonDocument/JsonElement etc.

    Note that for System.Text.Json, we have #4846 which is about moving that support out to a plugin. I originally filed that for in order to optimize for size (allow S.T.J to be trimmed if not in use), but we could also take advantage of that to allow users to specify CLR types to be mapped by default (we already have this API for the Json.NET plugin).

    This is because our type mapping currently doesn't support a situation where more than one CLR type is mapped to the same PostgreSQL type.

    I don't think this is a blocker any more.

  7. roji commented on Jan 9, 2023

    @roji
    MemberAuthor

    @maximiliancsuk

    And for that, the type mapper needs to be able to transform any JSON result column into a user-supplied CLR type, potentially including JSON serialization options.

    How do you propose Npgsql determine which user-supplied CLR type would be deserialized from incoming JSON documents? The only reasonable something would be for Npgsql to start looking at data inside the JSON document (i.e. discriminator column), which is something Npgsql generally doesn't do.

    Note that we do have value-dependent mappings for the write side (i.e. DateDate gets written as timestamp or timestamptz based on Kind), and also Array Nullability Mode=PerInstance, which returns arrays over nullable/non-nullable elements based on the contents. So we could add a feature where the user tells us what the discriminator property is, or even provides an arbitrary piece of code which determines which type should get deserialized. But that does seem like a separate feature than this was originally supposed to be.

    @vonzshik any thoughts?

  8. vonzshik commented on Jan 9, 2023

    @vonzshik
    Contributor

    @vonzshik this issue was about writing arbitrary user POCOs without NpgsqlDbType, not JsonDocument/JsonElement etc.

    And we can already do that, isn't it? (At least the basic test implies that).

    public async Task Basic()
    {
    await using var adminConnection = await OpenConnectionAsync();
    var type = await GetTempTypeName(adminConnection);
    await adminConnection.ExecuteNonQueryAsync($"CREATE TYPE {type} AS (x int, some_text text)");
    var dataSourceBuilder = CreateDataSourceBuilder();
    dataSourceBuilder.MapComposite<SomeComposite>(type);
    await using var dataSource = dataSourceBuilder.Build();
    await using var connection = await dataSource.OpenConnectionAsync();
    await AssertType(
    connection,
    new SomeComposite { SomeText = "foo", X = 8 },
    "(8,foo)",
    type,
    npgsqlDbType: null);
    }

  9. roji commented on Jan 9, 2023

    @roji
    MemberAuthor

    @vonzshik that's composite types, not jsonb

  10. roji commented on Jan 9, 2023

    @roji
    MemberAuthor

    The point was to allow users to tell us up-front, that certain CLR types should be written e.g. as jsonb. Then, they can give us parameters with these CLR types without setting the NpgsqlDbType. It's a bit like doing MapEnum/MapComposite in advance, but within the JSON plugin.

  11. vonzshik commented on Jan 9, 2023

    @vonzshik
    Contributor

    @vonzshik that's composite types, not jsonb

    Ah, OK, I finally got it, was extremely confused before.

  12. vonzshik commented on Jan 9, 2023

    @vonzshik
    Contributor

    OK. From my understanding JsonHandler already supports writing and reading POCO objects, the only issue is mapping.
    For reading you should be able to use GetFieldValue<POCO>, so I don't think there is a need for the discriminator property. The only thing lacking is the settings.
    For writing right now you have to provide NpgsqlDbType. I think having something similar to MapComposite (with the user potentially providing the settings to use for reading/writing) should suffice.
    Please tell me if I missed something @roji.

  13. roji commented on Jan 9, 2023

    @roji
    MemberAuthor

    For reading you should be able to use GetFieldValue, so I don't think there is a need for the discriminator property. The only thing lacking is the settings.

    Almost; discriminator support would allow using non-generic read APIs (e.g. GetValue), and still the right type based on the actual content of the JSON column. I don't think this is as far-fetched as it seems: JSON is frequently used precisely to have a weak schema and multiple "types" in the same column; a discriminator scheme would allow automatically getting a strongly-typed POCO out without having to know in advance what's in there (because there are multiple types in the column).

    For writing right now you have to provide NpgsqlDbType. I think having something similar to MapComposite (with the user potentially providing the settings to use for reading/writing) should suffice.

    Yep, that's what the Json.NET plugin already supports, and what #4860 adds to the System.Text.Json support. When opting into the plugin, you give a list of CLR types to be mapped by default; those types don't require NpgsqlDbType to be specified.

  14. roji commented on Jan 9, 2023

    @roji
    MemberAuthor

    I've opened #4862 to track disciminator-based read polymorphism; I'll go ahead and close this issue as it discusses multiple things and no longer seems very useful.

  15. removed this from the Backlog milestone on Mar 18, 2023
  16. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions