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
Allow mapping CLR types to JSON at the type mapper level #2568
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.
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.
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.
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.
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.
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.
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.
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.
Activity
vonzshik commentedon Jan 8, 2023
I think we can close this issue since we don't need to specify
NpgsqlDbType.{Json,Jsonb}anymore?maximiliancsuk commentedon Jan 9, 2023
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.roji commentedon Jan 9, 2023
@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).
I don't think this is a blocker any more.
roji commentedon Jan 9, 2023
@maximiliancsuk
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?
vonzshik commentedon Jan 9, 2023
And we can already do that, isn't it? (At least the basic test implies that).
npgsql/test/Npgsql.Tests/Types/CompositeTests.cs
Lines 14 to 32 in 0acc808
roji commentedon Jan 9, 2023
@vonzshik that's composite types, not jsonb
roji commentedon Jan 9, 2023
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.
vonzshik commentedon Jan 9, 2023
Ah, OK, I finally got it, was extremely confused before.
vonzshik commentedon Jan 9, 2023
OK. From my understanding
JsonHandleralready 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.
roji commentedon Jan 9, 2023
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).
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.
roji commentedon Jan 9, 2023
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.