Skip to content

Npgsql: Use EnableDynamicJson to unlock better container type mappings#777

Merged
amotl merged 4 commits into
mainfrom
npgsql-type-mapping
Dec 20, 2024
Merged

Npgsql: Use EnableDynamicJson to unlock better container type mappings#777
amotl merged 4 commits into
mainfrom
npgsql-type-mapping

Conversation

@amotl

@amotl amotl commented Dec 19, 2024

Copy link
Copy Markdown
Contributor

About

This patch resolves a few FIXME admonitions introduced by a recent patch that started to validate all of CrateDB's data types with Npgsql.

Details

Vanilla Npgsql provides good enough support to handle CrateDB's ARRAY and OBJECT types better than just plain strings. This patch demonstrates type mappings using three different variants:

  • Native .NET System.Collections.Generic.List and System.Collections.Generic.Dictionary types.
  • Native .NET System.Text.Json.JsonDocument type. 1 Thanks, @simonprickett.
  • Custom .NET POCO types.

What's Inside

  • Use NpgsqlDbType.Json explicitly where applicable, for example when communicating parameters using AddWithValue().
  • Use EnableDynamicJson() on the NpgsqlDataSourceBuilder, for POCO and Dictionary mappings that didn't work before.

What's Next

Include relevant information into a dedicated documentation page, in order to educate readers properly in the spirit of "seeing is believing".

Footnotes

  1. Currently works with OBJECT for me, but not ARRAY.

@amotl amotl force-pushed the npgsql-type-mapping branch 2 times, most recently from 5bd679a to e88bbc5 Compare December 19, 2024 22:06
@amotl amotl force-pushed the npgsql-type-mapping branch from e88bbc5 to 5fdd7ff Compare December 19, 2024 23:33
Vanilla Npgsql provides good enough support to handle CrateDB's ARRAY
and OBJECT types better than just plain strings. This patch demonstrates
type mappings to native .NET `List` and `Dictionary` types, as well as
type mappings to custom .NET POCO types.
CrateDB's OBJECT data type can also be communicated using .NET's native
`System.Text.Json.JsonDocument` type.
@amotl amotl force-pushed the npgsql-type-mapping branch from 5fdd7ff to 970c259 Compare December 19, 2024 23:34
@amotl amotl requested review from kneth and simonprickett December 19, 2024 23:40
@amotl amotl marked this pull request as ready for review December 19, 2024 23:40
Comment thread by-language/csharp-npgsql/README.rst
@amotl amotl merged commit 999bc20 into main Dec 20, 2024
@amotl amotl deleted the npgsql-type-mapping branch December 20, 2024 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants